Provide support for context hierarchies in the TCF

Prior to this commit the Spring TestContext Framework supported creating
only flat, non-hierarchical contexts. There was no easy way to create
contexts with parent-child relationships.

This commit addresses this issue by introducing a new @ContextHierarchy
annotation that can be used in conjunction with @ContextConfiguration
for declaring hierarchies of application contexts, either within a
single test class or within a test class hierarchy. In addition,
@DirtiesContext now supports a new 'hierarchyMode' attribute for
controlling context cache clearing for context hierarchies.

- Introduced a new @ContextHierarchy annotation.
- Introduced 'name' attribute in @ContextConfiguration.
- Introduced 'name' property in ContextConfigurationAttributes.
- TestContext is now aware of @ContextHierarchy in addition to
  @ContextConfiguration.
- Introduced findAnnotationDeclaringClassForTypes() in AnnotationUtils.
- Introduced resolveContextHierarchyAttributes() in ContextLoaderUtils.
- Introduced buildContextHierarchyMap() in ContextLoaderUtils.
- @ContextConfiguration and @ContextHierarchy may not be used as
  top-level, class-level annotations simultaneously.
- Introduced reference to the parent configuration in
  MergedContextConfiguration and WebMergedContextConfiguration.
- Introduced overloaded buildMergedContextConfiguration() methods in
  ContextLoaderUtils in order to handle context hierarchies separately
  from conventional, non-hierarchical contexts.
- Introduced hashCode() and equals() in ContextConfigurationAttributes.
- ContextLoaderUtils ensures uniqueness of @ContextConfiguration
  elements within a single @ContextHierarchy declaration.
- Introduced CacheAwareContextLoaderDelegate that can be used for
  loading contexts with transparent support for interacting with the
  context cache -- for example, for retrieving the parent application
  context in a context hierarchy.
- TestContext now delegates to CacheAwareContextLoaderDelegate for
  loading contexts.
- Introduced getParentApplicationContext() in MergedContextConfiguration
- The loadContext(MergedContextConfiguration) methods in
  AbstractGenericContextLoader and AbstractGenericWebContextLoader now
  set the parent context as appropriate.
- Introduced 'hierarchyMode' attribute in @DirtiesContext with a
  corresponding HierarchyMode enum that defines EXHAUSTIVE and
  CURRENT_LEVEL cache removal modes.
- ContextCache now internally tracks the relationships between contexts
  that make up a context hierarchy. Furthermore, when a context is
  removed, if it is part of a context hierarchy all corresponding
  contexts will be removed from the cache according to the supplied
  HierarchyMode.
- AbstractGenericWebContextLoader will set a loaded context as the
  ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE in the MockServletContext when
  context hierarchies are used if the context has no parent or if the
  context has a parent that is not a WAC.
- Where appropriate, updated Javadoc to refer to the
  ServletTestExecutionListener, which was introduced in 3.2.0.
- Updated Javadoc to avoid and/or suppress warnings in spring-test.
- Suppressed remaining warnings in code in spring-test.

Issue: SPR-5613, SPR-9863
This commit is contained in:
Sam Brannen
2013-01-11 01:02:08 +01:00
parent 7bc5353e07
commit 98074e7762
55 changed files with 3919 additions and 551 deletions

View File

@@ -0,0 +1,329 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.*;
import static org.springframework.test.context.SpringRunnerContextCacheTests.*;
/**
* Integration tests for verifying proper behavior of the {@link ContextCache} in
* conjunction with cache keys used in {@link TestContext}.
*
* @author Sam Brannen
* @since 3.1
* @see SpringRunnerContextCacheTests
*/
public class ContextCacheTests {
private ContextCache contextCache = new ContextCache();
@Before
public void initialCacheState() {
assertContextCacheStatistics(contextCache, "initial state", 0, 0, 0);
assertParentContextCount(0);
}
private void assertParentContextCount(int expected) {
assertEquals("parent context count", expected, contextCache.getParentContextCount());
}
private MergedContextConfiguration getMergedContextConfiguration(TestContext testContext) {
return (MergedContextConfiguration) ReflectionTestUtils.getField(testContext, "mergedContextConfiguration");
}
private ApplicationContext loadContext(Class<?> testClass) {
TestContext testContext = new TestContext(testClass, contextCache);
return testContext.getApplicationContext();
}
private void loadCtxAndAssertStats(Class<?> testClass, int expectedSize, int expectedHitCount, int expectedMissCount) {
assertNotNull(loadContext(testClass));
assertContextCacheStatistics(contextCache, testClass.getName(), expectedSize, expectedHitCount,
expectedMissCount);
}
@Test
public void verifyCacheKeyIsBasedOnContextLoader() {
loadCtxAndAssertStats(AnnotationConfigContextLoaderTestCase.class, 1, 0, 1);
loadCtxAndAssertStats(AnnotationConfigContextLoaderTestCase.class, 1, 1, 1);
loadCtxAndAssertStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 1, 2);
loadCtxAndAssertStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 2, 2);
loadCtxAndAssertStats(AnnotationConfigContextLoaderTestCase.class, 2, 3, 2);
loadCtxAndAssertStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 4, 2);
}
@Test
public void verifyCacheKeyIsBasedOnActiveProfiles() {
loadCtxAndAssertStats(FooBarProfilesTestCase.class, 1, 0, 1);
loadCtxAndAssertStats(FooBarProfilesTestCase.class, 1, 1, 1);
// Profiles {foo, bar} should hash to the same as {bar,foo}
loadCtxAndAssertStats(BarFooProfilesTestCase.class, 1, 2, 1);
loadCtxAndAssertStats(FooBarProfilesTestCase.class, 1, 3, 1);
loadCtxAndAssertStats(FooBarProfilesTestCase.class, 1, 4, 1);
loadCtxAndAssertStats(BarFooProfilesTestCase.class, 1, 5, 1);
}
@Test
public void verifyCacheBehaviorForContextHierarchies() {
int size = 0;
int hits = 0;
int misses = 0;
// Level 1
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel1TestCase.class, ++size, hits, ++misses);
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel1TestCase.class, size, ++hits, misses);
// Level 2
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel2TestCase.class, ++size /* L2 */, ++hits /* L1 */,
++misses /* L2 */);
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel2TestCase.class, size, ++hits /* L2 */, misses);
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel2TestCase.class, size, ++hits /* L2 */, misses);
// Level 3-A
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel3aTestCase.class, ++size /* L3A */, ++hits /* L2 */,
++misses /* L3A */);
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel3aTestCase.class, size, ++hits /* L3A */, misses);
// Level 3-B
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel3bTestCase.class, ++size /* L3B */, ++hits /* L2 */,
++misses /* L3B */);
loadCtxAndAssertStats(ClassHierarchyContextHierarchyLevel3bTestCase.class, size, ++hits /* L3B */, misses);
}
@Test
public void removeContextHierarchyCacheLevel1() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 1
// Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(),
HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
assertParentContextCount(0);
}
@Test
public void removeContextHierarchyCacheLevel1WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 1
// Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(),
HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
assertParentContextCount(0);
}
@Test
public void removeContextHierarchyCacheLevel2() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should also remove Levels 3-A and 3-B, leaving only Level 1 as a context in the
// cache but also removing the Level 1 hierarchy since all children have been
// removed.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
assertParentContextCount(0);
}
@Test
public void removeContextHierarchyCacheLevel2WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should wipe the cache
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
assertParentContextCount(0);
}
@Test
public void removeContextHierarchyCacheLevel3Then2() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 3-A
contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 3-A", 3, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should also remove Level 3-B, leaving only Level 1.
contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
assertParentContextCount(0);
}
@Test
public void removeContextHierarchyCacheLevel3Then2WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = new TestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = new TestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 3-A
// Should wipe the cache.
contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 3-A", 0, 1, 4);
assertParentContextCount(0);
// Remove Level 2
// Should not actually do anything since the cache was cleared in the
// previous step. So the stats should remain the same.
contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
assertParentContextCount(0);
}
@Configuration
static class Config {
}
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class AnnotationConfigContextLoaderTestCase {
}
@ContextConfiguration(classes = Config.class, loader = CustomAnnotationConfigContextLoader.class)
private static class CustomAnnotationConfigContextLoaderTestCase {
}
private static class CustomAnnotationConfigContextLoader extends AnnotationConfigContextLoader {
}
@ActiveProfiles({ "foo", "bar" })
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class FooBarProfilesTestCase {
}
@ActiveProfiles({ "bar", "foo" })
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class BarFooProfilesTestCase {
}
@ContextHierarchy({ @ContextConfiguration })
private static class ClassHierarchyContextHierarchyLevel1TestCase {
@Configuration
static class Level1Config {
}
}
@ContextHierarchy({ @ContextConfiguration })
private static class ClassHierarchyContextHierarchyLevel2TestCase extends
ClassHierarchyContextHierarchyLevel1TestCase {
@Configuration
static class Level2Config {
}
}
@ContextHierarchy({ @ContextConfiguration })
private static class ClassHierarchyContextHierarchyLevel3aTestCase extends
ClassHierarchyContextHierarchyLevel2TestCase {
@Configuration
static class Level3aConfig {
}
}
@ContextHierarchy({ @ContextConfiguration })
private static class ClassHierarchyContextHierarchyLevel3bTestCase extends
ClassHierarchyContextHierarchyLevel2TestCase {
@Configuration
static class Level3bConfig {
}
}
}

