Use TestPropertySourceUtils to convert properties

Fixes gh-4384
pull/4446/head
Phillip Webb 9 years ago
parent 09b5222f52
commit 6ae021969b

@ -16,8 +16,6 @@
package org.springframework.boot.test;
import java.io.IOException;
import java.io.StringReader;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
@ -26,7 +24,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.BeanUtils;
@ -75,8 +72,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
*/
public class SpringApplicationContextLoader extends AbstractContextLoader {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
@Override
public ApplicationContext loadContext(final MergedContextConfiguration config)
throws Exception {
@ -146,8 +141,8 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
Map<String, Object> properties = new LinkedHashMap<String, Object>();
// JMX bean names will clash if the same bean is used in multiple contexts
disableJmx(properties);
properties.putAll(
extractEnvironmentProperties(config.getPropertySourceProperties()));
properties.putAll(TestPropertySourceUtils
.convertInlinedPropertiesToMap(config.getPropertySourceProperties()));
if (!TestAnnotations.isIntegrationTest(config)) {
properties.putAll(getDefaultEnvironmentProperties());
}
@ -158,31 +153,6 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
properties.put("spring.jmx.enabled", "false");
}
final Map<String, Object> extractEnvironmentProperties(String[] values) {
// Instead of parsing the keys ourselves, we rely on standard handling
if (values == null) {
return Collections.emptyMap();
}
String content = StringUtils.arrayToDelimitedString(values, LINE_SEPARATOR);
Properties properties = new Properties();
try {
properties.load(new StringReader(content));
return asMap(properties);
}
catch (IOException ex) {
throw new IllegalStateException(
"Unexpected could not load properties from '" + content + "'", ex);
}
}
private Map<String, Object> asMap(Properties properties) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (String name : properties.stringPropertyNames()) {
map.put(name, properties.getProperty(name));
}
return map;
}
private Map<String, String> getDefaultEnvironmentProperties() {
return Collections.singletonMap("server.port", "-1");
}

@ -18,11 +18,13 @@ package org.springframework.boot.test;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
@ -35,8 +37,6 @@ import static org.junit.Assert.assertTrue;
*/
public class SpringApplicationContextLoaderTests {
private final SpringApplicationContextLoader loader = new SpringApplicationContextLoader();
@Test
public void environmentPropertiesSimple() throws Exception {
Map<String, Object> config = getEnvironmentProperties(SimpleConfig.class);
@ -72,6 +72,15 @@ public class SpringApplicationContextLoaderTests {
assertKey(config, "anotherKey", "another=Value");
}
@Test
@Ignore
public void environmentPropertiesNewLineInValue() throws Exception {
// gh-4384
Map<String, Object> config = getEnvironmentProperties(NewLineInValue.class);
assertKey(config, "key", "myValue");
assertKey(config, "variables", "foo=FOO\n bar=BAR");
}
private Map<String, Object> getEnvironmentProperties(Class<?> testClass)
throws Exception {
TestContext context = new ExposedTestContextManager(testClass)
@ -79,8 +88,8 @@ public class SpringApplicationContextLoaderTests {
new IntegrationTestPropertiesListener().prepareTestInstance(context);
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils
.getField(context, "mergedContextConfiguration");
return this.loader
.extractEnvironmentProperties(config.getPropertySourceProperties());
return TestPropertySourceUtils
.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
}
private void assertKey(Map<String, Object> actual, String key, Object value) {
@ -108,6 +117,10 @@ public class SpringApplicationContextLoaderTests {
static class AnotherSeparatorInValue {
}
@IntegrationTest({ "key=myValue", "variables=foo=FOO\n bar=BAR" })
static class NewLineInValue {
}
/**
* {@link TestContextManager} which exposes the {@link TestContext}.
*/

Loading…
Cancel
Save