Polish PropertyPlaceholderConfigurerTests

This commit is contained in:
Sam Brannen
2023-07-16 12:47:12 +02:00
parent f01fb19318
commit cebda46469

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@@ -37,46 +37,42 @@ import static org.springframework.beans.factory.support.BeanDefinitionReaderUtil
* Unit tests for {@link PropertyPlaceholderConfigurer}.
*
* @author Chris Beams
* @author Sam Brannen
*/
@SuppressWarnings("deprecation")
public class PropertyPlaceholderConfigurerTests {
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";
private DefaultListableBeanFactory bf;
private PropertyPlaceholderConfigurer ppc;
private Properties ppcProperties;
private final DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
private AbstractBeanDefinition p1BeanDef;
private final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
private final Properties ppcProperties = new Properties();
private AbstractBeanDefinition p1BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${" + P1 + "}")
.getBeanDefinition();
@BeforeEach
public void setup() {
p1BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${" + P1 + "}")
.getBeanDefinition();
bf = new DefaultListableBeanFactory();
ppcProperties = new Properties();
void setup() {
ppcProperties.setProperty(P1, P1_LOCAL_PROPS_VAL);
System.setProperty(P1, P1_SYSTEM_PROPS_VAL);
ppc = new PropertyPlaceholderConfigurer();
ppc.setProperties(ppcProperties);
}
@AfterEach
public void cleanup() {
void cleanup() {
System.clearProperty(P1);
System.clearProperty(P1_SYSTEM_PROPS_VAL);
}
@Test
public void localPropertiesViaResource() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
void localPropertiesViaResource() {
bf.registerBeanDefinition("testBean",
genericBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
@@ -89,7 +85,7 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void resolveFromSystemProperties() {
void resolveFromSystemProperties() {
System.setProperty("otherKey", "systemValue");
p1BeanDef = rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${" + P1 + "}")
@@ -104,7 +100,7 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void resolveFromLocalProperties() {
void resolveFromLocalProperties() {
System.clearProperty(P1);
registerWithGeneratedName(p1BeanDef, bf);
ppc.postProcessBeanFactory(bf);
@@ -113,7 +109,7 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void setSystemPropertiesMode_defaultIsFallback() {
void setSystemPropertiesMode_defaultIsFallback() {
registerWithGeneratedName(p1BeanDef, bf);
ppc.postProcessBeanFactory(bf);
TestBean bean = bf.getBean(TestBean.class);
@@ -121,7 +117,7 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemProperties() {
void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemProperties() {
registerWithGeneratedName(p1BeanDef, bf);
ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
ppc.postProcessBeanFactory(bf);
@@ -130,7 +126,7 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void setSystemSystemPropertiesMode_toOverride_andSetSearchSystemEnvironment_toFalse() {
void setSystemSystemPropertiesMode_toOverride_andSetSearchSystemEnvironment_toFalse() {
registerWithGeneratedName(p1BeanDef, bf);
System.clearProperty(P1); // will now fall all the way back to system environment
ppc.setSearchSystemEnvironment(false);
@@ -145,7 +141,7 @@ public class PropertyPlaceholderConfigurerTests {
* settings regarding resolving properties from the environment.
*/
@Test
public void twoPlaceholderConfigurers_withConflictingSettings() {
void twoPlaceholderConfigurers_withConflictingSettings() {
String P2 = "p2";
String P2_LOCAL_PROPS_VAL = "p2LocalPropsVal";
String P2_SYSTEM_PROPS_VAL = "p2SystemPropsVal";
@@ -184,12 +180,10 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void customPlaceholderPrefixAndSuffix() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
void customPlaceholderPrefixAndSuffix() {
ppc.setPlaceholderPrefix("@<");
ppc.setPlaceholderSuffix(">");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean",
rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "@<key1>")
@@ -207,11 +201,9 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void nullValueIsPreserved() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
void nullValueIsPreserved() {
ppc.setNullValue("customNull");
System.setProperty("my.name", "customNull");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
@@ -221,10 +213,8 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void trimValuesIsOffByDefault() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
void trimValuesIsOffByDefault() {
System.setProperty("my.name", " myValue ");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
@@ -234,11 +224,9 @@ public class PropertyPlaceholderConfigurerTests {
}
@Test
public void trimValuesIsApplied() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
void trimValuesIsApplied() {
ppc.setTrimValues(true);
System.setProperty("my.name", " myValue ");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());