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:
parent
8f5a753eff
commit
2fd8a58197
|
|
@ -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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue