Search in sources :

Example 11 with PropertySources

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

the class ConfigurationPropertiesTests method removeSystemProperties.

/**
 * Strict tests need a known set of properties so we remove system items which may be
 * environment specific.
 */
private void removeSystemProperties() {
    PropertySources sources = this.context.getEnvironment().getPropertySources();
    sources.remove("systemProperties");
    sources.remove("systemEnvironment");
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources)

Example 12 with PropertySources

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

Example 13 with PropertySources

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

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySources.

@Test
public void explicitPropertySources() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    PropertySources propertySources = new PropertySources();
    propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("foo");
    assertThat(propertySources.iterator().next()).isEqualTo(ppc.getAppliedPropertySources().iterator().next());
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 14 with PropertySources

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

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesEnvironment.

@Test
public void explicitPropertySourcesExcludesEnvironment() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    PropertySources propertySources = new PropertySources();
    propertySources.addLast(new MockPropertySource());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("${my.name}");
    assertThat(propertySources.iterator().next()).isEqualTo(ppc.getAppliedPropertySources().iterator().next());
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 15 with PropertySources

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

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