61
61
*/
62
62
public abstract class AbstractContextLoader implements SmartContextLoader {
63
63
64
- private static final Log logger = LogFactory .getLog (AbstractContextLoader .class );
65
-
66
64
private static final String [] EMPTY_STRING_ARRAY = new String [0 ];
65
+
67
66
private static final String SLASH = "/" ;
68
67
68
+ private static final Log logger = LogFactory .getLog (AbstractContextLoader .class );
69
+
69
70
70
71
// --- SmartContextLoader -----------------------------------------------
71
72
@@ -79,23 +80,19 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
79
80
* processed locations are then
80
81
* {@link ContextConfigurationAttributes#setLocations(String[]) set} in
81
82
* the supplied configuration attributes.
82
- *
83
83
* <p>Can be overridden in subclasses — for example, to process
84
84
* annotated classes instead of resource locations.
85
- *
86
85
* @since 3.1
87
86
* @see #processLocations(Class, String...)
88
87
*/
89
88
public void processContextConfiguration (ContextConfigurationAttributes configAttributes ) {
90
- String [] processedLocations = processLocations (configAttributes .getDeclaringClass (),
91
- configAttributes .getLocations ());
89
+ String [] processedLocations = processLocations (configAttributes .getDeclaringClass (), configAttributes .getLocations ());
92
90
configAttributes .setLocations (processedLocations );
93
91
}
94
92
95
93
/**
96
94
* Prepare the {@link ConfigurableApplicationContext} created by this
97
95
* {@code SmartContextLoader} <i>before</i> bean definitions are read.
98
- *
99
96
* <p>The default implementation:
100
97
* <ul>
101
98
* <li>Sets the <em>active bean definition profiles</em> from the supplied
@@ -106,17 +103,15 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
106
103
* {@linkplain ApplicationContextInitializer#initialize invokes each} with the
107
104
* given application context.</li>
108
105
* </ul>
109
- *
110
106
* <p>Any {@code ApplicationContextInitializers} implementing
111
107
* {@link org.springframework.core.Ordered Ordered} or marked with {@link
112
108
* org.springframework.core.annotation.Order @Order} will be sorted appropriately.
113
- *
114
109
* @param context the newly created application context
115
110
* @param mergedConfig the merged context configuration
111
+ * @since 3.2
116
112
* @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
117
113
* @see #loadContext(MergedContextConfiguration)
118
114
* @see ConfigurableApplicationContext#setId
119
- * @since 3.2
120
115
*/
121
116
@ SuppressWarnings ("unchecked" )
122
117
protected void prepareContext (ConfigurableApplicationContext context , MergedContextConfiguration mergedConfig ) {
@@ -129,17 +124,18 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
129
124
return ;
130
125
}
131
126
132
- final List <ApplicationContextInitializer <ConfigurableApplicationContext >> initializerInstances = new ArrayList <ApplicationContextInitializer <ConfigurableApplicationContext >>();
133
- final Class <?> contextClass = context .getClass ();
127
+ List <ApplicationContextInitializer <ConfigurableApplicationContext >> initializerInstances =
128
+ new ArrayList <ApplicationContextInitializer <ConfigurableApplicationContext >>();
129
+ Class <?> contextClass = context .getClass ();
134
130
135
131
for (Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >> initializerClass : initializerClasses ) {
136
132
Class <?> initializerContextClass = GenericTypeResolver .resolveTypeArgument (initializerClass ,
137
- ApplicationContextInitializer .class );
133
+ ApplicationContextInitializer .class );
138
134
Assert .isAssignable (initializerContextClass , contextClass , String .format (
139
- "Could not add context initializer [%s] since its generic parameter [%s] "
140
- + "is not assignable from the type of application context used by this "
141
- + "context loader [%s]: " , initializerClass .getName (), initializerContextClass .getName (),
142
- contextClass .getName ()));
135
+ "Could not add context initializer [%s] since its generic parameter [%s] " +
136
+ "is not assignable from the type of application context used by this " +
137
+ "context loader [%s]: " , initializerClass .getName (), initializerContextClass .getName (),
138
+ contextClass .getName ()));
143
139
initializerInstances .add ((ApplicationContextInitializer <ConfigurableApplicationContext >) BeanUtils .instantiateClass (initializerClass ));
144
140
}
145
141
@@ -160,7 +156,6 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
160
156
* {@link #getResourceSuffix() resource suffix}; otherwise, the supplied
161
157
* {@code locations} will be {@link #modifyLocations modified} if
162
158
* necessary and returned.
163
- *
164
159
* @param clazz the class with which the locations are associated: to be
165
160
* used when generating default locations
166
161
* @param locations the unmodified locations to use for loading the
@@ -174,29 +169,25 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
174
169
* @see #processContextConfiguration(ContextConfigurationAttributes)
175
170
*/
176
171
public final String [] processLocations (Class <?> clazz , String ... locations ) {
177
- return (ObjectUtils .isEmpty (locations ) && isGenerateDefaultLocations ()) ? generateDefaultLocations ( clazz )
178
- : modifyLocations (clazz , locations );
172
+ return (ObjectUtils .isEmpty (locations ) && isGenerateDefaultLocations ()) ?
173
+ generateDefaultLocations ( clazz ) : modifyLocations (clazz , locations );
179
174
}
180
175
181
176
/**
182
177
* Generate the default classpath resource locations array based on the
183
178
* supplied class.
184
- *
185
179
* <p>For example, if the supplied class is {@code com.example.MyTest},
186
180
* the generated locations will contain a single string with a value of
187
181
* "classpath:/com/example/MyTest{@code <suffix>}",
188
182
* where {@code <suffix>} is the value of the
189
183
* {@link #getResourceSuffix() resource suffix} string.
190
- *
191
184
* <p>As of Spring 3.1, the implementation of this method adheres to the
192
185
* contract defined in the {@link SmartContextLoader} SPI. Specifically,
193
186
* this method will <em>preemptively</em> verify that the generated default
194
187
* location actually exists. If it does not exist, this method will log a
195
188
* warning and return an empty array.
196
- *
197
189
* <p>Subclasses can override this method to implement a different
198
190
* <em>default location generation</em> strategy.
199
- *
200
191
* @param clazz the class for which the default locations are to be generated
201
192
* @return an array of default application context resource locations
202
193
* @since 2.5
@@ -212,23 +203,22 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
212
203
213
204
if (classPathResource .exists ()) {
214
205
if (logger .isInfoEnabled ()) {
215
- logger .info (String .format ("Detected default resource location \" %s\" for test class [%s]. " ,
216
- prefixedResourcePath , clazz .getName ()));
206
+ logger .info (String .format ("Detected default resource location \" %s\" for test class [%s]" ,
207
+ prefixedResourcePath , clazz .getName ()));
217
208
}
218
- return new String [] { prefixedResourcePath };
209
+ return new String [] {prefixedResourcePath };
219
210
}
220
-
221
- // else
222
- if (logger .isInfoEnabled ()) {
223
- logger .info (String .format ("Could not detect default resource locations for test class [%s]: "
224
- + "%s does not exist." , clazz .getName (), classPathResource ));
211
+ else {
212
+ if (logger .isInfoEnabled ()) {
213
+ logger .info (String .format ("Could not detect default resource locations for test class [%s]: " +
214
+ "%s does not exist" , clazz .getName (), classPathResource ));
215
+ }
216
+ return EMPTY_STRING_ARRAY ;
225
217
}
226
- return EMPTY_STRING_ARRAY ;
227
218
}
228
219
229
220
/**
230
221
* Generate a modified version of the supplied locations array and return it.
231
- *
232
222
* <p>A plain path — for example, "context.xml" — will
233
223
* be treated as a classpath resource that is relative to the package in which
234
224
* the specified class is defined. A path starting with a slash is treated
@@ -238,10 +228,8 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
238
228
* {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
239
229
* {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:},
240
230
* etc.) will be added to the results unchanged.
241
- *
242
231
* <p>Subclasses can override this method to implement a different
243
232
* <em>location modification</em> strategy.
244
- *
245
233
* @param clazz the class with which the locations are associated
246
234
* @param locations the resource locations to be modified
247
235
* @return an array of modified application context resource locations
@@ -253,10 +241,12 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
253
241
String path = locations [i ];
254
242
if (path .startsWith (SLASH )) {
255
243
modifiedLocations [i ] = ResourceUtils .CLASSPATH_URL_PREFIX + path ;
256
- } else if (!ResourcePatternUtils .isUrl (path )) {
257
- modifiedLocations [i ] = ResourceUtils .CLASSPATH_URL_PREFIX + SLASH
258
- + StringUtils .cleanPath (ClassUtils .classPackageAsResourcePath (clazz ) + SLASH + path );
259
- } else {
244
+ }
245
+ else if (!ResourcePatternUtils .isUrl (path )) {
246
+ modifiedLocations [i ] = ResourceUtils .CLASSPATH_URL_PREFIX + SLASH +
247
+ StringUtils .cleanPath (ClassUtils .classPackageAsResourcePath (clazz ) + SLASH + path );
248
+ }
249
+ else {
260
250
modifiedLocations [i ] = StringUtils .cleanPath (path );
261
251
}
262
252
}
@@ -267,7 +257,6 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
267
257
* Determine whether or not <em>default</em> resource locations should be
268
258
* generated if the {@code locations} provided to
269
259
* {@link #processLocations(Class, String...)} are {@code null} or empty.
270
- *
271
260
* <p>As of Spring 3.1, the semantics of this method have been overloaded
272
261
* to include detection of either default resource locations or default
273
262
* configuration classes. Consequently, this method can also be used to
@@ -276,9 +265,7 @@ protected String[] modifyLocations(Class<?> clazz, String... locations) {
276
265
* {@link ContextConfigurationAttributes configuration attributes} supplied
277
266
* to {@link #processContextConfiguration(ContextConfigurationAttributes)}
278
267
* are {@code null} or empty.
279
- *
280
268
* <p>Can be overridden by subclasses to change the default behavior.
281
- *
282
269
* @return always {@code true} by default
283
270
* @since 2.5
284
271
*/
@@ -289,9 +276,7 @@ protected boolean isGenerateDefaultLocations() {
289
276
/**
290
277
* Get the suffix to append to {@link ApplicationContext} resource locations
291
278
* when generating default locations.
292
- *
293
279
* <p>Must be implemented by subclasses.
294
- *
295
280
* @return the resource suffix; should not be {@code null} or empty
296
281
* @since 2.5
297
282
* @see #generateDefaultLocations(Class)
0 commit comments