16
16
17
17
package org .springframework .test .context .support ;
18
18
19
- import org .apache .commons .logging .Log ;
20
- import org .apache .commons .logging .LogFactory ;
19
+ import java .util .List ;
21
20
22
- import org .springframework .core .io .ClassPathResource ;
23
21
import org .springframework .core .style .ToStringCreator ;
24
- import org .springframework .lang .Nullable ;
25
22
import org .springframework .test .context .TestPropertySource ;
26
23
import org .springframework .util .Assert ;
27
- import org .springframework .util .ClassUtils ;
28
24
import org .springframework .util .ObjectUtils ;
29
- import org .springframework .util .ResourceUtils ;
30
25
31
26
/**
32
- * {@code TestPropertySourceAttributes} encapsulates the attributes declared
33
- * via {@link TestPropertySource @TestPropertySource}.
27
+ * {@code TestPropertySourceAttributes} encapsulates attributes declared
28
+ * via {@link TestPropertySource @TestPropertySource} annotations .
34
29
*
35
30
* <p>In addition to encapsulating declared attributes,
36
- * {@code TestPropertySourceAttributes} also enforces configuration rules
37
- * and detects default properties files.
31
+ * {@code TestPropertySourceAttributes} also enforces configuration rules.
38
32
*
39
33
* @author Sam Brannen
40
34
* @since 4.1
43
37
*/
44
38
class TestPropertySourceAttributes {
45
39
46
- private static final Log logger = LogFactory .getLog (TestPropertySourceAttributes .class );
47
-
48
40
private final Class <?> declaringClass ;
49
41
50
42
private final String [] locations ;
@@ -57,27 +49,29 @@ class TestPropertySourceAttributes {
57
49
58
50
59
51
/**
60
- * Create a new {@code TestPropertySourceAttributes} instance for the
61
- * supplied {@link TestPropertySource @TestPropertySource} annotation and
62
- * the {@linkplain Class test class} that declared it, enforcing
63
- * configuration rules and detecting a default properties file if
64
- * necessary.
52
+ * Create a new {@code TestPropertySourceAttributes} instance for the supplied
53
+ * values and enforce configuration rules.
65
54
* @param declaringClass the class that declared {@code @TestPropertySource}
66
- * @param testPropertySource the annotation from which to retrieve the attributes
67
- * @since 4.2
55
+ * @param locations the merged {@link TestPropertySource#locations()}
56
+ * @param inheritLocations the {@link TestPropertySource#inheritLocations()} flag
57
+ * @param properties the merged {@link TestPropertySource#properties()}
58
+ * @param inheritProperties the {@link TestPropertySource#inheritProperties()} flag
59
+ * @since 5.2
68
60
*/
69
- TestPropertySourceAttributes (Class <?> declaringClass , TestPropertySource testPropertySource ) {
70
- this (declaringClass , testPropertySource .locations (), testPropertySource .inheritLocations (),
71
- testPropertySource .properties (), testPropertySource .inheritProperties ());
61
+ TestPropertySourceAttributes (Class <?> declaringClass , List <String > locations , boolean inheritLocations ,
62
+ List <String > properties , boolean inheritProperties ) {
63
+
64
+ this (declaringClass , locations .toArray (new String [0 ]), inheritLocations , properties .toArray (new String [0 ]),
65
+ inheritProperties );
72
66
}
73
67
74
68
private TestPropertySourceAttributes (Class <?> declaringClass , String [] locations , boolean inheritLocations ,
75
69
String [] properties , boolean inheritProperties ) {
76
70
77
- Assert .notNull (declaringClass , "declaringClass must not be null" );
78
- if ( ObjectUtils .isEmpty (locations ) && ObjectUtils .isEmpty (properties )) {
79
- locations = new String [] { detectDefaultPropertiesFile ( declaringClass ) } ;
80
- }
71
+ Assert .notNull (declaringClass , "' declaringClass' must not be null" );
72
+ Assert . isTrue (! ObjectUtils .isEmpty (locations ) || ! ObjectUtils .isEmpty (properties ),
73
+ "Either ' locations' or 'properties' are required" ) ;
74
+
81
75
this .declaringClass = declaringClass ;
82
76
this .locations = locations ;
83
77
this .inheritLocations = inheritLocations ;
@@ -97,7 +91,8 @@ Class<?> getDeclaringClass() {
97
91
/**
98
92
* Get the resource locations that were declared via {@code @TestPropertySource}.
99
93
* <p>Note: The returned value may represent a <em>detected default</em>
100
- * that does not match the original value declared via {@code @TestPropertySource}.
94
+ * or merged locations that do not match the original value declared via a
95
+ * single {@code @TestPropertySource} annotation.
101
96
* @return the resource locations; potentially <em>empty</em>
102
97
* @see TestPropertySource#value
103
98
* @see TestPropertySource#locations
@@ -117,10 +112,12 @@ boolean isInheritLocations() {
117
112
118
113
/**
119
114
* Get the inlined properties that were declared via {@code @TestPropertySource}.
120
- * @return the inlined properties; potentially {@code null} or <em>empty</em>
115
+ * <p>Note: The returned value may represent merged properties that do not
116
+ * match the original value declared via a single {@code @TestPropertySource}
117
+ * annotation.
118
+ * @return the inlined properties; potentially <em>empty</em>
121
119
* @see TestPropertySource#properties
122
120
*/
123
- @ Nullable
124
121
String [] getProperties () {
125
122
return this .properties ;
126
123
}
@@ -149,31 +146,4 @@ public String toString() {
149
146
.toString ();
150
147
}
151
148
152
-
153
- /**
154
- * Detect a default properties file for the supplied class, as specified
155
- * in the class-level Javadoc for {@link TestPropertySource}.
156
- */
157
- private static String detectDefaultPropertiesFile (Class <?> testClass ) {
158
- String resourcePath = ClassUtils .convertClassNameToResourcePath (testClass .getName ()) + ".properties" ;
159
- ClassPathResource classPathResource = new ClassPathResource (resourcePath );
160
-
161
- if (classPathResource .exists ()) {
162
- String prefixedResourcePath = ResourceUtils .CLASSPATH_URL_PREFIX + resourcePath ;
163
- if (logger .isInfoEnabled ()) {
164
- logger .info (String .format ("Detected default properties file \" %s\" for test class [%s]" ,
165
- prefixedResourcePath , testClass .getName ()));
166
- }
167
- return prefixedResourcePath ;
168
- }
169
- else {
170
- String msg = String .format ("Could not detect default properties file for test [%s]: " +
171
- "%s does not exist. Either declare the 'locations' or 'properties' attributes " +
172
- "of @TestPropertySource or make the default properties file available." , testClass .getName (),
173
- classPathResource );
174
- logger .error (msg );
175
- throw new IllegalStateException (msg );
176
- }
177
- }
178
-
179
149
}
0 commit comments