21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
-
25
24
import org .springframework .context .ApplicationContext ;
26
25
import org .springframework .test .context .ContextConfiguration ;
27
26
import org .springframework .test .context .ContextConfigurationAttributes ;
@@ -86,7 +85,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
86
85
*/
87
86
protected abstract SmartContextLoader getAnnotationConfigLoader ();
88
87
89
-
90
88
// --- SmartContextLoader --------------------------------------------------
91
89
92
90
private static String name (SmartContextLoader loader ) {
@@ -112,10 +110,10 @@ private static ApplicationContext delegateLoading(SmartContextLoader loader, Mer
112
110
113
111
private boolean supports (SmartContextLoader loader , MergedContextConfiguration mergedConfig ) {
114
112
if (loader == getAnnotationConfigLoader ()) {
115
- return ObjectUtils . isEmpty ( mergedConfig .getLocations ()) && !ObjectUtils . isEmpty ( mergedConfig .getClasses () );
113
+ return mergedConfig .hasClasses () && !mergedConfig .hasLocations ( );
116
114
}
117
115
else {
118
- return ! ObjectUtils . isEmpty ( mergedConfig .getLocations ()) && ObjectUtils . isEmpty ( mergedConfig .getClasses () );
116
+ return mergedConfig .hasLocations () && ! mergedConfig .hasClasses ( );
119
117
}
120
118
}
121
119
@@ -152,11 +150,10 @@ private boolean supports(SmartContextLoader loader, MergedContextConfiguration m
152
150
*/
153
151
@ Override
154
152
public void processContextConfiguration (final ContextConfigurationAttributes configAttributes ) {
155
-
156
153
Assert .notNull (configAttributes , "configAttributes must not be null" );
157
154
Assert .isTrue (!(configAttributes .hasLocations () && configAttributes .hasClasses ()), String .format (
158
- "Cannot process locations AND classes for context configuration %s; configure one or the other, but not both." ,
159
- configAttributes ));
155
+ "Cannot process locations AND classes for context configuration %s: "
156
+ + "configure one or the other, but not both." , configAttributes ));
160
157
161
158
// If the original locations or classes were not empty, there's no
162
159
// need to bother with default detection checks; just let the
@@ -208,15 +205,15 @@ else if (configAttributes.hasClasses()) {
208
205
// throw an exception.
209
206
if (!configAttributes .hasResources () && ObjectUtils .isEmpty (configAttributes .getInitializers ())) {
210
207
throw new IllegalStateException (String .format (
211
- "Neither %s nor %s was able to detect defaults, and no ApplicationContextInitializers " +
212
- "were declared for context configuration %s" , name (getXmlLoader ()),
208
+ "Neither %s nor %s was able to detect defaults, and no ApplicationContextInitializers "
209
+ + "were declared for context configuration %s" , name (getXmlLoader ()),
213
210
name (getAnnotationConfigLoader ()), configAttributes ));
214
211
}
215
212
216
213
if (configAttributes .hasLocations () && configAttributes .hasClasses ()) {
217
214
String message = String .format (
218
- "Configuration error: both default locations AND default configuration classes " +
219
- "were detected for context configuration %s; configure one or the other, but not both." ,
215
+ "Configuration error: both default locations AND default configuration classes "
216
+ + "were detected for context configuration %s; configure one or the other, but not both." ,
220
217
configAttributes );
221
218
logger .error (message );
222
219
throw new IllegalStateException (message );
@@ -249,6 +246,13 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
249
246
Assert .notNull (mergedConfig , "mergedConfig must not be null" );
250
247
List <SmartContextLoader > candidates = Arrays .asList (getXmlLoader (), getAnnotationConfigLoader ());
251
248
249
+ if (mergedConfig .hasLocations () && mergedConfig .hasClasses ()) {
250
+ throw new IllegalStateException (String .format (
251
+ "Neither %s nor %s supports loading an ApplicationContext from %s: "
252
+ + "declare either 'locations' or 'classes' but not both." , name (getXmlLoader ()),
253
+ name (getAnnotationConfigLoader ()), mergedConfig ));
254
+ }
255
+
252
256
for (SmartContextLoader loader : candidates ) {
253
257
// Determine if each loader can load a context from the mergedConfig. If it
254
258
// can, let it; otherwise, keep iterating.
@@ -263,12 +267,12 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
263
267
return delegateLoading (getAnnotationConfigLoader (), mergedConfig );
264
268
}
265
269
270
+ // else...
266
271
throw new IllegalStateException (String .format (
267
272
"Neither %s nor %s was able to load an ApplicationContext from %s." , name (getXmlLoader ()),
268
273
name (getAnnotationConfigLoader ()), mergedConfig ));
269
274
}
270
275
271
-
272
276
// --- ContextLoader -------------------------------------------------------
273
277
274
278
/**
@@ -279,8 +283,8 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
279
283
*/
280
284
@ Override
281
285
public final String [] processLocations (Class <?> clazz , String ... locations ) {
282
- throw new UnsupportedOperationException ("DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
283
- "Call processContextConfiguration(ContextConfigurationAttributes) instead." );
286
+ throw new UnsupportedOperationException ("DelegatingSmartContextLoaders do not support the ContextLoader SPI. "
287
+ + "Call processContextConfiguration(ContextConfigurationAttributes) instead." );
284
288
}
285
289
286
290
/**
@@ -291,8 +295,8 @@ public final String[] processLocations(Class<?> clazz, String... locations) {
291
295
*/
292
296
@ Override
293
297
public final ApplicationContext loadContext (String ... locations ) throws Exception {
294
- throw new UnsupportedOperationException ("DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
295
- "Call loadContext(MergedContextConfiguration) instead." );
298
+ throw new UnsupportedOperationException ("DelegatingSmartContextLoaders do not support the ContextLoader SPI. "
299
+ + "Call loadContext(MergedContextConfiguration) instead." );
296
300
}
297
301
298
302
}
0 commit comments