From 2e83de58ecb9a45bea3760d7b15145fc6f0f4231 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 25 Apr 2017 13:04:55 -0700 Subject: [PATCH] Test placeholder expansion supports relaxed names A test to ensure that the updated configuration properties support correctly expands placeholders using mapped properties. Prior to the updated support an `application.properties` file of the form: my.prefix.some-url=http://github.com my.prefix.login-url=${my.prefix.some-url}/login Would not correctly expand the placeholder against the environment variable `MY_PREFIX_SOME_URL` Closes gh-2496 --- ...onPropertiesBindingPostProcessorTests.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java index 18838f6c06..8d6ceaf514 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.context.properties; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -41,6 +43,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.SystemEnvironmentPropertySource; import org.springframework.mock.env.MockEnvironment; import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.util.ReflectionTestUtils; @@ -362,6 +368,39 @@ public class ConfigurationPropertiesBindingPostProcessorTests { .containsEntry("foo", "bar"); } + @Test + public void overridingPropertiesInEnvShouldOverride() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + ConfigurableEnvironment env = this.context.getEnvironment(); + MutablePropertySources propertySources = env.getPropertySources(); + propertySources.addFirst(new SystemEnvironmentPropertySource("system", + Collections.singletonMap("COM_EXAMPLE_FOO", "10"))); + propertySources.addLast(new MapPropertySource("test", + Collections.singletonMap("com.example.foo", 5))); + this.context.register(TestConfiguration.class); + this.context.refresh(); + int foo = this.context.getBean(TestConfiguration.class).getFoo(); + assertThat(foo).isEqualTo(10); + } + + @Test + public void overridingPropertiesWithPlaceholderResolutionInEnvShouldOverride() + throws Exception { + this.context = new AnnotationConfigApplicationContext(); + ConfigurableEnvironment env = this.context.getEnvironment(); + MutablePropertySources propertySources = env.getPropertySources(); + propertySources.addFirst(new SystemEnvironmentPropertySource("system", + Collections.singletonMap("COM_EXAMPLE_BAR", "10"))); + Map source = new HashMap<>(); + source.put("com.example.bar", 5); + source.put("com.example.foo", "${com.example.bar}"); + propertySources.addLast(new MapPropertySource("test", source)); + this.context.register(TestConfiguration.class); + this.context.refresh(); + int foo = this.context.getBean(TestConfiguration.class).getFoo(); + assertThat(foo).isEqualTo(10); + } + private void assertBindingFailure(int errorCount) { try { this.context.refresh();