Skip to content

Commit 3d95175

Browse files
committed
Improve documentation for @IfProfileValue precedence
Issue: SPR-11902
1 parent cd9b390 commit 3d95175

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

spring-test/src/main/java/org/springframework/test/annotation/IfProfileValue.java

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,83 +24,102 @@
2424
import java.lang.annotation.Target;
2525

2626
/**
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:
3951
*
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>
4652
* <pre class="code">
47-
* &#064;IfProfileValue(name = &quot;java.vendor&quot;, value = &quot;Sun Microsystems Inc.&quot;)
53+
* &#064;IfProfileValue(name = &quot;java.vendor&quot;, value = &quot;Oracle Corporation&quot;)
4854
* 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-
* &quot;test-groups&quot; 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>.
5866
*
5967
* <pre class="code">
6068
* &#064;IfProfileValue(name = &quot;test-groups&quot;, values = { &quot;unit-tests&quot;, &quot;integration-tests&quot; })
6169
* 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.
6580
*
81+
* <h3>Meta-annotation Support</h3>
6682
* <p>As of Spring Framework 4.0, this annotation may be used as a
6783
* <em>meta-annotation</em> to create custom <em>composed annotations</em>.
6884
*
6985
* @author Rod Johnson
7086
* @author Sam Brannen
7187
* @since 2.0
7288
* @see ProfileValueSource
89+
* @see SystemProfileValueSource
7390
* @see ProfileValueSourceConfiguration
7491
* @see ProfileValueUtils
7592
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
7693
* @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
7797
*/
7898
@Documented
7999
@Inherited
80100
@Retention(RetentionPolicy.RUNTIME)
81-
@Target( { ElementType.TYPE, ElementType.METHOD })
101+
@Target({ ElementType.TYPE, ElementType.METHOD })
82102
public @interface IfProfileValue {
83103

84104
/**
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.
87106
*/
88107
String name();
89108

90109
/**
91110
* 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}
95114
* will lead to a configuration conflict.
96115
*/
97116
String value() default "";
98117

99118
/**
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}
104123
* will lead to a configuration conflict.
105124
*/
106125
String[] values() default {};

src/asciidoc/testing.adoc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,15 @@ The following annotations are __only__ supported when used in conjunction with t
991991

992992
Indicates that the annotated test is enabled for a specific testing environment. If the
993993
configured `ProfileValueSource` returns a matching `value` for the provided `name`, the
994-
test is enabled. This annotation can be applied to an entire class or to individual
995-
methods. Class-level usage overrides method-level usage.
994+
test is enabled. Otherwise, the test will be disabled and effectively _ignored_.
995+
996+
`@IfProfileValue` can be applied at the class level, the method level, or both.
997+
Class-level usage of `@IfProfileValue` takes precedence over method-level usage for any
998+
methods within that class or its subclasses. Specifically, a test is enabled if it is
999+
enabled both at the class level _and_ at the method level; the absence of
1000+
`@IfProfileValue` means the test is implicitly enabled. This is analogous to the
1001+
semantics of JUnit's `@Ignore` annotation, except that the presence of `@Ignore` always
1002+
disables a test.
9961003

9971004
+
9981005

0 commit comments

Comments
 (0)