Search in sources :

Example 1 with SystemEnvironmentPropertySource

use of cn.taketoday.core.env.SystemEnvironmentPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertiesTests method loadWhenEnvironmentPrefixSetShouldBind.

@Test
void loadWhenEnvironmentPrefixSetShouldBind() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    sources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, Collections.singletonMap("MY_SPRING_FOO_NAME", "Jane")));
    Application application = new Application(PrefixConfiguration.class);
    application.setApplicationContextFactory((webApplicationType) -> ConfigurationPropertiesTests.this.context);
    application.setEnvironmentPrefix("my");
    application.setEnvironment(this.context.getEnvironment());
    application.run();
    BasicProperties bean = this.context.getBean(BasicProperties.class);
    assertThat(bean.name).isEqualTo("Jane");
}
Also used : SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) PropertySources(cn.taketoday.core.env.PropertySources) Application(cn.taketoday.framework.Application) Test(org.junit.jupiter.api.Test)

Example 2 with SystemEnvironmentPropertySource

use of cn.taketoday.core.env.SystemEnvironmentPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertiesTests method loadWhenHasUnboundElementsFromSystemEnvironmentShouldNotThrowException.

@Test
void loadWhenHasUnboundElementsFromSystemEnvironmentShouldNotThrowException() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    sources.addFirst(new MapPropertySource("test", Collections.singletonMap("com.example.foo", 5)));
    sources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, Collections.singletonMap("COM_EXAMPLE_OTHER", "10")));
    load(SimplePrefixedProperties.class);
    SimplePrefixedProperties bean = this.context.getBean(SimplePrefixedProperties.class);
    assertThat(bean.getFoo()).isEqualTo(5);
}
Also used : SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) PropertySources(cn.taketoday.core.env.PropertySources) MapPropertySource(cn.taketoday.core.env.MapPropertySource) Test(org.junit.jupiter.api.Test)

Example 3 with SystemEnvironmentPropertySource

use of cn.taketoday.core.env.SystemEnvironmentPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertiesTests method loadWhenUsingSystemPropertiesShouldBindToMap.

@Test
void loadWhenUsingSystemPropertiesShouldBindToMap() {
    this.context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, Collections.singletonMap("TEST_MAP_FOO_BAR", "baz")));
    load(WithComplexMapProperties.class);
    WithComplexMapProperties bean = this.context.getBean(WithComplexMapProperties.class);
    assertThat(bean.getMap()).containsOnlyKeys("foo");
    assertThat(bean.getMap().get("foo")).containsOnly(entry("bar", "baz"));
}
Also used : SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) Test(org.junit.jupiter.api.Test)

Example 4 with SystemEnvironmentPropertySource

use of cn.taketoday.core.env.SystemEnvironmentPropertySource in project today-infrastructure by TAKETODAY.

the class CloudPlatformTests method getEnvironmentWithEnvVariables.

private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
    MockEnvironment environment = new MockEnvironment();
    PropertySource<?> propertySource = new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables);
    environment.getPropertySources().addFirst(propertySource);
    return environment;
}
Also used : SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) MockEnvironment(cn.taketoday.mock.env.MockEnvironment)

Example 5 with SystemEnvironmentPropertySource

use of cn.taketoday.core.env.SystemEnvironmentPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertySourcesTests method attachShouldAddAdapterAtBeginning.

@Test
void attachShouldAddAdapterAtBeginning() {
    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources sources = environment.getPropertySources();
    sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
    sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
    int size = sources.size();
    ConfigurationPropertySources.attach(environment);
    assertThat(sources.size()).isEqualTo(size + 1);
    PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
    assertThat(resolver.getProperty("server.port")).isEqualTo("1234");
}
Also used : PropertySourcesPropertyResolver(cn.taketoday.core.env.PropertySourcesPropertyResolver) SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MapPropertySource(cn.taketoday.core.env.MapPropertySource) PropertySourcesPropertyResolver(cn.taketoday.core.env.PropertySourcesPropertyResolver) PropertyResolver(cn.taketoday.core.env.PropertyResolver) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

SystemEnvironmentPropertySource (cn.taketoday.core.env.SystemEnvironmentPropertySource)39 Test (org.junit.jupiter.api.Test)32 PropertySources (cn.taketoday.core.env.PropertySources)22 MapPropertySource (cn.taketoday.core.env.MapPropertySource)15 LinkedHashMap (java.util.LinkedHashMap)7 MockEnvironment (cn.taketoday.mock.env.MockEnvironment)6 Map (java.util.Map)5 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)4 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)4 PropertyResolver (cn.taketoday.core.env.PropertyResolver)2 PropertySourcesPropertyResolver (cn.taketoday.core.env.PropertySourcesPropertyResolver)2 Application (cn.taketoday.framework.Application)2 HashMap (java.util.HashMap)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2