pull/8621/head
Phillip Webb 8 years ago
parent bfae0d7739
commit 40b3372884

@ -76,19 +76,14 @@ class OriginTrackedYamlLoader extends YamlProcessor {
public Map<String, Object> load() { public Map<String, Object> load() {
final Map<String, Object> result = new LinkedHashMap<String, Object>(); final Map<String, Object> result = new LinkedHashMap<String, Object>();
process(new MatchCallback() { process((properties, map) -> {
@Override
public void process(Properties properties, Map<String, Object> map) {
result.putAll(getFlattenedMap(map)); result.putAll(getFlattenedMap(map));
}
}); });
return result; return result;
} }
/** /**
* {@link Constructor}. * {@link Constructor} that tracks property origins.
*/ */
private class OriginTrackingConstructor extends StrictMapAppenderConstructor { private class OriginTrackingConstructor extends StrictMapAppenderConstructor {
@ -100,14 +95,16 @@ class OriginTrackedYamlLoader extends YamlProcessor {
} }
} }
else if (node instanceof MappingNode) { else if (node instanceof MappingNode) {
List<NodeTuple> value = ((MappingNode) node).getValue(); replaceMappingNodeKeys((MappingNode) node);
List<NodeTuple> updatedValues = value.stream().map(nt -> new NodeTuple(KeyScalarNode.get(nt.getKeyNode()),
nt.getValueNode())).collect(Collectors.toList());
((MappingNode) node).setValue(updatedValues);
} }
return super.constructObject(node); return super.constructObject(node);
} }
private void replaceMappingNodeKeys(MappingNode node) {
node.setValue(node.getValue().stream().map(KeyScalarNode::get)
.collect(Collectors.toList()));
}
private Object constructTrackedObject(Node node, Object value) { private Object constructTrackedObject(Node node, Object value) {
PropertyOrigin origin = getOrigin(node); PropertyOrigin origin = getOrigin(node);
return OriginTrackedValue.of(value, origin); return OriginTrackedValue.of(value, origin);
@ -128,7 +125,14 @@ class OriginTrackedYamlLoader extends YamlProcessor {
private static class KeyScalarNode extends ScalarNode { private static class KeyScalarNode extends ScalarNode {
KeyScalarNode(ScalarNode node) { KeyScalarNode(ScalarNode node) {
super(node.getTag(), node.getValue(), node.getStartMark(), node.getEndMark(), node.getStyle()); super(node.getTag(), node.getValue(), node.getStartMark(), node.getEndMark(),
node.getStyle());
}
public static NodeTuple get(NodeTuple nodeTuple) {
Node keyNode = nodeTuple.getKeyNode();
Node valueNode = nodeTuple.getValueNode();
return new NodeTuple(KeyScalarNode.get(keyNode), valueNode);
} }
private static Node get(Node node) { private static Node get(Node node) {
@ -155,6 +159,10 @@ class OriginTrackedYamlLoader extends YamlProcessor {
} }
/**
* {@link SpringProfileDocumentMatcher} that deals with {@link OriginTrackedValue
* OriginTrackedValues}.
*/
private static class OriginTrackedSpringProfileDocumentMatcher private static class OriginTrackedSpringProfileDocumentMatcher
extends SpringProfileDocumentMatcher { extends SpringProfileDocumentMatcher {

@ -29,7 +29,7 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testutil.MockServlet; import org.springframework.boot.testutil.MockServlet;
import org.springframework.boot.web.servlet.context.config.ExampleEmbeddedWebApplicationConfiguration; import org.springframework.boot.web.servlet.context.config.ExampleServletWebServerApplicationConfiguration;
import org.springframework.boot.web.servlet.server.MockServletWebServerFactory; import org.springframework.boot.web.servlet.server.MockServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -55,14 +55,15 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
@Test @Test
public void createFromScan() throws Exception { public void createFromScan() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext( this.context = new AnnotationConfigServletWebServerApplicationContext(
ExampleEmbeddedWebApplicationConfiguration.class.getPackage().getName()); ExampleServletWebServerApplicationConfiguration.class.getPackage()
.getName());
verifyContext(); verifyContext();
} }
@Test @Test
public void sessionScopeAvailable() throws Exception { public void sessionScopeAvailable() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext( this.context = new AnnotationConfigServletWebServerApplicationContext(
ExampleEmbeddedWebApplicationConfiguration.class, ExampleServletWebServerApplicationConfiguration.class,
SessionScopedComponent.class); SessionScopedComponent.class);
verifyContext(); verifyContext();
} }
@ -70,7 +71,7 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
@Test @Test
public void sessionScopeAvailableToServlet() throws Exception { public void sessionScopeAvailableToServlet() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext( this.context = new AnnotationConfigServletWebServerApplicationContext(
ExampleEmbeddedWebApplicationConfiguration.class, ExampleServletWebServerApplicationConfiguration.class,
ExampleServletWithAutowired.class, SessionScopedComponent.class); ExampleServletWithAutowired.class, SessionScopedComponent.class);
Servlet servlet = this.context.getBean(ExampleServletWithAutowired.class); Servlet servlet = this.context.getBean(ExampleServletWithAutowired.class);
assertThat(servlet).isNotNull(); assertThat(servlet).isNotNull();
@ -79,14 +80,14 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
@Test @Test
public void createFromConfigClass() throws Exception { public void createFromConfigClass() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext( this.context = new AnnotationConfigServletWebServerApplicationContext(
ExampleEmbeddedWebApplicationConfiguration.class); ExampleServletWebServerApplicationConfiguration.class);
verifyContext(); verifyContext();
} }
@Test @Test
public void registerAndRefresh() throws Exception { public void registerAndRefresh() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.register(ExampleEmbeddedWebApplicationConfiguration.class); this.context.register(ExampleServletWebServerApplicationConfiguration.class);
this.context.refresh(); this.context.refresh();
verifyContext(); verifyContext();
} }
@ -94,8 +95,8 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
@Test @Test
public void scanAndRefresh() throws Exception { public void scanAndRefresh() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.scan( this.context.scan(ExampleServletWebServerApplicationConfiguration.class
ExampleEmbeddedWebApplicationConfiguration.class.getPackage().getName()); .getPackage().getName());
this.context.refresh(); this.context.refresh();
verifyContext(); verifyContext();
} }

@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration;
* @author Phillip Webb * @author Phillip Webb
*/ */
@Configuration @Configuration
public class ExampleEmbeddedWebApplicationConfiguration { public class ExampleServletWebServerApplicationConfiguration {
@Bean @Bean
public MockServletWebServerFactory webServerFactory() { public MockServletWebServerFactory webServerFactory() {
Loading…
Cancel
Save