View File

@@ -0,0 +1,212 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context;
import org.junit.After;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Integration tests that verify proper behavior of {@link DirtiesContext @DirtiesContext}
* in conjunction with context hierarchies configured via {@link ContextHierarchy @ContextHierarchy}.
*
* @author Sam Brannen
* @author Tadaya Tsuyukubo
* @since 3.2.2
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ContextHierarchyDirtiesContextTests {
private static ApplicationContext context;
@After
public void cleanUp() {
ContextHierarchyDirtiesContextTests.context = null;
}
@Test
public void classLevelDirtiesContextWithCurrentLevelHierarchyMode() {
runTestAndVerifyHierarchies(ClassLevelDirtiesContextWithCurrentLevelModeTestCase.class, true, true, false);
}
@Test
public void classLevelDirtiesContextWithExhaustiveHierarchyMode() {
runTestAndVerifyHierarchies(ClassLevelDirtiesContextWithExhaustiveModeTestCase.class, false, false, false);
}
@Test
public void methodLevelDirtiesContextWithCurrentLevelHierarchyMode() {
runTestAndVerifyHierarchies(MethodLevelDirtiesContextWithCurrentLevelModeTestCase.class, true, true, false);
}
@Test
public void methodLevelDirtiesContextWithExhaustiveHierarchyMode() {
runTestAndVerifyHierarchies(MethodLevelDirtiesContextWithExhaustiveModeTestCase.class, false, false, false);
}
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
boolean isBarContextActive, boolean isBazContextActive) {
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(testClass);
assertTrue("all tests passed", result.wasSuccessful());
assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue());
ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context;
assertEquals("baz", bazContext.getBean("bean", String.class));
assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive));
ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent();
assertThat(barContext, notNullValue());
assertEquals("bar", barContext.getBean("bean", String.class));
assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive));
ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent();
assertThat(fooContext, notNullValue());
assertEquals("foo", fooContext.getBean("bean", String.class));
assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive));
}
// -------------------------------------------------------------------------
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration(name = "foo"))
static abstract class FooTestCase implements ApplicationContextAware {
@Configuration
static class Config {
@Bean
public String bean() {
return "foo";
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ContextHierarchyDirtiesContextTests.context = applicationContext;
}
}
@ContextHierarchy(@ContextConfiguration(name = "bar"))
static abstract class BarTestCase extends FooTestCase {
@Configuration
static class Config {
@Bean
public String bean() {
return "bar";
}
}
}
@ContextHierarchy(@ContextConfiguration(name = "baz"))
static abstract class BazTestCase extends BarTestCase {
@Configuration
static class Config {
@Bean
public String bean() {
return "baz";
}
}
}
// -------------------------------------------------------------------------
/**
* {@link DirtiesContext} is declared at the class level, without specifying
* the {@link DirtiesContext.HierarchyMode}.
* <p>After running this test class, the context cache should be <em>exhaustively</em>
* cleared beginning from the current context hierarchy, upwards to the highest
* parent context, and then back down through all subhierarchies of the parent
* context.
*/
@DirtiesContext
public static class ClassLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
@Test
public void test() {
}
}
/**
* {@link DirtiesContext} is declared at the class level, specifying the
* {@link DirtiesContext.HierarchyMode#CURRENT_LEVEL CURRENT_LEVEL} hierarchy mode.
* <p>After running this test class, the context cache should be cleared
* beginning from the current context hierarchy and down through all subhierarchies.
*/
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
public static class ClassLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
@Test
public void test() {
}
}
/**
* {@link DirtiesContext} is declared at the method level, without specifying
* the {@link DirtiesContext.HierarchyMode}.
* <p>After running this test class, the context cache should be <em>exhaustively</em>
* cleared beginning from the current context hierarchy, upwards to the highest
* parent context, and then back down through all subhierarchies of the parent
* context.
*/
public static class MethodLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
@Test
@DirtiesContext
public void test() {
}
}
/**
* {@link DirtiesContext} is declared at the method level, specifying the
* {@link DirtiesContext.HierarchyMode#CURRENT_LEVEL CURRENT_LEVEL} hierarchy mode.
* <p>After running this test class, the context cache should be cleared
* beginning from the current context hierarchy and down through all subhierarchies.
*/
public static class MethodLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
@Test
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
public void test() {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -16,13 +16,16 @@
package org.springframework.test.context;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.context.ContextLoaderUtils.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
@@ -105,6 +108,233 @@ public class ContextLoaderUtilsTests {
assertEquals(expectedInitializerClasses, mergedConfig.getContextInitializerClasses());
}
private void debugConfigAttributes(List<ContextConfigurationAttributes> configAttributesList) {
// for (ContextConfigurationAttributes configAttributes : configAttributesList) {
// System.err.println(configAttributes);
// }
}
@Test(expected = IllegalStateException.class)
public void resolveContextHierarchyAttributesForSingleTestClassWithContextConfigurationAndContextHierarchy() {
resolveContextHierarchyAttributes(SingleTestClassWithContextConfigurationAndContextHierarchy.class);
}
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithImplicitSingleLevelContextHierarchy() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(BareAnnotations.class);
assertEquals(1, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0);
assertEquals(1, configAttributesList.size());
debugConfigAttributes(configAttributesList);
}
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithSingleLevelContextHierarchy() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(SingleTestClassWithSingleLevelContextHierarchy.class);
assertEquals(1, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0);
assertEquals(1, configAttributesList.size());
debugConfigAttributes(configAttributesList);
}
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithTripleLevelContextHierarchy() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(SingleTestClassWithTripleLevelContextHierarchy.class);
assertEquals(1, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0);
assertEquals(3, configAttributesList.size());
debugConfigAttributes(configAttributesList);
}
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithSingleLevelContextHierarchies() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithSingleLevelContextHierarchy.class);
assertEquals(3, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
debugConfigAttributes(configAttributesListClassLevel1);
assertEquals(1, configAttributesListClassLevel1.size());
assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml"));
List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
debugConfigAttributes(configAttributesListClassLevel2);
assertEquals(1, configAttributesListClassLevel2.size());
assertArrayEquals(new String[] { "two-A.xml", "two-B.xml" },
configAttributesListClassLevel2.get(0).getLocations());
List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
debugConfigAttributes(configAttributesListClassLevel3);
assertEquals(1, configAttributesListClassLevel3.size());
assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("three.xml"));
}
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithBareContextConfigurationInSubclass() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass2WithBareContextConfigurationInSubclass.class);
assertEquals(2, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
debugConfigAttributes(configAttributesListClassLevel1);
assertEquals(1, configAttributesListClassLevel1.size());
assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml"));
List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
debugConfigAttributes(configAttributesListClassLevel2);
assertEquals(1, configAttributesListClassLevel2.size());
assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("two.xml"));
}
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithBareContextConfigurationInSuperclass() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass2WithBareContextConfigurationInSuperclass.class);
assertEquals(2, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
debugConfigAttributes(configAttributesListClassLevel1);
assertEquals(1, configAttributesListClassLevel1.size());
assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml"));
List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
debugConfigAttributes(configAttributesListClassLevel2);
assertEquals(1, configAttributesListClassLevel2.size());
assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("two.xml"));
}
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithMultiLevelContextHierarchies() {
List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithMultiLevelContextHierarchy.class);
assertEquals(3, hierarchyAttributes.size());
List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
debugConfigAttributes(configAttributesListClassLevel1);
assertEquals(2, configAttributesListClassLevel1.size());
assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("1-A.xml"));
assertThat(configAttributesListClassLevel1.get(1).getLocations()[0], equalTo("1-B.xml"));
List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
debugConfigAttributes(configAttributesListClassLevel2);
assertEquals(2, configAttributesListClassLevel2.size());
assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("2-A.xml"));
assertThat(configAttributesListClassLevel2.get(1).getLocations()[0], equalTo("2-B.xml"));
List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
debugConfigAttributes(configAttributesListClassLevel3);
assertEquals(3, configAttributesListClassLevel3.size());
assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("3-A.xml"));
assertThat(configAttributesListClassLevel3.get(1).getLocations()[0], equalTo("3-B.xml"));
assertThat(configAttributesListClassLevel3.get(2).getLocations()[0], equalTo("3-C.xml"));
}
private void assertContextConfigEntriesAreNotUnique(Class<?> testClass) {
try {
resolveContextHierarchyAttributes(testClass);
fail("Should throw an IllegalStateException");
}
catch (IllegalStateException e) {
String msg = String.format(
"The @ContextConfiguration elements configured via @ContextHierarchy in test class [%s] must define unique contexts to load.",
testClass.getName());
assertEquals(msg, e.getMessage());
}
}
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithMultiLevelContextHierarchyWithEmptyContextConfig() {
assertContextConfigEntriesAreNotUnique(SingleTestClassWithMultiLevelContextHierarchyWithEmptyContextConfig.class);
}
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithMultiLevelContextHierarchyWithDuplicatedContextConfig() {
assertContextConfigEntriesAreNotUnique(SingleTestClassWithMultiLevelContextHierarchyWithDuplicatedContextConfig.class);
}
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchies() {
Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchy.class);
assertThat(map.size(), is(3));
assertThat(map.keySet(), hasItems("alpha", "beta", "gamma"));
List<ContextConfigurationAttributes> alphaConfig = map.get("alpha");
assertThat(alphaConfig.size(), is(3));
assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml"));
assertThat(alphaConfig.get(1).getLocations()[0], is("2-A.xml"));
assertThat(alphaConfig.get(2).getLocations()[0], is("3-A.xml"));
List<ContextConfigurationAttributes> betaConfig = map.get("beta");
assertThat(betaConfig.size(), is(3));
assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml"));
assertThat(betaConfig.get(1).getLocations()[0], is("2-B.xml"));
assertThat(betaConfig.get(2).getLocations()[0], is("3-B.xml"));
List<ContextConfigurationAttributes> gammaConfig = map.get("gamma");
assertThat(gammaConfig.size(), is(1));
assertThat(gammaConfig.get(0).getLocations()[0], is("3-C.xml"));
}
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndUnnamedConfig() {
Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchyAndUnnamedConfig.class);
String level1 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 1;
String level2 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 2;
String level3 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 3;
String level4 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 4;
String level5 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 5;
String level6 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 6;
String level7 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 7;
assertThat(map.size(), is(7));
assertThat(map.keySet(), hasItems(level1, level2, level3, level4, level5, level6, level7));
List<ContextConfigurationAttributes> level1Config = map.get(level1);
assertThat(level1Config.size(), is(1));
assertThat(level1Config.get(0).getLocations()[0], is("1-A.xml"));
List<ContextConfigurationAttributes> level2Config = map.get(level2);
assertThat(level2Config.size(), is(1));
assertThat(level2Config.get(0).getLocations()[0], is("1-B.xml"));
List<ContextConfigurationAttributes> level3Config = map.get(level3);
assertThat(level3Config.size(), is(1));
assertThat(level3Config.get(0).getLocations()[0], is("2-A.xml"));
// ...
List<ContextConfigurationAttributes> level7Config = map.get(level7);
assertThat(level7Config.size(), is(1));
assertThat(level7Config.get(0).getLocations()[0], is("3-C.xml"));
}
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndPartiallyNamedConfig() {
Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyAndPartiallyNamedConfig.class);
String level1 = "parent";
String level2 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 2;
String level3 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 3;
assertThat(map.size(), is(3));
assertThat(map.keySet(), hasItems(level1, level2, level3));
Iterator<String> levels = map.keySet().iterator();
assertThat(levels.next(), is(level1));
assertThat(levels.next(), is(level2));
assertThat(levels.next(), is(level3));
List<ContextConfigurationAttributes> level1Config = map.get(level1);
assertThat(level1Config.size(), is(2));
assertThat(level1Config.get(0).getLocations()[0], is("1-A.xml"));
assertThat(level1Config.get(1).getLocations()[0], is("2-A.xml"));
List<ContextConfigurationAttributes> level2Config = map.get(level2);
assertThat(level2Config.size(), is(1));
assertThat(level2Config.get(0).getLocations()[0], is("1-B.xml"));
List<ContextConfigurationAttributes> level3Config = map.get(level3);
assertThat(level3Config.size(), is(1));
assertThat(level3Config.get(0).getLocations()[0], is("2-C.xml"));
}
@Test(expected = IllegalStateException.class)
public void resolveConfigAttributesWithConflictingLocations() {
resolveContextConfigurationAttributes(ConflictingLocations.class);
@@ -155,13 +385,13 @@ public class ContextLoaderUtilsTests {
@Test(expected = IllegalArgumentException.class)
public void buildMergedConfigWithoutAnnotation() {
buildMergedContextConfiguration(Enigma.class, null);
buildMergedContextConfiguration(Enigma.class, null, null);
}
@Test
public void buildMergedConfigWithBareAnnotations() {
Class<BareAnnotations> testClass = BareAnnotations.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(
mergedConfig,
@@ -173,7 +403,7 @@ public class ContextLoaderUtilsTests {
@Test
public void buildMergedConfigWithLocalAnnotationAndLocations() {
Class<?> testClass = LocationsFoo.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.xml" }, EMPTY_CLASS_ARRAY,
DelegatingSmartContextLoader.class);
@@ -182,7 +412,7 @@ public class ContextLoaderUtilsTests {
@Test
public void buildMergedConfigWithLocalAnnotationAndClasses() {
Class<?> testClass = ClassesFoo.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class },
DelegatingSmartContextLoader.class);
@@ -193,7 +423,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = LocationsFoo.class;
Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass,
expectedContextLoaderClass.getName());
expectedContextLoaderClass.getName(), null);
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.xml" }, EMPTY_CLASS_ARRAY,
expectedContextLoaderClass);
@@ -204,7 +434,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = ClassesFoo.class;
Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass,
expectedContextLoaderClass.getName());
expectedContextLoaderClass.getName(), null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class },
expectedContextLoaderClass);
@@ -215,7 +445,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = LocationsBar.class;
String[] expectedLocations = new String[] { "/foo.xml", "/bar.xml" };
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, expectedLocations, EMPTY_CLASS_ARRAY,
AnnotationConfigContextLoader.class);
}
@@ -225,7 +455,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = ClassesBar.class;
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses,
AnnotationConfigContextLoader.class);
}
@@ -235,7 +465,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = OverriddenLocationsBar.class;
String[] expectedLocations = new String[] { "/bar.xml" };
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, expectedLocations, EMPTY_CLASS_ARRAY,
AnnotationConfigContextLoader.class);
}
@@ -245,7 +475,7 @@ public class ContextLoaderUtilsTests {
Class<?> testClass = OverriddenClassesBar.class;
Class<?>[] expectedClasses = new Class<?>[] { BarConfig.class };
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses,
AnnotationConfigContextLoader.class);
}
@@ -258,7 +488,7 @@ public class ContextLoaderUtilsTests {
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(FooInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
}
@@ -272,7 +502,7 @@ public class ContextLoaderUtilsTests {
expectedInitializerClasses.add(FooInitializer.class);
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
}
@@ -285,7 +515,7 @@ public class ContextLoaderUtilsTests {
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
}
@@ -298,7 +528,7 @@ public class ContextLoaderUtilsTests {
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
}
@@ -377,6 +607,8 @@ public class ContextLoaderUtilsTests {
}
// -------------------------------------------------------------------------
private static class Enigma {
}
@@ -475,4 +707,140 @@ public class ContextLoaderUtilsTests {
private static class OverriddenInitializersAndClassesBar extends InitializersFoo {
}
@ContextConfiguration("foo.xml")
@ContextHierarchy(@ContextConfiguration("bar.xml"))
private static class SingleTestClassWithContextConfigurationAndContextHierarchy {
}
@ContextHierarchy(@ContextConfiguration("A.xml"))
private static class SingleTestClassWithSingleLevelContextHierarchy {
}
@ContextHierarchy({//
//
@ContextConfiguration("A.xml"),//
@ContextConfiguration("B.xml"),//
@ContextConfiguration("C.xml") //
})
private static class SingleTestClassWithTripleLevelContextHierarchy {
}
@ContextHierarchy(@ContextConfiguration("one.xml"))
private static class TestClass1WithSingleLevelContextHierarchy {
}
@ContextHierarchy(@ContextConfiguration({ "two-A.xml", "two-B.xml" }))
private static class TestClass2WithSingleLevelContextHierarchy extends TestClass1WithSingleLevelContextHierarchy {
}
@ContextHierarchy(@ContextConfiguration("three.xml"))
private static class TestClass3WithSingleLevelContextHierarchy extends TestClass2WithSingleLevelContextHierarchy {
}
@ContextConfiguration("one.xml")
private static class TestClass1WithBareContextConfigurationInSuperclass {
}
@ContextHierarchy(@ContextConfiguration("two.xml"))
private static class TestClass2WithBareContextConfigurationInSuperclass extends
TestClass1WithBareContextConfigurationInSuperclass {
}
@ContextHierarchy(@ContextConfiguration("one.xml"))
private static class TestClass1WithBareContextConfigurationInSubclass {
}
@ContextConfiguration("two.xml")
private static class TestClass2WithBareContextConfigurationInSubclass extends
TestClass1WithBareContextConfigurationInSuperclass {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "1-A.xml", name = "alpha"),//
@ContextConfiguration(locations = "1-B.xml", name = "beta") //
})
private static class TestClass1WithMultiLevelContextHierarchy {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "2-A.xml", name = "alpha"),//
@ContextConfiguration(locations = "2-B.xml", name = "beta") //
})
private static class TestClass2WithMultiLevelContextHierarchy extends TestClass1WithMultiLevelContextHierarchy {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "3-A.xml", name = "alpha"),//
@ContextConfiguration(locations = "3-B.xml", name = "beta"),//
@ContextConfiguration(locations = "3-C.xml", name = "gamma") //
})
private static class TestClass3WithMultiLevelContextHierarchy extends TestClass2WithMultiLevelContextHierarchy {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "1-A.xml"),//
@ContextConfiguration(locations = "1-B.xml") //
})
private static class TestClass1WithMultiLevelContextHierarchyAndUnnamedConfig {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "2-A.xml"),//
@ContextConfiguration(locations = "2-B.xml") //
})
private static class TestClass2WithMultiLevelContextHierarchyAndUnnamedConfig extends
TestClass1WithMultiLevelContextHierarchyAndUnnamedConfig {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "3-A.xml"),//
@ContextConfiguration(locations = "3-B.xml"),//
@ContextConfiguration(locations = "3-C.xml") //
})
private static class TestClass3WithMultiLevelContextHierarchyAndUnnamedConfig extends
TestClass2WithMultiLevelContextHierarchyAndUnnamedConfig {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "1-A.xml", name = "parent"),//
@ContextConfiguration(locations = "1-B.xml") //
})
private static class TestClass1WithMultiLevelContextHierarchyAndPartiallyNamedConfig {
}
@ContextHierarchy({//
//
@ContextConfiguration(locations = "2-A.xml", name = "parent"),//
@ContextConfiguration(locations = "2-C.xml") //
})
private static class TestClass2WithMultiLevelContextHierarchyAndPartiallyNamedConfig extends
TestClass1WithMultiLevelContextHierarchyAndPartiallyNamedConfig {
}
@ContextHierarchy({
//
@ContextConfiguration,//
@ContextConfiguration //
})
private static class SingleTestClassWithMultiLevelContextHierarchyWithEmptyContextConfig {
}
@ContextHierarchy({
//
@ContextConfiguration("foo.xml"),//
@ContextConfiguration(classes = BarConfig.class),// duplicate!
@ContextConfiguration("baz.xml"),//
@ContextConfiguration(classes = BarConfig.class),// duplicate!
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) //
})
private static class SingleTestClassWithMultiLevelContextHierarchyWithDuplicatedContextConfig {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -76,7 +76,7 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, new AnnotationConfigContextLoader());
assertFalse(mergedConfig1.hashCode() == mergedConfig2.hashCode());
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
@Test
@@ -97,7 +97,7 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), locations2,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.hashCode() == mergedConfig2.hashCode());
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
@Test
@@ -118,7 +118,7 @@ public class MergedContextConfigurationTests {
classes1, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
classes2, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.hashCode() == mergedConfig2.hashCode());
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
@Test
@@ -161,7 +161,7 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, activeProfiles1, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, activeProfiles2, loader);
assertFalse(mergedConfig1.hashCode() == mergedConfig2.hashCode());
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
@Test
@@ -197,15 +197,47 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, initializerClasses1, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, initializerClasses2, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.hashCode() == mergedConfig2.hashCode());
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
/**
* @since 3.2.2
*/
@Test
public void hashCodeWithSameParent() {
MergedContextConfiguration parent = new MergedContextConfiguration(getClass(), new String[] { "foo", "bar}" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent);
assertEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
/**
* @since 3.2.2
*/
@Test
public void hashCodeWithDifferentParents() {
MergedContextConfiguration parent1 = new MergedContextConfiguration(getClass(), new String[] { "foo", "bar}" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration parent2 = new MergedContextConfiguration(getClass(), new String[] { "baz", "quux" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent1);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent2);
assertNotEquals(mergedConfig1.hashCode(), mergedConfig2.hashCode());
}
@Test
public void equalsBasics() {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(null, null, null, null, null);
assertTrue(mergedConfig.equals(mergedConfig));
assertFalse(mergedConfig.equals(null));
assertFalse(mergedConfig.equals(new Integer(1)));
assertEquals(mergedConfig, mergedConfig);
assertNotEquals(mergedConfig, null);
assertNotEquals(mergedConfig, new Integer(1));
}
@Test
@@ -237,8 +269,8 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, new AnnotationConfigContextLoader());
assertFalse(mergedConfig1.equals(mergedConfig2));
assertFalse(mergedConfig2.equals(mergedConfig1));
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
@Test
@@ -259,8 +291,8 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), locations2,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.equals(mergedConfig2));
assertFalse(mergedConfig2.equals(mergedConfig1));
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
@Test
@@ -281,8 +313,8 @@ public class MergedContextConfigurationTests {
classes1, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
classes2, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.equals(mergedConfig2));
assertFalse(mergedConfig2.equals(mergedConfig1));
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
@Test
@@ -325,8 +357,8 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, activeProfiles1, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, activeProfiles2, loader);
assertFalse(mergedConfig1.equals(mergedConfig2));
assertFalse(mergedConfig2.equals(mergedConfig1));
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
@Test
@@ -362,8 +394,42 @@ public class MergedContextConfigurationTests {
EMPTY_CLASS_ARRAY, initializerClasses1, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, initializerClasses2, EMPTY_STRING_ARRAY, loader);
assertFalse(mergedConfig1.equals(mergedConfig2));
assertFalse(mergedConfig2.equals(mergedConfig1));
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
/**
* @since 3.2.2
*/
@Test
public void equalsWithSameParent() {
MergedContextConfiguration parent = new MergedContextConfiguration(getClass(), new String[] { "foo", "bar}" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent);
assertEquals(mergedConfig1, mergedConfig2);
assertEquals(mergedConfig2, mergedConfig1);
}
/**
* @since 3.2.2
*/
@Test
public void equalsWithDifferentParents() {
MergedContextConfiguration parent1 = new MergedContextConfiguration(getClass(), new String[] { "foo", "bar}" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration parent2 = new MergedContextConfiguration(getClass(), new String[] { "baz", "quux" },
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent1);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, loader, null, parent2);
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}

View File

@@ -44,7 +44,7 @@ import org.springframework.test.context.support.DirtiesContextTestExecutionListe
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
* @see TestContextCacheKeyTests
* @see ContextCacheTests
*/
@RunWith(OrderedMethodsSpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })

View File

@@ -1,102 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.context.SpringRunnerContextCacheTests.assertContextCacheStatistics;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
/**
* Unit tests for verifying proper behavior of the {@link ContextCache} in
* conjunction with cache keys used in {@link TestContext}.
*
* @author Sam Brannen
* @since 3.1
* @see SpringRunnerContextCacheTests
*/
public class TestContextCacheKeyTests {
private ContextCache contextCache = new ContextCache();
@Before
public void initialCacheState() {
assertContextCacheStatistics(contextCache, "initial state", 0, 0, 0);
}
private void loadAppCtxAndAssertCacheStats(Class<?> testClass, int expectedSize, int expectedHitCount,
int expectedMissCount) {
TestContext testContext = new TestContext(testClass, contextCache);
ApplicationContext context = testContext.getApplicationContext();
assertNotNull(context);
assertContextCacheStatistics(contextCache, testClass.getName(), expectedSize, expectedHitCount,
expectedMissCount);
}
@Test
public void verifyCacheKeyIsBasedOnContextLoader() {
loadAppCtxAndAssertCacheStats(AnnotationConfigContextLoaderTestCase.class, 1, 0, 1);
loadAppCtxAndAssertCacheStats(AnnotationConfigContextLoaderTestCase.class, 1, 1, 1);
loadAppCtxAndAssertCacheStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 1, 2);
loadAppCtxAndAssertCacheStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 2, 2);
loadAppCtxAndAssertCacheStats(AnnotationConfigContextLoaderTestCase.class, 2, 3, 2);
loadAppCtxAndAssertCacheStats(CustomAnnotationConfigContextLoaderTestCase.class, 2, 4, 2);
}
@Test
public void verifyCacheKeyIsBasedOnActiveProfiles() {
loadAppCtxAndAssertCacheStats(FooBarProfilesTestCase.class, 1, 0, 1);
loadAppCtxAndAssertCacheStats(FooBarProfilesTestCase.class, 1, 1, 1);
// Profiles {foo, bar} should hash to the same as {bar,foo}
loadAppCtxAndAssertCacheStats(BarFooProfilesTestCase.class, 1, 2, 1);
loadAppCtxAndAssertCacheStats(FooBarProfilesTestCase.class, 1, 3, 1);
loadAppCtxAndAssertCacheStats(FooBarProfilesTestCase.class, 1, 4, 1);
loadAppCtxAndAssertCacheStats(BarFooProfilesTestCase.class, 1, 5, 1);
}
@Configuration
static class Config {
}
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class AnnotationConfigContextLoaderTestCase {
}
@ContextConfiguration(classes = Config.class, loader = CustomAnnotationConfigContextLoader.class)
private static class CustomAnnotationConfigContextLoaderTestCase {
}
private static class CustomAnnotationConfigContextLoader extends AnnotationConfigContextLoader {
}
@ActiveProfiles({ "foo", "bar" })
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class FooBarProfilesTestCase {
}
@ActiveProfiles({ "bar", "foo" })
@ContextConfiguration(classes = Config.class, loader = AnnotationConfigContextLoader.class)
private static class BarFooProfilesTestCase {
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
//
@ContextConfiguration(name = "parent", classes = ClassHierarchyWithMergedConfigLevelOneTests.AppConfig.class),//
@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelOneTests.UserConfig.class) //
})
public class ClassHierarchyWithMergedConfigLevelOneTests {
@Configuration
static class AppConfig {
@Bean
public String parent() {
return "parent";
}
}
@Configuration
static class UserConfig {
@Autowired
private AppConfig appConfig;
@Bean
public String user() {
return appConfig.parent() + " + user";
}
@Bean
public String beanFromUserConfig() {
return "from UserConfig";
}
}
@Autowired
protected String parent;
@Autowired
protected String user;
@Autowired(required = false)
@Qualifier("beanFromUserConfig")
protected String beanFromUserConfig;
@Autowired
protected ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("parent", parent);
assertEquals("parent + user", user);
assertEquals("from UserConfig", beanFromUserConfig);
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelTwoTests.OrderConfig.class))
public class ClassHierarchyWithMergedConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {
@Configuration
static class OrderConfig {
@Autowired
private ClassHierarchyWithMergedConfigLevelOneTests.UserConfig userConfig;
@Bean
public String order() {
return userConfig.user() + " + order";
}
}
@Autowired
private String order;
@Test
@Override
public void loadContextHierarchy() {
super.loadContextHierarchy();
assertEquals("parent + user + order", order);
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithOverriddenConfigLevelTwoTests.TestUserConfig.class, inheritLocations = false))
public class ClassHierarchyWithOverriddenConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {
@Configuration
static class TestUserConfig {
@Autowired
private ClassHierarchyWithMergedConfigLevelOneTests.AppConfig appConfig;
@Bean
public String user() {
return appConfig.parent() + " + test user";
}
@Bean
public String beanFromTestUserConfig() {
return "from TestUserConfig";
}
}
@Autowired
private String beanFromTestUserConfig;
@Test
@Override
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("parent", parent);
assertEquals("parent + test user", user);
assertEquals("from TestUserConfig", beanFromTestUserConfig);
assertNull("Bean from UserConfig should not be present.", beanFromUserConfig);
}
}

