Search in sources :

Example 6 with MapPropertySource

use of cn.taketoday.core.env.MapPropertySource in project today-infrastructure 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 7 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToJavaBeanWithoutExplicitConstructorBindingOnNestedShouldUseSetterBasedBinding.

@Test
void loadWhenBindingToJavaBeanWithoutExplicitConstructorBindingOnNestedShouldUseSetterBasedBinding() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(JavaBeanNonDefaultConstructorPropertiesConfiguration.class);
    JavaBeanNonDefaultConstructorProperties bean = this.context.getBean(JavaBeanNonDefaultConstructorProperties.class);
    assertThat(bean.getNested().getAge()).isEqualTo(10);
}
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 8 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToJavaBeanWithNestedConstructorBindingShouldBind.

@Test
void loadWhenBindingToJavaBeanWithNestedConstructorBindingShouldBind() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(JavaBeanNestedConstructorBindingPropertiesConfiguration.class);
    JavaBeanNestedConstructorBindingProperties bean = this.context.getBean(JavaBeanNestedConstructorBindingProperties.class);
    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 9 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToNestedPropertiesWithSyntheticConstructorShouldBind.

@Test
void loadWhenBindingToNestedPropertiesWithSyntheticConstructorShouldBind() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested.age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(SyntheticConstructorPropertiesConfiguration.class);
    SyntheticNestedConstructorProperties bean = this.context.getBean(SyntheticNestedConstructorProperties.class);
    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 10 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadShouldSupportRebindableConfigurationProperties.

@Test
void loadShouldSupportRebindableConfigurationProperties() {
    // gh-9160
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new LinkedHashMap<>();
    source.put("example.one", "foo");
    sources.addFirst(new MapPropertySource("test-source", source));
    this.context.register(PrototypePropertiesConfiguration.class);
    this.context.refresh();
    PrototypeBean first = this.context.getBean(PrototypeBean.class);
    assertThat(first.getOne()).isEqualTo("foo");
    source.put("example.one", "bar");
    sources.addFirst(new MapPropertySource("extra", Collections.singletonMap("example.two", "baz")));
    PrototypeBean second = this.context.getBean(PrototypeBean.class);
    assertThat(second.getOne()).isEqualTo("bar");
    assertThat(second.getTwo()).isEqualTo("baz");
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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