From c4b5960c130871aa26b677b133cbe23a78ac03d2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 10 Jul 2009 11:45:31 +0000 Subject: [PATCH] [SPR-5914] ProfileValueUtils now properly ensures that class-level usage of @IfProfileValue overrides method-level usage. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1507 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../test/annotation/IfProfileValue.java | 33 +-- .../test/annotation/ProfileValueUtils.java | 122 +++++----- .../annotation/ProfileValueUtilsTests.java | 217 ++++++++++++++++++ ...ProfileValueJUnit38SpringContextTests.java | 108 +++++++-- .../EnabledAndIgnoredSpringRunnerTests.java | 25 +- ...edProfileValueSourceSpringRunnerTests.java | 17 +- 6 files changed, 400 insertions(+), 122 deletions(-) create mode 100644 org.springframework.test/src/test/java/org/springframework/test/annotation/ProfileValueUtilsTests.java diff --git a/org.springframework.test/src/main/java/org/springframework/test/annotation/IfProfileValue.java b/org.springframework.test/src/main/java/org/springframework/test/annotation/IfProfileValue.java index afbf289f688..9d2a6240b48 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/annotation/IfProfileValue.java +++ b/org.springframework.test/src/main/java/org/springframework/test/annotation/IfProfileValue.java @@ -31,62 +31,64 @@ import java.lang.annotation.Target; * test will be enabled. *

*

- * Note: @IfProfileValue can be applied at either the - * class or method level. + * Note: @IfProfileValue can be applied at the class level, + * the method level, or both. @IfProfileValue at the class + * level overrides method-level usage of @IfProfileValue for + * any methods within that class. *

*

* Examples: when using {@link SystemProfileValueSource} as the * {@link ProfileValueSource} implementation, you can configure a test method to * run only on Java VMs from Sun Microsystems as follows: *

- * + * *
- * @IfProfileValue(name="java.vendor", value="Sun Microsystems Inc.")
+ * @IfProfileValue(name = "java.vendor", value = "Sun Microsystems Inc.")
  * public void testSomething() {
- *     // ...
+ * 	// ...
  * }
  * 
- * *

