Merge branch '1.3.x'

This commit is contained in:
Phillip Webb 2016-06-10 20:57:17 -07:00
commit 10f8a2f6bd
2 changed files with 31 additions and 8 deletions

View File

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

View File

@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -93,6 +94,20 @@ public class SpringApplicationBuilderTests {
assertThat(this.context.getEnvironment().getProperty("bar")).isEqualTo("foo"); 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 @Test
public void specificApplicationContextClass() throws Exception { public void specificApplicationContextClass() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder() SpringApplicationBuilder application = new SpringApplicationBuilder()