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()));
}
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());
}
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"));
}
}
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();
}
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"));
}
Aggregations