[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

@@ -18,9 +18,9 @@ package org.springframework.test.context;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -243,13 +243,16 @@ abstract class ContextLoaderUtils {
annotationType, clazz));
}
final Set<String> activeProfiles = new LinkedHashSet<String>();
// Active profiles must be sorted due to cache key generation in
// TestContext. Specifically, profile sets {foo,bar} and {bar,foo}
// must both result in the same array (e.g., [bar,foo]).
final SortedSet<String> activeProfiles = new TreeSet<String>();
while (declaringClass != null) {
ActiveProfiles annotation = declaringClass.getAnnotation(annotationType);
if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", activeProfiles,
logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
declaringClass));
}

View File

@@ -170,12 +170,24 @@ public class TestContext extends AttributeAccessorSupport {
if (context == null) {
try {
context = loadApplicationContext();
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Storing ApplicationContext for test class [%s] in cache under key [%s].", testClass,
contextKey));
}
contextCache.put(contextKey, context);
}
catch (Exception ex) {
throw new IllegalStateException("Failed to load ApplicationContext", ex);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Retrieved ApplicationContext for test class [%s] from cache with key [%s].", testClass,
contextKey));
}
}
return context;
}
}