Skip to content

Commit 844f22b

Browse files
committed
Make locations unique in MergedContextConfiguration
Prior to this commit the same location could be added twice and this would cause the Context loader to load it multiple times. See spring-projectsgh-32534
1 parent db1010f commit 844f22b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ public MergedContextConfiguration(Class<?> testClass, @Nullable String[] locatio
288288
@Nullable MergedContextConfiguration parent) {
289289

290290
this.testClass = testClass;
291-
this.locations = processStrings(locations);
291+
this.locations = processUniqueStrings(locations);
292292
this.classes = processClasses(classes);
293293
this.contextInitializerClasses = processContextInitializerClasses(contextInitializerClasses);
294-
this.activeProfiles = processActiveProfiles(activeProfiles);
294+
this.activeProfiles = processUniqueStrings(activeProfiles);
295295
this.propertySourceDescriptors = Collections.unmodifiableList(propertySourceDescriptors);
296296
this.propertySourceLocations = this.propertySourceDescriptors.stream()
297297
.map(PropertySourceDescriptor::locations)
@@ -597,13 +597,13 @@ private static Set<ContextCustomizer> processContextCustomizers(
597597
Collections.unmodifiableSet(contextCustomizers) : EMPTY_CONTEXT_CUSTOMIZERS);
598598
}
599599

600-
private static String[] processActiveProfiles(@Nullable String[] activeProfiles) {
601-
if (activeProfiles == null) {
600+
private static String[] processUniqueStrings(@Nullable String[] array) {
601+
if (array == null) {
602602
return EMPTY_STRING_ARRAY;
603603
}
604604

605605
// Active profiles must be unique
606-
Set<String> profilesSet = new LinkedHashSet<>(Arrays.asList(activeProfiles));
606+
Set<String> profilesSet = new LinkedHashSet<>(Arrays.asList(array));
607607
return StringUtils.toStringArray(profilesSet);
608608
}
609609

Diff for: spring-test/src/test/java/org/springframework/test/context/MergedContextConfigurationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ void hashCodeWithDifferentLocations() {
105105
assertThat(mergedConfig2.hashCode()).isNotEqualTo(mergedConfig1.hashCode());
106106
}
107107

108+
@Test
109+
void hashCodeWithSameDuplicateLocations() {
110+
String[] locations1 = new String[] { "foo", "bar}" };
111+
String[] locations2 = new String[] { "foo", "bar}", "foo", "bar}" };
112+
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), locations1,
113+
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
114+
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), locations2,
115+
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
116+
assertThat(mergedConfig2.hashCode()).isEqualTo(mergedConfig1.hashCode());
117+
}
118+
119+
108120
@Test
109121
void hashCodeWithSameConfigClasses() {
110122
Class<?>[] classes = new Class<?>[] { String.class, Integer.class };

0 commit comments

Comments
 (0)