|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import java.lang.annotation.Target;
|
25 | 25 |
|
26 | 26 | /**
|
27 |
| - * <p> |
28 |
| - * Test annotation to indicate that a test is enabled for a specific testing |
29 |
| - * profile or environment. If the configured {@link ProfileValueSource} returns |
30 |
| - * a matching {@link #value} for the provided {@link #name}, the test will be |
31 |
| - * enabled. |
32 |
| - * </p> |
33 |
| - * <p> |
34 |
| - * Note: {@code @IfProfileValue} can be applied at the class level, the method |
35 |
| - * level, or both. {@code @IfProfileValue} at the class level overrides |
36 |
| - * method-level usage of {@code @IfProfileValue} for any methods within that |
37 |
| - * class. |
38 |
| - * </p> |
| 27 | + * Test annotation to indicate whether a test is enabled or disabled for a |
| 28 | + * specific testing profile. |
| 29 | + * |
| 30 | + * <p>In the context of this annotation, the term <em>profile</em> refers to |
| 31 | + * a Java system property by default; however, the semantics can be changed |
| 32 | + * by implementing a custom {@link ProfileValueSource}. If the configured |
| 33 | + * {@code ProfileValueSource} returns a matching {@link #value} for the |
| 34 | + * declared {@link #name}, the test will be enabled. Otherwise, the test |
| 35 | + * will be disabled and effectively <em>ignored</em>. |
| 36 | + * |
| 37 | + * <p>{@code @IfProfileValue} can be applied at the class level, the method |
| 38 | + * level, or both. Class-level usage of {@code @IfProfileValue} takes |
| 39 | + * precedence over method-level usage for any methods within that class or |
| 40 | + * its subclasses. Specifically, a test is enabled if it is enabled both at |
| 41 | + * the class level <em>and</em> at the method level; the absence of |
| 42 | + * {@code @IfProfileValue} means the test is implicitly enabled. This is |
| 43 | + * analogous to the semantics of JUnit's {@link org.junit.Ignore @Ignore} |
| 44 | + * annotation, except that the presence of {@code @Ignore} always disables |
| 45 | + * a test. |
| 46 | + * |
| 47 | + * <h3>Example</h3> |
| 48 | + * When using {@link SystemProfileValueSource} as the {@code ProfileValueSource} |
| 49 | + * implementation (which is configured by default), you can configure a test |
| 50 | + * method to run only on Java VMs from Oracle as follows: |
39 | 51 | *
|
40 |
| - * <h3>Examples</h3> |
41 |
| - * <p> |
42 |
| - * When using {@link SystemProfileValueSource} as the {@link ProfileValueSource} |
43 |
| - * implementation, you can configure a test method to run only on Java VMs from |
44 |
| - * Sun Microsystems as follows: |
45 |
| - * </p> |
46 | 52 | * <pre class="code">
|
47 |
| - * @IfProfileValue(name = "java.vendor", value = "Sun Microsystems Inc.") |
| 53 | + * @IfProfileValue(name = "java.vendor", value = "Oracle Corporation") |
48 | 54 | * public void testSomething() {
|
49 |
| - * // ... |
50 |
| - * } |
51 |
| - * </pre> |
52 |
| - * <p> |
53 |
| - * You can alternatively configure {@code @IfProfileValue} with <em>OR</em> |
54 |
| - * semantics for multiple {@link #values() values} as follows (assuming a |
55 |
| - * {@link ProfileValueSource} has been appropriately configured for the |
56 |
| - * "test-groups" name): |
57 |
| - * </p> |
| 55 | + * // ... |
| 56 | + * }</pre> |
| 57 | + * |
| 58 | + * <h3>'OR' Semantics</h3> |
| 59 | + * <p>You can alternatively configure {@code @IfProfileValue} with <em>OR</em> |
| 60 | + * semantics for multiple {@link #values}. The following test will be enabled |
| 61 | + * if a {@code ProfileValueSource} has been appropriately configured for the |
| 62 | + * {@code "test-groups"} profile with a value of either {@code unit-tests} |
| 63 | + * <em>or</em> {@code integration-tests}. This functionality is similar to |
| 64 | + * TestNG's support for test <em>groups</em> and JUnit's experimental support |
| 65 | + * for test <em>categories</em>. |
58 | 66 | *
|
59 | 67 | * <pre class="code">
|
60 | 68 | * @IfProfileValue(name = "test-groups", values = { "unit-tests", "integration-tests" })
|
61 | 69 | * public void testWhichRunsForUnitOrIntegrationTestGroups() {
|
62 |
| - * // ... |
63 |
| - * } |
64 |
| - * </pre> |
| 70 | + * // ... |
| 71 | + * }</pre> |
| 72 | + * |
| 73 | + * <h3>{@code @IfProfileValue} vs. {@code @Profile}</h3> |
| 74 | + * <p>Although the {@code @IfProfileValue} and |
| 75 | + * {@link org.springframework.context.annotation.Profile @Profile} annotations |
| 76 | + * both involve <em>profiles</em>, they are not directly related. {@code @Profile} |
| 77 | + * involves bean definition profiles configured in the |
| 78 | + * {@link org.springframework.core.env.Environment Environment}; whereas, |
| 79 | + * {@code @IfProfileValue} is used to enable or disable tests. |
65 | 80 | *
|
| 81 | + * <h3>Meta-annotation Support</h3> |
66 | 82 | * <p>As of Spring Framework 4.0, this annotation may be used as a
|
67 | 83 | * <em>meta-annotation</em> to create custom <em>composed annotations</em>.
|
68 | 84 | *
|
69 | 85 | * @author Rod Johnson
|
70 | 86 | * @author Sam Brannen
|
71 | 87 | * @since 2.0
|
72 | 88 | * @see ProfileValueSource
|
| 89 | + * @see SystemProfileValueSource |
73 | 90 | * @see ProfileValueSourceConfiguration
|
74 | 91 | * @see ProfileValueUtils
|
75 | 92 | * @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
|
76 | 93 | * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
| 94 | + * @see org.springframework.test.context.junit4.statements.ProfileValueChecker |
| 95 | + * @see org.springframework.context.annotation.Profile |
| 96 | + * @see org.springframework.test.context.ActiveProfiles |
77 | 97 | */
|
78 | 98 | @Documented
|
79 | 99 | @Inherited
|
80 | 100 | @Retention(RetentionPolicy.RUNTIME)
|
81 |
| -@Target( { ElementType.TYPE, ElementType.METHOD }) |
| 101 | +@Target({ ElementType.TYPE, ElementType.METHOD }) |
82 | 102 | public @interface IfProfileValue {
|
83 | 103 |
|
84 | 104 | /**
|
85 |
| - * The {@code name} of the <em>profile value</em> against which to |
86 |
| - * test. |
| 105 | + * The {@code name} of the <em>profile value</em> against which to test. |
87 | 106 | */
|
88 | 107 | String name();
|
89 | 108 |
|
90 | 109 | /**
|
91 | 110 | * A single, permissible {@code value} of the <em>profile value</em>
|
92 |
| - * for the given {@link #name() name}. |
93 |
| - * <p> |
94 |
| - * Note: Assigning values to both {@link #value()} and {@link #values()} |
| 111 | + * for the given {@link #name}. |
| 112 | + * |
| 113 | + * <p>Note: Assigning values to both {@link #value} and {@link #values} |
95 | 114 | * will lead to a configuration conflict.
|
96 | 115 | */
|
97 | 116 | String value() default "";
|
98 | 117 |
|
99 | 118 | /**
|
100 |
| - * A list of all permissible {@code values} of the |
101 |
| - * <em>profile value</em> for the given {@link #name() name}. |
102 |
| - * <p> |
103 |
| - * Note: Assigning values to both {@link #value()} and {@link #values()} |
| 119 | + * A list of all permissible {@code values} of the <em>profile value</em> |
| 120 | + * for the given {@link #name}. |
| 121 | + * |
| 122 | + * <p>Note: Assigning values to both {@link #value} and {@link #values} |
104 | 123 | * will lead to a configuration conflict.
|
105 | 124 | */
|
106 | 125 | String[] values() default {};
|
|
0 commit comments