2121
2222import org .apache .commons .logging .Log ;
2323import org .apache .commons .logging .LogFactory ;
24-
2524import org .springframework .context .ApplicationContext ;
2625import org .springframework .test .context .ContextConfiguration ;
2726import org .springframework .test .context .ContextConfigurationAttributes ;
@@ -86,7 +85,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
8685 */
8786 protected abstract SmartContextLoader getAnnotationConfigLoader ();
8887
89-
9088 // --- SmartContextLoader --------------------------------------------------
9189
9290 private static String name (SmartContextLoader loader ) {
@@ -112,10 +110,10 @@ private static ApplicationContext delegateLoading(SmartContextLoader loader, Mer
112110
113111 private boolean supports (SmartContextLoader loader , MergedContextConfiguration mergedConfig ) {
114112 if (loader == getAnnotationConfigLoader ()) {
115- return ObjectUtils . isEmpty ( mergedConfig .getLocations ()) && !ObjectUtils . isEmpty ( mergedConfig .getClasses () );
113+ return mergedConfig .hasClasses () && !mergedConfig .hasLocations ( );
116114 }
117115 else {
118- return ! ObjectUtils . isEmpty ( mergedConfig .getLocations ()) && ObjectUtils . isEmpty ( mergedConfig .getClasses () );
116+ return mergedConfig .hasLocations () && ! mergedConfig .hasClasses ( );
119117 }
120118 }
121119
@@ -152,11 +150,10 @@ private boolean supports(SmartContextLoader loader, MergedContextConfiguration m
152150 */
153151 @ Override
154152 public void processContextConfiguration (final ContextConfigurationAttributes configAttributes ) {
155-
156153 Assert .notNull (configAttributes , "configAttributes must not be null" );
157154 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 ));
160157
161158 // If the original locations or classes were not empty, there's no
162159 // need to bother with default detection checks; just let the
@@ -208,15 +205,15 @@ else if (configAttributes.hasClasses()) {
208205 // throw an exception.
209206 if (!configAttributes .hasResources () && ObjectUtils .isEmpty (configAttributes .getInitializers ())) {
210207 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 ()),
213210 name (getAnnotationConfigLoader ()), configAttributes ));
214211 }
215212
216213 if (configAttributes .hasLocations () && configAttributes .hasClasses ()) {
217214 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." ,
220217 configAttributes );
221218 logger .error (message );
222219 throw new IllegalStateException (message );
@@ -249,6 +246,13 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
249246 Assert .notNull (mergedConfig , "mergedConfig must not be null" );
250247 List <SmartContextLoader > candidates = Arrays .asList (getXmlLoader (), getAnnotationConfigLoader ());
251248
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+
252256 for (SmartContextLoader loader : candidates ) {
253257 // Determine if each loader can load a context from the mergedConfig. If it
254258 // can, let it; otherwise, keep iterating.
@@ -263,12 +267,12 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
263267 return delegateLoading (getAnnotationConfigLoader (), mergedConfig );
264268 }
265269
270+ // else...
266271 throw new IllegalStateException (String .format (
267272 "Neither %s nor %s was able to load an ApplicationContext from %s." , name (getXmlLoader ()),
268273 name (getAnnotationConfigLoader ()), mergedConfig ));
269274 }
270275
271-
272276 // --- ContextLoader -------------------------------------------------------
273277
274278 /**
@@ -279,8 +283,8 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
279283 */
280284 @ Override
281285 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." );
284288 }
285289
286290 /**
@@ -291,8 +295,8 @@ public final String[] processLocations(Class<?> clazz, String... locations) {
291295 */
292296 @ Override
293297 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." );
296300 }
297301
298302}
0 commit comments