|
|
|
@ -26,6 +26,7 @@ import org.springframework.test.context.ContextConfigurationAttributes; |
|
|
|
import org.springframework.test.context.ContextLoader; |
|
|
|
import org.springframework.test.context.ContextLoader; |
|
|
|
import org.springframework.test.context.MergedContextConfiguration; |
|
|
|
import org.springframework.test.context.MergedContextConfiguration; |
|
|
|
import org.springframework.test.context.SmartContextLoader; |
|
|
|
import org.springframework.test.context.SmartContextLoader; |
|
|
|
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -59,14 +60,20 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* TODO Document emptyResources(). |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean emptyResources(ContextConfigurationAttributes configAttributes) { |
|
|
|
|
|
|
|
return ObjectUtils.isEmpty(configAttributes.getLocations()) |
|
|
|
|
|
|
|
&& ObjectUtils.isEmpty(configAttributes.getClasses()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* TODO Document processContextConfiguration() implementation. |
|
|
|
* TODO Document processContextConfiguration() implementation. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { |
|
|
|
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { |
|
|
|
|
|
|
|
|
|
|
|
final String[] originalLocations = configAttributes.getLocations(); |
|
|
|
final boolean emptyResources = emptyResources(configAttributes); |
|
|
|
final Class<?>[] originalClasses = configAttributes.getClasses(); |
|
|
|
|
|
|
|
final boolean emptyResources = ObjectUtils.isEmpty(originalLocations) && ObjectUtils.isEmpty(originalClasses); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
@ -74,42 +81,43 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { |
|
|
|
loader.getClass().getName(), configAttributes)); |
|
|
|
loader.getClass().getName(), configAttributes)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO Implement processContextConfiguration().
|
|
|
|
// If the original locations and classes were not empty, there's no
|
|
|
|
//
|
|
|
|
|
|
|
|
// If the original locations and classes are not empty, there's no
|
|
|
|
|
|
|
|
// need to bother with default generation checks; just let each
|
|
|
|
// need to bother with default generation checks; just let each
|
|
|
|
// loader process the configuration.
|
|
|
|
// loader process the configuration.
|
|
|
|
if (!emptyResources) { |
|
|
|
if (!emptyResources) { |
|
|
|
loader.processContextConfiguration(configAttributes); |
|
|
|
loader.processContextConfiguration(configAttributes); |
|
|
|
} |
|
|
|
} |
|
|
|
// Otherwise, if a loader claims to generate defaults, let it
|
|
|
|
// Otherwise, if the loader claims to generate defaults, let it
|
|
|
|
// process the configuration, and then verify that it actually did
|
|
|
|
// process the configuration.
|
|
|
|
// generate defaults.
|
|
|
|
else if (loader.generatesDefaults()) { |
|
|
|
//
|
|
|
|
loader.processContextConfiguration(configAttributes); |
|
|
|
// If it generated defaults, there's no need to delegate to
|
|
|
|
if (!emptyResources(configAttributes) && logger.isInfoEnabled()) { |
|
|
|
// additional candidates. So:
|
|
|
|
logger.info(String.format("SmartContextLoader candidate %s " |
|
|
|
// 1) stop iterating
|
|
|
|
+ "generated defaults for context configuration [%s].", loader, configAttributes)); |
|
|
|
// 2) mark the current loader as the winning candidate (?)
|
|
|
|
|
|
|
|
// 3) log an info message.
|
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
if (loader.generatesDefaults()) { |
|
|
|
|
|
|
|
loader.processContextConfiguration(configAttributes); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If any loader claims to generate defaults but none actually did,
|
|
|
|
// If any loader claims to generate defaults but none actually did,
|
|
|
|
// throw an exception.
|
|
|
|
// throw an exception.
|
|
|
|
|
|
|
|
if (generatesDefaults() && emptyResources(configAttributes)) { |
|
|
|
|
|
|
|
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s " |
|
|
|
|
|
|
|
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* TODO Document supports(MergedContextConfiguration) implementation. |
|
|
|
* TODO Document supports(MergedContextConfiguration) implementation. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean supports(MergedContextConfiguration mergedConfig) { |
|
|
|
public boolean supports(MergedContextConfiguration mergedConfig) { |
|
|
|
|
|
|
|
Assert.notNull(mergedConfig, "mergedConfig must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
if (loader.supports(mergedConfig)) { |
|
|
|
if (loader.supports(mergedConfig)) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -117,6 +125,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { |
|
|
|
* TODO Document loadContext(MergedContextConfiguration) implementation. |
|
|
|
* TODO Document loadContext(MergedContextConfiguration) implementation. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
|
|
|
|
Assert.notNull(mergedConfig, "mergedConfig must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
for (SmartContextLoader loader : candidates) { |
|
|
|
|
|
|
|
|
|
|
|
|