View File

@@ -0,0 +1,149 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* Integration tests that verify support for {@link DirtiesContext.HierarchyMode}
* in conjunction with context hierarchies configured via {@link ContextHierarchy}.
*
* <p>Note that correct method execution order is essential, thus the use of
* {@link FixMethodOrder}.
*
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({ @ContextConfiguration(classes = DirtiesContextWithContextHierarchyTests.ParentConfig.class),
@ContextConfiguration(classes = DirtiesContextWithContextHierarchyTests.ChildConfig.class) })
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DirtiesContextWithContextHierarchyTests {
@Configuration
static class ParentConfig {
@Bean
public StringBuffer foo() {
return new StringBuffer("foo");
}
@Bean
public StringBuffer baz() {
return new StringBuffer("baz-parent");
}
}
@Configuration
static class ChildConfig {
@Bean
public StringBuffer baz() {
return new StringBuffer("baz-child");
}
}
@Autowired
private StringBuffer foo;
@Autowired
private StringBuffer baz;
@Autowired
private ApplicationContext context;
// -------------------------------------------------------------------------
private void reverseStringBuffers() {
foo.reverse();
baz.reverse();
}
private void assertOriginalState() {
assertCleanParentContext();
assertCleanChildContext();
}
private void assertCleanParentContext() {
assertEquals("foo", foo.toString());
}
private void assertCleanChildContext() {
assertEquals("baz-child", baz.toString());
}
private void assertDirtyParentContext() {
assertEquals("oof", foo.toString());
}
private void assertDirtyChildContext() {
assertEquals("dlihc-zab", baz.toString());
}
// -------------------------------------------------------------------------
@Before
public void verifyContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
}
@Test
public void test1_verifyOriginalStateAndDirtyContexts() {
assertOriginalState();
reverseStringBuffers();
}
@Test
@DirtiesContext
public void test2_verifyContextsWereDirtiedAndTriggerExhaustiveCacheClearing() {
assertDirtyParentContext();
assertDirtyChildContext();
}
@Test
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
public void test3_verifyOriginalStateWasReinstatedAndDirtyContextsAndTriggerCurrentLevelCacheClearing() {
assertOriginalState();
reverseStringBuffers();
}
@Test
public void test4_verifyParentContextIsStillDirtyButChildContextHasBeenReinstated() {
assertDirtyParentContext();
assertCleanChildContext();
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class SingleTestClassWithSingleLevelContextHierarchyTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo";
}
}
@Autowired
private String foo;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNull("parent ApplicationContext", context.getParent());
assertEquals("foo", foo);
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests.ParentConfig.class),
@ContextConfiguration("SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests-ChildConfig.xml") })
public class SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests {
@Configuration
static class ParentConfig {
@Bean
public String foo() {
return "foo";
}
@Bean
public String baz() {
return "baz-parent";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo", foo);
assertEquals("bar", bar);
assertEquals("baz-child", baz);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ParentConfig.class),
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ChildConfig.class) })
public class SingleTestClassWithTwoLevelContextHierarchyTests {
@Configuration
static class ParentConfig {
@Bean
public String foo() {
return "foo";
}
@Bean
public String baz() {
return "baz-parent";
}
}
@Configuration
static class ChildConfig {
@Bean
public String bar() {
return "bar";
}
@Bean
public String baz() {
return "baz-child";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo", foo);
assertEquals("bar", bar);
assertEquals("baz-child", baz);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class TestHierarchyLevelOneWithBareContextConfigurationInSubclassTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-1";
}
@Bean
public String bar() {
return "bar";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNull("parent ApplicationContext", context.getParent());
assertEquals("foo-level-1", foo);
assertEquals("bar", bar);
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestHierarchyLevelOneWithBareContextConfigurationInSuperclassTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-1";
}
@Bean
public String bar() {
return "bar";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNull("parent ApplicationContext", context.getParent());
assertEquals("foo-level-1", foo);
assertEquals("bar", bar);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class TestHierarchyLevelOneWithSingleLevelContextHierarchyTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-1";
}
@Bean
public String bar() {
return "bar";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNull("parent ApplicationContext", context.getParent());
assertEquals("foo-level-1", foo);
assertEquals("bar", bar);
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestHierarchyLevelTwoWithBareContextConfigurationInSubclassTests extends
TestHierarchyLevelOneWithBareContextConfigurationInSubclassTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-2";
}
@Bean
public String baz() {
return "baz";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
@Override
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo-level-2", foo);
assertEquals("bar", bar);
assertEquals("baz", baz);
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class TestHierarchyLevelTwoWithBareContextConfigurationInSuperclassTests extends
TestHierarchyLevelOneWithBareContextConfigurationInSuperclassTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-2";
}
@Bean
public String baz() {
return "baz";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
@Override
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo-level-2", foo);
assertEquals("bar", bar);
assertEquals("baz", baz);
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class TestHierarchyLevelTwoWithSingleLevelContextHierarchyAndMixedConfigTypesTests extends
TestHierarchyLevelOneWithSingleLevelContextHierarchyTests {
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
@Override
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo-level-2", foo);
assertEquals("bar", bar);
assertEquals("baz", baz);
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.standard;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy(@ContextConfiguration)
public class TestHierarchyLevelTwoWithSingleLevelContextHierarchyTests extends
TestHierarchyLevelOneWithSingleLevelContextHierarchyTests {
@Configuration
static class Config {
@Bean
public String foo() {
return "foo-level-2";
}
@Bean
public String baz() {
return "baz";
}
}
@Autowired
private String foo;
@Autowired
private String bar;
@Autowired
private String baz;
@Autowired
private ApplicationContext context;
@Test
@Override
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertEquals("foo-level-2", foo);
assertEquals("bar", bar);
assertEquals("baz", baz);
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.web;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.hierarchies.web.ControllerIntegrationTests.AppConfig;
import org.springframework.test.context.hierarchies.web.ControllerIntegrationTests.WebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
//
@ContextConfiguration(name = "root", classes = AppConfig.class),
@ContextConfiguration(name = "dispatcher", classes = WebConfig.class) //
})
public class ControllerIntegrationTests {
@Configuration
static class AppConfig {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
static class WebConfig {
@Bean
public String bar() {
return "bar";
}
}
// -------------------------------------------------------------------------
@Autowired
private WebApplicationContext wac;
@Autowired
private String foo;
@Autowired
private String bar;
@Test
public void verifyRootWacSupport() {
assertEquals("foo", foo);
assertEquals("bar", bar);
ApplicationContext parent = wac.getParent();
assertNotNull(parent);
assertTrue(parent instanceof WebApplicationContext);
WebApplicationContext root = (WebApplicationContext) parent;
assertFalse(root.getBeansOfType(String.class).containsKey("bar"));
ServletContext childServletContext = wac.getServletContext();
assertNotNull(childServletContext);
ServletContext rootServletContext = root.getServletContext();
assertNotNull(rootServletContext);
assertSame(childServletContext, rootServletContext);
assertSame(root, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertSame(root, childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.web;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@ContextHierarchy(@ContextConfiguration)
public class DispatcherWacRootWacEarTests extends RootWacEarTests {
@Autowired
private WebApplicationContext wac;
@Autowired
private String ear;
@Autowired
private String root;
@Autowired
private String dispatcher;
@Test
@Override
public void verifyEarConfig() {
/* no-op */
}
@Test
@Override
public void verifyRootWacConfig() {
/* no-op */
}
@Test
public void verifyDispatcherWacConfig() {
ApplicationContext parent = wac.getParent();
assertNotNull(parent);
assertTrue(parent instanceof WebApplicationContext);
ApplicationContext grandParent = parent.getParent();
assertNotNull(grandParent);
assertFalse(grandParent instanceof WebApplicationContext);
ServletContext dispatcherServletContext = wac.getServletContext();
assertNotNull(dispatcherServletContext);
ServletContext rootServletContext = ((WebApplicationContext) parent).getServletContext();
assertNotNull(rootServletContext);
assertSame(dispatcherServletContext, rootServletContext);
assertSame(parent,
rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertSame(parent,
dispatcherServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertEquals("ear", ear);
assertEquals("root", root);
assertEquals("dispatcher", dispatcher);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class EarTests {
@Configuration
static class EarConfig {
@Bean
public String ear() {
return "ear";
}
}
// -------------------------------------------------------------------------
@Autowired
private ApplicationContext context;
@Autowired
private String ear;
@Test
public void verifyEarConfig() {
assertFalse(context instanceof WebApplicationContext);
assertNull(context.getParent());
assertEquals("ear", ear);
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.hierarchies.web;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 3.2.2
*/
@WebAppConfiguration
@ContextHierarchy(@ContextConfiguration)
public class RootWacEarTests extends EarTests {
@Configuration
static class RootWacConfig {
@Bean
public String root() {
return "root";
}
}
// -------------------------------------------------------------------------
@Autowired
private WebApplicationContext wac;
@Autowired
private String ear;
@Autowired
private String root;
@Test
@Override
public void verifyEarConfig() {
/* no-op */
}
@Test
public void verifyRootWacConfig() {
ApplicationContext parent = wac.getParent();
assertNotNull(parent);
assertFalse(parent instanceof WebApplicationContext);
assertEquals("ear", ear);
assertEquals("root", root);
}
}

View File

@@ -33,21 +33,21 @@ import org.springframework.test.context.support.GenericPropertiesContextLoader;
/**
* <p>
* JUnit 4 based test class, which verifies the expected functionality of
* {@link SpringJUnit4ClassRunner} in conjunction with support for application
* contexts loaded from Java {@link Properties} files. Specifically, the
* {@link ContextConfiguration#loader() loaderClass} and
* {@link ContextConfiguration#resourceSuffix() resourceSuffix} attributes of
* &#064;ContextConfiguration are tested.
* {@link SpringJUnit4ClassRunner} in conjunction with support for application contexts
* loaded from Java {@link Properties} files. Specifically, the
* {@link ContextConfiguration#loader() loader} attribute of {@code ContextConfiguration}
* and the
* {@link org.springframework.test.context.support.GenericPropertiesContextLoader#getResourceSuffix()
* resourceSuffix} property of {@code GenericPropertiesContextLoader} are tested.
* </p>
* <p>
* Since no {@link ContextConfiguration#locations() locations} are explicitly
* defined, the {@link ContextConfiguration#resourceSuffix() resourceSuffix} is
* set to &quot;-context.properties&quot;, and
* {@link ContextConfiguration#generateDefaultLocations() generateDefaultLocations}
* is left set to its default value of {@code true}, this test class's
* dependencies will be injected via
* {@link Autowired annotation-based autowiring} from beans defined in the
* {@link ApplicationContext} loaded from the default classpath resource: &quot;{@code /org/springframework/test/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties}&quot;.
* Since no {@link ContextConfiguration#locations() locations} are explicitly defined, the
* {@code resourceSuffix} is set to &quot;-context.properties&quot;, and since default
* resource locations will be detected by default, this test class's dependencies will be
* injected via {@link Autowired annotation-based autowiring} from beans defined in the
* {@link ApplicationContext} loaded from the default classpath resource: &quot;
* {@code /org/springframework/test/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties}
* &quot;.
* </p>
*
* @author Sam Brannen

View File

@@ -38,6 +38,7 @@ import org.junit.runners.Suite.SuiteClasses;
* @author Sam Brannen
* @since 3.2
*/
@SuppressWarnings("javadoc")
@RunWith(Suite.class)
@SuiteClasses({ TestClass1.class, TestClass2.class })
public class Spr8849Tests {

View File

@@ -43,10 +43,11 @@ import org.testng.annotations.Test;
/**
* Integration tests that verify support for
* {@link import org.springframework.context.annotation.Configuration @Configuration}
* classes with TestNG-based tests.
* {@link org.springframework.context.annotation.Configuration @Configuration} classes
* with TestNG-based tests.
*
* <p>Configuration will be loaded from
* <p>
* Configuration will be loaded from
* {@link AnnotationConfigTransactionalTestNGSpringContextTests.ContextConfiguration}.
*
* @author Sam Brannen