Skip to content

Commit 53844b0

Browse files
committed
Test conversion support in PropertySourcesPlaceholderConfigurer
This commit introduces a @⁠Disabled "regression test" which demonstrates that PropertySourcesPlaceholderConfigurer uses the ConversionService from the Environment. See spring-projectsgh-34936
1 parent d782647 commit 53844b0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.junit.jupiter.api.AfterAll;
2727
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Disabled;
2829
import org.junit.jupiter.api.Nested;
2930
import org.junit.jupiter.api.Test;
3031
import org.junit.jupiter.params.ParameterizedTest;
@@ -41,6 +42,8 @@
4142
import org.springframework.context.annotation.Configuration;
4243
import org.springframework.context.annotation.Scope;
4344
import org.springframework.core.SpringProperties;
45+
import org.springframework.core.convert.ConversionService;
46+
import org.springframework.core.convert.converter.Converter;
4447
import org.springframework.core.convert.support.DefaultConversionService;
4548
import org.springframework.core.env.AbstractPropertyResolver;
4649
import org.springframework.core.env.EnumerablePropertySource;
@@ -90,6 +93,39 @@ void replacementFromEnvironmentProperties() {
9093
assertThat(ppc.getAppliedPropertySources()).isNotNull();
9194
}
9295

96+
/**
97+
* Ensure that a {@link Converter} registered in the {@link ConversionService}
98+
* used by the {@code Environment} is applied during placeholder resolution
99+
* against a {@link PropertySource} registered in the {@code Environment}.
100+
*/
101+
@Disabled("Disabled until gh-34936 is resolved")
102+
@Test // gh-34936
103+
void replacementFromEnvironmentPropertiesWithConversion() {
104+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
105+
bf.registerBeanDefinition("testBean",
106+
genericBeanDefinition(TestBean.class)
107+
.addPropertyValue("name", "${my.name}")
108+
.getBeanDefinition());
109+
110+
record Point(int x, int y) {
111+
}
112+
113+
Converter<Point, String> pointToStringConverter =
114+
point -> "(%d,%d)".formatted(point.x, point.y);
115+
116+
DefaultConversionService conversionService = new DefaultConversionService();
117+
conversionService.addConverter(Point.class, String.class, pointToStringConverter);
118+
119+
MockEnvironment env = new MockEnvironment();
120+
env.setConversionService(conversionService);
121+
env.setProperty("my.name", new Point(4,5));
122+
123+
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
124+
ppc.setEnvironment(env);
125+
ppc.postProcessBeanFactory(bf);
126+
assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("(4,5)");
127+
}
128+
93129
/**
94130
* Ensure that a {@link PropertySource} added to the {@code Environment} after context
95131
* refresh (i.e., after {@link PropertySourcesPlaceholderConfigurer#postProcessBeanFactory()}

0 commit comments

Comments
 (0)