[SPR-8387] Fleshing out the implementation of processContextConfiguration() in DelegatingSmartContextLoader.

This commit is contained in:
Sam Brannen 2011-07-15 17:15:45 +00:00
parent 12eb9d7ed6
commit d2e6f82aa3
3 changed files with 82 additions and 37 deletions

View File

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

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -53,9 +53,13 @@ public class TestContextManagerTests {
protected static final Log logger = LogFactory.getLog(TestContextManagerTests.class);
private TestContextManager testContextManager = null;
private final TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class);
private Method getTestMethod() throws NoSuchMethodException {
return ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null);
}
/**
* Asserts the <em>execution order</em> of 'before' and 'after' test method
* calls on {@link TestExecutionListener listeners} registered for the
@ -108,16 +112,10 @@ public class TestContextManagerTests {
@Before
public void setUpTestContextManager() throws Throwable {
final Method testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null);
this.testContextManager = new TestContextManager(ExampleTestCase.class);
this.testContextManager.registerTestExecutionListeners(new NamedTestExecutionListener(FIRST),
new NamedTestExecutionListener(SECOND), new NamedTestExecutionListener(THIRD));
assertEquals("Verifying the number of registered TestExecutionListeners.", 6,
assertEquals("Verifying the number of registered TestExecutionListeners.", 3,
this.testContextManager.getTestExecutionListeners().size());
this.testContextManager.beforeTestMethod(new ExampleTestCase(), testMethod);
this.testContextManager.beforeTestMethod(new ExampleTestCase(), getTestMethod());
}
/**
@ -133,13 +131,11 @@ public class TestContextManagerTests {
@After
public void tearDownTestContextManager() throws Throwable {
final Method testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null);
this.testContextManager.afterTestMethod(new ExampleTestCase(), testMethod, null);
this.testContextManager = null;
this.testContextManager.afterTestMethod(new ExampleTestCase(), getTestMethod(), null);
}
@ContextConfiguration
@TestExecutionListeners({ FirstTel.class, SecondTel.class, ThirdTel.class })
private static class ExampleTestCase {
@SuppressWarnings("unused")
@ -158,13 +154,13 @@ public class TestContextManagerTests {
}
@Override
public void afterTestMethod(final TestContext testContext) {
afterTestMethodCalls.add(this.name);
public void beforeTestMethod(final TestContext testContext) {
beforeTestMethodCalls.add(this.name);
}
@Override
public void beforeTestMethod(final TestContext testContext) {
beforeTestMethodCalls.add(this.name);
public void afterTestMethod(final TestContext testContext) {
afterTestMethodCalls.add(this.name);
}
@Override
@ -173,4 +169,25 @@ public class TestContextManagerTests {
}
}
private static class FirstTel extends NamedTestExecutionListener {
public FirstTel() {
super(FIRST);
}
}
private static class SecondTel extends NamedTestExecutionListener {
public SecondTel() {
super(SECOND);
}
}
private static class ThirdTel extends NamedTestExecutionListener {
public ThirdTel() {
super(THIRD);
}
}
}

View File

@ -30,8 +30,8 @@ import org.springframework.test.context.MergedContextConfiguration;
*/
public class DelegatingSmartContextLoaderTests {
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
@ -48,6 +48,12 @@ public class DelegatingSmartContextLoaderTests {
// TODO test processContextConfiguration().
}
@Test(expected = IllegalArgumentException.class)
public void doesNotSupportNullConfig() {
MergedContextConfiguration mergedConfig = null;
loader.supports(mergedConfig);
}
@Test
public void doesNotSupportEmptyConfig() {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
@ -76,6 +82,19 @@ public class DelegatingSmartContextLoaderTests {
assertTrue(loader.supports(mergedConfig));
}
@Test(expected = IllegalArgumentException.class)
public void loadContextWithNullConfig() throws Exception {
MergedContextConfiguration mergedConfig = null;
loader.loadContext(mergedConfig);
}
@Test(expected = IllegalStateException.class)
public void loadContextWithoutLocationsAndConfigurationClasses() throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
}
@Test
public void loadContext() {
// TODO test loadContext().