1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
27
27
28
28
import org .apache .commons .logging .Log ;
29
29
import org .apache .commons .logging .LogFactory ;
30
-
31
30
import org .springframework .context .ConfigurableApplicationContext ;
32
31
import org .springframework .core .env .ConfigurableEnvironment ;
33
32
import org .springframework .core .env .Environment ;
@@ -174,7 +173,7 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
174
173
* @throws IllegalStateException if an error occurs while processing a properties file
175
174
*/
176
175
public static void addPropertiesFilesToEnvironment (ConfigurableApplicationContext context ,
177
- String [] locations ) {
176
+ String ... locations ) {
178
177
Assert .notNull (context , "context must not be null" );
179
178
Assert .notNull (locations , "locations must not be null" );
180
179
try {
@@ -204,7 +203,7 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex
204
203
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
205
204
*/
206
205
public static void addInlinedPropertiesToEnvironment (ConfigurableApplicationContext context ,
207
- String [] inlinedProperties ) {
206
+ String ... inlinedProperties ) {
208
207
Assert .notNull (context , "context must not be null" );
209
208
Assert .notNull (inlinedProperties , "inlinedProperties must not be null" );
210
209
addInlinedPropertiesToEnvironment (context .getEnvironment (), inlinedProperties );
@@ -226,17 +225,21 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont
226
225
* @see TestPropertySource#properties
227
226
* @see #convertInlinedPropertiesToMap
228
227
*/
229
- public static void addInlinedPropertiesToEnvironment (ConfigurableEnvironment environment , String [] inlinedProperties ) {
228
+ public static void addInlinedPropertiesToEnvironment (ConfigurableEnvironment environment , String ... inlinedProperties ) {
230
229
Assert .notNull (environment , "environment must not be null" );
231
230
Assert .notNull (inlinedProperties , "inlinedProperties must not be null" );
232
231
if (!ObjectUtils .isEmpty (inlinedProperties )) {
233
232
if (logger .isDebugEnabled ()) {
234
233
logger .debug ("Adding inlined properties to environment: "
235
234
+ ObjectUtils .nullSafeToString (inlinedProperties ));
236
235
}
237
- MapPropertySource ps = new MapPropertySource (INLINED_PROPERTIES_PROPERTY_SOURCE_NAME ,
238
- convertInlinedPropertiesToMap (inlinedProperties ));
239
- environment .getPropertySources ().addFirst (ps );
236
+ MapPropertySource ps = (MapPropertySource ) environment .getPropertySources ().get (INLINED_PROPERTIES_PROPERTY_SOURCE_NAME );
237
+ if (ps == null ) {
238
+ ps = new MapPropertySource (INLINED_PROPERTIES_PROPERTY_SOURCE_NAME ,
239
+ new LinkedHashMap <String , Object >());
240
+ environment .getPropertySources ().addFirst (ps );
241
+ }
242
+ ps .getSource ().putAll (convertInlinedPropertiesToMap (inlinedProperties ));
240
243
}
241
244
}
242
245
@@ -257,7 +260,7 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env
257
260
* a given inlined property contains multiple key-value pairs
258
261
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
259
262
*/
260
- public static Map <String , Object > convertInlinedPropertiesToMap (String [] inlinedProperties ) {
263
+ public static Map <String , Object > convertInlinedPropertiesToMap (String ... inlinedProperties ) {
261
264
Assert .notNull (inlinedProperties , "inlinedProperties must not be null" );
262
265
Map <String , Object > map = new LinkedHashMap <String , Object >();
263
266
0 commit comments