Skip to content

Commit fc83933

Browse files
committed
Refine TestPropertySourceUtils for direct use
Update methods in TestPropertySourceUtils to accept var-args and allow addInlinedProperties to be called more than once. Issue: SPR-14088
1 parent fee056a commit fc83933

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -27,7 +27,6 @@
2727

2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30-
3130
import org.springframework.context.ConfigurableApplicationContext;
3231
import org.springframework.core.env.ConfigurableEnvironment;
3332
import org.springframework.core.env.Environment;
@@ -174,7 +173,7 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
174173
* @throws IllegalStateException if an error occurs while processing a properties file
175174
*/
176175
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context,
177-
String[] locations) {
176+
String... locations) {
178177
Assert.notNull(context, "context must not be null");
179178
Assert.notNull(locations, "locations must not be null");
180179
try {
@@ -204,7 +203,7 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex
204203
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
205204
*/
206205
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context,
207-
String[] inlinedProperties) {
206+
String... inlinedProperties) {
208207
Assert.notNull(context, "context must not be null");
209208
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
210209
addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties);
@@ -226,17 +225,21 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont
226225
* @see TestPropertySource#properties
227226
* @see #convertInlinedPropertiesToMap
228227
*/
229-
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String[] inlinedProperties) {
228+
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) {
230229
Assert.notNull(environment, "environment must not be null");
231230
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
232231
if (!ObjectUtils.isEmpty(inlinedProperties)) {
233232
if (logger.isDebugEnabled()) {
234233
logger.debug("Adding inlined properties to environment: "
235234
+ ObjectUtils.nullSafeToString(inlinedProperties));
236235
}
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));
240243
}
241244
}
242245

@@ -257,7 +260,7 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env
257260
* a given inlined property contains multiple key-value pairs
258261
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
259262
*/
260-
public static Map<String, Object> convertInlinedPropertiesToMap(String[] inlinedProperties) {
263+
public static Map<String, Object> convertInlinedPropertiesToMap(String... inlinedProperties) {
261264
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
262265
Map<String, Object> map = new LinkedHashMap<String, Object>();
263266

spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -140,7 +140,7 @@ public void addInlinedPropertiesToEnvironmentWithNullContext() {
140140
public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() {
141141
expectedException.expect(IllegalArgumentException.class);
142142
expectedException.expectMessage("inlined");
143-
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), null);
143+
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null);
144144
}
145145

146146
/**
@@ -160,7 +160,7 @@ public void addInlinedPropertiesToEnvironmentWithNullEnvironment() {
160160
public void addInlinedPropertiesToEnvironmentWithEnvironmentAndNullInlinedProperties() {
161161
expectedException.expect(IllegalArgumentException.class);
162162
expectedException.expectMessage("inlined");
163-
addInlinedPropertiesToEnvironment(new MockEnvironment(), null);
163+
addInlinedPropertiesToEnvironment(new MockEnvironment(), (String[]) null);
164164
}
165165

166166
/**
@@ -202,7 +202,7 @@ public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
202202
public void convertInlinedPropertiesToMapWithNullInlinedProperties() {
203203
expectedException.expect(IllegalArgumentException.class);
204204
expectedException.expectMessage("inlined");
205-
convertInlinedPropertiesToMap(null);
205+
convertInlinedPropertiesToMap((String[]) null);
206206
}
207207

208208
// -------------------------------------------------------------------

0 commit comments

Comments
 (0)