ContextCache.java 5.9 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2012 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;

/**
28
 * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test environment.
A
Arjen Poutsma 已提交
29
 *
30 31
 * <p>Maintains a cache of {@link ApplicationContext contexts} keyed by
 * {@link MergedContextConfiguration} instances. This has significant performance
A
Arjen Poutsma 已提交
32 33
 * benefits if initializing the context would take time. While initializing a
 * Spring context itself is very quick, some beans in a context, such as a
34 35 36
 * {@code LocalSessionFactoryBean} for working with Hibernate, may take some time
 * to initialize. Hence it often makes sense to perform that initialization only
 * once per test suite.
A
Arjen Poutsma 已提交
37 38 39 40 41 42 43 44 45 46
 *
 * @author Sam Brannen
 * @author Juergen Hoeller
 * @since 2.5
 */
class ContextCache {

	/**
	 * Map of context keys to Spring ApplicationContext instances.
	 */
47 48
	private final Map<MergedContextConfiguration, ApplicationContext> contextMap =
			new ConcurrentHashMap<MergedContextConfiguration, ApplicationContext>(64);
A
Arjen Poutsma 已提交
49 50 51 52 53 54 55 56 57 58

	private int hitCount;

	private int missCount;


	/**
	 * Clears all contexts from the cache.
	 */
	void clear() {
59
		this.contextMap.clear();
A
Arjen Poutsma 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
	}

	/**
	 * Clears hit and miss count statistics for the cache (i.e., resets counters
	 * to zero).
	 */
	void clearStatistics() {
		this.hitCount = 0;
		this.missCount = 0;
	}

	/**
	 * Return whether there is a cached context for the given key.
73
	 * @param key the context key (never {@code null})
A
Arjen Poutsma 已提交
74
	 */
75
	boolean contains(MergedContextConfiguration key) {
A
Arjen Poutsma 已提交
76
		Assert.notNull(key, "Key must not be null");
77
		return this.contextMap.containsKey(key);
A
Arjen Poutsma 已提交
78 79 80 81 82 83
	}

	/**
	 * Obtain a cached ApplicationContext for the given key.
	 * <p>The {@link #getHitCount() hit} and {@link #getMissCount() miss}
	 * counts will be updated accordingly.
84
	 * @param key the context key (never {@code null})
A
Arjen Poutsma 已提交
85
	 * @return the corresponding ApplicationContext instance,
86
	 * or {@code null} if not found in the cache.
A
Arjen Poutsma 已提交
87 88
	 * @see #remove
	 */
89
	ApplicationContext get(MergedContextConfiguration key) {
A
Arjen Poutsma 已提交
90
		Assert.notNull(key, "Key must not be null");
91
		ApplicationContext context = this.contextMap.get(key);
A
Arjen Poutsma 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		if (context == null) {
			incrementMissCount();
		}
		else {
			incrementHitCount();
		}
		return context;
	}

	/**
	 * Increment the hit count by one. A <em>hit</em> is an access to the
	 * cache, which returned a non-null context for a queried key.
	 */
	private void incrementHitCount() {
		this.hitCount++;
	}

	/**
	 * Increment the miss count by one. A <em>miss</em> is an access to the
111
	 * cache, which returned a {@code null} context for a queried key.
A
Arjen Poutsma 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	 */
	private void incrementMissCount() {
		this.missCount++;
	}

	/**
	 * Get the overall hit count for this cache. A <em>hit</em> is an access
	 * to the cache, which returned a non-null context for a queried key.
	 */
	int getHitCount() {
		return this.hitCount;
	}

	/**
	 * Get the overall miss count for this cache. A <em>miss</em> is an
127
	 * access to the cache, which returned a {@code null} context for a
A
Arjen Poutsma 已提交
128 129 130 131 132 133 134
	 * queried key.
	 */
	int getMissCount() {
		return this.missCount;
	}

	/**
135
	 * Explicitly add an ApplicationContext instance to the cache under the given key.
136 137
	 * @param key the context key (never {@code null})
	 * @param context the ApplicationContext instance (never {@code null})
A
Arjen Poutsma 已提交
138
	 */
139
	void put(MergedContextConfiguration key, ApplicationContext context) {
A
Arjen Poutsma 已提交
140 141
		Assert.notNull(key, "Key must not be null");
		Assert.notNull(context, "ApplicationContext must not be null");
142
		this.contextMap.put(key, context);
A
Arjen Poutsma 已提交
143 144 145 146
	}

	/**
	 * Remove the context with the given key.
147 148
	 * @param key the context key (never {@code null})
	 * @return the corresponding ApplicationContext instance, or {@code null}
149
	 * if not found in the cache.
A
Arjen Poutsma 已提交
150 151
	 * @see #setDirty
	 */
152 153
	ApplicationContext remove(MergedContextConfiguration key) {
		return this.contextMap.remove(key);
A
Arjen Poutsma 已提交
154 155 156 157 158
	}

	/**
	 * Mark the context with the given key as dirty, effectively
	 * {@link #remove removing} the context from the cache and explicitly
159 160
	 * {@link ConfigurableApplicationContext#close() closing} it if it is an
	 * instance of {@link ConfigurableApplicationContext}.
161 162 163
	 * <p>Generally speaking, you would only call this method if you change the
	 * state of a singleton bean, potentially affecting future interaction with
	 * the context.
164
	 * @param key the context key (never {@code null})
A
Arjen Poutsma 已提交
165 166
	 * @see #remove
	 */
167
	void setDirty(MergedContextConfiguration key) {
A
Arjen Poutsma 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180
		Assert.notNull(key, "Key must not be null");
		ApplicationContext context = remove(key);
		if (context instanceof ConfigurableApplicationContext) {
			((ConfigurableApplicationContext) context).close();
		}
	}

	/**
	 * Determine the number of contexts currently stored in the cache. If the
	 * cache contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
	 * <tt>Integer.MAX_VALUE</tt>.
	 */
	int size() {
181
		return this.contextMap.size();
A
Arjen Poutsma 已提交
182 183 184 185 186 187 188
	}

	/**
	 * Generates a text string, which contains the {@link #size() size} as well
	 * as the {@link #hitCount hit} and {@link #missCount miss} counts.
	 */
	public String toString() {
189 190
		return new ToStringCreator(this).append("size", size()).append("hitCount", getHitCount()).
				append("missCount", getMissCount()).toString();
A
Arjen Poutsma 已提交
191 192 193
	}

}