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);