[SPR-8387] Fleshing out unit tests for DelegatingSmartContextLoader.
This commit is contained in:
parent
cc725d7e5c
commit
12eb9d7ed6
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 -------------------------------------------------------
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,11 @@
|
|||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DelegatingSmartContextLoader}.
|
||||
|
@ -26,14 +30,17 @@ import org.junit.Test;
|
|||
*/
|
||||
public class DelegatingSmartContextLoaderTests {
|
||||
|
||||
private DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
|
||||
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
|
||||
|
||||
|
||||
// --- SmartContextLoader --------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void generatesDefaults() {
|
||||
// TODO test generateDefaults().
|
||||
assertTrue(loader.generatesDefaults());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -42,8 +49,31 @@ public class DelegatingSmartContextLoaderTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void supports() {
|
||||
// TODO test supports().
|
||||
public void doesNotSupportEmptyConfig() {
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
|
||||
assertFalse(loader.supports(mergedConfig));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotSupportLocationsAndConfigurationClasses() {
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
|
||||
new String[] { "foo.xml" }, new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
|
||||
assertFalse(loader.supports(mergedConfig));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsLocations() {
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
|
||||
new String[] { "foo.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
|
||||
assertTrue(loader.supports(mergedConfig));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsConfigurationClasses() {
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
|
||||
assertTrue(loader.supports(mergedConfig));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -55,12 +85,12 @@ public class DelegatingSmartContextLoaderTests {
|
|||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void processLocations() {
|
||||
loader.processLocations(getClass(), new String[0]);
|
||||
loader.processLocations(getClass(), EMPTY_STRING_ARRAY);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void loadContextFromLocations() throws Exception {
|
||||
loader.loadContext(new String[0]);
|
||||
loader.loadContext(EMPTY_STRING_ARRAY);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue