Refactor Environment and PropertySource

* Environment now extends PropertyResolver
* Environment no longer exposes resolver and sources
* PropertySource is String,Object instead of String,String
* PropertySource no longer assumes enumerability of property names
* Introduced EnumerablePropertySource for those that do have enumerable property names
This commit is contained in:
Chris Beams
2011-01-05 22:24:14 +00:00
parent 7c4582b4b3
commit 2b99cf6d29
39 changed files with 392 additions and 316 deletions

View File

@@ -235,7 +235,7 @@ public class EnvironmentTests {
getModifiableSystemEnvironment().put(DISALLOWED_PROPERTY_NAME, DISALLOWED_PROPERTY_VALUE);
{
Map<String, String> systemEnvironment = environment.getSystemEnvironment();
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
assertThat(systemEnvironment, notNullValue());
assertSame(systemEnvironment, System.getenv());
}
@@ -258,10 +258,10 @@ public class EnvironmentTests {
System.setSecurityManager(securityManager);
{
Map<String, String> systemEnvironment = environment.getSystemEnvironment();
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
assertThat(systemEnvironment, notNullValue());
assertThat(systemEnvironment, instanceOf(ReadOnlySystemAttributesMap.class));
assertThat(systemEnvironment.get(ALLOWED_PROPERTY_NAME), equalTo(ALLOWED_PROPERTY_VALUE));
assertThat(systemEnvironment.get(ALLOWED_PROPERTY_NAME), equalTo((Object)ALLOWED_PROPERTY_VALUE));
assertThat(systemEnvironment.get(DISALLOWED_PROPERTY_NAME), nullValue());
}

View File

@@ -18,7 +18,6 @@ package org.springframework.core.env;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -81,7 +80,7 @@ public class PropertyResolverTests {
@Test
public void getProperty_withExplicitNullValue() {
// java.util.Properties does not allow null values (because Hashtable does not)
Map<String, String> nullableProperties = new HashMap<String, String>();
Map<String, Object> nullableProperties = new HashMap<String, Object>();
propertySources.addLast(new MapPropertySource("nullableProperties", nullableProperties));
nullableProperties.put("foo", null);
assertThat(propertyResolver.getProperty("foo"), nullValue());
@@ -114,7 +113,7 @@ public class PropertyResolverTests {
String value1 = "bar";
String value2 = "biz";
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(key, value1); // before construction
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MapPropertySource("testProperties", map));
@@ -126,7 +125,7 @@ public class PropertyResolverTests {
@Test
public void getProperty_doesNotCache_addNewKeyPostConstruction() {
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, Object> map = new HashMap<String, Object>();
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MapPropertySource("testProperties", map));
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
@@ -180,45 +179,9 @@ public class PropertyResolverTests {
}
}
@Test
public void asProperties() {
propertySources = new MutablePropertySources();
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
assertThat(propertyResolver.asProperties(), notNullValue());
propertySources.addLast(new MockPropertySource("highestPrecedence").withProperty("common", "highCommon").withProperty("highKey", "highVal"));
propertySources.addLast(new MockPropertySource("middlePrecedence").withProperty("common", "midCommon").withProperty("midKey", "midVal"));
propertySources.addLast(new MockPropertySource("lowestPrecedence").withProperty("common", "lowCommon").withProperty("lowKey", "lowVal"));
Properties props = propertyResolver.asProperties();
assertThat(props.getProperty("common"), is("highCommon"));
assertThat(props.getProperty("lowKey"), is("lowVal"));
assertThat(props.getProperty("midKey"), is("midVal"));
assertThat(props.getProperty("highKey"), is("highVal"));
assertThat(props.size(), is(4));
}
@Test
public void asProperties_withMixedPropertySourceTypes() {
class Foo { }
class FooPropertySource extends PropertySource<Foo> {
public FooPropertySource() { super("fooProperties", new Foo()); }
public String[] getPropertyNames() { return new String[] {"pName"}; }
public String getProperty(String key) { return "fooValue"; }
}
propertySources = new MutablePropertySources();
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
assertThat(propertyResolver.asProperties(), notNullValue());
propertySources.addLast(new MockPropertySource());
propertySources.addLast(new FooPropertySource());
Properties props = propertyResolver.asProperties();
assertThat(props.getProperty("pName"), is("fooValue"));
assertThat(props.size(), is(1));
}
/*
@Test
public void resolvePlaceholders() {
MutablePropertySources propertySources = new MutablePropertySources();
@@ -279,4 +242,5 @@ public class PropertyResolverTests {
public void resolveRequiredPlaceholders_withNullInput() {
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null);
}
*/
}

View File

@@ -39,8 +39,8 @@ import org.junit.Test;
public class PropertySourceTests {
@Test @SuppressWarnings("serial")
public void equals() {
Map<String, String> map1 = new HashMap<String, String>() {{ put("a", "b"); }};
Map<String, String> map2 = new HashMap<String, String>() {{ put("c", "d"); }};
Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }};
Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }};
Properties props1 = new Properties() {{ setProperty("a", "b"); }};
Properties props2 = new Properties() {{ setProperty("c", "d"); }};
@@ -62,8 +62,8 @@ public class PropertySourceTests {
@Test @SuppressWarnings("serial")
public void collectionsOperations() {
Map<String, String> map1 = new HashMap<String, String>() {{ put("a", "b"); }};
Map<String, String> map2 = new HashMap<String, String>() {{ put("c", "d"); }};
Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }};
Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }};
PropertySource<?> ps1 = new MapPropertySource("ps1", map1);
ps1.getSource();
@@ -92,7 +92,7 @@ public class PropertySourceTests {
@Test @SuppressWarnings("serial")
public void toString_verbosityVariesOnLogLevel() {
String name = "props";
Map<String, String> map = new HashMap<String, String>() {{ put("k1", "v1"); }};
Map<String, Object> map = new HashMap<String, Object>() {{ put("k1", "v1"); }};
MapPropertySource ps = new MapPropertySource(name, map);
Logger logger = Logger.getLogger(ps.getClass());
Level original = logger.getLevel();
@@ -110,7 +110,7 @@ public class PropertySourceTests {
logger.setLevel(Level.INFO);
assertThat("PropertySource.toString() should render concise output for log levels >= INFO",
ps.toString(),
equalTo(String.format("%s [name='%s', propertyCount=%d]",
equalTo(String.format("%s [name='%s']",
ps.getClass().getSimpleName(),
name,
map.size())));

View File

@@ -45,9 +45,9 @@ public class PropertySourcesTests {
assertThat(sources.contains("g"), is(false));
assertThat(sources.get("b"), not(nullValue()));
assertThat(sources.get("b").getProperty("p1"), equalTo("bValue"));
assertThat(sources.get("b").getProperty("p1"), equalTo((Object)"bValue"));
assertThat(sources.get("d"), not(nullValue()));
assertThat(sources.get("d").getProperty("p1"), equalTo("dValue"));
assertThat(sources.get("d").getProperty("p1"), equalTo((Object)"dValue"));
sources.addBefore("b", new MockPropertySource("a"));
sources.addAfter("b", new MockPropertySource("c"));

View File

@@ -73,7 +73,6 @@ public class PropertyPlaceholderHelperTests {
assertEquals("foo=bar",
this.helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
public String resolvePlaceholder(String placeholderName) {
if ("foo".equals(placeholderName)) {
return "bar";