1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
26
26
import org .springframework .beans .BeanUtils ;
27
27
import org .springframework .context .ApplicationContext ;
28
+ import org .springframework .context .ApplicationContextException ;
28
29
import org .springframework .context .ApplicationContextInitializer ;
29
30
import org .springframework .context .ConfigurableApplicationContext ;
30
31
import org .springframework .core .GenericTypeResolver ;
@@ -86,8 +87,8 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
86
87
*/
87
88
@ Override
88
89
public void processContextConfiguration (ContextConfigurationAttributes configAttributes ) {
89
- String [] processedLocations = processLocations ( configAttributes . getDeclaringClass (),
90
- configAttributes .getLocations ());
90
+ String [] processedLocations =
91
+ processLocations ( configAttributes . getDeclaringClass (), configAttributes .getLocations ());
91
92
configAttributes .setLocations (processedLocations );
92
93
}
93
94
@@ -135,7 +136,9 @@ protected void prepareContext(ConfigurableApplicationContext context, MergedCont
135
136
@ SuppressWarnings ("unchecked" )
136
137
private void invokeApplicationContextInitializers (ConfigurableApplicationContext context ,
137
138
MergedContextConfiguration mergedConfig ) {
138
- Set <Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>> initializerClasses = mergedConfig .getContextInitializerClasses ();
139
+
140
+ Set <Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>> initializerClasses =
141
+ mergedConfig .getContextInitializerClasses ();
139
142
if (initializerClasses .isEmpty ()) {
140
143
// no ApplicationContextInitializers have been declared -> nothing to do
141
144
return ;
@@ -145,13 +148,15 @@ private void invokeApplicationContextInitializers(ConfigurableApplicationContext
145
148
Class <?> contextClass = context .getClass ();
146
149
147
150
for (Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >> initializerClass : initializerClasses ) {
148
- Class <?> initializerContextClass = GenericTypeResolver .resolveTypeArgument (initializerClass ,
149
- ApplicationContextInitializer .class );
150
- Assert .isAssignable (initializerContextClass , contextClass , String .format (
151
- "Could not add context initializer [%s] since its generic parameter [%s] "
152
- + "is not assignable from the type of application context used by this "
153
- + "context loader [%s]: " , initializerClass .getName (), initializerContextClass .getName (),
154
- contextClass .getName ()));
151
+ Class <?> initializerContextClass =
152
+ GenericTypeResolver .resolveTypeArgument (initializerClass , ApplicationContextInitializer .class );
153
+ if (initializerContextClass != null && !initializerContextClass .isInstance (context )) {
154
+ throw new ApplicationContextException (String .format (
155
+ "Could not apply context initializer [%s] since its generic parameter [%s] " +
156
+ "is not assignable from the type of application context used by this " +
157
+ "context loader: [%s]" , initializerClass .getName (), initializerContextClass .getName (),
158
+ contextClass .getName ()));
159
+ }
155
160
initializerInstances .add ((ApplicationContextInitializer <ConfigurableApplicationContext >) BeanUtils .instantiateClass (initializerClass ));
156
161
}
157
162
@@ -161,6 +166,7 @@ private void invokeApplicationContextInitializers(ConfigurableApplicationContext
161
166
}
162
167
}
163
168
169
+
164
170
// --- ContextLoader -------------------------------------------------------
165
171
166
172
/**
@@ -171,7 +177,6 @@ private void invokeApplicationContextInitializers(ConfigurableApplicationContext
171
177
* and the configured {@linkplain #getResourceSuffixes() resource suffixes};
172
178
* otherwise, the supplied {@code locations} will be
173
179
* {@linkplain #modifyLocations modified} if necessary and returned.
174
- *
175
180
* @param clazz the class with which the locations are associated: to be
176
181
* used when generating default locations
177
182
* @param locations the unmodified locations to use for loading the
@@ -186,30 +191,26 @@ private void invokeApplicationContextInitializers(ConfigurableApplicationContext
186
191
*/
187
192
@ Override
188
193
public final String [] processLocations (Class <?> clazz , String ... locations ) {
189
- return (ObjectUtils .isEmpty (locations ) && isGenerateDefaultLocations ()) ? generateDefaultLocations ( clazz )
190
- : modifyLocations (clazz , locations );
194
+ return (ObjectUtils .isEmpty (locations ) && isGenerateDefaultLocations ()) ?
195
+ generateDefaultLocations ( clazz ) : modifyLocations (clazz , locations );
191
196
}
192
197
193
198
/**
194
199
* Generate the default classpath resource locations array based on the
195
200
* supplied class.
196
- *
197
201
* <p>For example, if the supplied class is {@code com.example.MyTest},
198
202
* the generated locations will contain a single string with a value of
199
203
* {@code "classpath:com/example/MyTest<suffix>"}, where {@code <suffix>}
200
204
* is the value of the first configured
201
205
* {@linkplain #getResourceSuffixes() resource suffix} for which the
202
206
* generated location actually exists in the classpath.
203
- *
204
207
* <p>As of Spring 3.1, the implementation of this method adheres to the
205
208
* contract defined in the {@link SmartContextLoader} SPI. Specifically,
206
209
* this method will <em>preemptively</em> verify that the generated default
207
210
* location actually exists. If it does not exist, this method will log a
208
211
* warning and return an empty array.
209
- *
210
212
* <p>Subclasses can override this method to implement a different
211
213
* <em>default location generation</em> strategy.
212
- *
213
214
* @param clazz the class for which the default locations are to be generated
214
215
* @return an array of default application context resource locations
215
216
* @since 2.5
@@ -224,23 +225,22 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
224
225
String resourcePath = ClassUtils .convertClassNameToResourcePath (clazz .getName ()) + suffix ;
225
226
String prefixedResourcePath = ResourceUtils .CLASSPATH_URL_PREFIX + resourcePath ;
226
227
ClassPathResource classPathResource = new ClassPathResource (resourcePath );
227
-
228
228
if (classPathResource .exists ()) {
229
229
if (logger .isInfoEnabled ()) {
230
230
logger .info (String .format ("Detected default resource location \" %s\" for test class [%s]" ,
231
- prefixedResourcePath , clazz .getName ()));
231
+ prefixedResourcePath , clazz .getName ()));
232
232
}
233
- return new String [] { prefixedResourcePath };
233
+ return new String [] {prefixedResourcePath };
234
234
}
235
235
else if (logger .isDebugEnabled ()) {
236
- logger .debug (String .format ("Did not detect default resource location for test class [%s]: "
237
- + "%s does not exist" , clazz .getName (), classPathResource ));
236
+ logger .debug (String .format ("Did not detect default resource location for test class [%s]: " +
237
+ "%s does not exist" , clazz .getName (), classPathResource ));
238
238
}
239
239
}
240
240
241
241
if (logger .isInfoEnabled ()) {
242
- logger .info (String .format ("Could not detect default resource locations for test class [%s]: "
243
- + "no resource found for suffixes %s." , clazz .getName (), ObjectUtils .nullSafeToString (suffixes )));
242
+ logger .info (String .format ("Could not detect default resource locations for test class [%s]: " +
243
+ "no resource found for suffixes %s." , clazz .getName (), ObjectUtils .nullSafeToString (suffixes )));
244
244
}
245
245
246
246
return EMPTY_STRING_ARRAY ;
@@ -282,36 +282,31 @@ protected boolean isGenerateDefaultLocations() {
282
282
}
283
283
284
284
/**
285
- * Get the suffix to append to {@link ApplicationContext} resource
286
- * locations when detecting default locations.
287
- *
288
- * <p>Subclasses must provide an implementation of this method that
289
- * returns a single suffix. Alternatively subclasses may provide a
290
- * <em>no-op</em> implementation of this method and override
291
- * {@link #getResourceSuffixes()} in order to provide multiple custom
292
- * suffixes.
293
- *
294
- * @return the resource suffix; never {@code null} or empty
295
- * @since 2.5
296
- * @see #generateDefaultLocations(Class)
297
- * @see #getResourceSuffixes()
298
- */
299
- protected abstract String getResourceSuffix ();
300
-
301
- /**
302
- * Get the suffixes to append to {@link ApplicationContext} resource
303
- * locations when detecting default locations.
304
- *
285
+ * Get the suffixes to append to {@link ApplicationContext} resource locations
286
+ * when detecting default locations.
305
287
* <p>The default implementation simply wraps the value returned by
306
288
* {@link #getResourceSuffix()} in a single-element array, but this
307
289
* can be overridden by subclasses in order to support multiple suffixes.
308
- *
309
290
* @return the resource suffixes; never {@code null} or empty
310
291
* @since 4.1
311
292
* @see #generateDefaultLocations(Class)
312
293
*/
313
294
protected String [] getResourceSuffixes () {
314
- return new String [] { getResourceSuffix () };
295
+ return new String [] {getResourceSuffix ()};
315
296
}
316
297
298
+ /**
299
+ * Get the suffix to append to {@link ApplicationContext} resource locations
300
+ * when detecting default locations.
301
+ * <p>Subclasses must provide an implementation of this method that returns
302
+ * a single suffix. Alternatively subclasses may provide a <em>no-op</em>
303
+ * implementation of this method and override {@link #getResourceSuffixes()}
304
+ * in order to provide multiple custom suffixes.
305
+ * @return the resource suffix; never {@code null} or empty
306
+ * @since 2.5
307
+ * @see #generateDefaultLocations(Class)
308
+ * @see #getResourceSuffixes()
309
+ */
310
+ protected abstract String getResourceSuffix ();
311
+
317
312
}
0 commit comments