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
This commit is contained in:
Andy Wilkinson
2015-09-15 06:55:34 -04:00
parent 8f5a753eff
commit 2fd8a58197
2 changed files with 29 additions and 5 deletions

View File

@@ -154,9 +154,9 @@ public class EmbeddedMongoAutoConfiguration {
setPortProperty(this.context, port); setPortProperty(this.context, port);
} }
private void setPortProperty(ApplicationContext context, int port) { private void setPortProperty(ApplicationContext currentContext, int port) {
if (context instanceof ConfigurableApplicationContext) { if (currentContext instanceof ConfigurableApplicationContext) {
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context) ConfigurableEnvironment environment = ((ConfigurableApplicationContext) currentContext)
.getEnvironment(); .getEnvironment();
MutablePropertySources sources = environment.getPropertySources(); MutablePropertySources sources = environment.getPropertySources();
Map<String, Object> map; Map<String, Object> map;
@@ -173,8 +173,8 @@ public class EmbeddedMongoAutoConfiguration {
} }
map.put("local.mongo.port", port); map.put("local.mongo.port", port);
} }
if (context.getParent() != null) { if (currentContext.getParent() != null) {
setPortProperty(context.getParent(), port); setPortProperty(currentContext.getParent(), port);
} }
} }

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.equalTo;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
@@ -93,6 +96,27 @@ public class EmbeddedMongoAutoConfigurationTests {
"local.mongo.port")))); "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, private void assertVersionConfiguration(String configuredVersion,
String expectedVersion) { String expectedVersion) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();