|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2002-2013 the original author or authors.
|
|
|
|
|
* Copyright 2002-2016 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
@@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.context.support;
|
|
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
@@ -24,6 +25,7 @@ import org.junit.rules.ExpectedException;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
|
|
import org.springframework.core.convert.support.DefaultConversionService;
|
|
|
|
|
import org.springframework.core.env.MutablePropertySources;
|
|
|
|
|
import org.springframework.core.env.PropertySource;
|
|
|
|
|
import org.springframework.core.env.StandardEnvironment;
|
|
|
|
|
@@ -39,6 +41,7 @@ import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Chris Beams
|
|
|
|
|
* @author Juergen Hoeller
|
|
|
|
|
* @since 3.1
|
|
|
|
|
*/
|
|
|
|
|
public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
@@ -46,6 +49,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
@Rule
|
|
|
|
|
public ExpectedException thrown = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void replacementFromEnvironmentProperties() {
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
@@ -57,8 +61,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
MockEnvironment env = new MockEnvironment();
|
|
|
|
|
env.setProperty("my.name", "myValue");
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc =
|
|
|
|
|
new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setEnvironment(env);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
|
|
|
|
|
@@ -73,10 +76,10 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
Resource resource = new ClassPathResource("PropertySourcesPlaceholderConfigurerTests.properties", this.getClass());
|
|
|
|
|
pc.setLocation(resource);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
ppc.setLocation(resource);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -101,11 +104,11 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
MutablePropertySources propertySources = new MutablePropertySources();
|
|
|
|
|
propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setPropertySources(propertySources);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setPropertySources(propertySources);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
|
|
|
|
|
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
|
|
|
|
|
assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@@ -119,13 +122,13 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
MutablePropertySources propertySources = new MutablePropertySources();
|
|
|
|
|
propertySources.addLast(new MockPropertySource());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setPropertySources(propertySources);
|
|
|
|
|
pc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
|
|
|
|
|
pc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
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(), equalTo("${my.name}"));
|
|
|
|
|
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
|
|
|
|
|
assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@@ -140,17 +143,17 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
MutablePropertySources propertySources = new MutablePropertySources();
|
|
|
|
|
propertySources.addLast(new MockPropertySource());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setPropertySources(propertySources);
|
|
|
|
|
pc.setProperties(new Properties() {{
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setPropertySources(propertySources);
|
|
|
|
|
ppc.setProperties(new Properties() {{
|
|
|
|
|
put("my.name", "local");
|
|
|
|
|
}});
|
|
|
|
|
pc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
ppc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=BeanDefinitionStoreException.class)
|
|
|
|
|
@Test(expected = BeanDefinitionStoreException.class)
|
|
|
|
|
public void ignoreUnresolvablePlaceholders_falseIsDefault() {
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
bf.registerBeanDefinition("testBean",
|
|
|
|
|
@@ -158,9 +161,9 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
//pc.setIgnoreUnresolvablePlaceholders(false); // the default
|
|
|
|
|
pc.postProcessBeanFactory(bf); // should throw
|
|
|
|
|
ppc.postProcessBeanFactory(bf); // should throw
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@@ -171,13 +174,13 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=BeanDefinitionStoreException.class)
|
|
|
|
|
@Test(expected = BeanDefinitionStoreException.class)
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public void nestedUnresolvablePlaceholder() {
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
@@ -186,11 +189,11 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setProperties(new Properties() {{
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setProperties(new Properties() {{
|
|
|
|
|
put("my.name", "${bogus}");
|
|
|
|
|
}});
|
|
|
|
|
pc.postProcessBeanFactory(bf); // should throw
|
|
|
|
|
ppc.postProcessBeanFactory(bf); // should throw
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@@ -202,12 +205,12 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
pc.setProperties(new Properties() {{
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setProperties(new Properties() {{
|
|
|
|
|
put("my.name", "${bogus}");
|
|
|
|
|
}});
|
|
|
|
|
pc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
pc.postProcessBeanFactory(bf);
|
|
|
|
|
ppc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${bogus}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -295,6 +298,31 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), nullValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void trimValuesIsOffByDefault() {
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
ppc.setEnvironment(new MockEnvironment().withProperty("my.name", " myValue "));
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue "));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void trimValuesIsApplied() {
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setTrimValues(true);
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
|
|
|
|
|
.addPropertyValue("name", "${my.name}")
|
|
|
|
|
.getBeanDefinition());
|
|
|
|
|
ppc.setEnvironment(new MockEnvironment().withProperty("my.name", " myValue "));
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getAppliedPropertySourcesTooEarly() throws Exception {
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
@@ -308,7 +336,7 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ClassPathResource doesNotHave = new ClassPathResource("test.properties", getClass());
|
|
|
|
|
ClassPathResource setToTrue = new ClassPathResource("placeholder.properties", getClass());
|
|
|
|
|
ppc.setLocations(new Resource[] { doesNotHave, setToTrue });
|
|
|
|
|
ppc.setLocations(doesNotHave, setToTrue);
|
|
|
|
|
ppc.setIgnoreResourceNotFound(true);
|
|
|
|
|
ppc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
|
|
|
@@ -320,4 +348,57 @@ public class PropertySourcesPlaceholderConfigurerTests {
|
|
|
|
|
assertThat(bf.getBean(TestBean.class).isJedi(), equalTo(true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void optionalPropertyWithValue() {
|
|
|
|
|
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", "myValue");
|
|
|
|
|
|
|
|
|
|
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
|
ppc.setEnvironment(env);
|
|
|
|
|
ppc.setIgnoreUnresolvablePlaceholders(true);
|
|
|
|
|
ppc.postProcessBeanFactory(bf);
|
|
|
|
|
assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class OptionalTestBean {
|
|
|
|
|
|
|
|
|
|
private Optional<String> name;
|
|
|
|
|
|
|
|
|
|
public Optional<String> getName() {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setName(Optional<String> name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|