* You can alternatively configure @IfProfileValue with * OR semantics for multiple {@link #values() values} as follows * (assuming a {@link ProfileValueSource} has been appropriately configured for * the "test-groups" name): *

- * + * *
- * @IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"})
+ * @IfProfileValue(name = "test-groups", values = { "unit-tests", "integration-tests" })
  * public void testWhichRunsForUnitOrIntegrationTestGroups() {
- *     // ...
+ * 	// ...
  * }
  * 
- * + * * @author Rod Johnson * @author Sam Brannen * @since 2.0 * @see ProfileValueSource * @see ProfileValueSourceConfiguration * @see ProfileValueUtils - * @see AbstractAnnotationAwareTransactionalTests * @see org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests * @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner */ -@Target({ElementType.TYPE, ElementType.METHOD}) +@Target( { ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface IfProfileValue { /** - * The name of the profile value against which to test. + * The name of the profile value against which to + * test. */ String name(); /** * A single, permissible value of the profile value * for the given {@link #name() name}. - *

Note: Assigning values to both {@link #value()} and {@link #values()} + *

+ * Note: Assigning values to both {@link #value()} and {@link #values()} * will lead to a configuration conflict. */ String value() default ""; @@ -94,7 +96,8 @@ public @interface IfProfileValue { /** * A list of all permissible values of the * profile value for the given {@link #name() name}. - *

Note: Assigning values to both {@link #value()} and {@link #values()} + *

+ * Note: Assigning values to both {@link #value()} and {@link #values()} * will lead to a configuration conflict. */ String[] values() default {}; diff --git a/org.springframework.test/src/main/java/org/springframework/test/annotation/ProfileValueUtils.java b/org.springframework.test/src/main/java/org/springframework/test/annotation/ProfileValueUtils.java index ea1a6797af5..f43ea26f8c6 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/annotation/ProfileValueUtils.java +++ b/org.springframework.test/src/main/java/org/springframework/test/annotation/ProfileValueUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 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,7 +20,6 @@ import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -28,7 +27,7 @@ import org.springframework.util.StringUtils; /** * General utility methods for working with profile values. - * + * * @author Sam Brannen * @author Juergen Hoeller * @since 2.5 @@ -44,15 +43,15 @@ public abstract class ProfileValueUtils { /** * Retrieves the {@link ProfileValueSource} type for the specified * {@link Class test class} as configured via the - * {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} - * annotation and instantiates a new instance of that type. + * {@link ProfileValueSourceConfiguration + * @ProfileValueSourceConfiguration} annotation and instantiates a new + * instance of that type. *

- * If - * {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} - * is not present on the specified class or if a custom - * {@link ProfileValueSource} is not declared, the default - * {@link SystemProfileValueSource} will be returned instead. - * + * If {@link ProfileValueSourceConfiguration + * @ProfileValueSourceConfiguration} is not present on the specified + * class or if a custom {@link ProfileValueSource} is not declared, the + * default {@link SystemProfileValueSource} will be returned instead. + * * @param testClass The test class for which the ProfileValueSource should * be retrieved * @return the configured (or default) ProfileValueSource for the specified @@ -103,95 +102,92 @@ public abstract class ProfileValueUtils { } /** - * Determine if the supplied testClass is enabled - * in the current environment, as specified by the - * {@link IfProfileValue @IfProfileValue} annotation at the class level. + * Determine if the supplied testClass is enabled in + * the current environment, as specified by the {@link IfProfileValue + * @IfProfileValue} annotation at the class level. *

- * Defaults to true if no - * {@link IfProfileValue @IfProfileValue} annotation is declared. - * + * Defaults to true if no {@link IfProfileValue + * @IfProfileValue} annotation is declared. + * * @param testClass the test class - * @return true if the test is enabled in the - * current environment + * @return true if the test is enabled in the current + * environment */ public static boolean isTestEnabledInThisEnvironment(Class testClass) { IfProfileValue ifProfileValue = testClass.getAnnotation(IfProfileValue.class); - if (ifProfileValue == null) { - return true; - } - ProfileValueSource profileValueSource = retrieveProfileValueSource(testClass); - return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); + return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), ifProfileValue); } /** - * Determine if the supplied testMethod is enabled - * in the current environment, as specified by the - * {@link IfProfileValue @IfProfileValue} annotation, which may be declared - * on the test method itself or at the class level. + * Determine if the supplied testMethod is enabled in + * the current environment, as specified by the {@link IfProfileValue + * @IfProfileValue} annotation, which may be declared on the test + * method itself or at the class level. Class-level usage overrides + * method-level usage. *

- * Defaults to true if no - * {@link IfProfileValue @IfProfileValue} annotation is declared. - * + * Defaults to true if no {@link IfProfileValue + * @IfProfileValue} annotation is declared. + * * @param testMethod the test method * @param testClass the test class - * @return true if the test is enabled in the - * current environment + * @return true if the test is enabled in the current + * environment */ public static boolean isTestEnabledInThisEnvironment(Method testMethod, Class testClass) { - IfProfileValue ifProfileValue = testMethod.getAnnotation(IfProfileValue.class); - if (ifProfileValue == null) { - ifProfileValue = testClass.getAnnotation(IfProfileValue.class); - if (ifProfileValue == null) { - return true; - } - } - ProfileValueSource profileValueSource = retrieveProfileValueSource(testClass); - return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); + return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), testMethod, testClass); } /** - * Determine if the supplied testMethod is enabled - * in the current environment, as specified by the - * {@link IfProfileValue @IfProfileValue} annotation, which may be declared - * on the test method itself or at the class level. + * Determine if the supplied testMethod is enabled in + * the current environment, as specified by the {@link IfProfileValue + * @IfProfileValue} annotation, which may be declared on the test + * method itself or at the class level. Class-level usage overrides + * method-level usage. *

- * Defaults to true if no - * {@link IfProfileValue @IfProfileValue} annotation is declared. - * + * Defaults to true if no {@link IfProfileValue + * @IfProfileValue} annotation is declared. + * * @param profileValueSource the ProfileValueSource to use to determine if * the test is enabled * @param testMethod the test method * @param testClass the test class - * @return true if the test is enabled in the - * current environment + * @return true if the test is enabled in the current + * environment */ public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod, Class testClass) { - IfProfileValue ifProfileValue = testMethod.getAnnotation(IfProfileValue.class); - if (ifProfileValue == null) { - ifProfileValue = testClass.getAnnotation(IfProfileValue.class); - if (ifProfileValue == null) { - return true; - } + IfProfileValue ifProfileValue = testClass.getAnnotation(IfProfileValue.class); + boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); + + if (classLevelEnabled) { + ifProfileValue = testMethod.getAnnotation(IfProfileValue.class); + return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); } - return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); + + return false; } /** * Determine if the value (or one of the values) - * in the supplied {@link IfProfileValue @IfProfileValue} annotation is + * in the supplied {@link IfProfileValue @IfProfileValue} annotation is * enabled in the current environment. - * + * * @param profileValueSource the ProfileValueSource to use to determine if * the test is enabled - * @param ifProfileValue the annotation to introspect - * @return true if the test is enabled in the - * current environment + * @param ifProfileValue the annotation to introspect; may be + * null + * @return true if the test is enabled in the current + * environment or if the supplied ifProfileValue is + * null */ private static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, IfProfileValue ifProfileValue) { + if (ifProfileValue == null) { + return true; + } + String environmentValue = profileValueSource.get(ifProfileValue.name()); String[] annotatedValues = ifProfileValue.values(); if (StringUtils.hasLength(ifProfileValue.value())) { diff --git a/org.springframework.test/src/test/java/org/springframework/test/annotation/ProfileValueUtilsTests.java b/org.springframework.test/src/test/java/org/springframework/test/annotation/ProfileValueUtilsTests.java new file mode 100644 index 00000000000..8a66fde60ea --- /dev/null +++ b/org.springframework.test/src/test/java/org/springframework/test/annotation/ProfileValueUtilsTests.java @@ -0,0 +1,217 @@ +/* + * Copyright 2002-2009 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.annotation; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Unit tests for {@link ProfileValueUtils}. + * + * @author Sam Brannen + * @since 3.0 + */ +@RunWith(JUnit4.class) +public class ProfileValueUtilsTests { + + private static final String NON_ANNOTATED_METHOD = "nonAnnotatedMethod"; + private static final String ENABLED_ANNOTATED_METHOD = "enabledAnnotatedMethod"; + private static final String DISABLED_ANNOTATED_METHOD = "disabledAnnotatedMethod"; + + private static final String NAME = "ProfileValueUtilsTests.profile_value.name"; + private static final String VALUE = "enigma"; + + + @BeforeClass + public static void setProfileValue() { + System.setProperty(NAME, VALUE); + } + + private void assertClassIsEnabled(Class testClass) throws Exception { + assertTrue("Test class [" + testClass + "] should be enabled.", + ProfileValueUtils.isTestEnabledInThisEnvironment(testClass)); + } + + private void assertClassIsDisabled(Class testClass) throws Exception { + assertFalse("Test class [" + testClass + "] should be disbled.", + ProfileValueUtils.isTestEnabledInThisEnvironment(testClass)); + } + + private void assertMethodIsEnabled(String methodName, Class testClass) throws Exception { + Method testMethod = testClass.getMethod(methodName); + assertTrue("Test method [" + testMethod + "] should be enabled.", + ProfileValueUtils.isTestEnabledInThisEnvironment(testMethod, testClass)); + } + + private void assertMethodIsDisabled(String methodName, Class testClass) throws Exception { + Method testMethod = testClass.getMethod(methodName); + assertFalse("Test method [" + testMethod + "] should be disabled.", + ProfileValueUtils.isTestEnabledInThisEnvironment(testMethod, testClass)); + } + + private void assertMethodIsEnabled(ProfileValueSource profileValueSource, String methodName, Class testClass) + throws Exception { + Method testMethod = testClass.getMethod(methodName); + assertTrue("Test method [" + testMethod + "] should be enabled for ProfileValueSource [" + profileValueSource + + "].", ProfileValueUtils.isTestEnabledInThisEnvironment(profileValueSource, testMethod, testClass)); + } + + private void assertMethodIsDisabled(ProfileValueSource profileValueSource, String methodName, Class testClass) + throws Exception { + Method testMethod = testClass.getMethod(methodName); + assertFalse("Test method [" + testMethod + "] should be disabled for ProfileValueSource [" + profileValueSource + + "].", ProfileValueUtils.isTestEnabledInThisEnvironment(profileValueSource, testMethod, testClass)); + } + + // ------------------------------------------------------------------- + + @Test + public void isTestEnabledInThisEnvironmentForProvidedClass() throws Exception { + assertClassIsEnabled(NonAnnotated.class); + assertClassIsEnabled(EnabledAnnotatedSingleValue.class); + assertClassIsEnabled(EnabledAnnotatedMultiValue.class); + assertClassIsDisabled(DisabledAnnotatedSingleValue.class); + assertClassIsDisabled(DisabledAnnotatedMultiValue.class); + } + + @Test + public void isTestEnabledInThisEnvironmentForProvidedMethodAndClass() throws Exception { + assertMethodIsEnabled(NON_ANNOTATED_METHOD, NonAnnotated.class); + + assertMethodIsEnabled(NON_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + assertMethodIsEnabled(ENABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + + assertMethodIsEnabled(NON_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + assertMethodIsEnabled(ENABLED_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + + assertMethodIsDisabled(NON_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + assertMethodIsDisabled(ENABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + + assertMethodIsDisabled(NON_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + assertMethodIsDisabled(ENABLED_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + } + + @Test + public void isTestEnabledInThisEnvironmentForProvidedProfileValueSourceMethodAndClass() throws Exception { + + ProfileValueSource profileValueSource = SystemProfileValueSource.getInstance(); + + assertMethodIsEnabled(profileValueSource, NON_ANNOTATED_METHOD, NonAnnotated.class); + + assertMethodIsEnabled(profileValueSource, NON_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + assertMethodIsEnabled(profileValueSource, ENABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + assertMethodIsDisabled(profileValueSource, DISABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class); + + assertMethodIsEnabled(profileValueSource, NON_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + assertMethodIsEnabled(profileValueSource, ENABLED_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + assertMethodIsDisabled(profileValueSource, DISABLED_ANNOTATED_METHOD, EnabledAnnotatedMultiValue.class); + + assertMethodIsDisabled(profileValueSource, NON_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + assertMethodIsDisabled(profileValueSource, ENABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + assertMethodIsDisabled(profileValueSource, DISABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class); + + assertMethodIsDisabled(profileValueSource, NON_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + assertMethodIsDisabled(profileValueSource, ENABLED_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + assertMethodIsDisabled(profileValueSource, DISABLED_ANNOTATED_METHOD, DisabledAnnotatedMultiValue.class); + } + + + // ------------------------------------------------------------------- + + @SuppressWarnings("unused") + private static class NonAnnotated { + + public void nonAnnotatedMethod() { + } + } + + @SuppressWarnings("unused") + @IfProfileValue(name = NAME, value = VALUE) + private static class EnabledAnnotatedSingleValue { + + public void nonAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE) + public void enabledAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE + "X") + public void disabledAnnotatedMethod() { + } + } + + @SuppressWarnings("unused") + @IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" }) + private static class EnabledAnnotatedMultiValue { + + public void nonAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE) + public void enabledAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE + "X") + public void disabledAnnotatedMethod() { + } + } + + @SuppressWarnings("unused") + @IfProfileValue(name = NAME, value = VALUE + "X") + private static class DisabledAnnotatedSingleValue { + + public void nonAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE) + public void enabledAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE + "X") + public void disabledAnnotatedMethod() { + } + } + + @SuppressWarnings("unused") + @IfProfileValue(name = NAME, values = { "foo", "bar" }) + private static class DisabledAnnotatedMultiValue { + + public void nonAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE) + public void enabledAnnotatedMethod() { + } + + @IfProfileValue(name = NAME, value = VALUE + "X") + public void disabledAnnotatedMethod() { + } + } + +} diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit38/ProfileValueJUnit38SpringContextTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit38/ProfileValueJUnit38SpringContextTests.java index 8c48a84d70e..92b6322ea3d 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit38/ProfileValueJUnit38SpringContextTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit38/ProfileValueJUnit38SpringContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 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. @@ -16,6 +16,9 @@ package org.springframework.test.context.junit38; +import java.util.HashMap; +import java.util.Map; + import junit.framework.TestCase; import junit.framework.TestResult; @@ -26,61 +29,108 @@ import org.springframework.test.annotation.SystemProfileValueSource; import org.springframework.test.context.TestExecutionListeners; /** - * Verifies proper handling of {@link IfProfileValue @IfProfileValue} and - * {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} in - * conjunction with {@link AbstractJUnit38SpringContextTests}. - * + * Verifies proper handling of {@link IfProfileValue @IfProfileValue} and + * {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} + * in conjunction with {@link AbstractJUnit38SpringContextTests}. + * * @author Sam Brannen * @since 2.5 */ public class ProfileValueJUnit38SpringContextTests extends TestCase { - private static final String NAME = "ProfileValueAnnotationAwareTransactionalTests.profile_value.name"; + private static final String EMPTY = "testIfProfileValueEmpty"; + private static final String DISABLED_VIA_WRONG_NAME = "testIfProfileValueDisabledViaWrongName"; + private static final String DISABLED_VIA_WRONG_VALUE = "testIfProfileValueDisabledViaWrongValue"; + private static final String ENABLED_VIA_MULTIPLE_VALUES = "testIfProfileValueEnabledViaMultipleValues"; + private static final String ENABLED_VIA_SINGLE_VALUE = "testIfProfileValueEnabledViaSingleValue"; + private static final String NOT_CONFIGURED = "testIfProfileValueNotConfigured"; + private static final String NAME = "ProfileValueAnnotationAwareTransactionalTests.profile_value.name"; private static final String VALUE = "enigma"; + private final Map expectedInvocationCounts = new HashMap(); + public ProfileValueJUnit38SpringContextTests() { System.setProperty(NAME, VALUE); } + @Override + protected void setUp() throws Exception { + this.expectedInvocationCounts.put(EMPTY, 0); + this.expectedInvocationCounts.put(DISABLED_VIA_WRONG_NAME, 0); + this.expectedInvocationCounts.put(DISABLED_VIA_WRONG_VALUE, 0); + this.expectedInvocationCounts.put(ENABLED_VIA_SINGLE_VALUE, 1); + this.expectedInvocationCounts.put(ENABLED_VIA_MULTIPLE_VALUES, 1); + this.expectedInvocationCounts.put(NOT_CONFIGURED, 1); + } + + private void configureDisabledClassExpectations() { + this.expectedInvocationCounts.put(ENABLED_VIA_SINGLE_VALUE, 0); + this.expectedInvocationCounts.put(ENABLED_VIA_MULTIPLE_VALUES, 0); + this.expectedInvocationCounts.put(NOT_CONFIGURED, 0); + } private void runTestAndAssertCounters(Class testCaseType, - String testName, int expectedInvocationCount, int expectedErrorCount, - int expectedFailureCount) throws Exception { + String testName, int expectedInvocationCount, int expectedErrorCount, int expectedFailureCount) + throws Exception { DefaultProfileValueSourceTestCase testCase = testCaseType.newInstance(); testCase.setName(testName); TestResult testResult = testCase.run(); assertEquals("Verifying number of invocations for test method [" + testName + "].", expectedInvocationCount, - testCase.invocationCount); + testCase.invocationCount); assertEquals("Verifying number of errors for test method [" + testName + "].", expectedErrorCount, - testResult.errorCount()); + testResult.errorCount()); assertEquals("Verifying number of failures for test method [" + testName + "].", expectedFailureCount, - testResult.failureCount()); + testResult.failureCount()); } private void runTests(final Class testCaseType) throws Exception { - runTestAndAssertCounters(testCaseType, "testIfProfileValueEmpty", 0, 0, 0); - runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongName", 0, 0, 0); - runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongValue", 0, 0, 0); - runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaSingleValue", 1, 0, 0); - runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaMultipleValues", 1, 0, 0); - runTestAndAssertCounters(testCaseType, "testIfProfileValueNotConfigured", 1, 0, 0); + runTestAndAssertCounters(testCaseType, EMPTY, expectedInvocationCounts.get(EMPTY), 0, 0); + runTestAndAssertCounters(testCaseType, DISABLED_VIA_WRONG_NAME, + expectedInvocationCounts.get(DISABLED_VIA_WRONG_NAME), 0, 0); + runTestAndAssertCounters(testCaseType, DISABLED_VIA_WRONG_VALUE, + expectedInvocationCounts.get(DISABLED_VIA_WRONG_VALUE), 0, 0); + runTestAndAssertCounters(testCaseType, ENABLED_VIA_SINGLE_VALUE, + expectedInvocationCounts.get(ENABLED_VIA_SINGLE_VALUE), 0, 0); + runTestAndAssertCounters(testCaseType, ENABLED_VIA_MULTIPLE_VALUES, + expectedInvocationCounts.get(ENABLED_VIA_MULTIPLE_VALUES), 0, 0); + runTestAndAssertCounters(testCaseType, NOT_CONFIGURED, expectedInvocationCounts.get(NOT_CONFIGURED), 0, 0); } public void testDefaultProfileValueSource() throws Exception { assertEquals("Verifying the type of the configured ProfileValueSource.", SystemProfileValueSource.class, - new DefaultProfileValueSourceTestCase().getProfileValueSource().getClass()); + new DefaultProfileValueSourceTestCase().getProfileValueSource().getClass()); runTests(DefaultProfileValueSourceTestCase.class); } public void testHardCodedProfileValueSource() throws Exception { assertEquals("Verifying the type of the configured ProfileValueSource.", HardCodedProfileValueSource.class, - new HardCodedProfileValueSourceTestCase().getProfileValueSource().getClass()); + new HardCodedProfileValueSourceTestCase().getProfileValueSource().getClass()); runTests(HardCodedProfileValueSourceTestCase.class); } + public void testClassLevelIfProfileValueEnabledSingleValue() throws Exception { + runTests(ClassLevelIfProfileValueEnabledSingleValueTestCase.class); + } + + public void testClassLevelIfProfileValueDisabledSingleValue() throws Exception { + configureDisabledClassExpectations(); + runTests(ClassLevelIfProfileValueDisabledSingleValueTestCase.class); + } + + public void testClassLevelIfProfileValueEnabledMultiValue() throws Exception { + runTests(ClassLevelIfProfileValueEnabledMultiValueTestCase.class); + } + + public void testClassLevelIfProfileValueDisabledMultiValue() throws Exception { + configureDisabledClassExpectations(); + runTests(ClassLevelIfProfileValueDisabledMultiValueTestCase.class); + } + + + // ------------------------------------------------------------------- /** * Note that {@link TestExecutionListeners @TestExecutionListeners} is @@ -92,6 +142,7 @@ public class ProfileValueJUnit38SpringContextTests extends TestCase { int invocationCount = 0; + public ProfileValueSource getProfileValueSource() { return super.profileValueSource; } @@ -129,13 +180,10 @@ public class ProfileValueJUnit38SpringContextTests extends TestCase { } } - @ProfileValueSourceConfiguration(HardCodedProfileValueSource.class) public static class HardCodedProfileValueSourceTestCase extends DefaultProfileValueSourceTestCase { - } - public static class HardCodedProfileValueSource implements ProfileValueSource { public String get(final String key) { @@ -143,4 +191,20 @@ public class ProfileValueJUnit38SpringContextTests extends TestCase { } } + @IfProfileValue(name = NAME, value = VALUE) + public static class ClassLevelIfProfileValueEnabledSingleValueTestCase extends DefaultProfileValueSourceTestCase { + } + + @IfProfileValue(name = NAME, value = VALUE + "X") + public static class ClassLevelIfProfileValueDisabledSingleValueTestCase extends DefaultProfileValueSourceTestCase { + } + + @IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" }) + public static class ClassLevelIfProfileValueEnabledMultiValueTestCase extends DefaultProfileValueSourceTestCase { + } + + @IfProfileValue(name = NAME, values = { "foo", "bar", "baz" }) + public static class ClassLevelIfProfileValueDisabledMultiValueTestCase extends DefaultProfileValueSourceTestCase { + } + } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/EnabledAndIgnoredSpringRunnerTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/EnabledAndIgnoredSpringRunnerTests.java index dddb935b8b7..b9c6c811a30 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/EnabledAndIgnoredSpringRunnerTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/EnabledAndIgnoredSpringRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 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. @@ -16,37 +16,36 @@ package org.springframework.test.context.junit4; -import org.junit.AfterClass; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; + +import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.test.annotation.IfProfileValue; import org.springframework.test.annotation.ProfileValueSource; import org.springframework.test.annotation.ProfileValueSourceConfiguration; import org.springframework.test.context.TestExecutionListeners; /** - * Verifies proper handling of JUnit's {@link org.junit.Ignore @Ignore} and - * Spring's - * {@link org.springframework.test.annotation.IfProfileValue @IfProfileValue} - * and {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} - * (with the implicit, default {@link ProfileValueSource}) - * annotations in conjunction with the {@link SpringJUnit4ClassRunner}. - * - *

Note that {@link TestExecutionListeners @TestExecutionListeners} is + * Verifies proper handling of JUnit's {@link Ignore @Ignore} and Spring's + * {@link IfProfileValue @IfProfileValue} and + * {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} + * (with the implicit, default {@link ProfileValueSource}) annotations in + * conjunction with the {@link SpringJUnit4ClassRunner}. + *

+ * Note that {@link TestExecutionListeners @TestExecutionListeners} is * explicitly configured with an empty list, thus disabling all default * listeners. - * + * * @author Sam Brannen * @since 2.5 * @see HardCodedProfileValueSourceSpringRunnerTests */ @RunWith(SpringJUnit4ClassRunner.class) -@TestExecutionListeners({}) +@TestExecutionListeners( {}) public class EnabledAndIgnoredSpringRunnerTests { protected static final String NAME = "EnabledAndIgnoredSpringRunnerTests.profile_value.name"; diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/HardCodedProfileValueSourceSpringRunnerTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/HardCodedProfileValueSourceSpringRunnerTests.java index f6ecb2aa282..62118b48df6 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/HardCodedProfileValueSourceSpringRunnerTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/HardCodedProfileValueSourceSpringRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 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. @@ -17,20 +17,19 @@ package org.springframework.test.context.junit4; import org.junit.BeforeClass; - import org.springframework.test.annotation.ProfileValueSource; import org.springframework.test.annotation.ProfileValueSourceConfiguration; /** *

- * Verifies proper handling of JUnit's {@link org.junit.Ignore @Ignore} and - * Spring's - * {@link org.springframework.test.annotation.IfProfileValue @IfProfileValue} - * and {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} - * (with an explicit, custom defined {@link ProfileValueSource}) - * annotations in conjunction with the {@link SpringJUnit4ClassRunner}. + * Verifies proper handling of JUnit's {@link org.junit.Ignore @Ignore} and + * Spring's {@link org.springframework.test.annotation.IfProfileValue + * @IfProfileValue} and {@link ProfileValueSourceConfiguration + * @ProfileValueSourceConfiguration} (with an + * explicit, custom defined {@link ProfileValueSource}) annotations in + * conjunction with the {@link SpringJUnit4ClassRunner}. *

- * + * * @author Sam Brannen * @since 2.5 * @see EnabledAndIgnoredSpringRunnerTests