Skip to content

Commit dbe96b5

Browse files
committed
[SPR-7326] MergedContextConfiguration now ensures that it holds non-null arrays with proper semantics for TestContext's cache key generation.
1 parent 3f58da1 commit dbe96b5

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.HashSet;
2122
import java.util.List;
22-
import java.util.SortedSet;
23-
import java.util.TreeSet;
23+
import java.util.Set;
2424

2525
import org.apache.commons.logging.Log;
2626
import org.apache.commons.logging.LogFactory;
@@ -243,10 +243,7 @@ static String[] resolveActiveProfiles(Class<?> clazz) {
243243
annotationType, clazz));
244244
}
245245

246-
// Active profiles must be sorted due to cache key generation in
247-
// TestContext. Specifically, profile sets {foo,bar} and {bar,foo}
248-
// must both result in the same array (e.g., [bar,foo]).
249-
final SortedSet<String> activeProfiles = new TreeSet<String>();
246+
final Set<String> activeProfiles = new HashSet<String>();
250247

251248
while (declaringClass != null) {
252249
ActiveProfiles annotation = declaringClass.getAnnotation(annotationType);

org.springframework.test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package org.springframework.test.context;
1818

19+
import java.util.Arrays;
20+
import java.util.SortedSet;
21+
import java.util.TreeSet;
22+
1923
import org.springframework.core.style.ToStringCreator;
2024
import org.springframework.util.ObjectUtils;
25+
import org.springframework.util.StringUtils;
2126

2227
/**
2328
* TODO [SPR-8386] Document MergedContextConfiguration.
@@ -40,6 +45,26 @@ public class MergedContextConfiguration {
4045
private final ContextLoader contextLoader;
4146

4247

48+
private static String[] processLocations(String[] locations) {
49+
return locations == null ? new String[] {} : locations;
50+
}
51+
52+
private static Class<?>[] processClasses(Class<?>[] classes) {
53+
return classes == null ? new Class<?>[] {} : classes;
54+
}
55+
56+
private static String[] processActiveProfiles(String[] activeProfiles) {
57+
if (activeProfiles == null) {
58+
return new String[] {};
59+
}
60+
61+
// Active profiles must be unique and sorted due to cache key generation
62+
// in TestContext. Specifically, profile sets {foo,bar} and {bar,foo}
63+
// must both result in the same array (e.g., [bar,foo]).
64+
SortedSet<String> sortedProfilesSet = new TreeSet<String>(Arrays.asList(activeProfiles));
65+
return StringUtils.toStringArray(sortedProfilesSet);
66+
}
67+
4368
/**
4469
* TODO Document MergedContextConfiguration constructor.
4570
*
@@ -52,9 +77,9 @@ public class MergedContextConfiguration {
5277
public MergedContextConfiguration(Class<?> testClass, String[] locations, Class<?>[] classes,
5378
String[] activeProfiles, ContextLoader contextLoader) {
5479
this.testClass = testClass;
55-
this.locations = locations;
56-
this.classes = classes;
57-
this.activeProfiles = activeProfiles;
80+
this.locations = processLocations(locations);
81+
this.classes = processClasses(classes);
82+
this.activeProfiles = processActiveProfiles(activeProfiles);
5883
this.contextLoader = contextLoader;
5984
}
6085

0 commit comments

Comments
 (0)