|
16 | 16 |
|
17 | 17 | package org.springframework.context.support;
|
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
19 | 22 | import java.util.Optional;
|
20 | 23 | import java.util.Properties;
|
21 | 24 |
|
22 | 25 | import org.junit.jupiter.api.Nested;
|
23 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
24 | 29 |
|
25 | 30 | import org.springframework.beans.factory.BeanCreationException;
|
26 | 31 | import org.springframework.beans.factory.BeanDefinitionStoreException;
|
|
32 | 37 | import org.springframework.context.annotation.Bean;
|
33 | 38 | import org.springframework.context.annotation.Configuration;
|
34 | 39 | import org.springframework.core.convert.support.DefaultConversionService;
|
| 40 | +import org.springframework.core.env.EnumerablePropertySource; |
35 | 41 | import org.springframework.core.env.MutablePropertySources;
|
36 | 42 | import org.springframework.core.env.PropertySource;
|
37 | 43 | import org.springframework.core.env.StandardEnvironment;
|
@@ -90,14 +96,29 @@ void localPropertiesViaResource() {
|
90 | 96 | assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("foo");
|
91 | 97 | }
|
92 | 98 |
|
93 |
| - @Test |
94 |
| - void localPropertiesOverrideFalse() { |
95 |
| - localPropertiesOverride(false); |
96 |
| - } |
| 99 | + @ParameterizedTest |
| 100 | + @ValueSource(booleans = {true, false}) |
| 101 | + void localPropertiesOverride(boolean override) { |
| 102 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 103 | + bf.registerBeanDefinition("testBean", |
| 104 | + genericBeanDefinition(TestBean.class) |
| 105 | + .addPropertyValue("name", "${foo}") |
| 106 | + .getBeanDefinition()); |
97 | 107 |
|
98 |
| - @Test |
99 |
| - void localPropertiesOverrideTrue() { |
100 |
| - localPropertiesOverride(true); |
| 108 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 109 | + |
| 110 | + ppc.setLocalOverride(override); |
| 111 | + ppc.setProperties(new Properties() {{ |
| 112 | + setProperty("foo", "local"); |
| 113 | + }}); |
| 114 | + ppc.setEnvironment(new MockEnvironment().withProperty("foo", "enclosing")); |
| 115 | + ppc.postProcessBeanFactory(bf); |
| 116 | + if (override) { |
| 117 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("local"); |
| 118 | + } |
| 119 | + else { |
| 120 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("enclosing"); |
| 121 | + } |
101 | 122 | }
|
102 | 123 |
|
103 | 124 | @Test
|
@@ -283,28 +304,58 @@ public Object getProperty(String key) {
|
283 | 304 | assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("bar");
|
284 | 305 | }
|
285 | 306 |
|
286 |
| - @SuppressWarnings("serial") |
287 |
| - private void localPropertiesOverride(boolean override) { |
| 307 | + @Test // gh-34861 |
| 308 | + void withEnumerableAndNonEnumerablePropertySourcesInTheEnvironmentAndLocalProperties() { |
288 | 309 | DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
289 | 310 | bf.registerBeanDefinition("testBean",
|
290 | 311 | genericBeanDefinition(TestBean.class)
|
291 |
| - .addPropertyValue("name", "${foo}") |
| 312 | + .addPropertyValue("name", "${foo:bogus}") |
| 313 | + .addPropertyValue("jedi", "${local:false}") |
292 | 314 | .getBeanDefinition());
|
293 | 315 |
|
294 |
| - PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 316 | + // 1) MockPropertySource is an EnumerablePropertySource. |
| 317 | + MockPropertySource mockPropertySource = new MockPropertySource("mockPropertySource") |
| 318 | + .withProperty("foo", "${bar}"); |
295 | 319 |
|
296 |
| - ppc.setLocalOverride(override); |
| 320 | + // 2) PropertySource is not an EnumerablePropertySource. |
| 321 | + PropertySource<?> rawPropertySource = new PropertySource<>("rawPropertySource", new Object()) { |
| 322 | + @Override |
| 323 | + public Object getProperty(String key) { |
| 324 | + return ("bar".equals(key) ? "quux" : null); |
| 325 | + } |
| 326 | + }; |
| 327 | + |
| 328 | + MockEnvironment env = new MockEnvironment(); |
| 329 | + env.getPropertySources().addFirst(mockPropertySource); |
| 330 | + env.getPropertySources().addLast(rawPropertySource); |
| 331 | + |
| 332 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 333 | + ppc.setEnvironment(env); |
| 334 | + // 3) Local properties are stored in a PropertiesPropertySource which is an EnumerablePropertySource. |
297 | 335 | ppc.setProperties(new Properties() {{
|
298 |
| - setProperty("foo", "local"); |
| 336 | + setProperty("local", "true"); |
299 | 337 | }});
|
300 |
| - ppc.setEnvironment(new MockEnvironment().withProperty("foo", "enclosing")); |
301 | 338 | ppc.postProcessBeanFactory(bf);
|
302 |
| - if (override) { |
303 |
| - assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("local"); |
304 |
| - } |
305 |
| - else { |
306 |
| - assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("enclosing"); |
| 339 | + |
| 340 | + // Verify all properties can be resolved via the Environment. |
| 341 | + assertThat(env.getProperty("foo")).isEqualTo("quux"); |
| 342 | + assertThat(env.getProperty("bar")).isEqualTo("quux"); |
| 343 | + |
| 344 | + // Verify that placeholder resolution works. |
| 345 | + TestBean testBean = bf.getBean(TestBean.class); |
| 346 | + assertThat(testBean.getName()).isEqualTo("quux"); |
| 347 | + assertThat(testBean.isJedi()).isTrue(); |
| 348 | + |
| 349 | + // Verify that the presence of a non-EnumerablePropertySource does not prevent |
| 350 | + // accessing EnumerablePropertySources via getAppliedPropertySources(). |
| 351 | + List<String> propertyNames = new ArrayList<>(); |
| 352 | + for (PropertySource<?> propertySource : ppc.getAppliedPropertySources()) { |
| 353 | + if (propertySource instanceof EnumerablePropertySource<?> enumerablePropertySource) { |
| 354 | + Collections.addAll(propertyNames, enumerablePropertySource.getPropertyNames()); |
| 355 | + } |
307 | 356 | }
|
| 357 | + // Should not contain "foo" or "bar" from the Environment. |
| 358 | + assertThat(propertyNames).containsOnly("local"); |
308 | 359 | }
|
309 | 360 |
|
310 | 361 | @Test
|
|
0 commit comments