Placeholder configurers allow for trimming of property values
Issue: SPR-5839
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -18,14 +18,10 @@ package org.springframework.context.config;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
|
||||
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
@@ -53,9 +49,6 @@ public class ContextNamespaceHandlerTests {
|
||||
public void propertyPlaceholder() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-replace.xml", getClass());
|
||||
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
|
||||
.getBeansOfType(PlaceholderConfigurerSupport.class);
|
||||
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
|
||||
assertEquals("bar", applicationContext.getBean("string"));
|
||||
assertEquals("null", applicationContext.getBean("nullString"));
|
||||
}
|
||||
@@ -66,9 +59,6 @@ public class ContextNamespaceHandlerTests {
|
||||
try {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-system.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
|
||||
assertEquals("spam", applicationContext.getBean("string"));
|
||||
assertEquals("none", applicationContext.getBean("fallback"));
|
||||
}
|
||||
@@ -86,9 +76,6 @@ public class ContextNamespaceHandlerTests {
|
||||
applicationContext.setEnvironment(env);
|
||||
applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
|
||||
applicationContext.refresh();
|
||||
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
|
||||
.getBeansOfType(PlaceholderConfigurerSupport.class);
|
||||
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
|
||||
assertEquals("spam", applicationContext.getBean("string"));
|
||||
assertEquals("none", applicationContext.getBean("fallback"));
|
||||
}
|
||||
@@ -97,9 +84,6 @@ public class ContextNamespaceHandlerTests {
|
||||
public void propertyPlaceholderLocation() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-location.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
|
||||
assertEquals("bar", applicationContext.getBean("foo"));
|
||||
assertEquals("foo", applicationContext.getBean("bar"));
|
||||
assertEquals("maps", applicationContext.getBean("spam"));
|
||||
@@ -109,9 +93,6 @@ public class ContextNamespaceHandlerTests {
|
||||
public void propertyPlaceholderIgnored() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-replace-ignore.xml", getClass());
|
||||
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
|
||||
.getBeansOfType(PlaceholderConfigurerSupport.class);
|
||||
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
|
||||
assertEquals("${bar}", applicationContext.getBean("string"));
|
||||
assertEquals("null", applicationContext.getBean("nullString"));
|
||||
}
|
||||
@@ -120,9 +101,6 @@ public class ContextNamespaceHandlerTests {
|
||||
public void propertyOverride() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-override.xml", getClass());
|
||||
Map<String, PropertyOverrideConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyOverrideConfigurer.class);
|
||||
assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty());
|
||||
Date date = (Date) applicationContext.getBean("date");
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user