Search in sources :

Example 21 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method optionalPropertyWithoutValue.

@Test
public void optionalPropertyWithoutValue() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setConversionService(new DefaultConversionService());
    bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MockEnvironment env = new MockEnvironment();
    env.setProperty("my.name", "");
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setEnvironment(env);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.setNullValue("");
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.empty()));
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 22 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method nullValueIsPreserved.

@Test
public void nullValueIsPreserved() {
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setNullValue("customNull");
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "customNull"));
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), nullValue());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 23 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method localPropertiesOverride.

@SuppressWarnings("serial")
private void localPropertiesOverride(boolean override) {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${foo}").getBeanDefinition());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setLocalOverride(override);
    ppc.setProperties(new Properties() {

        {
            setProperty("foo", "local");
        }
    });
    ppc.setEnvironment(new MockEnvironment().withProperty("foo", "enclosing"));
    ppc.postProcessBeanFactory(bf);
    if (override) {
        assertThat(bf.getBean(TestBean.class).getName(), equalTo("local"));
    } else {
        assertThat(bf.getBean(TestBean.class).getName(), equalTo("enclosing"));
    }
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties)

Example 24 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class EnableMBeanExportConfigurationTests method testPlaceholderBased.

@Test
public void testPlaceholderBased() throws Exception {
    MockEnvironment env = new MockEnvironment();
    env.setProperty("serverName", "server");
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setEnvironment(env);
    context.register(PlaceholderBasedConfiguration.class);
    context.refresh();
    this.ctx = context;
    validateAnnotationTestBean();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.Test)

Example 25 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class TestPropertySourceUtilsTests method addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile.

@Test
public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
    ConfigurableEnvironment environment = new MockEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
    assertEquals(0, propertySources.size());
    String pair = "key = value";
    ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    when(resourceLoader.getResource(anyString())).thenReturn(resource);
    addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
    assertEquals(1, propertySources.size());
    assertEquals("value", environment.getProperty("key"));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Aggregations

MockEnvironment (org.springframework.mock.env.MockEnvironment)63 Test (org.junit.Test)53 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 TestBean (org.springframework.tests.sample.beans.TestBean)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 LoggingInitializationContext (org.springframework.boot.logging.LoggingInitializationContext)5 CoffeeNetConfigurationProperties (coffee.synyx.autoconfigure.CoffeeNetConfigurationProperties)4 URL (java.net.URL)4 Before (org.junit.Before)4 ServerProperties (org.springframework.boot.autoconfigure.web.ServerProperties)4 InetUtils (org.springframework.cloud.commons.util.InetUtils)4 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)4 Environment (org.springframework.core.env.Environment)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2