Browse Source

Enable reuse of DefaultActiveProfilesResolver

In order to allow DefaultActiveProfilesResolver to be reused (e.g., via
extension or delegation), the check which asserts that the 'resolver'
attribute of @ActiveProfiles is not set to a customer resolver class
has been removed.

Issue: SPR-12611
pull/719/head
Sam Brannen 10 years ago
parent
commit
276712dcd1
  1. 12
      spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java
  2. 42
      spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java

12
spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -73,7 +73,6 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver { @@ -73,7 +73,6 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {
}
}
else {
Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
Class<?> declaringClass = descriptor.getDeclaringClass();
AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
@ -82,15 +81,6 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver { @@ -82,15 +81,6 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver {
annAttrs, declaringClass.getName()));
}
Class<? extends ActiveProfilesResolver> resolverClass = annAttrs.getClass("resolver");
if (!ActiveProfilesResolver.class.equals(resolverClass)) {
String msg = String.format("Configuration error for test class [%s]: %s cannot be used "
+ "in conjunction with custom resolver [%s].", rootDeclaringClass.getName(),
getClass().getSimpleName(), resolverClass.getName());
logger.error(msg);
throw new IllegalStateException(msg);
}
String[] profiles = annAttrs.getStringArray("profiles");
String[] valueProfiles = annAttrs.getStringArray("value");
boolean valueDeclared = !ObjectUtils.isEmpty(valueProfiles);

42
spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,14 +20,17 @@ import java.lang.annotation.ElementType; @@ -20,14 +20,17 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.util.StringUtils;
import static org.junit.Assert.*;
import static org.springframework.test.context.support.ActiveProfilesUtils.*;
@ -211,7 +214,6 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT @@ -211,7 +214,6 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT
/**
* This test verifies that the actual test class, not the composed annotation,
* is passed to the resolver.
*
* @since 4.0.3
*/
@Test
@ -220,6 +222,24 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT @@ -220,6 +222,24 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT
assertResolvedProfiles(testClass, testClass.getSimpleName());
}
/**
* This test verifies that {@link DefaultActiveProfilesResolver} can be declared explicitly.
* @since 4.1.5
*/
@Test
public void resolveActiveProfilesWithDefaultActiveProfilesResolver() {
assertResolvedProfiles(DefaultActiveProfilesResolverTestCase.class, "default");
}
/**
* This test verifies that {@link DefaultActiveProfilesResolver} can be extended.
* @since 4.1.5
*/
@Test
public void resolveActiveProfilesWithExtendedDefaultActiveProfilesResolver() {
assertResolvedProfiles(ExtendedDefaultActiveProfilesResolverTestCase.class, "default", "foo");
}
// -------------------------------------------------------------------------
@ -294,6 +314,14 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT @@ -294,6 +314,14 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT
private static class TestClassVerifyingActiveProfilesResolverTestCase {
}
@ActiveProfiles(profiles = "default", resolver = DefaultActiveProfilesResolver.class)
private static class DefaultActiveProfilesResolverTestCase {
}
@ActiveProfiles(profiles = "default", resolver = ExtendedDefaultActiveProfilesResolver.class)
private static class ExtendedDefaultActiveProfilesResolverTestCase {
}
@ActiveProfiles(profiles = "conflict 1", value = "conflict 2")
private static class ConflictingProfilesAndValueTestCase {
}
@ -343,4 +371,14 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT @@ -343,4 +371,14 @@ public class ActiveProfilesUtilsTests extends AbstractContextConfigurationUtilsT
}
}
private static class ExtendedDefaultActiveProfilesResolver extends DefaultActiveProfilesResolver {
@Override
public String[] resolve(Class<?> testClass) {
List<String> profiles = new ArrayList<String>(Arrays.asList(super.resolve(testClass)));
profiles.add("foo");
return StringUtils.toStringArray(profiles);
}
}
}

Loading…
Cancel
Save