Search in sources :

Example 36 with MutablePropertySources

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

the class SpringProfileDocumentMatcher method extractSpringProfiles.

protected List<String> extractSpringProfiles(Properties properties) {
    SpringProperties springProperties = new SpringProperties();
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new PropertiesPropertySource("profiles", properties));
    PropertyValues propertyValues = new PropertySourcesPropertyValues(propertySources);
    new RelaxedDataBinder(springProperties, "spring").bind(propertyValues);
    List<String> profiles = springProperties.getProfiles();
    return profiles;
}
Also used : PropertySourcesPropertyValues(org.springframework.boot.bind.PropertySourcesPropertyValues) PropertyValues(org.springframework.beans.PropertyValues) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) RelaxedDataBinder(org.springframework.boot.bind.RelaxedDataBinder) MutablePropertySources(org.springframework.core.env.MutablePropertySources) PropertySourcesPropertyValues(org.springframework.boot.bind.PropertySourcesPropertyValues)

Example 37 with MutablePropertySources

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

the class StandardServletEnvironmentTests method propertySourceOrder.

@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));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Example 38 with MutablePropertySources

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

the class EmbeddedMongoAutoConfiguration method setPortProperty.

private void setPortProperty(ApplicationContext currentContext, int port) {
    if (currentContext instanceof ConfigurableApplicationContext) {
        MutablePropertySources sources = ((ConfigurableApplicationContext) currentContext).getEnvironment().getPropertySources();
        getMongoPorts(sources).put("local.mongo.port", port);
    }
    if (currentContext.getParent() != null) {
        setPortProperty(currentContext.getParent(), port);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 39 with MutablePropertySources

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

the class EnvironmentTestUtils method addEnvironment.

/**
	 * Add additional (high priority) values to an {@link Environment}. Name-value pairs
	 * can be specified with colon (":") or equals ("=") separators.
	 * @param environment the environment to modify
	 * @param name the property source name
	 * @param pairs the name:value pairs
	 */
public static void addEnvironment(String name, ConfigurableEnvironment environment, String... pairs) {
    MutablePropertySources sources = environment.getPropertySources();
    Map<String, Object> map = getOrAdd(sources, name);
    for (String pair : pairs) {
        int index = getSeparatorIndex(pair);
        String key = pair.substring(0, index > 0 ? index : pair.length());
        String value = index > 0 ? pair.substring(index + 1) : "";
        map.put(key.trim(), value.trim());
    }
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 40 with MutablePropertySources

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

the class SpringBootTestContextBootstrapper method convertToPropertySources.

private PropertySources convertToPropertySources(String[] properties) {
    Map<String, Object> source = TestPropertySourceUtils.convertInlinedPropertiesToMap(properties);
    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new MapPropertySource("inline", source));
    return sources;
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Aggregations

MutablePropertySources (org.springframework.core.env.MutablePropertySources)51 Test (org.junit.Test)20 MapPropertySource (org.springframework.core.env.MapPropertySource)12 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