Search in sources :

Example 1 with MockPropertySource

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

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

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

the class EnvironmentAccessorTests method braceAccess.

@Test
public void braceAccess() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "#{environment['my.name']}").getBeanDefinition());
    GenericApplicationContext ctx = new GenericApplicationContext(bf);
    ctx.getEnvironment().getPropertySources().addFirst(new MockPropertySource().withProperty("my.name", "myBean"));
    ctx.refresh();
    assertThat(ctx.getBean(TestBean.class).getName()).isEqualTo("myBean");
    ctx.close();
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) 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 4 with MockPropertySource

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

the class AbstractApplicationEnvironmentTests method getActiveProfilesDoesNotResolveProperty.

@Test
void getActiveProfilesDoesNotResolveProperty() {
    StandardEnvironment environment = createEnvironment();
    new MockPropertySource().withProperty("", "");
    environment.getPropertySources().addFirst(new MockPropertySource().withProperty(AbstractEnvironment.KEY_ACTIVE_PROFILES, "test"));
    assertThat(environment.getActiveProfiles()).isEmpty();
}
Also used : MockPropertySource(cn.taketoday.core.env.MockPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 5 with MockPropertySource

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

the class AbstractApplicationEnvironmentTests method getDefaultProfilesDoesNotResolveProperty.

@Test
void getDefaultProfilesDoesNotResolveProperty() {
    StandardEnvironment environment = createEnvironment();
    new MockPropertySource().withProperty("", "");
    environment.getPropertySources().addFirst(new MockPropertySource().withProperty(AbstractEnvironment.KEY_DEFAULT_PROFILES, "test"));
    assertThat(environment.getDefaultProfiles()).containsExactly("default");
}
Also used : MockPropertySource(cn.taketoday.core.env.MockPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

MockPropertySource (cn.taketoday.core.env.MockPropertySource)16 Test (org.junit.jupiter.api.Test)16 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)8 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)8 PropertySources (cn.taketoday.core.env.PropertySources)6 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)4 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)4 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)2 Properties (java.util.Properties)2