From 2fd8a581977e8a82250b1f92f412c7ebd09cbcea Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 15 Sep 2015 06:55:34 -0400 Subject: [PATCH] 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();