From 6ae021969bd041ab09b24212a0a5f780493c7ec5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 11 Nov 2015 21:24:20 -0800 Subject: [PATCH] Use TestPropertySourceUtils to convert properties Fixes gh-4384 --- .../test/SpringApplicationContextLoader.java | 34 ++----------------- .../SpringApplicationContextLoaderTests.java | 21 +++++++++--- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index c357a7a55e..5b91c51dae 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -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 properties = new LinkedHashMap(); // 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 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 asMap(Properties properties) { - Map map = new LinkedHashMap(); - for (String name : properties.stringPropertyNames()) { - map.put(name, properties.getProperty(name)); - } - return map; - } - private Map getDefaultEnvironmentProperties() { return Collections.singletonMap("server.port", "-1"); } diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java index 6bc10d501b..e62b8593d9 100644 --- a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoaderTests.java @@ -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 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 config = getEnvironmentProperties(NewLineInValue.class); + assertKey(config, "key", "myValue"); + assertKey(config, "variables", "foo=FOO\n bar=BAR"); + } + private Map 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 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}. */