Skip to content

Commit 95c8af4

Browse files
committed
Merge branch 'spring-projectsgh-3955'
2 parents b79ca61 + 2fd8a58 commit 95c8af4

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ private void publishPortInfo(int port) {
154154
setPortProperty(this.context, port);
155155
}
156156

157-
private void setPortProperty(ApplicationContext context, int port) {
158-
if (context instanceof ConfigurableApplicationContext) {
159-
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) context)
157+
private void setPortProperty(ApplicationContext currentContext, int port) {
158+
if (currentContext instanceof ConfigurableApplicationContext) {
159+
ConfigurableEnvironment environment = ((ConfigurableApplicationContext) currentContext)
160160
.getEnvironment();
161161
MutablePropertySources sources = environment.getPropertySources();
162162
Map<String, Object> map;
@@ -173,8 +173,8 @@ private void setPortProperty(ApplicationContext context, int port) {
173173
}
174174
map.put("local.mongo.port", port);
175175
}
176-
if (this.context.getParent() != null) {
177-
setPortProperty(this.context.getParent(), port);
176+
if (currentContext.getParent() != null) {
177+
setPortProperty(currentContext.getParent(), port);
178178
}
179179
}
180180

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
2727
import org.springframework.boot.test.EnvironmentTestUtils;
28+
import org.springframework.context.ConfigurableApplicationContext;
2829
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2930
import org.springframework.context.annotation.Bean;
3031
import org.springframework.context.annotation.Configuration;
@@ -38,6 +39,8 @@
3839

3940
import static org.hamcrest.Matchers.equalTo;
4041
import static org.hamcrest.Matchers.hasItems;
42+
import static org.hamcrest.Matchers.is;
43+
import static org.hamcrest.Matchers.notNullValue;
4144
import static org.junit.Assert.assertThat;
4245

4346
/**
@@ -93,6 +96,27 @@ public void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() {
9396
"local.mongo.port"))));
9497
}
9598

99+
@Test
100+
public void portIsAvailableInParentContext() {
101+
ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext();
102+
parent.refresh();
103+
try {
104+
this.context = new AnnotationConfigApplicationContext();
105+
this.context.setParent(parent);
106+
EnvironmentTestUtils.addEnvironment(this.context,
107+
"spring.data.mongodb.port=0");
108+
this.context.register(EmbeddedMongoAutoConfiguration.class,
109+
MongoClientConfiguration.class,
110+
PropertyPlaceholderAutoConfiguration.class);
111+
this.context.refresh();
112+
assertThat(parent.getEnvironment().getProperty("local.mongo.port"),
113+
is(notNullValue()));
114+
}
115+
finally {
116+
parent.close();
117+
}
118+
}
119+
96120
private void assertVersionConfiguration(String configuredVersion,
97121
String expectedVersion) {
98122
this.context = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)