18
18
19
19
import org .apache .commons .logging .Log ;
20
20
import org .apache .commons .logging .LogFactory ;
21
+
22
+ import org .springframework .context .ApplicationContextInitializer ;
23
+ import org .springframework .context .ConfigurableApplicationContext ;
21
24
import org .springframework .core .style .ToStringCreator ;
22
25
import org .springframework .util .Assert ;
23
26
import org .springframework .util .ObjectUtils ;
24
27
25
28
/**
26
- * < code> ContextConfigurationAttributes</code> encapsulates the context
29
+ * {@ code ContextConfigurationAttributes} encapsulates the context
27
30
* configuration attributes declared on a test class via
28
31
* {@link ContextConfiguration @ContextConfiguration}.
29
32
*
@@ -47,6 +50,10 @@ public class ContextConfigurationAttributes {
47
50
48
51
private final Class <? extends ContextLoader > contextLoaderClass ;
49
52
53
+ private final Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>[] initializers ;
54
+
55
+ private final boolean inheritInitializers ;
56
+
50
57
51
58
/**
52
59
* Resolve resource locations from the {@link ContextConfiguration#locations() locations}
@@ -68,8 +75,7 @@ private static String[] resolveLocations(Class<?> declaringClass, ContextConfigu
68
75
ObjectUtils .nullSafeToString (valueLocations ), ObjectUtils .nullSafeToString (locations ));
69
76
logger .error (msg );
70
77
throw new IllegalStateException (msg );
71
- }
72
- else if (!ObjectUtils .isEmpty (valueLocations )) {
78
+ } else if (!ObjectUtils .isEmpty (valueLocations )) {
73
79
locations = valueLocations ;
74
80
}
75
81
@@ -79,31 +85,59 @@ else if (!ObjectUtils.isEmpty(valueLocations)) {
79
85
/**
80
86
* Construct a new {@link ContextConfigurationAttributes} instance for the
81
87
* supplied {@link ContextConfiguration @ContextConfiguration} annotation and
82
- * the {@link Class test class} that declared it.
88
+ * the {@linkplain Class test class} that declared it.
83
89
* @param declaringClass the test class that declared {@code @ContextConfiguration}
84
90
* @param contextConfiguration the annotation from which to retrieve the attributes
85
91
*/
86
92
public ContextConfigurationAttributes (Class <?> declaringClass , ContextConfiguration contextConfiguration ) {
87
93
this (declaringClass , resolveLocations (declaringClass , contextConfiguration ), contextConfiguration .classes (),
88
- contextConfiguration .inheritLocations (), contextConfiguration .loader ());
94
+ contextConfiguration .inheritLocations (), contextConfiguration .initializers (),
95
+ contextConfiguration .inheritInitializers (), contextConfiguration .loader ());
89
96
}
90
97
91
98
/**
92
99
* Construct a new {@link ContextConfigurationAttributes} instance for the
93
- * {@link Class test class} that declared the
100
+ * {@linkplain Class test class} that declared the
94
101
* {@link ContextConfiguration @ContextConfiguration} annotation and its
95
102
* corresponding attributes.
96
103
*
97
104
* @param declaringClass the test class that declared {@code @ContextConfiguration}
98
105
* @param locations the resource locations declared via {@code @ContextConfiguration}
99
106
* @param classes the annotated classes declared via {@code @ContextConfiguration}
100
- * @param inheritLocations the < code> inheritLocations</code> flag declared via {@code @ContextConfiguration}
107
+ * @param inheritLocations the {@ code inheritLocations} flag declared via {@code @ContextConfiguration}
101
108
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
102
109
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
103
- * <code>null</code>, or if the {@code locations} and {@code classes} are both non-empty
110
+ * {@code null}, or if the {@code locations} and {@code classes} are both non-empty
111
+ * @deprecated as of Spring 3.2, use
112
+ * {@link #ContextConfigurationAttributes(Class, String[], Class[], boolean, Class[], boolean, Class)}
113
+ * instead
104
114
*/
115
+ @ Deprecated
105
116
public ContextConfigurationAttributes (Class <?> declaringClass , String [] locations , Class <?>[] classes ,
106
117
boolean inheritLocations , Class <? extends ContextLoader > contextLoaderClass ) {
118
+ this (declaringClass , locations , classes , inheritLocations , null , true , contextLoaderClass );
119
+ }
120
+
121
+ /**
122
+ * Construct a new {@link ContextConfigurationAttributes} instance for the
123
+ * {@linkplain Class test class} that declared the
124
+ * {@link ContextConfiguration @ContextConfiguration} annotation and its
125
+ * corresponding attributes.
126
+ *
127
+ * @param declaringClass the test class that declared {@code @ContextConfiguration}
128
+ * @param locations the resource locations declared via {@code @ContextConfiguration}
129
+ * @param classes the annotated classes declared via {@code @ContextConfiguration}
130
+ * @param inheritLocations the {@code inheritLocations} flag declared via {@code @ContextConfiguration}
131
+ * @param initializers the context initializers declared via {@code @ContextConfiguration}
132
+ * @param inheritInitializers the {@code inheritInitializers} flag declared via {@code @ContextConfiguration}
133
+ * @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
134
+ * @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
135
+ * {@code null}, or if the {@code locations} and {@code classes} are both non-empty
136
+ */
137
+ public ContextConfigurationAttributes (Class <?> declaringClass , String [] locations , Class <?>[] classes ,
138
+ boolean inheritLocations ,
139
+ Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>[] initializers ,
140
+ boolean inheritInitializers , Class <? extends ContextLoader > contextLoaderClass ) {
107
141
108
142
Assert .notNull (declaringClass , "declaringClass must not be null" );
109
143
Assert .notNull (contextLoaderClass , "contextLoaderClass must not be null" );
@@ -122,14 +156,16 @@ public ContextConfigurationAttributes(Class<?> declaringClass, String[] location
122
156
this .locations = locations ;
123
157
this .classes = classes ;
124
158
this .inheritLocations = inheritLocations ;
159
+ this .initializers = initializers ;
160
+ this .inheritInitializers = inheritInitializers ;
125
161
this .contextLoaderClass = contextLoaderClass ;
126
162
}
127
163
128
164
/**
129
- * Get the {@link Class class} that declared the
165
+ * Get the {@linkplain Class class} that declared the
130
166
* {@link ContextConfiguration @ContextConfiguration} annotation.
131
167
*
132
- * @return the declaring class; never < code> null</code>
168
+ * @return the declaring class; never {@ code null}
133
169
*/
134
170
public Class <?> getDeclaringClass () {
135
171
return declaringClass ;
@@ -143,7 +179,7 @@ public Class<?> getDeclaringClass() {
143
179
* represent a <em>processed</em> value that does not match the original value
144
180
* declared via {@link ContextConfiguration @ContextConfiguration}.
145
181
*
146
- * @return the resource locations; potentially < code> null</code> or <em>empty</em>
182
+ * @return the resource locations; potentially {@ code null} or <em>empty</em>
147
183
* @see ContextConfiguration#value
148
184
* @see ContextConfiguration#locations
149
185
* @see #setLocations(String[])
@@ -170,7 +206,7 @@ public void setLocations(String[] locations) {
170
206
* represent a <em>processed</em> value that does not match the original value
171
207
* declared via {@link ContextConfiguration @ContextConfiguration}.
172
208
*
173
- * @return the annotated classes; potentially < code> null</code> or <em>empty</em>
209
+ * @return the annotated classes; potentially {@ code null} or <em>empty</em>
174
210
* @see ContextConfiguration#classes
175
211
* @see #setClasses(Class[])
176
212
*/
@@ -192,7 +228,7 @@ public void setClasses(Class<?>[] classes) {
192
228
* Determine if this {@code ContextConfigurationAttributes} instance has
193
229
* path-based resource locations.
194
230
*
195
- * @return < code> true</code> if the {@link #getLocations() locations} array is not empty
231
+ * @return {@ code true} if the {@link #getLocations() locations} array is not empty
196
232
* @see #hasResources()
197
233
* @see #hasClasses()
198
234
*/
@@ -204,7 +240,7 @@ public boolean hasLocations() {
204
240
* Determine if this {@code ContextConfigurationAttributes} instance has
205
241
* class-based resources.
206
242
*
207
- * @return < code> true</code> if the {@link #getClasses() classes} array is not empty
243
+ * @return {@ code true} if the {@link #getClasses() classes} array is not empty
208
244
* @see #hasResources()
209
245
* @see #hasLocations()
210
246
*/
@@ -216,7 +252,7 @@ public boolean hasClasses() {
216
252
* Determine if this {@code ContextConfigurationAttributes} instance has
217
253
* either path-based resource locations or class-based resources.
218
254
*
219
- * @return < code> true</code> if either the {@link #getLocations() locations}
255
+ * @return {@ code true} if either the {@link #getLocations() locations}
220
256
* or the {@link #getClasses() classes} array is not empty
221
257
* @see #hasLocations()
222
258
* @see #hasClasses()
@@ -226,21 +262,43 @@ public boolean hasResources() {
226
262
}
227
263
228
264
/**
229
- * Get the < code> inheritLocations</code> flag that was declared via
265
+ * Get the {@ code inheritLocations} flag that was declared via
230
266
* {@link ContextConfiguration @ContextConfiguration}.
231
267
*
232
- * @return the < code> inheritLocations</code> flag
268
+ * @return the {@ code inheritLocations} flag
233
269
* @see ContextConfiguration#inheritLocations
234
270
*/
235
271
public boolean isInheritLocations () {
236
272
return inheritLocations ;
237
273
}
238
274
239
275
/**
240
- * Get the <code>ContextLoader</code> class that was declared via
276
+ * Get the {@code ApplicationContextInitializer} classes that were declared via
277
+ * {@link ContextConfiguration @ContextConfiguration}.
278
+ *
279
+ * @return the {@code ApplicationContextInitializer} classes
280
+ * @since 3.2
281
+ */
282
+ public Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>[] getInitializers () {
283
+ return initializers ;
284
+ }
285
+
286
+ /**
287
+ * Get the {@code inheritInitializers} flag that was declared via
288
+ * {@link ContextConfiguration @ContextConfiguration}.
289
+ *
290
+ * @return the {@code inheritInitializers} flag
291
+ * @since 3.2
292
+ */
293
+ public boolean isInheritInitializers () {
294
+ return inheritInitializers ;
295
+ }
296
+
297
+ /**
298
+ * Get the {@code ContextLoader} class that was declared via
241
299
* {@link ContextConfiguration @ContextConfiguration}.
242
300
*
243
- * @return the < code> ContextLoader</code> class
301
+ * @return the {@ code ContextLoader} class
244
302
* @see ContextConfiguration#loader
245
303
*/
246
304
public Class <? extends ContextLoader > getContextLoaderClass () {
@@ -258,6 +316,8 @@ public String toString() {
258
316
.append ("locations" , ObjectUtils .nullSafeToString (locations ))//
259
317
.append ("classes" , ObjectUtils .nullSafeToString (classes ))//
260
318
.append ("inheritLocations" , inheritLocations )//
319
+ .append ("initializers" , ObjectUtils .nullSafeToString (initializers ))//
320
+ .append ("inheritInitializers" , inheritInitializers )//
261
321
.append ("contextLoaderClass" , contextLoaderClass .getName ())//
262
322
.toString ();
263
323
}
0 commit comments