Search in sources :

Example 16 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method systemPropertyBindingFailuresAreIgnored.

@Test
public void systemPropertyBindingFailuresAreIgnored() throws Exception {
    setupFactory();
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource propertySource = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
    propertySource.setProperty("doesNotExist", "foo");
    propertySource.setProperty("name", "bar");
    propertySources.addFirst(propertySource);
    this.factory.setPropertySources(propertySources);
    this.factory.setIgnoreUnknownFields(false);
    this.factory.afterPropertiesSet();
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 17 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method systemEnvironmentBindingFailuresAreIgnored.

@Test
public void systemEnvironmentBindingFailuresAreIgnored() throws Exception {
    setupFactory();
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource propertySource = new MockPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
    propertySource.setProperty("doesNotExist", "foo");
    propertySource.setProperty("name", "bar");
    propertySources.addFirst(propertySource);
    this.factory.setPropertySources(propertySources);
    this.factory.setIgnoreUnknownFields(false);
    this.factory.afterPropertiesSet();
    Foo foo = this.factory.getObject();
    assertThat(foo.name).isEqualTo("bar");
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 18 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method systemEnvironmentNoResolvePlaceholders.

@Test
public void systemEnvironmentNoResolvePlaceholders() throws Exception {
    setupFactory();
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource propertySource = new MockPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
    propertySource.setProperty("name", "${foo.name:bar}");
    propertySources.addFirst(propertySource);
    this.factory.setPropertySources(propertySources);
    this.factory.setResolvePlaceholders(false);
    this.factory.afterPropertiesSet();
    Foo foo = this.factory.getObject();
    assertThat(foo.name).isEqualTo("${foo.name:bar}");
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 19 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySources.

@Test
public void explicitPropertySources() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
    assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 20 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesEnvironment.

@Test
public void explicitPropertySourcesExcludesEnvironment() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MockPropertySource());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
    assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Aggregations

MutablePropertySources (org.springframework.core.env.MutablePropertySources)50 Test (org.junit.Test)19 MapPropertySource (org.springframework.core.env.MapPropertySource)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)8 MockPropertySource (org.springframework.mock.env.MockPropertySource)8 Properties (java.util.Properties)6 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)5 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 LinkedHashMap (java.util.LinkedHashMap)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 RandomValuePropertySource (org.springframework.boot.env.RandomValuePropertySource)3 PropertySource (org.springframework.core.env.PropertySource)3 StandardEnvironment (org.springframework.core.env.StandardEnvironment)3 SystemEnvironmentPropertySource (org.springframework.core.env.SystemEnvironmentPropertySource)3 MockEnvironment (org.springframework.mock.env.MockEnvironment)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2