@Test public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); String pair = "key = value"; ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair); ResourceLoader resourceLoader = mock(ResourceLoader.class); when(resourceLoader.getResource(anyString())).thenReturn(resource); addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS); assertEquals(1, propertySources.size()); assertEquals("value", environment.getProperty("key")); }
@Test @SuppressWarnings("rawtypes") public void addInlinedPropertiesToEnvironmentWithEmptyProperty() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); addInlinedPropertiesToEnvironment(environment, asArray(" ")); assertEquals(1, propertySources.size()); assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size()); }
@Test public void propertySourceOrder() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.size(), is(2)); }
@Test public void propertySourceOrder() throws Exception { SimpleNamingContextBuilder.emptyActivatedContextBuilder(); ConfigurableEnvironment env = new StandardServletEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named( StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4)); assertThat(sources.size(), is(5)); }
@Test public void getPropertySources_replacePropertySource() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue")); propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue")); // 'local' was added first so has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("localValue")); // replace 'local' with new property source propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue")); // 'system' now has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("newValue")); assertThat(propertySources.size(), is(2)); }
assertThat(sources.size(), equalTo(3)); assertThat(sources.contains("a"), is(false)); assertThat(sources.contains("b"), is(true)); assertThat(sources.size(), equalTo(5)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.size(), equalTo(7)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.size(), equalTo(7)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(1)); assertThat(sources.size(), equalTo(7)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.size(), equalTo(6)); assertThat(sources.contains("a"), is(false)); assertThat(sources.size(), equalTo(6)); assertThat(sources.size(), equalTo(7)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
/** * Remove all property sources from <code>env</code> and replace them with a single <code>PropertiesPropertySource</code> backed by <code>properties</code> */ public static void reconfigurePropertySources(ConfigurableEnvironment env, String name, Properties properties) { // Remove all existing property sources removeAllPropertySources(env); // MutablePropertySources allow us to manipulate the list of property sources MutablePropertySources mps = env.getPropertySources(); // Make sure there are no existing property sources Assert.isTrue(mps.size() == 0); // Create a property source backed by the properties object passed in PropertiesPropertySource pps = new PropertiesPropertySource(name, properties); // Add it to the environment mps.addFirst(pps); }
if (propertySources.size() == 0) { logger.warn("No property files were found.");