From cebda46469cefccc06c95a3e7d6b687f23bae698 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 16 Jul 2023 12:47:12 +0200 Subject: [PATCH] Polish PropertyPlaceholderConfigurerTests --- .../PropertyPlaceholderConfigurerTests.java | 62 ++++++++----------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java index 13d8b138ff..8be82a310c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java @@ -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", "@") @@ -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());