Search in sources :

Example 1 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToMultiConstructorConfigurationPropertiesUsingShortcutSyntax.

@Test
// gh-18485
void loadWhenBindingToMultiConstructorConfigurationPropertiesUsingShortcutSyntax() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested[0]", "spring");
    sources.addLast(new MapPropertySource("test", source));
    load(MultiConstructorConfigurationPropertiesConfiguration.class);
    MultiConstructorConfigurationListProperties bean = this.context.getBean(MultiConstructorConfigurationListProperties.class);
    MultiConstructorConfigurationProperties nested = bean.getNested().get(0);
    assertThat(nested.getName()).isEqualTo("spring");
    assertThat(nested.getAge()).isEqualTo(0);
}
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 2 with MapPropertySource

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

the class ConfigurationPropertiesTests method loadWhenBindingToMultiConstructorConfigurationProperties.

@Test
// gh-18485
void loadWhenBindingToMultiConstructorConfigurationProperties() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.nested[0].name", "spring");
    source.put("test.nested[0].age", "5");
    sources.addLast(new MapPropertySource("test", source));
    load(MultiConstructorConfigurationPropertiesConfiguration.class);
    MultiConstructorConfigurationListProperties bean = this.context.getBean(MultiConstructorConfigurationListProperties.class);
    MultiConstructorConfigurationProperties nested = bean.getNested().get(0);
    assertThat(nested.getName()).isEqualTo("spring");
    assertThat(nested.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 3 with MapPropertySource

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

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

the class ConfigurationPropertiesTests method loadWhenBindingToConstructorParametersWithNotMatchingCustomDurationFormatShouldFail.

@Test
void loadWhenBindingToConstructorParametersWithNotMatchingCustomDurationFormatShouldFail() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    Map<String, Object> source = new HashMap<>();
    source.put("test.duration", "P12D");
    sources.addLast(new MapPropertySource("test", source));
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class)).havingCause().isInstanceOf(BindException.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) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) ParseException(java.text.ParseException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) BindException(cn.taketoday.context.properties.bind.BindException) BindValidationException(cn.taketoday.context.properties.bind.validation.BindValidationException) Test(org.junit.jupiter.api.Test)

Example 5 with MapPropertySource

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

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