[SPR-5914] ProfileValueUtils now properly ensures that class-level usage of @IfProfileValue overrides method-level usage.

This commit is contained in:
Sam Brannen 2009-07-10 11:45:31 +00:00
parent 0d98baa5c1
commit 696d78c144
6 changed files with 400 additions and 122 deletions

View File

@ -31,62 +31,64 @@ import java.lang.annotation.Target;
* test will be enabled.
* </p>
* <p>
* Note: <code>&#064;IfProfileValue</code> can be applied at either the
* class or method level.
* Note: <code>&#064;IfProfileValue</code> can be applied at the class level,
* the method level, or both. <code>&#064;IfProfileValue</code> at the class
* level overrides method-level usage of <code>&#064;IfProfileValue</code> for
* any methods within that class.
* </p>
* <p>
* 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:
* </p>
*
*
* <pre class="code">
* &#064;IfProfileValue(name=&quot;java.vendor&quot;, value=&quot;Sun Microsystems Inc.&quot;)
* &#064;IfProfileValue(name = &quot;java.vendor&quot;, value = &quot;Sun Microsystems Inc.&quot;)
* public void testSomething() {
* // ...
* // ...
* }
* </pre>
*
* <p>
* You can alternatively configure <code>&#064;IfProfileValue</code> with
* <em>OR</em> semantics for multiple {@link #values() values} as follows
* (assuming a {@link ProfileValueSource} has been appropriately configured for
* the &quot;test-groups&quot; name):
* </p>
*
*
* <pre class="code">
* &#064;IfProfileValue(name=&quot;test-groups&quot;, values={&quot;unit-tests&quot;, &quot;integration-tests&quot;})
* &#064;IfProfileValue(name = &quot;test-groups&quot;, values = { &quot;unit-tests&quot;, &quot;integration-tests&quot; })
* public void testWhichRunsForUnitOrIntegrationTestGroups() {
* // ...
* // ...
* }
* </pre>
*
*
* @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 <code>name</code> of the <em>profile value</em> against which to test.
* The <code>name</code> of the <em>profile value</em> against which to
* test.
*/
String name();
/**
* A single, permissible <code>value</code> of the <em>profile value</em>
* for the given {@link #name() name}.
* <p>Note: Assigning values to both {@link #value()} and {@link #values()}
* <p>
* 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 <code>values</code> of the
* <em>profile value</em> for the given {@link #name() name}.
* <p>Note: Assigning values to both {@link #value()} and {@link #values()}
* <p>
* Note: Assigning values to both {@link #value()} and {@link #values()}
* will lead to a configuration conflict.
*/
String[] values() default {};

View File

@ -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 <em>profile values</em>.
*
*
* @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
* &#064;ProfileValueSourceConfiguration} annotation and instantiates a new
* instance of that type.
* <p>
* 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
* &#064;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 <code>testClass</code> is <em>enabled</em>
* in the current environment, as specified by the
* {@link IfProfileValue @IfProfileValue} annotation at the class level.
* Determine if the supplied <code>testClass</code> is <em>enabled</em> in
* the current environment, as specified by the {@link IfProfileValue
* &#064;IfProfileValue} annotation at the class level.
* <p>
* Defaults to <code>true</code> if no
* {@link IfProfileValue @IfProfileValue} annotation is declared.
*
* Defaults to <code>true</code> if no {@link IfProfileValue
* &#064;IfProfileValue} annotation is declared.
*
* @param testClass the test class
* @return <code>true</code> if the test is <em>enabled</em> in the
* current environment
* @return <code>true</code> if the test is <em>enabled</em> 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 <code>testMethod</code> is <em>enabled</em>
* 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 <code>testMethod</code> is <em>enabled</em> in
* the current environment, as specified by the {@link IfProfileValue
* &#064;IfProfileValue} annotation, which may be declared on the test
* method itself or at the class level. Class-level usage overrides
* method-level usage.
* <p>
* Defaults to <code>true</code> if no
* {@link IfProfileValue @IfProfileValue} annotation is declared.
*
* Defaults to <code>true</code> if no {@link IfProfileValue
* &#064;IfProfileValue} annotation is declared.
*
* @param testMethod the test method
* @param testClass the test class
* @return <code>true</code> if the test is <em>enabled</em> in the
* current environment
* @return <code>true</code> if the test is <em>enabled</em> 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 <code>testMethod</code> is <em>enabled</em>
* 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 <code>testMethod</code> is <em>enabled</em> in
* the current environment, as specified by the {@link IfProfileValue
* &#064;IfProfileValue} annotation, which may be declared on the test
* method itself or at the class level. Class-level usage overrides
* method-level usage.
* <p>
* Defaults to <code>true</code> if no
* {@link IfProfileValue @IfProfileValue} annotation is declared.
*
* Defaults to <code>true</code> if no {@link IfProfileValue
* &#064;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 <code>true</code> if the test is <em>enabled</em> in the
* current environment
* @return <code>true</code> if the test is <em>enabled</em> 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 <code>value</code> (or one of the <code>values</code>)
* in the supplied {@link IfProfileValue @IfProfileValue} annotation is
* in the supplied {@link IfProfileValue &#064;IfProfileValue} annotation is
* <em>enabled</em> in the current environment.
*
*
* @param profileValueSource the ProfileValueSource to use to determine if
* the test is enabled
* @param ifProfileValue the annotation to introspect
* @return <code>true</code> if the test is <em>enabled</em> in the
* current environment
* @param ifProfileValue the annotation to introspect; may be
* <code>null</code>
* @return <code>true</code> if the test is <em>enabled</em> in the current
* environment or if the supplied <code>ifProfileValue</code> is
* <code>null</code>
*/
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())) {

View File

@ -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() {
}
}
}

View File

@ -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 &#064;IfProfileValue} and
* {@link ProfileValueSourceConfiguration &#064;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<String, Integer> expectedInvocationCounts = new HashMap<String, Integer>();
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<? extends DefaultProfileValueSourceTestCase> 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<? extends DefaultProfileValueSourceTestCase> 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 {
}
}

View File

@ -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 <em>implicit, default {@link ProfileValueSource}</em>)
* annotations in conjunction with the {@link SpringJUnit4ClassRunner}.
*
* <p>Note that {@link TestExecutionListeners @TestExecutionListeners} is
* Verifies proper handling of JUnit's {@link Ignore &#064;Ignore} and Spring's
* {@link IfProfileValue &#064;IfProfileValue} and
* {@link ProfileValueSourceConfiguration &#064;ProfileValueSourceConfiguration}
* (with the <em>implicit, default {@link ProfileValueSource}</em>) annotations in
* conjunction with the {@link SpringJUnit4ClassRunner}.
* <p>
* Note that {@link TestExecutionListeners &#064;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";

View File

@ -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;
/**
* <p>
* 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 <em>explicit, custom defined {@link ProfileValueSource}</em>)
* annotations in conjunction with the {@link SpringJUnit4ClassRunner}.
* Verifies proper handling of JUnit's {@link org.junit.Ignore &#064;Ignore} and
* Spring's {@link org.springframework.test.annotation.IfProfileValue
* &#064;IfProfileValue} and {@link ProfileValueSourceConfiguration
* &#064;ProfileValueSourceConfiguration} (with an
* <em>explicit, custom defined {@link ProfileValueSource}</em>) annotations in
* conjunction with the {@link SpringJUnit4ClassRunner}.
* </p>
*
*
* @author Sam Brannen
* @since 2.5
* @see EnabledAndIgnoredSpringRunnerTests