Search in sources :

Example 6 with PropertySources

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

the class ConfigurationPropertiesTests method loadWhenHasPropertySourcesPlaceholderConfigurerShouldSupportRebindableConfigurationProperties.

@Test
void loadWhenHasPropertySourcesPlaceholderConfigurerShouldSupportRebindableConfigurationProperties() {
    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.register(PropertySourcesPlaceholderConfigurer.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)

Example 7 with PropertySources

use of cn.taketoday.core.env.PropertySources 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 8 with PropertySources

use of cn.taketoday.core.env.PropertySources 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 9 with PropertySources

use of cn.taketoday.core.env.PropertySources 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 10 with PropertySources

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

Aggregations

PropertySources (cn.taketoday.core.env.PropertySources)139 Test (org.junit.jupiter.api.Test)104 MapPropertySource (cn.taketoday.core.env.MapPropertySource)78 HashMap (java.util.HashMap)50 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)44 LinkedHashMap (java.util.LinkedHashMap)42 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)24 SystemEnvironmentPropertySource (cn.taketoday.core.env.SystemEnvironmentPropertySource)22 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)16 PropertySourcesPropertyResolver (cn.taketoday.core.env.PropertySourcesPropertyResolver)8 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 ApplicationContext (cn.taketoday.context.ApplicationContext)6 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)6 MockPropertySource (cn.taketoday.core.env.MockPropertySource)6 Map (java.util.Map)6 Properties (java.util.Properties)6 ConfigurationPropertySources (cn.taketoday.context.properties.source.ConfigurationPropertySources)5 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)4 BindException (cn.taketoday.context.properties.bind.BindException)4