Search in sources :

Example 96 with MapPropertySource

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

the class ServerPortInfoApplicationContextInitializer method setPortProperty.

@SuppressWarnings("unchecked")
private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) {
    PropertySources sources = environment.getPropertySources();
    PropertySource<?> source = sources.get("server.ports");
    if (source == null) {
        source = new MapPropertySource("server.ports", new HashMap<>());
        sources.addFirst(source);
    }
    ((Map<String, Object>) source.getSource()).put(propertyName, port);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) HashMap(java.util.HashMap) MapPropertySource(cn.taketoday.core.env.MapPropertySource) HashMap(java.util.HashMap) Map(java.util.Map)

Example 97 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToNestedConstructorPropertiesWithDeducedNestedShouldBind.

@Test
// gh-18481
void loadWhenBindingToNestedConstructorPropertiesWithDeducedNestedShouldBind() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.name", "spring");
    source.put("test.nested.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(DeducedNestedConstructorPropertiesConfiguration.class);
    DeducedNestedConstructorProperties bean = this.context.getBean(DeducedNestedConstructorProperties.class);
    assertThat(bean.getName()).isEqualTo("spring");
    assertThat(bean.getNested().getAge()).isEqualTo(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 98 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenConstructorBindingWithOuterClassAndNestedAutowiredShouldThrowException.

@Test
void loadWhenConstructorBindingWithOuterClassAndNestedAutowiredShouldThrowException() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    assertThatExceptionOfType(ConfigurationPropertiesBindException.class).isThrownBy(() -> load(ConstructorBindingWithOuterClassConstructorBoundAndNestedAutowiredConfiguration.class));
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 99 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToConstructorParametersShouldBind.

@Test
void loadWhenBindingToConstructorParametersShouldBind() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.foo", "baz");
    source.put("test.bar", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(ConstructorParameterConfiguration.class);
    ConstructorParameterProperties bean = this.context.getBean(ConstructorParameterProperties.class);
    assertThat(bean.getFoo()).isEqualTo("baz");
    assertThat(bean.getBar()).isEqualTo(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 100 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenConstructorBindingWithOuterClassDeducedConstructorBound.

@Test
void loadWhenConstructorBindingWithOuterClassDeducedConstructorBound() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested.outer.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(ConstructorBindingWithOuterClassConstructorBoundConfiguration.class);
    ConstructorBindingWithOuterClassConstructorBoundProperties bean = this.context.getBean(ConstructorBindingWithOuterClassConstructorBoundProperties.class);
    assertThat(bean.getNested().getOuter().getAge()).isEqualTo(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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