Commit e005ba72 authored by Dave Syer's avatar Dave Syer

SpringApplicationBuilder.properties fixes

Previously: properties(Map) behaved differently to
properties(String...).

Fixed by merging the implementations.

Also added properties(Properties).
parent 63a2d067
...@@ -17,10 +17,12 @@ package org.springframework.boot.builder; ...@@ -17,10 +17,12 @@ package org.springframework.boot.builder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
...@@ -326,13 +328,7 @@ public class SpringApplicationBuilder { ...@@ -326,13 +328,7 @@ public class SpringApplicationBuilder {
* @return the current builder * @return the current builder
*/ */
public SpringApplicationBuilder properties(String... defaultProperties) { public SpringApplicationBuilder properties(String... defaultProperties) {
this.defaultProperties.putAll(getMapFromKeyValuePairs(defaultProperties)); return properties(getMapFromKeyValuePairs(defaultProperties));
this.application.setDefaultProperties(this.defaultProperties);
if (this.parent != null) {
this.parent.properties(defaultProperties);
this.parent.environment(this.environment);
}
return this;
} }
private Map<String, Object> getMapFromKeyValuePairs(String[] args) { private Map<String, Object> getMapFromKeyValuePairs(String[] args) {
...@@ -349,6 +345,25 @@ public class SpringApplicationBuilder { ...@@ -349,6 +345,25 @@ public class SpringApplicationBuilder {
return map; return map;
} }
/**
* Default properties for the environment in the form <code>key=value</code> or
* <code>key:value</code>.
*
* @param defaultProperties the properties to set.
* @return the current builder
*/
public SpringApplicationBuilder properties(Properties defaultProperties) {
return properties(getMapFromProperties(defaultProperties));
}
private Map<String, Object> getMapFromProperties(Properties properties) {
HashMap<String, Object> map = new HashMap<String, Object>();
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 * Default properties for the environment. Multiple calls to this method are
* cumulative. * cumulative.
...@@ -360,6 +375,11 @@ public class SpringApplicationBuilder { ...@@ -360,6 +375,11 @@ public class SpringApplicationBuilder {
*/ */
public SpringApplicationBuilder properties(Map<String, Object> defaults) { public SpringApplicationBuilder properties(Map<String, Object> defaults) {
this.defaultProperties.putAll(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; return this;
} }
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.builder; ...@@ -18,6 +18,7 @@ package org.springframework.boot.builder;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Collections;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
...@@ -28,6 +29,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -28,6 +29,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
...@@ -64,6 +66,28 @@ public class SpringApplicationBuilderTests { ...@@ -64,6 +66,28 @@ public class SpringApplicationBuilderTests {
assertThat(this.context.getEnvironment().acceptsProfiles("foo"), is(true)); 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.<String, Object> 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 @Test
public void specificApplicationContextClass() throws Exception { public void specificApplicationContextClass() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder().sources( SpringApplicationBuilder application = new SpringApplicationBuilder().sources(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment