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.
This commit is contained in:
Dave Syer
2016-08-22 09:35:34 +01:00
parent f8fc4e1937
commit 618eef147f
2 changed files with 52 additions and 4 deletions

View File

@@ -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

View File

@@ -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<String, Object> map = new LinkedHashMap<String, Object>();
@@ -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<String, Object> map = new LinkedHashMap<String, Object>();
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);