Allow access to applied property sources

Add PropertySourcesPlaceholderConfigurer.getAppliedPropertySources() to
allow access to the PropertySources that were actually applied.

Issue: SPR-10545
This commit is contained in:
Phillip Webb
2013-05-08 19:39:12 -07:00
parent 0652febe34
commit eb1776e79d
2 changed files with 37 additions and 10 deletions

View File

@@ -16,28 +16,26 @@
package org.springframework.context.support;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.tests.sample.beans.TestBean;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
/**
* Unit tests for {@link PropertySourcesPlaceholderConfigurer}.
*
@@ -46,6 +44,9 @@ import org.springframework.tests.sample.beans.TestBean;
*/
public class PropertySourcesPlaceholderConfigurerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void replacementFromEnvironmentProperties() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -62,6 +63,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
ppc.setEnvironment(env);
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
assertThat(ppc.getAppliedPropertySources(), not(nullValue()));
}
@Test
@@ -104,6 +106,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
pc.setPropertySources(propertySources);
pc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
@Test
@@ -123,6 +126,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
pc.setIgnoreUnresolvablePlaceholders(true);
pc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
@Test
@@ -254,4 +258,11 @@ public class PropertySourcesPlaceholderConfigurerTests {
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), nullValue());
}
@Test
public void getAppliedPropertySourcesTooEarly() throws Exception {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
thrown.expect(IllegalStateException.class);
ppc.getAppliedPropertySources();
}
}