Placeholder configurers allow for trimming of property values

Issue: SPR-5839
This commit is contained in:
Juergen Hoeller
2016-02-19 19:19:39 +01:00
parent 890819f886
commit a7ad49adcf
10 changed files with 213 additions and 93 deletions

View File

@@ -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.
@@ -39,11 +39,10 @@ import static org.springframework.beans.factory.support.BeanDefinitionReaderUtil
/**
* Unit tests for {@link PropertyPlaceholderConfigurer}.
*
* @see PropertySourcesPlaceholderConfigurerTests
* @see PropertyResourceConfigurerTests
* @author Chris Beams
*/
public class PropertyPlaceholderConfigurerTests {
private static final String P1 = "p1";
private static final String P1_LOCAL_PROPS_VAL = "p1LocalPropsVal";
private static final String P1_SYSTEM_PROPS_VAL = "p1SystemPropsVal";
@@ -55,11 +54,12 @@ public class PropertyPlaceholderConfigurerTests {
private AbstractBeanDefinition p1BeanDef;
@Before
public void setUp() {
p1BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${"+P1+"}")
.getBeanDefinition();
.addPropertyValue("name", "${" + P1 + "}")
.getBeanDefinition();
bf = new DefaultListableBeanFactory();
@@ -97,9 +97,9 @@ public class PropertyPlaceholderConfigurerTests {
public void resolveFromSystemProperties() {
getModifiableSystemEnvironment().put("otherKey", "systemValue");
p1BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${"+P1+"}")
.addPropertyValue("sex", "${otherKey}")
.getBeanDefinition();
.addPropertyValue("name", "${" + P1 + "}")
.addPropertyValue("sex", "${otherKey}")
.getBeanDefinition();
registerWithGeneratedName(p1BeanDef, bf);
ppc.postProcessBeanFactory(bf);
TestBean bean = bf.getBean(TestBean.class);
@@ -167,9 +167,9 @@ public class PropertyPlaceholderConfigurerTests {
String P2_SYSTEM_ENV_VAL = "p2SystemEnvVal";
AbstractBeanDefinition p2BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${"+P1+"}")
.addPropertyValue("country", "${"+P2+"}")
.getBeanDefinition();
.addPropertyValue("name", "${" + P1 + "}")
.addPropertyValue("country", "${" + P2 + "}")
.getBeanDefinition();
bf.registerBeanDefinition("p1Bean", p1BeanDef);
bf.registerBeanDefinition("p2Bean", p2BeanDef);
@@ -238,6 +238,33 @@ public class PropertyPlaceholderConfigurerTests {
getModifiableSystemEnvironment().remove("my.name");
}
@Test
public void trimValuesIsOffByDefault() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
getModifiableSystemEnvironment().put("my.name", " myValue ");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue "));
getModifiableSystemEnvironment().remove("my.name");
}
@Test
public void trimValuesIsApplied() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setTrimValues(true);
getModifiableSystemEnvironment().put("my.name", " myValue ");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
getModifiableSystemEnvironment().remove("my.name");
}
@SuppressWarnings("unchecked")
private static Map<String, String> getModifiableSystemEnvironment() {
@@ -253,7 +280,8 @@ public class PropertyPlaceholderConfigurerTests {
if (obj != null && obj.getClass().getName().equals("java.lang.ProcessEnvironment$StringEnvironment")) {
return (Map<String, String>) obj;
}
} catch (Exception ex) {
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@@ -263,8 +291,9 @@ public class PropertyPlaceholderConfigurerTests {
Class<?> processEnvironmentClass;
try {
processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
} catch (Exception e) {
throw new RuntimeException(e);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
try {
@@ -272,10 +301,12 @@ public class PropertyPlaceholderConfigurerTests {
theCaseInsensitiveEnvironmentField.setAccessible(true);
Object obj = theCaseInsensitiveEnvironmentField.get(null);
return (Map<String, String>) obj;
} catch (NoSuchFieldException e) {
}
catch (NoSuchFieldException ex) {
// do nothing
} catch (Exception e) {
throw new RuntimeException(e);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
try {
@@ -283,10 +314,12 @@ public class PropertyPlaceholderConfigurerTests {
theEnvironmentField.setAccessible(true);
Object obj = theEnvironmentField.get(null);
return (Map<String, String>) obj;
} catch (NoSuchFieldException e) {
}
catch (NoSuchFieldException ex) {
// do nothing
} catch (Exception e) {
throw new RuntimeException(e);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
throw new IllegalStateException();