Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractDataSetLoader |
|
| 1.75;1.75 |
1 | /* | |
2 | * Copyright 2002-2016 the original author or authors | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package com.github.springtestdbunit.dataset; | |
18 | ||
19 | import org.dbunit.dataset.IDataSet; | |
20 | import org.springframework.core.io.ClassRelativeResourceLoader; | |
21 | import org.springframework.core.io.Resource; | |
22 | import org.springframework.core.io.ResourceLoader; | |
23 | ||
24 | /** | |
25 | * Abstract data set loader, which provides a basis for concrete implementations of the {@link DataSetLoader} strategy. | |
26 | * Provides a <em>Template Method</em> based approach for {@link #loadDataSet(Class, String) loading} data using a | |
27 | * Spring {@link #getResourceLoader resource loader}. | |
28 | * | |
29 | * @author Phillip Webb | |
30 | * | |
31 | * @see #getResourceLoader | |
32 | * @see #getResourceLocations | |
33 | * @see #createDataSet(Resource) | |
34 | */ | |
35 | 636 | public abstract class AbstractDataSetLoader implements DataSetLoader { |
36 | ||
37 | /** | |
38 | * Loads a {@link IDataSet dataset} from {@link Resource}s obtained from the specified <tt>location</tt>. Each | |
39 | * <tt>location</tt> can be mapped to a number of potential {@link #getResourceLocations resources}, the first | |
40 | * resource that {@link Resource#exists() exists} will be used. {@link Resource}s are loaded using the | |
41 | * {@link ResourceLoader} returned from {@link #getResourceLoader}. | |
42 | * <p> | |
43 | * If no resource can be found then <tt>null</tt> will be returned. | |
44 | * | |
45 | * @see #createDataSet(Resource) | |
46 | * @see com.github.springtestdbunit.dataset.DataSetLoader#loadDataSet(Class, String) java.lang.String) | |
47 | */ | |
48 | public IDataSet loadDataSet(Class<?> testClass, String location) throws Exception { | |
49 | 744 | ResourceLoader resourceLoader = getResourceLoader(testClass); |
50 | 744 | String[] resourceLocations = getResourceLocations(testClass, location); |
51 | 768 | for (String resourceLocation : resourceLocations) { |
52 | 744 | Resource resource = resourceLoader.getResource(resourceLocation); |
53 | 744 | if (resource.exists()) { |
54 | 720 | return createDataSet(resource); |
55 | } | |
56 | } | |
57 | 24 | return null; |
58 | } | |
59 | ||
60 | /** | |
61 | * Gets the {@link ResourceLoader} that will be used to load the dataset {@link Resource}s. | |
62 | * @param testClass The class under test | |
63 | * @return a resource loader | |
64 | */ | |
65 | protected ResourceLoader getResourceLoader(Class<?> testClass) { | |
66 | 744 | return new ClassRelativeResourceLoader(testClass); |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the resource locations that should be considered when attempting to load a dataset from the specified | |
71 | * location. | |
72 | * @param testClass The class under test | |
73 | * @param location The source location | |
74 | * @return an array of potential resource locations | |
75 | */ | |
76 | protected String[] getResourceLocations(Class<?> testClass, String location) { | |
77 | 744 | return new String[] { location }; |
78 | } | |
79 | ||
80 | /** | |
81 | * Factory method used to create the {@link IDataSet dataset} | |
82 | * @param resource an existing resource that contains the dataset data | |
83 | * @return a dataset | |
84 | * @throws Exception if the dataset could not be loaded | |
85 | */ | |
86 | protected abstract IDataSet createDataSet(Resource resource) throws Exception; | |
87 | ||
88 | } |