提交 dbe96b5c 编写于 作者: S Sam Brannen

[SPR-7326] MergedContextConfiguration now ensures that it holds non-null...

[SPR-7326] MergedContextConfiguration now ensures that it holds non-null arrays with proper semantics for TestContext's cache key generation.
上级 3f58da1c
...@@ -18,9 +18,9 @@ package org.springframework.test.context; ...@@ -18,9 +18,9 @@ package org.springframework.test.context;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.SortedSet; import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -243,10 +243,7 @@ abstract class ContextLoaderUtils { ...@@ -243,10 +243,7 @@ abstract class ContextLoaderUtils {
annotationType, clazz)); annotationType, clazz));
} }
// Active profiles must be sorted due to cache key generation in final Set<String> activeProfiles = new HashSet<String>();
// 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) { while (declaringClass != null) {
ActiveProfiles annotation = declaringClass.getAnnotation(annotationType); ActiveProfiles annotation = declaringClass.getAnnotation(annotationType);
......
...@@ -16,8 +16,13 @@ ...@@ -16,8 +16,13 @@
package org.springframework.test.context; 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.core.style.ToStringCreator;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/** /**
* TODO [SPR-8386] Document MergedContextConfiguration. * TODO [SPR-8386] Document MergedContextConfiguration.
...@@ -40,6 +45,26 @@ public class MergedContextConfiguration { ...@@ -40,6 +45,26 @@ public class MergedContextConfiguration {
private final ContextLoader contextLoader; 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<String> sortedProfilesSet = new TreeSet<String>(Arrays.asList(activeProfiles));
return StringUtils.toStringArray(sortedProfilesSet);
}
/** /**
* TODO Document MergedContextConfiguration constructor. * TODO Document MergedContextConfiguration constructor.
* *
...@@ -52,9 +77,9 @@ public class MergedContextConfiguration { ...@@ -52,9 +77,9 @@ public class MergedContextConfiguration {
public MergedContextConfiguration(Class<?> testClass, String[] locations, Class<?>[] classes, public MergedContextConfiguration(Class<?> testClass, String[] locations, Class<?>[] classes,
String[] activeProfiles, ContextLoader contextLoader) { String[] activeProfiles, ContextLoader contextLoader) {
this.testClass = testClass; this.testClass = testClass;
this.locations = locations; this.locations = processLocations(locations);
this.classes = classes; this.classes = processClasses(classes);
this.activeProfiles = activeProfiles; this.activeProfiles = processActiveProfiles(activeProfiles);
this.contextLoader = contextLoader; this.contextLoader = contextLoader;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册