From 53844b068c8fa459d78c1ee6346b3c407cf627ee Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 26 May 2025 18:18:52 +0200 Subject: [PATCH] Test conversion support in PropertySourcesPlaceholderConfigurer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a @⁠Disabled "regression test" which demonstrates that PropertySourcesPlaceholderConfigurer uses the ConversionService from the Environment. See gh-34936 --- ...ertySourcesPlaceholderConfigurerTests.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java b/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java index d78bb08406..8e8cdef945 100644 --- a/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java @@ -25,6 +25,7 @@ import java.util.Properties; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -41,6 +42,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.core.SpringProperties; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.env.AbstractPropertyResolver; import org.springframework.core.env.EnumerablePropertySource; @@ -90,6 +93,39 @@ class PropertySourcesPlaceholderConfigurerTests { assertThat(ppc.getAppliedPropertySources()).isNotNull(); } + /** + * Ensure that a {@link Converter} registered in the {@link ConversionService} + * used by the {@code Environment} is applied during placeholder resolution + * against a {@link PropertySource} registered in the {@code Environment}. + */ + @Disabled("Disabled until gh-34936 is resolved") + @Test // gh-34936 + void replacementFromEnvironmentPropertiesWithConversion() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.registerBeanDefinition("testBean", + genericBeanDefinition(TestBean.class) + .addPropertyValue("name", "${my.name}") + .getBeanDefinition()); + + record Point(int x, int y) { + } + + Converter pointToStringConverter = + point -> "(%d,%d)".formatted(point.x, point.y); + + DefaultConversionService conversionService = new DefaultConversionService(); + conversionService.addConverter(Point.class, String.class, pointToStringConverter); + + MockEnvironment env = new MockEnvironment(); + env.setConversionService(conversionService); + env.setProperty("my.name", new Point(4,5)); + + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); + ppc.setEnvironment(env); + ppc.postProcessBeanFactory(bf); + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("(4,5)"); + } + /** * Ensure that a {@link PropertySource} added to the {@code Environment} after context * refresh (i.e., after {@link PropertySourcesPlaceholderConfigurer#postProcessBeanFactory()}