[SPR-7326] Added unit tests to verify proper semantics of TestContext's cache key generation.

This commit is contained in:
Sam Brannen
2011-06-03 21:38:05 +00:00
parent 72420c79cb
commit 3f58da1cd6
4 changed files with 141 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
* @see TestContextCacheKeyTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
@@ -51,17 +52,31 @@ public class SpringRunnerContextCacheTests {
/**
* Asserts the statistics of the supplied context cache.
* Asserts the statistics of the context cache in {@link TestContextManager}.
*
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
public static final void assertContextCacheStatistics(String usageScenario, int expectedSize, int expectedHitCount,
int expectedMissCount) {
private static final void assertContextCacheStatistics(String usageScenario, int expectedSize,
int expectedHitCount, int expectedMissCount) {
assertContextCacheStatistics(TestContextManager.contextCache, usageScenario, expectedSize, expectedHitCount,
expectedMissCount);
}
/**
* Asserts the statistics of the supplied context cache.
*
* @param contextCache the cache to assert against
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
public static final void assertContextCacheStatistics(ContextCache contextCache, String usageScenario,
int expectedSize, int expectedHitCount, int expectedMissCount) {
ContextCache contextCache = TestContextManager.contextCache;
assertEquals("Verifying number of contexts in cache (" + usageScenario + ").", expectedSize,
contextCache.size());
assertEquals("Verifying number of cache hits (" + usageScenario + ").", expectedHitCount,

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* 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 generated 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 {
}
}