Search in sources :

Example 16 with MapPropertySource

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

the class ConfigurationPropertySourcesTests method getWhenAttachedShouldReturnAttached.

@Test
void getWhenAttachedShouldReturnAttached() {
    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources sources = environment.getPropertySources();
    sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
    int expectedSize = sources.size();
    ConfigurationPropertySources.attach(environment);
    assertThat(ConfigurationPropertySources.get(environment)).hasSize(expectedSize);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MapPropertySource(cn.taketoday.core.env.MapPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 17 with MapPropertySource

use of cn.taketoday.core.env.MapPropertySource 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)

Example 18 with MapPropertySource

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

the class ConfigurationPropertySourcesTests method fromPropertySourceShouldFlattenPropertySources.

@Test
void fromPropertySourceShouldFlattenPropertySources() {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
    environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
    PropertySources sources = new PropertySources();
    sources.addFirst(new PropertySource<Environment>("env", environment) {

        @Override
        public String getProperty(String key) {
            return this.source.getProperty(key);
        }
    });
    sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
    Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
    assertThat(configurationSources.iterator()).toIterable().hasSize(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Environment(cn.taketoday.core.env.Environment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 19 with MapPropertySource

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

the class ConfigurationPropertySourcesTests method attachShouldReattachInMergedSetup.

@Test
void attachShouldReattachInMergedSetup() {
    ConfigurableEnvironment parent = new StandardEnvironment();
    ConfigurationPropertySources.attach(parent);
    ConfigurableEnvironment child = new StandardEnvironment();
    child.merge(parent);
    child.getPropertySources().addLast(new MapPropertySource("config", Collections.singletonMap("my.example_property", "1234")));
    ConfigurationPropertySources.attach(child);
    assertThat(child.getProperty("my.example-property")).isEqualTo("1234");
}
Also used : ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MapPropertySource(cn.taketoday.core.env.MapPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 20 with MapPropertySource

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

the class DefaultConfigurationPropertySourceTests method getValueWhenOriginCapableShouldIncludeSourceOrigin.

@Test
void getValueWhenOriginCapableShouldIncludeSourceOrigin() {
    Map<String, Object> source = new LinkedHashMap<>();
    source.put("key", "value");
    PropertySource<?> propertySource = new OriginCapablePropertySource<>(new MapPropertySource("test", source));
    TestPropertyMapper mapper = new TestPropertyMapper();
    ConfigurationPropertyName name = ConfigurationPropertyName.of("my.key");
    mapper.addFromConfigurationProperty(name, "key");
    DefaultConfigurationPropertySource adapter = new DefaultConfigurationPropertySource(propertySource, mapper);
    assertThat(adapter.getConfigurationProperty(name).getOrigin().toString()).isEqualTo("TestOrigin key");
}
Also used : MapPropertySource(cn.taketoday.core.env.MapPropertySource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

MapPropertySource (cn.taketoday.core.env.MapPropertySource)145 Test (org.junit.jupiter.api.Test)120 PropertySources (cn.taketoday.core.env.PropertySources)78 LinkedHashMap (java.util.LinkedHashMap)67 HashMap (java.util.HashMap)54 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)42 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)20 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)16 SystemEnvironmentPropertySource (cn.taketoday.core.env.SystemEnvironmentPropertySource)15 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)12 ArrayList (java.util.ArrayList)8 InvalidConfigurationPropertyValueException (cn.taketoday.context.properties.source.InvalidConfigurationPropertyValueException)6 MutuallyExclusiveConfigurationPropertiesException (cn.taketoday.context.properties.source.MutuallyExclusiveConfigurationPropertiesException)6 PropertySourcesPropertyResolver (cn.taketoday.core.env.PropertySourcesPropertyResolver)6 MockEnvironment (cn.taketoday.mock.env.MockEnvironment)6 Map (java.util.Map)5 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)4 BindException (cn.taketoday.context.properties.bind.BindException)4 BindValidationException (cn.taketoday.context.properties.bind.validation.BindValidationException)4 Environment (cn.taketoday.core.env.Environment)4