From 276712dcd1eef93e2a9add83e3d11f0eb6f06095 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 10 Jan 2015 20:36:48 +0100 Subject: [PATCH] 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 --- .../DefaultActiveProfilesResolver.java | 12 +----- .../support/ActiveProfilesUtilsTests.java | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java b/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java index 31a38b2a1d..7651d3dac0 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java @@ -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 { } } else { - Class rootDeclaringClass = descriptor.getRootDeclaringClass(); Class declaringClass = descriptor.getDeclaringClass(); AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes(); @@ -82,15 +81,6 @@ public class DefaultActiveProfilesResolver implements ActiveProfilesResolver { annAttrs, declaringClass.getName())); } - Class 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); diff --git a/spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java b/spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java index 46ff1f44c3..a4a7c9081d 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/ActiveProfilesUtilsTests.java @@ -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; 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 /** * 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 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 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 } } + private static class ExtendedDefaultActiveProfilesResolver extends DefaultActiveProfilesResolver { + + @Override + public String[] resolve(Class testClass) { + List profiles = new ArrayList(Arrays.asList(super.resolve(testClass))); + profiles.add("foo"); + return StringUtils.toStringArray(profiles); + } + } + }