23
23
import java .lang .annotation .RetentionPolicy ;
24
24
import java .lang .annotation .Target ;
25
25
26
+ import org .apache .commons .logging .Log ;
27
+ import org .apache .commons .logging .LogFactory ;
28
+
29
+ import org .springframework .lang .Nullable ;
30
+
26
31
/**
27
32
* {@code @NestedTestConfiguration} is a type-level annotation that is used to
28
33
* configure how Spring test configuration annotations are processed within
29
34
* enclosing class hierarchies (i.e., for <em>inner</em> test classes).
30
35
*
31
36
* <p>If {@code @NestedTestConfiguration} is not <em>present</em> or
37
+ * <em>meta-present</em> on a test class, in its super type hierarchy, or in its
38
+ * enclosing class hierarchy, the default <em>enclosing configuration inheritance
39
+ * mode</em> will be used. See {@link #ENCLOSING_CONFIGURATION_PROPERTY_NAME} for
40
+ * details on how to change the default mode.
41
+ *
42
+ * <p>By default, if {@code @NestedTestConfiguration} is not <em>present</em> or
32
43
* <em>meta-present</em> on a test class, configuration from the test class will
33
44
* propagate to inner test classes (see {@link EnclosingConfiguration#INHERIT}).
34
45
* If {@code @NestedTestConfiguration(OVERRIDE)} is used to switch the mode,
35
46
* inner test classes will have to declare their own Spring test configuration
36
47
* annotations. If you wish to explicitly configure the mode, annotate either
37
- * the inner test class or the enclosing class with
48
+ * the inner test class or an enclosing class with
38
49
* {@code @NestedTestConfiguration(...}. Note that a
39
50
* {@code @NestedTestConfiguration(...)} declaration is inherited within the
40
51
* superclass hierarchy as well as within the enclosing class hierarchy. Thus,
57
68
* @see ActiveProfiles @ActiveProfiles
58
69
* @see TestPropertySource @TestPropertySource
59
70
*/
60
- @ Target ({ ElementType .TYPE , ElementType . METHOD } )
71
+ @ Target (ElementType .TYPE )
61
72
@ Retention (RetentionPolicy .RUNTIME )
62
73
@ Documented
63
74
@ Inherited
64
75
public @interface NestedTestConfiguration {
65
76
77
+ /**
78
+ * JVM system property used to change the default <em>enclosing configuration
79
+ * inheritance mode</em>: {@value #ENCLOSING_CONFIGURATION_PROPERTY_NAME}.
80
+ * <p>Supported values include enum constants defined in
81
+ * {@link EnclosingConfiguration}, ignoring case. For example, the default
82
+ * may be changed to {@link EnclosingConfiguration#OVERRIDE} by supplying
83
+ * the following JVM system property via the command line.
84
+ * <pre style="code">-Dspring.test.enclosing.configuration=override</pre>
85
+ * <p>If the property is not set to {@code OVERRIDE}, test configuration for
86
+ * an inner test class will be <em>inherited</em> according to
87
+ * {@link EnclosingConfiguration#INHERIT} semantics by default.
88
+ * <p>May alternatively be configured via the
89
+ * {@link org.springframework.core.SpringProperties SpringProperties}
90
+ * mechanism.
91
+ * @see #value
92
+ */
93
+ String ENCLOSING_CONFIGURATION_PROPERTY_NAME = "spring.test.enclosing.configuration" ;
94
+
95
+
66
96
/**
67
97
* Configures the {@link EnclosingConfiguration} mode.
98
+ * @see EnclosingConfiguration#INHERIT
99
+ * @see EnclosingConfiguration#OVERRIDE
68
100
*/
69
101
EnclosingConfiguration value ();
70
102
71
103
72
104
/**
73
105
* Enumeration of <em>modes</em> that dictate how test configuration from
74
106
* enclosing classes is processed for inner test classes.
107
+ * @see #INHERIT
108
+ * @see #OVERRIDE
75
109
*/
76
110
enum EnclosingConfiguration {
77
111
@@ -87,7 +121,34 @@ enum EnclosingConfiguration {
87
121
* <em>override</em> configuration from its
88
122
* {@linkplain Class#getEnclosingClass() enclosing class}.
89
123
*/
90
- OVERRIDE
124
+ OVERRIDE ;
125
+
126
+
127
+ /**
128
+ * Get the {@code EnclosingConfiguration} enum constant with the supplied
129
+ * name, ignoring case.
130
+ * @param name the name of the enum constant to retrieve
131
+ * @return the corresponding enum constant or {@code null} if not found
132
+ * @see EnclosingConfiguration#valueOf(String)
133
+ */
134
+ @ Nullable
135
+ public static EnclosingConfiguration from (@ Nullable String name ) {
136
+ if (name == null ) {
137
+ return null ;
138
+ }
139
+ try {
140
+ return EnclosingConfiguration .valueOf (name .trim ().toUpperCase ());
141
+ }
142
+ catch (IllegalArgumentException ex ) {
143
+ Log logger = LogFactory .getLog (EnclosingConfiguration .class );
144
+ if (logger .isDebugEnabled ()) {
145
+ logger .debug (String .format (
146
+ "Failed to parse enclosing configuration mode from '%s': %s" ,
147
+ name , ex .getMessage ()));
148
+ }
149
+ return null ;
150
+ }
151
+ }
91
152
92
153
}
93
154
0 commit comments