Introduce context bootstrap strategy in the TCF
Work done in conjunction with SPR-5243 and SPR-4588 introduced physical package cycles in the spring-test module. The work performed in conjunction with SPR-9924 uses reflection to resolve these physical package cycles; however, prior to this commit the logical package cycles still remain. Furthermore, over time it has become apparent that the Spring TestContext Framework (TCF) could better serve application developers and especially third-party framework developers by providing a more flexible mechanism for "bootstrapping" the TCF. For example, prior to this commit, default TestExecutionListeners could only be registered by subclassing TestContextManager (and SpringJUnit4ClassRunner if using JUnit). Similarly, the default ContextLoader could only be set by subclassing SpringJUnit4ClassRunner for JUnit and by copying and modifying AbstractTestNGSpringContextTests for TestNG. This commit addresses the aforementioned issues by introducing a bootstrap strategy in the TestContext framework that is responsible for determining default TestExecutionListeners and the default ContextLoader in an extensible fashion. The new TestContextBootstrapper SPI also provides a mechanism for supporting various types of MergedContextConfiguration depending on the feature set of the context loaders supported by the strategy. The following provides an overview of the most significant changes in this commit. - Introduced TestContextBootstrapper strategy SPI, BootstrapContext, and @BootstrapWith. - Introduced AbstractTestContextBootstrapper, DefaultTestContextBootstrapper, and WebTestContextBootstrapper implementations of the TestContextBootstrapper SPI and extracted related reflection code from ContextLoaderUtils & TestContextManager. - Introduced BootstrapUtils for retrieving the TestContextBootstrapper from @BootstrapWith, falling back to a default if @BootstrapWith is not present. - @WebAppConfiguration is now annotated with @BootstrapWith(WebTestContextBootstrapper.class). - CacheAwareContextLoaderDelegate is now an interface with a new DefaultCacheAwareContextLoaderDelegate implementation class. - Introduced closeContext(MergedContextConfiguration, HierarchyMode) in CacheAwareContextLoaderDelegate. - DefaultTestContext now uses CacheAwareContextLoaderDelegate instead of interacting directly with the ContextCache. - DefaultTestContext now delegates to a TestContextBootstrapper for building the MergedContextConfiguration. - TestContextManager now delegates to TestContextBootstrapper for retrieving TestExecutionListeners. - Deleted TestContextManager(Class, String) constructor and SpringJUnit4ClassRunner.getDefaultContextLoaderClassName(Class) method since default ContextLoader support is now implemented by TestContextBootstrappers. - Extracted ActiveProfilesUtils from ContextLoaderUtils. - Extracted ApplicationContextInitializerUtils from ContextLoaderUtils. - MetaAnnotationUtils is now a public utility class in the test.util package. - Removed restriction in @ActiveProfiles that a custom resolver cannot be used with the 'value' or 'profiles' attributes. - Introduced DefaultActiveProfilesResolver. Issue: SPR-9955
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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;
|
||||
|
||||
/**
|
||||
* Collection of test-related utility methods for working with {@link BootstrapContext
|
||||
* BootstrapContexts} and {@link TestContextBootstrapper TestContextBootstrappers}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.1
|
||||
*/
|
||||
public abstract class BootstrapTestUtils {
|
||||
|
||||
private BootstrapTestUtils() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public static BootstrapContext buildBootstrapContext(Class<?> testClass,
|
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
|
||||
return new DefaultBootstrapContext(testClass, cacheAwareContextLoaderDelegate);
|
||||
}
|
||||
|
||||
public static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext bootstrapContext) {
|
||||
return BootstrapUtils.resolveTestContextBootstrapper(bootstrapContext);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -56,7 +56,7 @@ public class ContextCacheTests {
|
||||
}
|
||||
|
||||
private ApplicationContext loadContext(Class<?> testClass) {
|
||||
TestContext testContext = new DefaultTestContext(testClass, contextCache);
|
||||
TestContext testContext = TestContextTestUtils.buildTestContext(testClass, contextCache);
|
||||
return testContext.getApplicationContext();
|
||||
}
|
||||
|
||||
@@ -119,15 +119,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel1() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
@@ -144,15 +144,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel1WithExhaustiveMode() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
@@ -169,15 +169,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel2() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
@@ -195,15 +195,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel2WithExhaustiveMode() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
@@ -219,15 +219,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel3Then2() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
@@ -248,15 +248,15 @@ public class ContextCacheTests {
|
||||
public void removeContextHierarchyCacheLevel3Then2WithExhaustiveMode() {
|
||||
|
||||
// Load Level 3-A
|
||||
TestContext testContext3a = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3a = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
|
||||
testContext3a.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
|
||||
assertParentContextCount(2);
|
||||
|
||||
// Load Level 3-B
|
||||
TestContext testContext3b = new DefaultTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class,
|
||||
contextCache);
|
||||
TestContext testContext3b = TestContextTestUtils.buildTestContext(
|
||||
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
|
||||
testContext3b.getApplicationContext();
|
||||
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
|
||||
assertParentContextCount(2);
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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;
|
||||
|
||||
/**
|
||||
* Collection of test-related utility methods for working with {@link TestContext TestContexts}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.1
|
||||
*/
|
||||
public abstract class TestContextTestUtils {
|
||||
|
||||
private TestContextTestUtils() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public static TestContext buildTestContext(Class<?> testClass, ContextCache contextCache) {
|
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = new DefaultCacheAwareContextLoaderDelegate(
|
||||
contextCache);
|
||||
return buildTestContext(testClass, null, cacheAwareContextLoaderDelegate);
|
||||
}
|
||||
|
||||
public static TestContext buildTestContext(Class<?> testClass, String customDefaultContextLoaderClassName,
|
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
|
||||
BootstrapContext bootstrapContext = new DefaultBootstrapContext(testClass, cacheAwareContextLoaderDelegate);
|
||||
TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(bootstrapContext);
|
||||
|
||||
return new DefaultTestContext(testContextBootstrapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -45,7 +45,7 @@ public class TestExecutionListenersTests {
|
||||
@Test
|
||||
public void verifyNumDefaultListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(DefaultListenersExampleTestCase.class);
|
||||
assertEquals("Num registered TELs for DefaultListenersExampleTestCase.", 4,
|
||||
assertEquals("Num registered TELs for DefaultListenersExampleTestCase.", 3,
|
||||
testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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,27 +16,29 @@
|
||||
|
||||
package org.springframework.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.springframework.tests.sample.beans.Pet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.BootstrapWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
|
||||
import org.springframework.test.context.support.GenericPropertiesContextLoader;
|
||||
import org.springframework.tests.sample.beans.Pet;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration tests which verify that a subclass of {@link SpringJUnit4ClassRunner}
|
||||
* can specify a custom <em>default ContextLoader class name</em> that overrides
|
||||
* the standard default class name.
|
||||
* Integration tests which verify that a subclass of {@link DefaultTestContextBootstrapper}
|
||||
* can specify a custom <em>default ContextLoader class</em> that overrides the standard
|
||||
* default class name.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.0
|
||||
*/
|
||||
@RunWith(CustomDefaultContextLoaderClassSpringRunnerTests.PropertiesBasedSpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@BootstrapWith(CustomDefaultContextLoaderClassSpringRunnerTests.PropertiesBasedTestContextBootstrapper.class)
|
||||
@ContextConfiguration("PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties")
|
||||
public class CustomDefaultContextLoaderClassSpringRunnerTests {
|
||||
|
||||
@Autowired
|
||||
@@ -56,16 +58,12 @@ public class CustomDefaultContextLoaderClassSpringRunnerTests {
|
||||
}
|
||||
|
||||
|
||||
public static final class PropertiesBasedSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
|
||||
|
||||
public PropertiesBasedSpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
|
||||
super(clazz);
|
||||
}
|
||||
public static class PropertiesBasedTestContextBootstrapper extends DefaultTestContextBootstrapper {
|
||||
|
||||
@Override
|
||||
protected String getDefaultContextLoaderClassName(Class<?> clazz) {
|
||||
return GenericPropertiesContextLoader.class.getName();
|
||||
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
|
||||
return GenericPropertiesContextLoader.class;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -23,10 +23,19 @@ import java.lang.annotation.Target;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.BootstrapTestUtils;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -44,6 +53,14 @@ abstract class AbstractContextLoaderUtilsTests {
|
||||
Collections.<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> emptySet();
|
||||
|
||||
|
||||
MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
|
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = Mockito.mock(CacheAwareContextLoaderDelegate.class);
|
||||
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(testClass,
|
||||
cacheAwareContextLoaderDelegate);
|
||||
TestContextBootstrapper bootstrapper = BootstrapTestUtils.resolveTestContextBootstrapper(bootstrapContext);
|
||||
return bootstrapper.buildMergedContextConfiguration();
|
||||
}
|
||||
|
||||
void assertAttributes(ContextConfigurationAttributes attributes, Class<?> expectedDeclaringClass,
|
||||
String[] expectedLocations, Class<?>[] expectedClasses,
|
||||
Class<? extends ContextLoader> expectedContextLoaderClass, boolean expectedInheritLocations) {
|
||||
@@ -173,4 +190,16 @@ abstract class AbstractContextLoaderUtilsTests {
|
||||
static class OverriddenClassesBar extends ClassesFoo {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = "/foo.properties", loader = GenericPropertiesContextLoader.class)
|
||||
@ActiveProfiles(profiles = "foo")
|
||||
static class PropertiesLocationsFoo {
|
||||
}
|
||||
|
||||
// Combining @Configuration classes with a Properties based loader doesn't really make
|
||||
// sense, but that's OK for unit testing purposes.
|
||||
@ContextConfiguration(classes = FooConfig.class, loader = GenericPropertiesContextLoader.class)
|
||||
@ActiveProfiles(profiles = "foo")
|
||||
static class PropertiesClassesFoo {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -25,19 +25,21 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ActiveProfilesResolver;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.context.ContextLoaderUtils.*;
|
||||
import static org.springframework.test.context.support.ActiveProfilesUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContextLoaderUtils} involving resolution of active bean
|
||||
* Unit tests for {@link ActiveProfilesUtils} involving resolution of active bean
|
||||
* definition profiles.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Michail Nikolaev
|
||||
* @since 3.1
|
||||
*/
|
||||
public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoaderUtilsTests {
|
||||
public class ActiveProfilesUtilsTests extends AbstractContextLoaderUtilsTests {
|
||||
|
||||
private void assertResolvedProfiles(Class<?> testClass, String... expected) {
|
||||
assertNotNull(testClass);
|
||||
@@ -168,17 +170,17 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void resolveActiveProfilesWithConflictingResolverAndProfiles() {
|
||||
resolveActiveProfiles(ConflictingResolverAndProfilesTestCase.class);
|
||||
@Test
|
||||
public void resolveActiveProfilesWithResolverAndProfiles() {
|
||||
assertResolvedProfiles(ResolverAndProfilesTestCase.class, "bar");
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
*/
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void resolveActiveProfilesWithConflictingResolverAndValue() {
|
||||
resolveActiveProfiles(ConflictingResolverAndValueTestCase.class);
|
||||
@Test
|
||||
public void resolveActiveProfilesWithResolverAndValue() {
|
||||
assertResolvedProfiles(ResolverAndValueTestCase.class, "bar");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -279,19 +281,19 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
|
||||
InheritedFooActiveProfilesResolverTestCase {
|
||||
}
|
||||
|
||||
@ActiveProfiles(resolver = BarActiveProfilesResolver.class, profiles = "conflict")
|
||||
private static class ConflictingResolverAndProfilesTestCase {
|
||||
@ActiveProfiles(resolver = BarActiveProfilesResolver.class, profiles = "ignored by custom resolver")
|
||||
private static class ResolverAndProfilesTestCase {
|
||||
}
|
||||
|
||||
@ActiveProfiles(resolver = BarActiveProfilesResolver.class, value = "conflict")
|
||||
private static class ConflictingResolverAndValueTestCase {
|
||||
@ActiveProfiles(resolver = BarActiveProfilesResolver.class, value = "ignored by custom resolver")
|
||||
private static class ResolverAndValueTestCase {
|
||||
}
|
||||
|
||||
@MetaResolverConfig
|
||||
private static class TestClassVerifyingActiveProfilesResolverTestCase {
|
||||
}
|
||||
|
||||
@ActiveProfiles(profiles = "conflict", value = "conflict")
|
||||
@ActiveProfiles(profiles = "conflict 1", value = "conflict 2")
|
||||
private static class ConflictingProfilesAndValueTestCase {
|
||||
}
|
||||
|
||||
@@ -14,15 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.support.ContextLoaderUtils;
|
||||
|
||||
import static org.springframework.test.context.support.ContextLoaderUtils.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.context.ContextLoaderUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContextLoaderUtils} involving {@link ContextConfigurationAttributes}.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -25,11 +25,16 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextHierarchy;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.support.ContextLoaderUtils;
|
||||
|
||||
import static org.springframework.test.context.support.ContextLoaderUtils.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.context.ContextLoaderUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContextLoaderUtils} involving context hierarchies.
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -23,11 +23,10 @@ import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.support.DelegatingSmartContextLoader;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
|
||||
import static org.springframework.test.context.ContextLoaderUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContextLoaderUtils} involving {@link ApplicationContextInitializer}s.
|
||||
*
|
||||
@@ -44,7 +43,8 @@ public class ContextLoaderUtilsContextInitializerTests extends AbstractContextLo
|
||||
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
|
||||
expectedInitializerClasses.add(FooInitializer.class);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
|
||||
DelegatingSmartContextLoader.class);
|
||||
}
|
||||
@@ -58,7 +58,8 @@ public class ContextLoaderUtilsContextInitializerTests extends AbstractContextLo
|
||||
expectedInitializerClasses.add(FooInitializer.class);
|
||||
expectedInitializerClasses.add(BarInitializer.class);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
|
||||
DelegatingSmartContextLoader.class);
|
||||
}
|
||||
@@ -71,7 +72,8 @@ public class ContextLoaderUtilsContextInitializerTests extends AbstractContextLo
|
||||
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
|
||||
expectedInitializerClasses.add(BarInitializer.class);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
|
||||
DelegatingSmartContextLoader.class);
|
||||
}
|
||||
@@ -84,7 +86,8 @@ public class ContextLoaderUtilsContextInitializerTests extends AbstractContextLo
|
||||
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
|
||||
expectedInitializerClasses.add(BarInitializer.class);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
|
||||
DelegatingSmartContextLoader.class);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -14,14 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.support.DelegatingSmartContextLoader;
|
||||
import org.springframework.test.context.support.GenericPropertiesContextLoader;
|
||||
|
||||
import static org.springframework.test.context.ContextLoaderUtils.*;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContextLoaderUtils} involving {@link MergedContextConfiguration}.
|
||||
@@ -33,28 +30,28 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
|
||||
@Test
|
||||
public void buildMergedConfigWithoutAnnotation() {
|
||||
Class<Enigma> testClass = Enigma.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
Class<?> testClass = Enigma.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildMergedConfigWithBareAnnotations() {
|
||||
Class<BareAnnotations> testClass = BareAnnotations.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
Class<?> testClass = BareAnnotations.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(
|
||||
mergedConfig,
|
||||
testClass,
|
||||
new String[] { "classpath:/org/springframework/test/context/AbstractContextLoaderUtilsTests$BareAnnotations-context.xml" },
|
||||
new String[] { "classpath:/org/springframework/test/context/support/AbstractContextLoaderUtilsTests$BareAnnotations-context.xml" },
|
||||
EMPTY_CLASS_ARRAY, DelegatingSmartContextLoader.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildMergedConfigWithLocalAnnotationAndLocations() {
|
||||
Class<?> testClass = LocationsFoo.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.xml" }, EMPTY_CLASS_ARRAY,
|
||||
DelegatingSmartContextLoader.class);
|
||||
@@ -63,7 +60,7 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
@Test
|
||||
public void buildMergedConfigWithMetaAnnotationAndLocations() {
|
||||
Class<?> testClass = MetaLocationsFoo.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.xml" }, EMPTY_CLASS_ARRAY,
|
||||
DelegatingSmartContextLoader.class);
|
||||
@@ -72,7 +69,7 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
@Test
|
||||
public void buildMergedConfigWithLocalAnnotationAndClasses() {
|
||||
Class<?> testClass = ClassesFoo.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class },
|
||||
DelegatingSmartContextLoader.class);
|
||||
@@ -80,21 +77,19 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
|
||||
@Test
|
||||
public void buildMergedConfigWithLocalAnnotationAndOverriddenContextLoaderAndLocations() {
|
||||
Class<?> testClass = LocationsFoo.class;
|
||||
Class<?> testClass = PropertiesLocationsFoo.class;
|
||||
Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass,
|
||||
expectedContextLoaderClass.getName(), null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.xml" }, EMPTY_CLASS_ARRAY,
|
||||
assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.properties" }, EMPTY_CLASS_ARRAY,
|
||||
expectedContextLoaderClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildMergedConfigWithLocalAnnotationAndOverriddenContextLoaderAndClasses() {
|
||||
Class<?> testClass = ClassesFoo.class;
|
||||
Class<?> testClass = PropertiesClassesFoo.class;
|
||||
Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass,
|
||||
expectedContextLoaderClass.getName(), null);
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class },
|
||||
expectedContextLoaderClass);
|
||||
@@ -104,8 +99,8 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
public void buildMergedConfigWithLocalAndInheritedAnnotationsAndLocations() {
|
||||
Class<?> testClass = LocationsBar.class;
|
||||
String[] expectedLocations = new String[] { "/foo.xml", "/bar.xml" };
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
assertMergedConfig(mergedConfig, testClass, expectedLocations, EMPTY_CLASS_ARRAY,
|
||||
AnnotationConfigContextLoader.class);
|
||||
}
|
||||
@@ -114,8 +109,8 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
public void buildMergedConfigWithLocalAndInheritedAnnotationsAndClasses() {
|
||||
Class<?> testClass = ClassesBar.class;
|
||||
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses,
|
||||
AnnotationConfigContextLoader.class);
|
||||
}
|
||||
@@ -124,8 +119,8 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
public void buildMergedConfigWithAnnotationsAndOverriddenLocations() {
|
||||
Class<?> testClass = OverriddenLocationsBar.class;
|
||||
String[] expectedLocations = new String[] { "/bar.xml" };
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
assertMergedConfig(mergedConfig, testClass, expectedLocations, EMPTY_CLASS_ARRAY,
|
||||
AnnotationConfigContextLoader.class);
|
||||
}
|
||||
@@ -134,8 +129,8 @@ public class ContextLoaderUtilsMergedConfigTests extends AbstractContextLoaderUt
|
||||
public void buildMergedConfigWithAnnotationsAndOverriddenClasses() {
|
||||
Class<?> testClass = OverriddenClassesBar.class;
|
||||
Class<?>[] expectedClasses = new Class<?>[] { BarConfig.class };
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
|
||||
|
||||
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass, null, null);
|
||||
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses,
|
||||
AnnotationConfigContextLoader.class);
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Documented;
|
||||
@@ -27,12 +27,15 @@ import org.junit.Test;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.test.context.MetaAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.context.MetaAnnotationUtils.UntypedAnnotationDescriptor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.util.MetaAnnotationUtils;
|
||||
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.springframework.test.util.MetaAnnotationUtils.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.context.MetaAnnotationUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MetaAnnotationUtils}.
|
||||
@@ -14,17 +14,20 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context;
|
||||
package org.springframework.test.util;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.test.context.MetaAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.util.MetaAnnotationUtils;
|
||||
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
|
||||
|
||||
import static org.springframework.test.util.MetaAnnotationUtils.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.context.MetaAnnotationUtils.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MetaAnnotationUtils} that verify support for overridden
|
||||
Reference in New Issue
Block a user