Browse Source
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-9863pull/247/head
55 changed files with 3908 additions and 540 deletions
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* 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.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@code CacheAwareContextLoaderDelegate} loads application contexts from |
||||
* {@link MergedContextConfiguration} by delegating to the |
||||
* {@link ContextLoader} configured in the {@code MergedContextConfiguration} |
||||
* and interacting transparently with the {@link ContextCache} behind the scenes. |
||||
* |
||||
* <p>Note: {@code CacheAwareContextLoaderDelegate} does not implement the |
||||
* {@link ContextLoader} or {@link SmartContextLoader} interface. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.2.2 |
||||
*/ |
||||
public class CacheAwareContextLoaderDelegate { |
||||
|
||||
private static final Log logger = LogFactory.getLog(CacheAwareContextLoaderDelegate.class); |
||||
|
||||
private final ContextCache contextCache; |
||||
|
||||
|
||||
CacheAwareContextLoaderDelegate(ContextCache contextCache) { |
||||
Assert.notNull(contextCache, "ContextCache must not be null"); |
||||
this.contextCache = contextCache; |
||||
} |
||||
|
||||
/** |
||||
* Load the {@code ApplicationContext} for the supplied merged context |
||||
* configuration. Supports both the {@link SmartContextLoader} and |
||||
* {@link ContextLoader} SPIs. |
||||
* @throws Exception if an error occurs while loading the application context |
||||
*/ |
||||
private ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) |
||||
throws Exception { |
||||
ContextLoader contextLoader = mergedContextConfiguration.getContextLoader(); |
||||
Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. " |
||||
+ "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); |
||||
|
||||
ApplicationContext applicationContext; |
||||
|
||||
if (contextLoader instanceof SmartContextLoader) { |
||||
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader; |
||||
applicationContext = smartContextLoader.loadContext(mergedContextConfiguration); |
||||
} |
||||
else { |
||||
String[] locations = mergedContextConfiguration.getLocations(); |
||||
Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. " |
||||
+ "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); |
||||
applicationContext = contextLoader.loadContext(locations); |
||||
} |
||||
|
||||
return applicationContext; |
||||
} |
||||
|
||||
/** |
||||
* Load the {@link ApplicationContext application context} for the supplied |
||||
* merged context configuration. |
||||
* |
||||
* <p>If the context is present in the cache it will simply be returned; |
||||
* otherwise, it will be loaded, stored in the cache, and returned. |
||||
* @return the application context |
||||
* @throws IllegalStateException if an error occurs while retrieving or |
||||
* loading the application context |
||||
*/ |
||||
public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) { |
||||
synchronized (contextCache) { |
||||
ApplicationContext context = contextCache.get(mergedContextConfiguration); |
||||
if (context == null) { |
||||
try { |
||||
context = loadContextInternal(mergedContextConfiguration); |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug(String.format("Storing ApplicationContext in cache under key [%s].", |
||||
mergedContextConfiguration)); |
||||
} |
||||
contextCache.put(mergedContextConfiguration, context); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new IllegalStateException("Failed to load ApplicationContext", ex); |
||||
} |
||||
} |
||||
else { |
||||
if (logger.isDebugEnabled()) { |
||||
logger.debug(String.format("Retrieved ApplicationContext from cache with key [%s].", |
||||
mergedContextConfiguration)); |
||||
} |
||||
} |
||||
return context; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
/* |
||||
* 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 java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Inherited; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* {@code @ContextHierarchy} is a class-level annotation that is used to define |
||||
* a hierarchy of {@link org.springframework.context.ApplicationContext |
||||
* ApplicationContexts} for integration tests. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 3.2.2 |
||||
* @see ContextConfiguration |
||||
* @see org.springframework.context.ApplicationContext |
||||
*/ |
||||
@Documented |
||||
@Inherited |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Target(ElementType.TYPE) |
||||
public @interface ContextHierarchy { |
||||
|
||||
/** |
||||
* A list of {@link ContextConfiguration @ContextConfiguration} instances, |
||||
* each of which defines a level in the context hierarchy. |
||||
* |
||||
* <p>If you need to merge or override the configuration for a given level |
||||
* of the context hierarchy within a test class hierarchy, you must explicitly |
||||
* name that level by supplying the same value to the {@link ContextConfiguration#name |
||||
* name} attribute in {@code @ContextConfiguration} at each level in the |
||||
* class hierarchy. |
||||
*/ |
||||
ContextConfiguration[] value(); |
||||
|
||||
} |
||||
@ -0,0 +1,329 @@
@@ -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 { |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,212 @@
@@ -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() { |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,102 +0,0 @@
@@ -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 { |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,96 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,62 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,149 @@
@@ -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(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,63 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,93 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,72 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,71 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,72 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,62 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,102 @@
@@ -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)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,88 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,65 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||
|
||||
<bean id="bar" class="java.lang.String" c:_="bar" /> |
||||
|
||||
<bean id="baz" class="java.lang.String" c:_="baz-child" /> |
||||
|
||||
</beans> |
||||
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||
|
||||
<bean id="foo" class="java.lang.String" c:_="foo-level-2" /> |
||||
|
||||
<bean id="baz" class="java.lang.String" c:_="baz" /> |
||||
|
||||
</beans> |
||||
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||
|
||||
<bean id="dispatcher" class="java.lang.String" c:_="dispatcher" /> |
||||
|
||||
</beans> |
||||
Loading…
Reference in new issue