From 0392a88c6954bdcd391eea07b513973ebee3ba08 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 18 Apr 2015 20:17:20 +0200 Subject: [PATCH] Move static ContextCache to DefaultCacheAwareContextLoaderDelegate Issue: SPR-12683 --- ...efaultCacheAwareContextLoaderDelegate.java | 23 ++++++++++++++++++- .../test/context/TestContextManager.java | 12 ++-------- .../test/context/ContextCacheTestUtils.java | 10 ++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/DefaultCacheAwareContextLoaderDelegate.java b/spring-test/src/main/java/org/springframework/test/context/DefaultCacheAwareContextLoaderDelegate.java index a9d38dfa08..a13e347c2c 100644 --- a/spring-test/src/main/java/org/springframework/test/context/DefaultCacheAwareContextLoaderDelegate.java +++ b/spring-test/src/main/java/org/springframework/test/context/DefaultCacheAwareContextLoaderDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -40,9 +40,30 @@ class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContextLoaderD private static final Log statsLogger = LogFactory.getLog("org.springframework.test.context.cache"); + /** + * Default cache of Spring application contexts. + * + *

This default cache is static, so that each context can be cached + * and reused for all subsequent tests that declare the same unique + * context configuration within the same JVM process. + */ + static final ContextCache defaultContextCache = new ContextCache(); + private final ContextCache contextCache; + /** + * Construct a new {@code DefaultCacheAwareContextLoaderDelegate} that + * uses the default, static {@code ContextCache}. + */ + DefaultCacheAwareContextLoaderDelegate() { + this(defaultContextCache); + } + + /** + * Construct a new {@code DefaultCacheAwareContextLoaderDelegate} with + * the supplied {@code ContextCache}. + */ DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache) { Assert.notNull(contextCache, "ContextCache must not be null"); this.contextCache = contextCache; diff --git a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java index 9b75bffab3..9b7fa95735 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java @@ -73,14 +73,6 @@ public class TestContextManager { private static final Log logger = LogFactory.getLog(TestContextManager.class); - /** - * Cache of Spring application contexts. - *

This needs to be static, since test instances may be destroyed and - * recreated between invocations of individual test methods, as is the case - * with JUnit. - */ - static final ContextCache contextCache = new ContextCache(); - private final TestContext testContext; private final List testExecutionListeners = new ArrayList(); @@ -88,14 +80,14 @@ public class TestContextManager { /** * Construct a new {@code TestContextManager} for the specified {@linkplain Class test class} - * and automatically {@link #registerTestExecutionListeners register} the + * and automatically {@linkplain #registerTestExecutionListeners register} the * {@link TestExecutionListener TestExecutionListeners} configured for the test class * via the {@link TestExecutionListeners @TestExecutionListeners} annotation. * @param testClass the test class to be managed * @see #registerTestExecutionListeners */ public TestContextManager(Class testClass) { - CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = new DefaultCacheAwareContextLoaderDelegate(contextCache); + CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = new DefaultCacheAwareContextLoaderDelegate(); BootstrapContext bootstrapContext = new DefaultBootstrapContext(testClass, cacheAwareContextLoaderDelegate); TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(bootstrapContext); this.testContext = new DefaultTestContext(testContextBootstrapper); diff --git a/spring-test/src/test/java/org/springframework/test/context/ContextCacheTestUtils.java b/spring-test/src/test/java/org/springframework/test/context/ContextCacheTestUtils.java index 3ffe61fe68..6291e5a1d2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/ContextCacheTestUtils.java +++ b/spring-test/src/test/java/org/springframework/test/context/ContextCacheTestUtils.java @@ -28,14 +28,14 @@ import static org.junit.Assert.*; public class ContextCacheTestUtils { /** - * Reset the state of the context cache. + * Reset the state of the static context cache in {@link DefaultCacheAwareContextLoaderDelegate}. */ public static final void resetContextCache() { - TestContextManager.contextCache.reset(); + DefaultCacheAwareContextLoaderDelegate.defaultContextCache.reset(); } /** - * Assert the statistics of the context cache in {@link TestContextManager}. + * Assert the statistics of the static context cache in {@link DefaultCacheAwareContextLoaderDelegate}. * * @param usageScenario the scenario in which the statistics are used * @param expectedSize the expected number of contexts in the cache @@ -44,8 +44,8 @@ public class ContextCacheTestUtils { */ public static final void assertContextCacheStatistics(String usageScenario, int expectedSize, int expectedHitCount, int expectedMissCount) { - assertContextCacheStatistics(TestContextManager.contextCache, usageScenario, expectedSize, expectedHitCount, - expectedMissCount); + assertContextCacheStatistics(DefaultCacheAwareContextLoaderDelegate.defaultContextCache, usageScenario, + expectedSize, expectedHitCount, expectedMissCount); } /**