Simplify ContextLoaderUtilsActiveProfilesTests

master
Sam Brannen 11 years ago
parent 7e38f479e0
commit 34e90ba7f7
  1. 109
      spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java

@ -21,7 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.HashSet;
import java.util.Set;
import org.junit.Test; import org.junit.Test;
@ -38,77 +39,58 @@ import static org.springframework.test.context.ContextLoaderUtils.*;
*/ */
public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoaderUtilsTests { public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoaderUtilsTests {
private void assertResolvedProfiles(Class<?> testClass, String... expected) {
assertNotNull(testClass);
assertNotNull(expected);
String[] actual = resolveActiveProfiles(testClass);
Set<String> expectedSet = new HashSet<String>(Arrays.asList(expected));
Set<String> actualSet = new HashSet<String>(Arrays.asList(actual));
assertEquals(expectedSet, actualSet);
}
@Test @Test
public void resolveActiveProfilesWithoutAnnotation() { public void resolveActiveProfilesWithoutAnnotation() {
String[] profiles = resolveActiveProfiles(Enigma.class); assertArrayEquals(EMPTY_STRING_ARRAY, resolveActiveProfiles(Enigma.class));
assertArrayEquals(EMPTY_STRING_ARRAY, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithNoProfilesDeclared() { public void resolveActiveProfilesWithNoProfilesDeclared() {
String[] profiles = resolveActiveProfiles(BareAnnotations.class); assertArrayEquals(EMPTY_STRING_ARRAY, resolveActiveProfiles(BareAnnotations.class));
assertArrayEquals(EMPTY_STRING_ARRAY, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithEmptyProfiles() { public void resolveActiveProfilesWithEmptyProfiles() {
String[] profiles = resolveActiveProfiles(EmptyProfiles.class); assertArrayEquals(EMPTY_STRING_ARRAY, resolveActiveProfiles(EmptyProfiles.class));
assertArrayEquals(EMPTY_STRING_ARRAY, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithDuplicatedProfiles() { public void resolveActiveProfilesWithDuplicatedProfiles() {
String[] profiles = resolveActiveProfiles(DuplicatedProfiles.class); assertResolvedProfiles(DuplicatedProfiles.class, "foo", "bar", "baz");
assertNotNull(profiles);
assertEquals(3, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
assertTrue(list.contains("baz"));
} }
@Test @Test
public void resolveActiveProfilesWithLocalAnnotation() { public void resolveActiveProfilesWithLocalAnnotation() {
String[] profiles = resolveActiveProfiles(LocationsFoo.class); assertResolvedProfiles(LocationsFoo.class, "foo");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithInheritedAnnotationAndLocations() { public void resolveActiveProfilesWithInheritedAnnotationAndLocations() {
String[] profiles = resolveActiveProfiles(InheritedLocationsFoo.class); assertResolvedProfiles(InheritedLocationsFoo.class, "foo");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithInheritedAnnotationAndClasses() { public void resolveActiveProfilesWithInheritedAnnotationAndClasses() {
String[] profiles = resolveActiveProfiles(InheritedClassesFoo.class); assertResolvedProfiles(InheritedClassesFoo.class, "foo");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
@Test @Test
public void resolveActiveProfilesWithLocalAndInheritedAnnotations() { public void resolveActiveProfilesWithLocalAndInheritedAnnotations() {
String[] profiles = resolveActiveProfiles(LocationsBar.class); assertResolvedProfiles(LocationsBar.class, "foo", "bar");
assertNotNull(profiles);
assertEquals(2, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
} }
@Test @Test
public void resolveActiveProfilesWithOverriddenAnnotation() { public void resolveActiveProfilesWithOverriddenAnnotation() {
String[] profiles = resolveActiveProfiles(Animals.class); assertResolvedProfiles(Animals.class, "dog", "cat");
assertNotNull(profiles);
assertEquals(2, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("dog"));
assertTrue(list.contains("cat"));
} }
/** /**
@ -116,9 +98,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithMetaAnnotation() { public void resolveActiveProfilesWithMetaAnnotation() {
String[] profiles = resolveActiveProfiles(MetaLocationsFoo.class); assertResolvedProfiles(MetaLocationsFoo.class, "foo");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
/** /**
@ -126,9 +106,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithMetaAnnotationAndOverrides() { public void resolveActiveProfilesWithMetaAnnotationAndOverrides() {
String[] profiles = resolveActiveProfiles(MetaLocationsFooWithOverrides.class); assertResolvedProfiles(MetaLocationsFooWithOverrides.class, "foo");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
/** /**
@ -136,9 +114,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithMetaAnnotationAndOverriddenAttributes() { public void resolveActiveProfilesWithMetaAnnotationAndOverriddenAttributes() {
String[] profiles = resolveActiveProfiles(MetaLocationsFooWithOverriddenAttributes.class); assertResolvedProfiles(MetaLocationsFooWithOverriddenAttributes.class, "foo1", "foo2");
assertNotNull(profiles);
assertArrayEquals(new String[] { "foo1", "foo2" }, profiles);
} }
/** /**
@ -146,13 +122,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithLocalAndInheritedMetaAnnotations() { public void resolveActiveProfilesWithLocalAndInheritedMetaAnnotations() {
String[] profiles = resolveActiveProfiles(MetaLocationsBar.class); assertResolvedProfiles(MetaLocationsBar.class, "foo", "bar");
assertNotNull(profiles);
assertEquals(2, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
} }
/** /**
@ -160,13 +130,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithOverriddenMetaAnnotation() { public void resolveActiveProfilesWithOverriddenMetaAnnotation() {
String[] profiles = resolveActiveProfiles(MetaAnimals.class); assertResolvedProfiles(MetaAnimals.class, "dog", "cat");
assertNotNull(profiles);
assertEquals(2, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("dog"));
assertTrue(list.contains("cat"));
} }
/** /**
@ -174,10 +138,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithResolver() { public void resolveActiveProfilesWithResolver() {
String[] profiles = resolveActiveProfiles(FooActiveProfilesResolverTestCase.class); assertResolvedProfiles(FooActiveProfilesResolverTestCase.class, "foo");
assertNotNull(profiles);
assertEquals(1, profiles.length);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
/** /**
@ -185,10 +146,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithInheritedResolver() { public void resolveActiveProfilesWithInheritedResolver() {
String[] profiles = resolveActiveProfiles(InheritedFooActiveProfilesResolverTestCase.class); assertResolvedProfiles(InheritedFooActiveProfilesResolverTestCase.class, "foo");
assertNotNull(profiles);
assertEquals(1, profiles.length);
assertArrayEquals(new String[] { "foo" }, profiles);
} }
/** /**
@ -196,12 +154,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithMergedInheritedResolver() { public void resolveActiveProfilesWithMergedInheritedResolver() {
String[] profiles = resolveActiveProfiles(MergedInheritedFooActiveProfilesResolverTestCase.class); assertResolvedProfiles(MergedInheritedFooActiveProfilesResolverTestCase.class, "foo", "bar");
assertNotNull(profiles);
assertEquals(2, profiles.length);
List<String> list = Arrays.asList(profiles);
assertTrue(list.contains("foo"));
assertTrue(list.contains("bar"));
} }
/** /**
@ -209,10 +162,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
*/ */
@Test @Test
public void resolveActiveProfilesWithOverridenInheritedResolver() { public void resolveActiveProfilesWithOverridenInheritedResolver() {
String[] profiles = resolveActiveProfiles(OverridenInheritedFooActiveProfilesResolverTestCase.class); assertResolvedProfiles(OverridenInheritedFooActiveProfilesResolverTestCase.class, "bar");
assertNotNull(profiles);
assertEquals(1, profiles.length);
assertArrayEquals(new String[] { "bar" }, profiles);
} }
/** /**
@ -279,6 +229,7 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader
@ActiveProfiles @ActiveProfiles
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
// TODO Write test with @MetaProfilesWithOverrides.
private static @interface MetaProfilesWithOverrides { private static @interface MetaProfilesWithOverrides {
String[] profiles() default { "dog", "cat" }; String[] profiles() default { "dog", "cat" };

Loading…
Cancel
Save