From 618eef147f834a260640c622a22a11c646c2f4e0 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 22 Aug 2016 09:35:34 +0100 Subject: [PATCH] Additional tests for placeholder replacement It turns out that the mechanism we use to convert properties to YAML (and JSON) means that placeholders are always replaced if there are defaults (e.g. ${foo:bar} always resolves to "bar" if "foo" is undefined). This seems soprt of reasonable, if a little surprising. We could change the way YAML and JSON are generated, but that would be a big deal, and probably not worth the effort to fix a bit of slightly surprising behaviour. --- ...EnvironmentControllerIntegrationTests.java | 6 +-- .../EnvironmentControllerTests.java | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java index eba498e4..949867ae 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java @@ -23,14 +23,13 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.environment.EnvironmentControllerIntegrationTests.ControllerConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -44,8 +43,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; * @author Ivan Corrales Solera */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ControllerConfiguration.class) -@WebAppConfiguration +@SpringBootTest(classes = ControllerConfiguration.class) public class EnvironmentControllerIntegrationTests { @Autowired diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java index 3c5ee24a..d429e7c9 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java @@ -127,6 +127,21 @@ public class EnvironmentControllerTests { assertEquals("a:\n b:\n c: ${foo}\n", yaml); } + @Test + public void placeholdersNotResolvedInYamlFromSystemPropertiesWhenNotFlagged() throws Exception { + whenPlaceholdersSystemProps(); + String yaml = this.controller.yaml("foo", "bar", false).getBody(); + assertEquals("a:\n b:\n c: ${foo}\n", yaml); + } + + @Test + public void placeholdersNotResolvedInYamlFromSystemPropertiesWhenNotFlaggedWithDefault() throws Exception { + whenPlaceholdersSystemPropsWithDefault(); + String yaml = this.controller.yaml("foo", "bar", false).getBody(); + // If there is a default value we can't prevent the placeholder being resolved + assertEquals("a:\n b:\n c: spam\n", yaml); + } + @Test public void arrayInYaml() throws Exception { Map map = new LinkedHashMap(); @@ -277,6 +292,20 @@ public class EnvironmentControllerTests { assertEquals("a.b.c: ${foo}", text); } + @Test + public void placeholdersNotResolvedInPropertiesFromSystemPropertiesWhenNotFlagged() throws Exception { + whenPlaceholdersSystemProps(); + String text = this.controller.properties("foo", "bar", false).getBody(); + assertEquals("a.b.c: ${foo}", text); + } + + @Test + public void placeholdersNotResolvedInPropertiesFromSystemPropertiesWhenNotFlaggedWithDefault() throws Exception { + whenPlaceholdersSystemPropsWithDefault(); + String text = this.controller.properties("foo", "bar", false).getBody(); + assertEquals("a.b.c: ${foo:spam}", text); + } + @Test public void placeholdersResolvedInJson() throws Exception { whenPlaceholders(); @@ -298,6 +327,21 @@ public class EnvironmentControllerTests { assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}}}", json); } + @Test + public void placeholdersNotResolvedInJsonFromSystemPropertiesWhenNotFlagged() throws Exception { + whenPlaceholdersSystemProps(); + String json = this.controller.jsonProperties("foo", "bar", false).getBody(); + assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}}}", json); + } + + @Test + public void placeholdersResolvedInJsonFromSystemPropertiesWhenNotFlaggedWithDefault() throws Exception { + whenPlaceholdersSystemPropsWithDefault(); + String json = this.controller.jsonProperties("foo", "bar", false).getBody(); + // If there is a default value we can't prevent the placeholder being resolved + assertEquals("{\"a\":{\"b\":{\"c\":\"spam\"}}}", json); + } + private void whenPlaceholders() { Map map = new LinkedHashMap(); map.put("foo", "bar"); @@ -312,6 +356,12 @@ public class EnvironmentControllerTests { Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment); } + private void whenPlaceholdersSystemPropsWithDefault() { + System.setProperty("foo", "bar"); + this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo:spam}"))); + Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment); + } + @Test public void mappingForEnvironment() throws Exception { Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);