From dbe96b5cf9727302509ce36aa831184ba5c25463 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 3 Jun 2011 22:46:43 +0000 Subject: [PATCH] [SPR-7326] MergedContextConfiguration now ensures that it holds non-null arrays with proper semantics for TestContext's cache key generation. --- .../test/context/ContextLoaderUtils.java | 9 ++---- .../context/MergedContextConfiguration.java | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index 773821a65e..996c93dfa3 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -18,9 +18,9 @@ package org.springframework.test.context; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -243,10 +243,7 @@ abstract class ContextLoaderUtils { annotationType, clazz)); } - // 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 activeProfiles = new TreeSet(); + final Set activeProfiles = new HashSet(); while (declaringClass != null) { ActiveProfiles annotation = declaringClass.getAnnotation(annotationType); diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java b/org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java index 87829aedcf..a7836dd2b2 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java @@ -16,8 +16,13 @@ package org.springframework.test.context; +import java.util.Arrays; +import java.util.SortedSet; +import java.util.TreeSet; + import org.springframework.core.style.ToStringCreator; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; /** * TODO [SPR-8386] Document MergedContextConfiguration. @@ -40,6 +45,26 @@ public class MergedContextConfiguration { private final ContextLoader contextLoader; + private static String[] processLocations(String[] locations) { + return locations == null ? new String[] {} : locations; + } + + private static Class[] processClasses(Class[] classes) { + return classes == null ? new Class[] {} : classes; + } + + private static String[] processActiveProfiles(String[] activeProfiles) { + if (activeProfiles == null) { + return new String[] {}; + } + + // Active profiles must be unique and 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]). + SortedSet sortedProfilesSet = new TreeSet(Arrays.asList(activeProfiles)); + return StringUtils.toStringArray(sortedProfilesSet); + } + /** * TODO Document MergedContextConfiguration constructor. * @@ -52,9 +77,9 @@ public class MergedContextConfiguration { public MergedContextConfiguration(Class testClass, String[] locations, Class[] classes, String[] activeProfiles, ContextLoader contextLoader) { this.testClass = testClass; - this.locations = locations; - this.classes = classes; - this.activeProfiles = activeProfiles; + this.locations = processLocations(locations); + this.classes = processClasses(classes); + this.activeProfiles = processActiveProfiles(activeProfiles); this.contextLoader = contextLoader; } -- GitLab