From e005ba7232a4c0694d2c0cb651e1320ee3339522 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 1 Nov 2013 08:42:10 +0000 Subject: [PATCH] SpringApplicationBuilder.properties fixes Previously: properties(Map) behaved differently to properties(String...). Fixed by merging the implementations. Also added properties(Properties). --- .../builder/SpringApplicationBuilder.java | 34 +++++++++++++++---- .../SpringApplicationBuilderTests.java | 24 +++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index f06a44c620..0627d65604 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -17,10 +17,12 @@ package org.springframework.boot.builder; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; @@ -326,13 +328,7 @@ public class SpringApplicationBuilder { * @return the current builder */ public SpringApplicationBuilder properties(String... defaultProperties) { - this.defaultProperties.putAll(getMapFromKeyValuePairs(defaultProperties)); - this.application.setDefaultProperties(this.defaultProperties); - if (this.parent != null) { - this.parent.properties(defaultProperties); - this.parent.environment(this.environment); - } - return this; + return properties(getMapFromKeyValuePairs(defaultProperties)); } private Map getMapFromKeyValuePairs(String[] args) { @@ -349,6 +345,25 @@ public class SpringApplicationBuilder { return map; } + /** + * Default properties for the environment in the form key=value or + * key:value. + * + * @param defaultProperties the properties to set. + * @return the current builder + */ + public SpringApplicationBuilder properties(Properties defaultProperties) { + return properties(getMapFromProperties(defaultProperties)); + } + + private Map getMapFromProperties(Properties properties) { + HashMap map = new HashMap(); + for (Object key : Collections.list(properties.propertyNames())) { + map.put((String) key, properties.get(key)); + } + return map; + } + /** * Default properties for the environment. Multiple calls to this method are * cumulative. @@ -360,6 +375,11 @@ public class SpringApplicationBuilder { */ public SpringApplicationBuilder properties(Map defaults) { this.defaultProperties.putAll(defaults); + this.application.setDefaultProperties(this.defaultProperties); + if (this.parent != null) { + this.parent.properties(this.defaultProperties); + this.parent.environment(this.environment); + } return this; } diff --git a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 33ffeab5b7..0f330280ad 100644 --- a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.builder; import java.net.URL; import java.net.URLClassLoader; +import java.util.Collections; import org.junit.After; import org.junit.Test; @@ -28,6 +29,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; +import org.springframework.util.StringUtils; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -64,6 +66,28 @@ public class SpringApplicationBuilderTests { assertThat(this.context.getEnvironment().acceptsProfiles("foo"), is(true)); } + @Test + public void propertiesAsMap() throws Exception { + SpringApplicationBuilder application = new SpringApplicationBuilder() + .sources(ExampleConfig.class) + .contextClass(StaticApplicationContext.class) + .properties(Collections. singletonMap("bar", "foo")); + this.context = application.run(); + assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo"))); + } + + @Test + public void propertiesAsProperties() throws Exception { + SpringApplicationBuilder application = new SpringApplicationBuilder() + .sources(ExampleConfig.class) + .contextClass(StaticApplicationContext.class) + .properties( + StringUtils.splitArrayElementsIntoProperties( + new String[] { "bar=foo" }, "=")); + this.context = application.run(); + assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo"))); + } + @Test public void specificApplicationContextClass() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder().sources(