Skip to content

Commit 6ae0219

Browse files
committed
Use TestPropertySourceUtils to convert properties
Fixes gh-4384
1 parent 09b5222 commit 6ae0219

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.test;
1818

19-
import java.io.IOException;
20-
import java.io.StringReader;
2119
import java.lang.annotation.Annotation;
2220
import java.util.ArrayList;
2321
import java.util.Arrays;
@@ -26,7 +24,6 @@
2624
import java.util.LinkedHashSet;
2725
import java.util.List;
2826
import java.util.Map;
29-
import java.util.Properties;
3027
import java.util.Set;
3128

3229
import org.springframework.beans.BeanUtils;
@@ -75,8 +72,6 @@
7572
*/
7673
public class SpringApplicationContextLoader extends AbstractContextLoader {
7774

78-
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
79-
8075
@Override
8176
public ApplicationContext loadContext(final MergedContextConfiguration config)
8277
throws Exception {
@@ -146,8 +141,8 @@ protected Map<String, Object> getEnvironmentProperties(
146141
Map<String, Object> properties = new LinkedHashMap<String, Object>();
147142
// JMX bean names will clash if the same bean is used in multiple contexts
148143
disableJmx(properties);
149-
properties.putAll(
150-
extractEnvironmentProperties(config.getPropertySourceProperties()));
144+
properties.putAll(TestPropertySourceUtils
145+
.convertInlinedPropertiesToMap(config.getPropertySourceProperties()));
151146
if (!TestAnnotations.isIntegrationTest(config)) {
152147
properties.putAll(getDefaultEnvironmentProperties());
153148
}
@@ -158,31 +153,6 @@ private void disableJmx(Map<String, Object> properties) {
158153
properties.put("spring.jmx.enabled", "false");
159154
}
160155

161-
final Map<String, Object> extractEnvironmentProperties(String[] values) {
162-
// Instead of parsing the keys ourselves, we rely on standard handling
163-
if (values == null) {
164-
return Collections.emptyMap();
165-
}
166-
String content = StringUtils.arrayToDelimitedString(values, LINE_SEPARATOR);
167-
Properties properties = new Properties();
168-
try {
169-
properties.load(new StringReader(content));
170-
return asMap(properties);
171-
}
172-
catch (IOException ex) {
173-
throw new IllegalStateException(
174-
"Unexpected could not load properties from '" + content + "'", ex);
175-
}
176-
}
177-
178-
private Map<String, Object> asMap(Properties properties) {
179-
Map<String, Object> map = new LinkedHashMap<String, Object>();
180-
for (String name : properties.stringPropertyNames()) {
181-
map.put(name, properties.getProperty(name));
182-
}
183-
return map;
184-
}
185-
186156
private Map<String, String> getDefaultEnvironmentProperties() {
187157
return Collections.singletonMap("server.port", "-1");
188158
}

spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.util.Map;
2020

21+
import org.junit.Ignore;
2122
import org.junit.Test;
2223

2324
import org.springframework.test.context.MergedContextConfiguration;
2425
import org.springframework.test.context.TestContext;
2526
import org.springframework.test.context.TestContextManager;
27+
import org.springframework.test.context.support.TestPropertySourceUtils;
2628
import org.springframework.test.util.ReflectionTestUtils;
2729

2830
import static org.junit.Assert.assertEquals;
@@ -35,8 +37,6 @@
3537
*/
3638
public class SpringApplicationContextLoaderTests {
3739

38-
private final SpringApplicationContextLoader loader = new SpringApplicationContextLoader();
39-
4040
@Test
4141
public void environmentPropertiesSimple() throws Exception {
4242
Map<String, Object> config = getEnvironmentProperties(SimpleConfig.class);
@@ -72,15 +72,24 @@ public void environmentPropertiesAnotherSeparatorInValue() throws Exception {
7272
assertKey(config, "anotherKey", "another=Value");
7373
}
7474

75+
@Test
76+
@Ignore
77+
public void environmentPropertiesNewLineInValue() throws Exception {
78+
// gh-4384
79+
Map<String, Object> config = getEnvironmentProperties(NewLineInValue.class);
80+
assertKey(config, "key", "myValue");
81+
assertKey(config, "variables", "foo=FOO\n bar=BAR");
82+
}
83+
7584
private Map<String, Object> getEnvironmentProperties(Class<?> testClass)
7685
throws Exception {
7786
TestContext context = new ExposedTestContextManager(testClass)
7887
.getExposedTestContext();
7988
new IntegrationTestPropertiesListener().prepareTestInstance(context);
8089
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils
8190
.getField(context, "mergedContextConfiguration");
82-
return this.loader
83-
.extractEnvironmentProperties(config.getPropertySourceProperties());
91+
return TestPropertySourceUtils
92+
.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
8493
}
8594

8695
private void assertKey(Map<String, Object> actual, String key, Object value) {
@@ -108,6 +117,10 @@ static class SameSeparatorInValue {
108117
static class AnotherSeparatorInValue {
109118
}
110119

120+
@IntegrationTest({ "key=myValue", "variables=foo=FOO\n bar=BAR" })
121+
static class NewLineInValue {
122+
}
123+
111124
/**
112125
* {@link TestContextManager} which exposes the {@link TestContext}.
113126
*/

0 commit comments

Comments
 (0)