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

@@ -433,18 +433,17 @@ public class EnvironmentIntegrationTests {
ConfigurableEnvironment environment = ctx.getEnvironment();
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
MutablePropertySources propertySources = environment.getPropertySources();
PropertyResolver propertyResolver = environment.getPropertyResolver();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
// ServletConfig gets precedence
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
// but all params are available
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
assertThat(propertyResolver.getProperty("pConfig1"), is("pConfig1Value"));
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
// Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
@@ -457,8 +456,8 @@ public class EnvironmentIntegrationTests {
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
// assert that servletconfig params resolve with higher precedence than sysprops
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
@Test
@@ -474,12 +473,11 @@ public class EnvironmentIntegrationTests {
ConfigurableEnvironment environment = ctx.getEnvironment();
assertThat(environment, instanceOf(DefaultWebEnvironment.class));
MutablePropertySources propertySources = environment.getPropertySources();
PropertyResolver propertyResolver = environment.getPropertyResolver();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
// ServletContext params are available
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonContextValue"));
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
// Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
@@ -492,8 +490,8 @@ public class EnvironmentIntegrationTests {
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
// assert that servletcontext init params resolve with higher precedence than sysprops
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonContextValue"));
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
@Test
@@ -512,18 +510,17 @@ public class EnvironmentIntegrationTests {
ConfigurableEnvironment environment = ctx.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
PropertyResolver propertyResolver = environment.getPropertyResolver();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
// ServletConfig gets precedence
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
// but all params are available
assertThat(propertyResolver.getProperty("pContext1"), is("pContext1Value"));
assertThat(propertyResolver.getProperty("pConfig1"), is("pConfig1Value"));
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
// Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
@@ -536,8 +533,8 @@ public class EnvironmentIntegrationTests {
propertySources.replace(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
// assert that servletconfig params resolve with higher precedence than sysprops
assertThat(propertyResolver.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertyResolver.getProperty("pSysProps1"), is("pSysProps1Value"));
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
@Test