Search in sources :

Example 6 with MockPropertySource

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

use of cn.taketoday.core.env.MockPropertySource in project today-framework 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 8 with MockPropertySource

use of cn.taketoday.core.env.MockPropertySource in project today-framework 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 9 with MockPropertySource

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

the class PropertySourcesPlaceholderConfigurerTests method ignoreUnresolvablePlaceholdersInAtValueAnnotation_true.

@Test
public // https://github.com/spring-projects/spring-framework/issues/27947
void ignoreUnresolvablePlaceholdersInAtValueAnnotation_true() {
    MockPropertySource mockPropertySource = new MockPropertySource("test");
    mockPropertySource.setProperty("my.key", "${enigma}");
    @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().getPropertySources().addLast(mockPropertySource);
    context.register(IgnoreUnresolvablePlaceholdersTrueConfig.class);
    context.refresh();
    IgnoreUnresolvablePlaceholdersTrueConfig config = context.getBean(IgnoreUnresolvablePlaceholdersTrueConfig.class);
    assertThat(config.value).isEqualTo("${enigma}");
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 10 with MockPropertySource

use of cn.taketoday.core.env.MockPropertySource in project today-framework 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)

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