Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4706 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
Sam Brannen 15 years ago
parent
commit
02fc11191f
  1. 15
      org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java
  2. 2
      org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
  3. 10
      org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java
  4. 42
      org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java

15
org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java

@ -49,8 +49,8 @@ import org.springframework.util.StringUtils; @@ -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 { @@ -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 { @@ -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 { @@ -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();
}

2
org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java

@ -95,7 +95,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader { @@ -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 -------------------------------------------------------

10
org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java

@ -119,20 +119,20 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { @@ -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));
}

42
org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java

@ -16,7 +16,11 @@ @@ -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; @@ -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 { @@ -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 { @@ -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…
Cancel
Save