Search in sources :

Example 11 with MockPropertySource

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

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesLocalProperties.

@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
    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.setProperties(new Properties() {

        {
            put("my.name", "local");
        }
    });
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("${my.name}");
}
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) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 12 with MockPropertySource

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

the class PropertySourcesPlaceholderConfigurerTests method ignoreUnresolvablePlaceholdersInAtValueAnnotation__falseIsDefault.

@Test
public // https://github.com/spring-projects/spring-framework/issues/27947
void ignoreUnresolvablePlaceholdersInAtValueAnnotation__falseIsDefault() {
    MockPropertySource mockPropertySource = new MockPropertySource("test");
    mockPropertySource.setProperty("my.key", "${enigma}");
    @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().getPropertySources().addLast(mockPropertySource);
    context.register(IgnoreUnresolvablePlaceholdersFalseConfig.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(context::refresh).havingCause().isExactlyInstanceOf(IllegalArgumentException.class).withMessage("Could not resolve placeholder 'enigma' in value \"${enigma}\"");
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 13 with MockPropertySource

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

Example 14 with MockPropertySource

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

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

the class PropertySourcesPlaceholderConfigurerTests method ignoreUnresolvablePlaceholdersInAtValueAnnotation__falseIsDefault.

@Test
public // https://github.com/spring-projects/spring-framework/issues/27947
void ignoreUnresolvablePlaceholdersInAtValueAnnotation__falseIsDefault() {
    MockPropertySource mockPropertySource = new MockPropertySource("test");
    mockPropertySource.setProperty("my.key", "${enigma}");
    @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().getPropertySources().addLast(mockPropertySource);
    context.register(IgnoreUnresolvablePlaceholdersFalseConfig.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(context::refresh).havingCause().isExactlyInstanceOf(IllegalArgumentException.class).withMessage("Could not resolve placeholder 'enigma' in value \"${enigma}\"");
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) 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