[SPR-8387] Fleshing out unit tests for DelegatingSmartContextLoader.

This commit is contained in:
Sam Brannen
2011-07-15 16:12:34 +00:00
parent cc725d7e5c
commit 12eb9d7ed6
4 changed files with 53 additions and 16 deletions

View File

@@ -49,8 +49,8 @@ import org.springframework.util.StringUtils;
*/
public class MergedContextConfiguration {
private static final String[] EMPTY_STRING_ARRAY = new String[] {};
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[] {};
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private final Class<?> testClass;
@@ -85,6 +85,13 @@ public class MergedContextConfiguration {
return StringUtils.toStringArray(sortedProfilesSet);
}
/**
* Generate a null-safe {@link String} representation of the supplied {@link ContextLoader}.
*/
private static String nullSafeToString(ContextLoader contextLoader) {
return contextLoader == null ? "null" : contextLoader.getClass().getName();
}
/**
* Generate a context <em>key</em> from the supplied values.
*/
@@ -94,7 +101,7 @@ public class MergedContextConfiguration {
String locationsKey = ObjectUtils.nullSafeToString(locations);
String classesKey = ObjectUtils.nullSafeToString(classes);
String activeProfilesKey = ObjectUtils.nullSafeToString(activeProfiles);
String contextLoaderKey = contextLoader == null ? "null" : contextLoader.getClass().getName();
String contextLoaderKey = nullSafeToString(contextLoader);
return String.format("locations = %s, classes = %s, activeProfiles = %s, contextLoader = %s", locationsKey,
classesKey, activeProfilesKey, contextLoaderKey);
@@ -187,7 +194,7 @@ public class MergedContextConfiguration {
.append("locations", ObjectUtils.nullSafeToString(this.locations))//
.append("classes", ObjectUtils.nullSafeToString(this.classes))//
.append("activeProfiles", ObjectUtils.nullSafeToString(this.activeProfiles))//
.append("contextLoader", this.contextLoader)//
.append("contextLoader", nullSafeToString(contextLoader))//
.append("contextKey", this.contextKey)//
.toString();
}

View File

@@ -95,7 +95,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
* TODO Document default supports(MergedContextConfiguration) implementation.
*/
public boolean supports(MergedContextConfiguration mergedConfig) {
return !ObjectUtils.isEmpty(mergedConfig.getLocations());
return !ObjectUtils.isEmpty(mergedConfig.getLocations()) && ObjectUtils.isEmpty(mergedConfig.getClasses());
}
// --- ContextLoader -------------------------------------------------------

View File

@@ -119,20 +119,20 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
for (SmartContextLoader loader : candidates) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to load context from [%s].", loader.getClass().getName(),
mergedConfig));
}
// 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 (loader.supports(mergedConfig)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to load context from [%s].",
loader.getClass().getName(), mergedConfig));
}
return loader.loadContext(mergedConfig);
}
}
throw new IllegalStateException(String.format("None of the candidate SmartContextLoaders %s "
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
+ "was able to load an ApplicationContext from [%s].", candidates, mergedConfig));
}