[SPR-8386] SmartContextLoader enhancements:

- introduced processContextConfigurationAttributes() method in SmartContextLoader SPI
- refactored AnnotationConfigContextLoader, AbstractContextLoader, AbstractGenericContextLoader, ContextLoaderUtils, and TestContext implementations to take advantage of the SmartContextLoader SPI, MergedContextConfiguration, and ContextConfigurationAttributes
- deleted ResourceTypeAwareContextLoader
- deleted ContextLoaderUtils.LocationsResolver and implementations
- moved context key generation from TestContext to MergedContextConfiguration
This commit is contained in:
Sam Brannen
2011-06-17 21:49:06 +00:00
parent 8e35734856
commit 9a56deb283
12 changed files with 317 additions and 452 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.test.context.support.GenericXmlContextLoader;
*/
public class ContextLoaderUtilsTests {
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[] {};
private static final String[] EMPTY_STRING_ARRAY = new String[] {};
@@ -48,7 +49,7 @@ public class ContextLoaderUtilsTests {
assertArrayEquals(expectedLocations, attributes.getLocations());
assertArrayEquals(expectedClasses, attributes.getClasses());
assertEquals(expectedInheritLocations, attributes.isInheritLocations());
assertEquals(expectedContextLoaderClass, attributes.getContextLoader());
assertEquals(expectedContextLoaderClass, attributes.getContextLoaderClass());
}
private void assertFooAttributes(ContextConfigurationAttributes attributes) {
@@ -62,12 +63,14 @@ public class ContextLoaderUtilsTests {
}
private void assertMergedContextConfiguration(MergedContextConfiguration mergedConfig, Class<?> expectedTestClass,
String[] expectedLocations, Class<? extends ContextLoader> expectedContextLoaderClass) {
String[] expectedLocations, Class<?>[] expectedClasses,
Class<? extends ContextLoader> expectedContextLoaderClass) {
assertNotNull(mergedConfig);
assertEquals(expectedTestClass, mergedConfig.getTestClass());
assertNotNull(mergedConfig.getLocations());
assertArrayEquals(expectedLocations, mergedConfig.getLocations());
assertNotNull(mergedConfig.getClasses());
assertArrayEquals(expectedClasses, mergedConfig.getClasses());
assertNotNull(mergedConfig.getActiveProfiles());
assertEquals(expectedContextLoaderClass, mergedConfig.getContextLoader().getClass());
}
@@ -82,7 +85,7 @@ public class ContextLoaderUtilsTests {
List<ContextConfigurationAttributes> attributesList = ContextLoaderUtils.resolveContextConfigurationAttributes(BareAnnotations.class);
assertNotNull(attributesList);
assertEquals(1, attributesList.size());
assertAttributes(attributesList.get(0), BareAnnotations.class, EMPTY_STRING_ARRAY, new Class<?>[] {},
assertAttributes(attributesList.get(0), BareAnnotations.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY,
ContextLoader.class, true);
}
@@ -117,7 +120,7 @@ public class ContextLoaderUtilsTests {
mergedConfig,
testClass,
new String[] { "classpath:/org/springframework/test/context/ContextLoaderUtilsTests$BareAnnotations-context.xml" },
GenericXmlContextLoader.class);
EMPTY_CLASS_ARRAY, GenericXmlContextLoader.class);
}
@Test
@@ -126,31 +129,28 @@ public class ContextLoaderUtilsTests {
MergedContextConfiguration mergedConfig = ContextLoaderUtils.buildMergedContextConfiguration(testClass, null);
assertMergedContextConfiguration(mergedConfig, testClass, new String[] { "classpath:/foo.xml" },
GenericXmlContextLoader.class);
new Class<?>[] { FooConfig.class }, GenericXmlContextLoader.class);
}
@Test
public void buildMergedContextConfigurationWithLocalAnnotationAndOverriddenContexLoader() {
public void buildMergedContextConfigurationWithLocalAnnotationAndOverriddenContextLoader() {
Class<?> testClass = Foo.class;
Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
MergedContextConfiguration mergedConfig = ContextLoaderUtils.buildMergedContextConfiguration(testClass,
expectedContextLoaderClass.getName());
assertMergedContextConfiguration(mergedConfig, testClass, new String[] { "classpath:/foo.xml" },
expectedContextLoaderClass);
new Class<?>[] { FooConfig.class }, expectedContextLoaderClass);
}
@Test
public void buildMergedContextConfigurationWithLocalAndInheritedAnnotations() {
Class<?> testClass = Bar.class;
String[] expectedLocations = new String[] { "/foo.xml", "/bar.xml" };
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
MergedContextConfiguration mergedConfig = ContextLoaderUtils.buildMergedContextConfiguration(testClass, null);
// TODO Assert @Configuration classes instead of locations
String[] expectedLocations = new String[] {
"org.springframework.test.context.ContextLoaderUtilsTests$FooConfig",
"org.springframework.test.context.ContextLoaderUtilsTests$BarConfig" };
assertMergedContextConfiguration(mergedConfig, testClass, expectedLocations,
assertMergedContextConfiguration(mergedConfig, testClass, expectedLocations, expectedClasses,
AnnotationConfigContextLoader.class);
}

View File

@@ -27,7 +27,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
/**
* Unit tests for verifying proper behavior of the {@link ContextCache} in
* conjunction with cache keys generated in {@link TestContext}.
* conjunction with cache keys used in {@link TestContext}.
*
* @author Sam Brannen
* @since 3.1

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 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.
@@ -22,8 +22,11 @@ import org.junit.runners.Suite.SuiteClasses;
import org.springframework.test.context.ClassLevelDirtiesContextTests;
import org.springframework.test.context.SpringRunnerContextCacheTests;
import org.springframework.test.context.junit4.annotation.AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests;
import org.springframework.test.context.junit4.annotation.BeanOverridingDefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.BeanOverridingExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.ExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests;
import org.springframework.test.context.junit4.profile.annotation.DefaultProfileAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.annotation.DevProfileAnnotationConfigTests;
@@ -31,20 +34,16 @@ import org.springframework.test.context.junit4.profile.xml.DefaultProfileXmlConf
import org.springframework.test.context.junit4.profile.xml.DevProfileXmlConfigTests;
/**
* <p>
* JUnit 4 based test suite for tests involving {@link SpringJUnit4ClassRunner}
* and the <em>Spring TestContext Framework</em>.
* </p>
* <p>
* This test suite serves a dual purpose of verifying that tests run with
* {@link SpringJUnit4ClassRunner} can be used in conjunction with JUnit 4's
* JUnit test suite for tests involving {@link SpringJUnit4ClassRunner} and the
* <em>Spring TestContext Framework</em>.
*
* <p>This test suite serves a dual purpose of verifying that tests run with
* {@link SpringJUnit4ClassRunner} can be used in conjunction with JUnit's
* {@link Suite} runner.
* </p>
* <p>
* Note that tests included in this suite will be executed at least twice if run
* from an automated build process, test runner, etc. that is configured to run
* tests based on a &quot;*Tests.class&quot; pattern match.
* </p>
*
* <p>Note that tests included in this suite will be executed at least twice if
* run from an automated build process, test runner, etc. that is configured to
* run tests based on a &quot;*Tests.class&quot; pattern match.
*
* @author Sam Brannen
* @since 2.5
@@ -58,6 +57,9 @@ StandardJUnit4FeaturesTests.class,//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
DefaultConfigClassesBaseTests.class,//
DefaultConfigClassesInheritedTests.class,//
BeanOverridingDefaultConfigClassesInheritedTests.class,//
ExplicitConfigClassesInheritedTests.class,//
BeanOverridingExplicitConfigClassesInheritedTests.class,//
DefaultProfileAnnotationConfigTests.class,//
DevProfileAnnotationConfigTests.class, //
DefaultProfileXmlConfigTests.class,//