[SPR-8387] Introduced hasResources() in ContextConfigurationAttributes; plus minor polishing.

This commit is contained in:
Sam Brannen 2011-07-15 19:03:16 +00:00
parent d2e6f82aa3
commit b8624b470c
3 changed files with 23 additions and 22 deletions

View File

@ -55,7 +55,7 @@ public class ContextConfigurationAttributes {
*
* @throws IllegalStateException if both the locations and value attributes have been declared
*/
static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
private static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
Assert.notNull(declaringClass, "declaringClass must not be null");
String[] locations = contextConfiguration.locations();
@ -165,6 +165,18 @@ public class ContextConfigurationAttributes {
this.classes = classes;
}
/**
* Determine if this {@code ContextConfigurationAttributes} instance has
* either path-based resource locations or class-based resources.
* @return <code>true</code> if neither the {@link #getLocations() locations}
* nor the {@link #getClasses() classes} array is empty
* @see #getLocations()
* @see #getClasses()
*/
public boolean hasResources() {
return !ObjectUtils.isEmpty(getLocations()) && !ObjectUtils.isEmpty(getClasses());
}
/**
* Get the <code>inheritLocations</code> flag that was declared via
* {@link ContextConfiguration @ContextConfiguration}.

View File

@ -138,16 +138,16 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
List<Class<?>> configClasses = new ArrayList<Class<?>>();
for (Class<?> configClass : declaringClass.getDeclaredClasses()) {
if (isDefaultConfigurationClassCandidate(configClass)) {
configClasses.add(configClass);
for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
if (isDefaultConfigurationClassCandidate(candidate)) {
configClasses.add(candidate);
}
else {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
+ "with @Configuration to be considered a default configuration class.",
configClass.getName()));
candidate.getName()));
}
}
}

View File

@ -27,7 +27,6 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* TODO Document DelegatingSmartContextLoader.
@ -60,38 +59,30 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
return false;
}
/**
* TODO Document emptyResources().
*/
private boolean emptyResources(ContextConfigurationAttributes configAttributes) {
return ObjectUtils.isEmpty(configAttributes.getLocations())
&& ObjectUtils.isEmpty(configAttributes.getClasses());
}
/**
* TODO Document processContextConfiguration() implementation.
*/
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
final boolean emptyResources = emptyResources(configAttributes);
final boolean hasResources = configAttributes.hasResources();
for (SmartContextLoader loader : candidates) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to process context configuration [%s].",
logger.debug(String.format("Potentially delegating to %s to process context configuration [%s].",
loader.getClass().getName(), configAttributes));
}
// If the original locations and classes were not empty, there's no
// need to bother with default generation checks; just let each
// loader process the configuration.
if (!emptyResources) {
if (hasResources) {
loader.processContextConfiguration(configAttributes);
}
// Otherwise, if the loader claims to generate defaults, let it
// process the configuration.
else if (loader.generatesDefaults()) {
loader.processContextConfiguration(configAttributes);
if (!emptyResources(configAttributes) && logger.isInfoEnabled()) {
if (configAttributes.hasResources() && logger.isInfoEnabled()) {
logger.info(String.format("SmartContextLoader candidate %s "
+ "generated defaults for context configuration [%s].", loader, configAttributes));
}
@ -100,7 +91,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
// If any loader claims to generate defaults but none actually did,
// throw an exception.
if (generatesDefaults() && emptyResources(configAttributes)) {
if (generatesDefaults() && !configAttributes.hasResources()) {
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes));
}
@ -128,10 +119,8 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
Assert.notNull(mergedConfig, "mergedConfig must not be null");
for (SmartContextLoader loader : candidates) {
// Ask each loader if it can load a context from the mergedConfig.
// If a loader can, let it; otherwise, continue iterating over all
// remaining candidates.
// If it can, let it; otherwise, keep iterating.
if (loader.supports(mergedConfig)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to load context from [%s].",