[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.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.SmartContextLoader; import org.springframework.test.context.SmartContextLoader;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
@ -59,14 +60,20 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
return false; return false;
} }
/**
* TODO Document emptyResources().
*/
private boolean emptyResources(ContextConfigurationAttributes configAttributes) {
return ObjectUtils.isEmpty(configAttributes.getLocations())
&& ObjectUtils.isEmpty(configAttributes.getClasses());
}
/** /**
* TODO Document processContextConfiguration() implementation. * TODO Document processContextConfiguration() implementation.
*/ */
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
final String[] originalLocations = configAttributes.getLocations(); final boolean emptyResources = emptyResources(configAttributes);
final Class<?>[] originalClasses = configAttributes.getClasses();
final boolean emptyResources = ObjectUtils.isEmpty(originalLocations) && ObjectUtils.isEmpty(originalClasses);
for (SmartContextLoader loader : candidates) { for (SmartContextLoader loader : candidates) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -74,42 +81,43 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
loader.getClass().getName(), configAttributes)); loader.getClass().getName(), configAttributes));
} }
// TODO Implement processContextConfiguration(). // If the original locations and classes were not empty, there's no
//
// If the original locations and classes are not empty, there's no
// need to bother with default generation checks; just let each // need to bother with default generation checks; just let each
// loader process the configuration. // loader process the configuration.
if (!emptyResources) { if (!emptyResources) {
loader.processContextConfiguration(configAttributes); loader.processContextConfiguration(configAttributes);
} }
// Otherwise, if a loader claims to generate defaults, let it // Otherwise, if the loader claims to generate defaults, let it
// process the configuration, and then verify that it actually did // process the configuration.
// generate defaults. else if (loader.generatesDefaults()) {
// loader.processContextConfiguration(configAttributes);
// If it generated defaults, there's no need to delegate to if (!emptyResources(configAttributes) && logger.isInfoEnabled()) {
// additional candidates. So: logger.info(String.format("SmartContextLoader candidate %s "
// 1) stop iterating + "generated defaults for context configuration [%s].", loader, configAttributes));
// 2) mark the current loader as the winning candidate (?)
// 3) log an info message.
else {
if (loader.generatesDefaults()) {
loader.processContextConfiguration(configAttributes);
} }
} }
} }
// If any loader claims to generate defaults but none actually did, // If any loader claims to generate defaults but none actually did,
// throw an exception. // 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. * TODO Document supports(MergedContextConfiguration) implementation.
*/ */
public boolean supports(MergedContextConfiguration mergedConfig) { public boolean supports(MergedContextConfiguration mergedConfig) {
Assert.notNull(mergedConfig, "mergedConfig must not be null");
for (SmartContextLoader loader : candidates) { for (SmartContextLoader loader : candidates) {
if (loader.supports(mergedConfig)) { if (loader.supports(mergedConfig)) {
return true; return true;
} }
} }
return false; return false;
} }
@ -117,6 +125,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
* TODO Document loadContext(MergedContextConfiguration) implementation. * TODO Document loadContext(MergedContextConfiguration) implementation.
*/ */
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
Assert.notNull(mergedConfig, "mergedConfig must not be null");
for (SmartContextLoader loader : candidates) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); 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 * Asserts the <em>execution order</em> of 'before' and 'after' test method
* calls on {@link TestExecutionListener listeners} registered for the * calls on {@link TestExecutionListener listeners} registered for the
@ -108,16 +112,10 @@ public class TestContextManagerTests {
@Before @Before
public void setUpTestContextManager() throws Throwable { public void setUpTestContextManager() throws Throwable {
assertEquals("Verifying the number of registered TestExecutionListeners.", 3,
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,
this.testContextManager.getTestExecutionListeners().size()); this.testContextManager.getTestExecutionListeners().size());
this.testContextManager.beforeTestMethod(new ExampleTestCase(), testMethod); this.testContextManager.beforeTestMethod(new ExampleTestCase(), getTestMethod());
} }
/** /**
@ -133,13 +131,11 @@ public class TestContextManagerTests {
@After @After
public void tearDownTestContextManager() throws Throwable { public void tearDownTestContextManager() throws Throwable {
final Method testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null); this.testContextManager.afterTestMethod(new ExampleTestCase(), getTestMethod(), null);
this.testContextManager.afterTestMethod(new ExampleTestCase(), testMethod, null);
this.testContextManager = null;
} }
@ContextConfiguration @TestExecutionListeners({ FirstTel.class, SecondTel.class, ThirdTel.class })
private static class ExampleTestCase { private static class ExampleTestCase {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -158,13 +154,13 @@ public class TestContextManagerTests {
} }
@Override @Override
public void afterTestMethod(final TestContext testContext) { public void beforeTestMethod(final TestContext testContext) {
afterTestMethodCalls.add(this.name); beforeTestMethodCalls.add(this.name);
} }
@Override @Override
public void beforeTestMethod(final TestContext testContext) { public void afterTestMethod(final TestContext testContext) {
beforeTestMethodCalls.add(this.name); afterTestMethodCalls.add(this.name);
} }
@Override @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 { 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 String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader(); private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
@ -48,6 +48,12 @@ public class DelegatingSmartContextLoaderTests {
// TODO test processContextConfiguration(). // TODO test processContextConfiguration().
} }
@Test(expected = IllegalArgumentException.class)
public void doesNotSupportNullConfig() {
MergedContextConfiguration mergedConfig = null;
loader.supports(mergedConfig);
}
@Test @Test
public void doesNotSupportEmptyConfig() { public void doesNotSupportEmptyConfig() {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY, MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
@ -76,6 +82,19 @@ public class DelegatingSmartContextLoaderTests {
assertTrue(loader.supports(mergedConfig)); 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 @Test
public void loadContext() { public void loadContext() {
// TODO test loadContext(). // TODO test loadContext().