Merge branch '1.3.x'

pull/6145/head
Phillip Webb 9 years ago
commit 10f8a2f6bd

@ -382,20 +382,28 @@ public class SpringApplicationBuilder {
return properties(getMapFromKeyValuePairs(defaultProperties));
}
private Map<String, Object> getMapFromKeyValuePairs(String[] args) {
private Map<String, Object> getMapFromKeyValuePairs(String[] properties) {
Map<String, Object> map = new HashMap<String, Object>();
for (String pair : args) {
int index = pair.indexOf(":");
if (index <= 0) {
index = pair.indexOf("=");
}
String key = pair.substring(0, index > 0 ? index : pair.length());
String value = index > 0 ? pair.substring(index + 1) : "";
for (String property : properties) {
int index = lowestIndexOf(property, ":", "=");
String key = property.substring(0, index > 0 ? index : property.length());
String value = index > 0 ? property.substring(index + 1) : "";
map.put(key, value);
}
return map;
}
private int lowestIndexOf(String property, String... candidates) {
int index = -1;
for (String candidate : candidates) {
int candidateIndex = property.indexOf(candidate);
if (candidateIndex > 0) {
index = (index == -1 ? candidateIndex : Math.min(index, candidateIndex));
}
}
return index;
}
/**
* Default properties for the environment in the form {@code key=value} or
* {@code key:value}.

@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
@ -93,6 +94,20 @@ public class SpringApplicationBuilderTests {
assertThat(this.context.getEnvironment().getProperty("bar")).isEqualTo("foo");
}
@Test
public void propertiesWithRepeatSeparator() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder()
.sources(ExampleConfig.class).contextClass(StaticApplicationContext.class)
.properties("one=c:\\logging.file", "two=a:b", "three:c:\\logging.file",
"four:a:b");
this.context = application.run();
ConfigurableEnvironment environment = this.context.getEnvironment();
assertThat(environment.getProperty("one")).isEqualTo("c:\\logging.file");
assertThat(environment.getProperty("two")).isEqualTo("a:b");
assertThat(environment.getProperty("three")).isEqualTo("c:\\logging.file");
assertThat(environment.getProperty("four")).isEqualTo("a:b");
}
@Test
public void specificApplicationContextClass() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder()

Loading…
Cancel
Save