Search in sources :

Example 6 with MutablePropertySources

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

the class PropertiesConfigurationFactoryTests method bindFoo.

private Foo bindFoo(final String values) throws Exception {
    Properties properties = PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values.getBytes()));
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new PropertiesPropertySource("test", properties));
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    return this.factory.getObject();
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties)

Example 7 with MutablePropertySources

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

the class PropertiesConfigurationFactoryTests method testBindWithDelimitedPrefixUsingDifferentDelimiter.

@Test
public void testBindWithDelimitedPrefixUsingDifferentDelimiter() throws Exception {
    this.targetName = "env.foo";
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("ENV_FOO_NAME", "blah")));
    propertySources.addLast(new RandomValuePropertySource("random"));
    this.ignoreUnknownFields = false;
    setupFactory();
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    Foo foo = this.factory.getObject();
    assertThat(foo.name).isEqualTo("blah");
}
Also used : SystemEnvironmentPropertySource(org.springframework.core.env.SystemEnvironmentPropertySource) RandomValuePropertySource(org.springframework.boot.env.RandomValuePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Example 8 with MutablePropertySources

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

the class EnableConfigurationPropertiesTests method removeSystemProperties.

/**
	 * Strict tests need a known set of properties so we remove system items which may be
	 * environment specific.
	 */
private void removeSystemProperties() {
    MutablePropertySources sources = this.context.getEnvironment().getPropertySources();
    sources.remove("systemProperties");
    sources.remove("systemEnvironment");
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 9 with MutablePropertySources

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

the class RelaxedPropertyResolverTests method testPropertySource.

@Test
public void testPropertySource() throws Exception {
    Properties properties;
    PropertiesPropertySource propertySource;
    String propertyPrefix = "spring.datasource.";
    String propertyName = "password";
    String fullPropertyName = propertyPrefix + propertyName;
    StandardEnvironment environment = new StandardEnvironment();
    MutablePropertySources sources = environment.getPropertySources();
    properties = new Properties();
    properties.put(fullPropertyName, "systemPassword");
    propertySource = new PropertiesPropertySource("system", properties);
    sources.addLast(propertySource);
    properties = new Properties();
    properties.put(fullPropertyName, "propertiesPassword");
    propertySource = new PropertiesPropertySource("properties", properties);
    sources.addLast(propertySource);
    RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, propertyPrefix);
    String directProperty = propertyResolver.getProperty(propertyName);
    Map<String, Object> subProperties = propertyResolver.getSubProperties("");
    String subProperty = (String) subProperties.get(propertyName);
    assertThat(subProperty).isEqualTo(directProperty);
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 10 with MutablePropertySources

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

the class ConfigFileApplicationListenerTests method matchingPropertySource.

private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
    return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {

        @Override
        public boolean matches(ConfigurableEnvironment value) {
            MutablePropertySources sources = new MutablePropertySources(value.getPropertySources());
            ConfigurationPropertySources.finishAndRelocate(sources);
            return sources.contains(sourceName);
        }
    };
}
Also used : Condition(org.assertj.core.api.Condition) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

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