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 6f677a610f..2e540dda97 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,8 +18,9 @@ package org.springframework.test.context; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -228,7 +229,7 @@ abstract class ContextLoaderUtils { annotationType, clazz)); } - List profilesList = new ArrayList(); + final Set activeProfiles = new LinkedHashSet(); while (declaringClass != null) { ActivateProfiles activateProfiles = declaringClass.getAnnotation(annotationType); @@ -253,19 +254,9 @@ abstract class ContextLoaderUtils { profiles = valueProfiles; } - if (!ObjectUtils.isEmpty(profiles)) { - - // Preserve the order in which the profiles were declared by - // reversing the array. This is necessary since we prepend each - // non-empty element to the aggregated list as we traverse the - // class hierarchy. Note that the following works because - // Arrays.asList() "writes through" to the underlying array. - Collections.reverse(Arrays. asList(profiles)); - - for (String profile : profiles) { - if (StringUtils.hasText(profile)) { - profilesList.add(0, profile); - } + for (String profile : profiles) { + if (StringUtils.hasText(profile)) { + activeProfiles.add(profile); } } @@ -273,7 +264,7 @@ abstract class ContextLoaderUtils { annotationType, declaringClass.getSuperclass()) : null; } - return profilesList.toArray(new String[profilesList.size()]); + return StringUtils.toStringArray(activeProfiles); } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java index 333422fe67..7242ea7552 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java @@ -18,6 +18,10 @@ package org.springframework.test.context; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; import org.junit.Test; @@ -50,6 +54,18 @@ public class ContextLoaderUtilsTests { assertEquals(0, profiles.length); } + @Test + public void resolveActivatedProfilesWithDuplicatedProfiles() { + String[] profiles = ContextLoaderUtils.resolveActivatedProfiles(DuplicatedProfiles.class); + assertNotNull(profiles); + assertEquals(3, profiles.length); + + List list = Arrays.asList(profiles); + assertTrue(list.contains("foo")); + assertTrue(list.contains("bar")); + assertTrue(list.contains("baz")); + } + @Test public void resolveActivatedProfilesWithLocalAnnotation() { String[] profiles = ContextLoaderUtils.resolveActivatedProfiles(Foo.class); @@ -71,8 +87,10 @@ public class ContextLoaderUtilsTests { String[] profiles = ContextLoaderUtils.resolveActivatedProfiles(Bar.class); assertNotNull(profiles); assertEquals(2, profiles.length); - assertEquals("foo", profiles[0]); - assertEquals("bar", profiles[1]); + + List list = Arrays.asList(profiles); + assertTrue(list.contains("foo")); + assertTrue(list.contains("bar")); } @Test @@ -80,8 +98,10 @@ public class ContextLoaderUtilsTests { String[] profiles = ContextLoaderUtils.resolveActivatedProfiles(Animals.class); assertNotNull(profiles); assertEquals(2, profiles.length); - assertEquals("dog", profiles[0]); - assertEquals("cat", profiles[1]); + + List list = Arrays.asList(profiles); + assertTrue(list.contains("dog")); + assertTrue(list.contains("cat")); } @@ -96,6 +116,10 @@ public class ContextLoaderUtilsTests { private static class EmptyProfiles { } + @ActivateProfiles({ "foo", "bar", "foo", "bar", "baz" }) + private static class DuplicatedProfiles { + } + @ActivateProfiles(profiles = "foo") private static class Foo { }