PropertyPlaceholderConfigurer supports "${myKey:myDefaultValue}" defaulting syntax

This commit is contained in:
Juergen Hoeller
2009-09-24 22:34:02 +00:00
parent 8eca898d44
commit 0f43d6c592
6 changed files with 103 additions and 42 deletions

View File

@@ -18,10 +18,12 @@ package org.springframework.util;
import java.util.Properties;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/** @author Rob Harrop */
/**
* @author Rob Harrop
*/
public class PropertyPlaceholderHelperTests {
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
@@ -98,7 +100,8 @@ public class PropertyPlaceholderHelperTests {
Properties props = new Properties();
props.setProperty("foo", "bar");
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", false);
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
assertEquals("foo=bar,bar=${bar}", helper.replacePlaceholders(text, props));
}
}

View File

@@ -18,33 +18,54 @@ package org.springframework.util;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/** @author Rob Harrop */
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class SystemPropertyUtilsTests {
@Test
public void testReplaceFromSystemProperty() {
System.setProperty("test.prop", "bar");
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");
assertEquals("bar", resolved);
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceWithDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}");
assertEquals("foo", resolved);
}
@Test
public void testRecursiveFromSystemProperty() {
System.setProperty("test.prop", "foo=${bar}");
System.setProperty("bar", "baz");
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");
assertEquals("foo=baz", resolved);
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");
assertEquals("foo=baz", resolved);
}
finally {
System.getProperties().remove("test.prop");
System.getProperties().remove("bar");
}
}
@Test
public void testReplaceFromEnv() {
Map<String,String> env = System.getenv();
if(env.containsKey("PATH")) {
if (env.containsKey("PATH")) {
String text = "${PATH}";
assertEquals(env.get("PATH"), SystemPropertyUtils.resolvePlaceholders(text));
}
}
}