From 8f5a753eff6b59270961edd109acd6cb3c19769b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doleci=C5=84ski?= Date: Mon, 14 Sep 2015 10:26:52 +0200 Subject: [PATCH 1/2] Fix propagation of local.mongo.port up the context hierarchy Previously, a StackOverflowError would occur when using a random port for embedded mongo as the logic for propagating the property up the context hierarchy would repeatedly use the leaf context's parent. This commit updates the logic to look to see if the current context has a parent, only calling the method again if it does. Closes gh-3956 --- .../mongo/embedded/EmbeddedMongoAutoConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index b0916b3311..6e7cd0fba6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -173,8 +173,8 @@ public class EmbeddedMongoAutoConfiguration { } map.put("local.mongo.port", port); } - if (this.context.getParent() != null) { - setPortProperty(this.context.getParent(), port); + if (context.getParent() != null) { + setPortProperty(context.getParent(), port); } } From 2fd8a581977e8a82250b1f92f412c7ebd09cbcea Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 15 Sep 2015 06:55:34 -0400 Subject: [PATCH 2/2] Polish contribution - Rename local variable to avoid shadowing field with the same name - Add a test to verify that local.mongo.port is set on the parent context Closes gh-3955 --- .../EmbeddedMongoAutoConfiguration.java | 10 ++++---- .../EmbeddedMongoAutoConfigurationTests.java | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index 6e7cd0fba6..bed967c385 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -154,9 +154,9 @@ public class EmbeddedMongoAutoConfiguration { setPortProperty(this.context, port); } - private void setPortProperty(ApplicationContext context, int port) { - if (context instanceof ConfigurableApplicationContext) { - ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context) + private void setPortProperty(ApplicationContext currentContext, int port) { + if (currentContext instanceof ConfigurableApplicationContext) { + ConfigurableEnvironment environment = ((ConfigurableApplicationContext) currentContext) .getEnvironment(); MutablePropertySources sources = environment.getPropertySources(); Map map; @@ -173,8 +173,8 @@ public class EmbeddedMongoAutoConfiguration { } map.put("local.mongo.port", port); } - if (context.getParent() != null) { - setPortProperty(context.getParent(), port); + if (currentContext.getParent() != null) { + setPortProperty(currentContext.getParent(), port); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java index 1d01c6d527..eab858ec98 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -38,6 +39,8 @@ import de.flapdoodle.embed.mongo.distribution.Feature; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; /** @@ -93,6 +96,27 @@ public class EmbeddedMongoAutoConfigurationTests { "local.mongo.port")))); } + @Test + public void portIsAvailableInParentContext() { + ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext(); + parent.refresh(); + try { + this.context = new AnnotationConfigApplicationContext(); + this.context.setParent(parent); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.data.mongodb.port=0"); + this.context.register(EmbeddedMongoAutoConfiguration.class, + MongoClientConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertThat(parent.getEnvironment().getProperty("local.mongo.port"), + is(notNullValue())); + } + finally { + parent.close(); + } + } + private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext();