1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 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.
34
34
/**
35
35
* {@code MergedContextConfiguration} encapsulates the <em>merged</em>
36
36
* context configuration declared on a test class and all of its superclasses
37
- * via {@link ContextConfiguration @ContextConfiguration} and
38
- * {@link ActiveProfiles @ActiveProfiles}.
37
+ * via {@link ContextConfiguration @ContextConfiguration},
38
+ * {@link ActiveProfiles @ActiveProfiles}, and
39
+ * {@link TestPropertySource @TestPropertySource}.
39
40
*
40
- * <p>Merged resource locations, annotated classes, and active profiles
41
- * represent all declared values in the test class hierarchy taking into
42
- * consideration the semantics of the
43
- * {@link ContextConfiguration#inheritLocations inheritLocations} and
44
- * {@link ActiveProfiles#inheritProfiles inheritProfiles} flags in
45
- * {@code @ContextConfiguration} and {@code @ActiveProfiles}, respectively.
41
+ * <p>Merged context resource locations, annotated classes, active profiles,
42
+ * property resource locations, and in-lined properties represent all declared
43
+ * values in the test class hierarchy taking into consideration the semantics
44
+ * of the {@link ContextConfiguration#inheritLocations},
45
+ * {@link ActiveProfiles#inheritProfiles},
46
+ * {@link TestPropertySource#inheritLocations}, and
47
+ * {@link TestPropertySource#inheritProperties} flags.
46
48
*
47
49
* <p>A {@link SmartContextLoader} uses {@code MergedContextConfiguration}
48
50
* to load an {@link org.springframework.context.ApplicationContext ApplicationContext}.
@@ -73,13 +75,15 @@ public class MergedContextConfiguration implements Serializable {
73
75
private final Class <?>[] classes ;
74
76
private final Set <Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>> contextInitializerClasses ;
75
77
private final String [] activeProfiles ;
78
+ private final String [] propertySourceLocations ;
79
+ private final String [] propertySourceProperties ;
76
80
private final ContextLoader contextLoader ;
77
81
private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate ;
78
82
private final MergedContextConfiguration parent ;
79
83
80
84
81
- private static String [] processLocations (String [] locations ) {
82
- return locations == null ? EMPTY_STRING_ARRAY : locations ;
85
+ private static String [] processStrings (String [] array ) {
86
+ return array == null ? EMPTY_STRING_ARRAY : array ;
83
87
}
84
88
85
89
private static Class <?>[] processClasses (Class <?>[] classes ) {
@@ -115,20 +119,15 @@ protected static String nullSafeToString(ContextLoader contextLoader) {
115
119
116
120
/**
117
121
* Create a new {@code MergedContextConfiguration} instance for the
118
- * supplied test class, resource locations, annotated classes, active
119
- * profiles, and {@code ContextLoader}.
120
- *
121
- * <p>If a {@code null} value is supplied for {@code locations},
122
- * {@code classes}, or {@code activeProfiles} an empty array will
123
- * be stored instead. Furthermore, active profiles will be sorted, and duplicate
124
- * profiles will be removed.
122
+ * supplied parameters.
123
+ * <p>Delegates to
124
+ * {@link #MergedContextConfiguration(Class, String[], Class[], Set, String[], String[], String[], ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}.
125
125
*
126
126
* @param testClass the test class for which the configuration was merged
127
- * @param locations the merged resource locations
127
+ * @param locations the merged context resource locations
128
128
* @param classes the merged annotated classes
129
129
* @param activeProfiles the merged active bean definition profiles
130
130
* @param contextLoader the resolved {@code ContextLoader}
131
- * @see #MergedContextConfiguration(Class, String[], Class[], Set, String[], ContextLoader)
132
131
*/
133
132
public MergedContextConfiguration (Class <?> testClass , String [] locations , Class <?>[] classes ,
134
133
String [] activeProfiles , ContextLoader contextLoader ) {
@@ -137,18 +136,12 @@ public MergedContextConfiguration(Class<?> testClass, String[] locations, Class<
137
136
138
137
/**
139
138
* Create a new {@code MergedContextConfiguration} instance for the
140
- * supplied test class, resource locations, annotated classes, context
141
- * initializers, active profiles, and {@code ContextLoader}.
142
- *
143
- * <p>If a {@code null} value is supplied for {@code locations},
144
- * {@code classes}, or {@code activeProfiles} an empty array will
145
- * be stored instead. If a {@code null} value is supplied for the
146
- * {@code contextInitializerClasses} an empty set will be stored instead.
147
- * Furthermore, active profiles will be sorted, and duplicate profiles will
148
- * be removed.
139
+ * supplied parameters.
140
+ * <p>Delegates to
141
+ * {@link #MergedContextConfiguration(Class, String[], Class[], Set, String[], String[], String[], ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}.
149
142
*
150
143
* @param testClass the test class for which the configuration was merged
151
- * @param locations the merged resource locations
144
+ * @param locations the merged context resource locations
152
145
* @param classes the merged annotated classes
153
146
* @param contextInitializerClasses the merged context initializer classes
154
147
* @param activeProfiles the merged active bean definition profiles
@@ -166,19 +159,12 @@ public MergedContextConfiguration(
166
159
167
160
/**
168
161
* Create a new {@code MergedContextConfiguration} instance for the
169
- * supplied test class, resource locations, annotated classes, context
170
- * initializers, active profiles, {@code ContextLoader}, and parent
171
- * configuration.
172
- *
173
- * <p>If a {@code null} value is supplied for {@code locations},
174
- * {@code classes}, or {@code activeProfiles} an empty array will
175
- * be stored instead. If a {@code null} value is supplied for the
176
- * {@code contextInitializerClasses} an empty set will be stored instead.
177
- * Furthermore, active profiles will be sorted, and duplicate profiles will
178
- * be removed.
162
+ * supplied parameters.
163
+ * <p>Delegates to
164
+ * {@link #MergedContextConfiguration(Class, String[], Class[], Set, String[], String[], String[], ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}.
179
165
*
180
166
* @param testClass the test class for which the configuration was merged
181
- * @param locations the merged resource locations
167
+ * @param locations the merged context resource locations
182
168
* @param classes the merged annotated classes
183
169
* @param contextInitializerClasses the merged context initializer classes
184
170
* @param activeProfiles the merged active bean definition profiles
@@ -195,11 +181,50 @@ public MergedContextConfiguration(
195
181
Set <Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>> contextInitializerClasses ,
196
182
String [] activeProfiles , ContextLoader contextLoader ,
197
183
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate , MergedContextConfiguration parent ) {
184
+ this (testClass , locations , classes , contextInitializerClasses , activeProfiles , null , null , contextLoader ,
185
+ cacheAwareContextLoaderDelegate , parent );
186
+ }
187
+
188
+ /**
189
+ * Create a new {@code MergedContextConfiguration} instance for the
190
+ * supplied parameters.
191
+ *
192
+ * <p>If a {@code null} value is supplied for {@code locations},
193
+ * {@code classes}, {@code activeProfiles}, {@code propertySourceLocations},
194
+ * or {@code propertySourceProperties} an empty array will be stored instead.
195
+ * If a {@code null} value is supplied for the
196
+ * {@code contextInitializerClasses} an empty set will be stored instead.
197
+ * Furthermore, active profiles will be sorted, and duplicate profiles
198
+ * will be removed.
199
+ *
200
+ * @param testClass the test class for which the configuration was merged
201
+ * @param locations the merged context resource locations
202
+ * @param classes the merged annotated classes
203
+ * @param contextInitializerClasses the merged context initializer classes
204
+ * @param activeProfiles the merged active bean definition profiles
205
+ * @param propertySourceLocations the merged {@code PropertySource} locations
206
+ * @param propertySourceProperties the merged {@code PropertySource} properties
207
+ * @param contextLoader the resolved {@code ContextLoader}
208
+ * @param cacheAwareContextLoaderDelegate a cache-aware context loader
209
+ * delegate with which to retrieve the parent context
210
+ * @param parent the parent configuration or {@code null} if there is no parent
211
+ * @since 4.1
212
+ */
213
+ public MergedContextConfiguration (
214
+ Class <?> testClass ,
215
+ String [] locations ,
216
+ Class <?>[] classes ,
217
+ Set <Class <? extends ApplicationContextInitializer <? extends ConfigurableApplicationContext >>> contextInitializerClasses ,
218
+ String [] activeProfiles , String [] propertySourceLocations , String [] propertySourceProperties ,
219
+ ContextLoader contextLoader , CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate ,
220
+ MergedContextConfiguration parent ) {
198
221
this .testClass = testClass ;
199
- this .locations = processLocations (locations );
222
+ this .locations = processStrings (locations );
200
223
this .classes = processClasses (classes );
201
224
this .contextInitializerClasses = processContextInitializerClasses (contextInitializerClasses );
202
225
this .activeProfiles = processActiveProfiles (activeProfiles );
226
+ this .propertySourceLocations = processStrings (propertySourceLocations );
227
+ this .propertySourceProperties = processStrings (propertySourceProperties );
203
228
this .contextLoader = contextLoader ;
204
229
this .cacheAwareContextLoaderDelegate = cacheAwareContextLoaderDelegate ;
205
230
this .parent = parent ;
@@ -213,7 +238,10 @@ public Class<?> getTestClass() {
213
238
}
214
239
215
240
/**
216
- * Get the merged resource locations for the {@linkplain #getTestClass() test class}.
241
+ * Get the merged resource locations for {@code ApplicationContext}
242
+ * configuration files for the {@linkplain #getTestClass() test class}.
243
+ * <p>Context resource locations typically represent XML configuration
244
+ * files or Groovy scripts.
217
245
*/
218
246
public String [] getLocations () {
219
247
return locations ;
@@ -228,7 +256,7 @@ public Class<?>[] getClasses() {
228
256
229
257
/**
230
258
* Determine if this {@code MergedContextConfiguration} instance has
231
- * path-based resource locations.
259
+ * path-based context resource locations.
232
260
*
233
261
* @return {@code true} if the {@link #getLocations() locations} array is not empty
234
262
* @since 4.0.4
@@ -254,7 +282,7 @@ public boolean hasClasses() {
254
282
255
283
/**
256
284
* Determine if this {@code MergedContextConfiguration} instance has
257
- * either path-based resource locations or class-based resources.
285
+ * either path-based context resource locations or class-based resources.
258
286
*
259
287
* @return {@code true} if either the {@link #getLocations() locations}
260
288
* or the {@link #getClasses() classes} array is not empty
@@ -275,12 +303,36 @@ public Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableA
275
303
}
276
304
277
305
/**
278
- * Get the merged active bean definition profiles for the {@linkplain #getTestClass() test class}.
306
+ * Get the merged active bean definition profiles for the
307
+ * {@linkplain #getTestClass() test class}.
308
+ * @see ActiveProfiles
279
309
*/
280
310
public String [] getActiveProfiles () {
281
311
return activeProfiles ;
282
312
}
283
313
314
+ /**
315
+ * Get the merged resource locations for test {@code PropertySources} for the
316
+ * {@linkplain #getTestClass() test class}.
317
+ * @see TestPropertySource#locations
318
+ * @see java.util.Properties
319
+ */
320
+ public String [] getPropertySourceLocations () {
321
+ return propertySourceLocations ;
322
+ }
323
+
324
+ /**
325
+ * Get the merged test {@code PropertySource} properties for the
326
+ * {@linkplain #getTestClass() test class}.
327
+ * <p>Properties will be loaded into the {@code Environment}'s set of
328
+ * {@code PropertySources}.
329
+ * @see TestPropertySource#properties
330
+ * @see java.util.Properties
331
+ */
332
+ public String [] getPropertySourceProperties () {
333
+ return propertySourceProperties ;
334
+ }
335
+
284
336
/**
285
337
* Get the resolved {@link ContextLoader} for the {@linkplain #getTestClass() test class}.
286
338
*/
@@ -334,6 +386,8 @@ public int hashCode() {
334
386
result = prime * result + Arrays .hashCode (classes );
335
387
result = prime * result + contextInitializerClasses .hashCode ();
336
388
result = prime * result + Arrays .hashCode (activeProfiles );
389
+ result = prime * result + Arrays .hashCode (propertySourceLocations );
390
+ result = prime * result + Arrays .hashCode (propertySourceProperties );
337
391
result = prime * result + (parent == null ? 0 : parent .hashCode ());
338
392
result = prime * result + nullSafeToString (contextLoader ).hashCode ();
339
393
return result ;
@@ -345,6 +399,8 @@ public int hashCode() {
345
399
* {@linkplain #getClasses() annotated classes},
346
400
* {@linkplain #getContextInitializerClasses() context initializer classes},
347
401
* {@linkplain #getActiveProfiles() active profiles},
402
+ * {@linkplain #getPropertySourceLocations() property source locations},
403
+ * {@linkplain #getPropertySourceProperties() property source properties},
348
404
* {@linkplain #getParent() parents}, and the fully qualified names of their
349
405
* {@link #getContextLoader() ContextLoaders}.
350
406
*/
@@ -376,6 +432,14 @@ public boolean equals(Object obj) {
376
432
return false ;
377
433
}
378
434
435
+ if (!Arrays .equals (this .propertySourceLocations , that .propertySourceLocations )) {
436
+ return false ;
437
+ }
438
+
439
+ if (!Arrays .equals (this .propertySourceProperties , that .propertySourceProperties )) {
440
+ return false ;
441
+ }
442
+
379
443
if (this .parent == null ) {
380
444
if (that .parent != null ) {
381
445
return false ;
@@ -396,8 +460,10 @@ else if (!this.parent.equals(that.parent)) {
396
460
* Provide a String representation of the {@linkplain #getTestClass() test class},
397
461
* {@linkplain #getLocations() locations}, {@linkplain #getClasses() annotated classes},
398
462
* {@linkplain #getContextInitializerClasses() context initializer classes},
399
- * {@linkplain #getActiveProfiles() active profiles}, the name of the
400
- * {@link #getContextLoader() ContextLoader}, and the
463
+ * {@linkplain #getActiveProfiles() active profiles},
464
+ * {@linkplain #getPropertySourceLocations() property source locations},
465
+ * {@linkplain #getPropertySourceProperties() property source properties},
466
+ * the name of the {@link #getContextLoader() ContextLoader}, and the
401
467
* {@linkplain #getParent() parent configuration}.
402
468
*/
403
469
@ Override
@@ -408,6 +474,8 @@ public String toString() {
408
474
.append ("classes" , ObjectUtils .nullSafeToString (classes ))//
409
475
.append ("contextInitializerClasses" , ObjectUtils .nullSafeToString (contextInitializerClasses ))//
410
476
.append ("activeProfiles" , ObjectUtils .nullSafeToString (activeProfiles ))//
477
+ .append ("propertySourceLocations" , ObjectUtils .nullSafeToString (propertySourceLocations ))//
478
+ .append ("propertySourceProperties" , ObjectUtils .nullSafeToString (propertySourceProperties ))//
411
479
.append ("contextLoader" , nullSafeToString (contextLoader ))//
412
480
.append ("parent" , parent )//
413
481
.toString ();
0 commit comments