pull/14681/merge
Andy Wilkinson 6 years ago
parent b4638b82d0
commit b473f2996d

@ -21,6 +21,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
/** /**
@ -29,9 +30,9 @@ import org.springframework.test.context.support.TestPropertySourceUtils;
* different port. * different port.
* *
* @author Madhura Bhave * @author Madhura Bhave
* @since 2.1.0 * @author Andy Wilkinson
*/ */
public class SpringBootTestRandomPortEnvironmentPostProcessor class SpringBootTestRandomPortEnvironmentPostProcessor
implements EnvironmentPostProcessor { implements EnvironmentPostProcessor {
private static final String MANAGEMENT_PORT_PROPERTY = "management.server.port"; private static final String MANAGEMENT_PORT_PROPERTY = "management.server.port";
@ -43,27 +44,29 @@ public class SpringBootTestRandomPortEnvironmentPostProcessor
SpringApplication application) { SpringApplication application) {
MapPropertySource source = (MapPropertySource) environment.getPropertySources() MapPropertySource source = (MapPropertySource) environment.getPropertySources()
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); .get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
if (isTestServerPortRandom(source)) { if (source == null || isTestServerPortFixed(source)
if (source.getProperty(MANAGEMENT_PORT_PROPERTY) == null) { || isTestManagementPortConfigured(source)) {
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, return;
null);
if (managementPort != null && !managementPort.equals("-1")) {
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY,
"8080");
if (!managementPort.equals(serverPort)) {
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
}
else {
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "");
}
}
}
} }
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null);
if (managementPort == null || managementPort.equals("-1")) {
return;
}
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080");
if (!managementPort.equals(serverPort)) {
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
}
else {
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "");
}
}
private boolean isTestServerPortFixed(MapPropertySource source) {
return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY));
} }
private boolean isTestServerPortRandom(MapPropertySource source) { private boolean isTestManagementPortConfigured(PropertySource<?> source) {
return (source != null && "0".equals(source.getProperty(SERVER_PORT_PROPERTY))); return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null;
} }
private String getProperty(ConfigurableEnvironment environment, String property, private String getProperty(ConfigurableEnvironment environment, String property,

@ -15,6 +15,7 @@
*/ */
package org.springframework.boot.test.web; package org.springframework.boot.test.web;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -99,13 +100,10 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
@Test @Test
public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDefaultSameInProduction() { public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDefaultSameInProduction() {
// mgmt port is 8080 which means it's on the same port as main server since that // mgmt port is 8080 which means it's on the same port as main server since that
// is // is null in app properties
// null in app properties
addTestPropertySource("0", null); addTestPropertySource("0", null);
Map<String, Object> other = new HashMap<>(); this.propertySources.addLast(new MapPropertySource("other",
other.put("management.server.port", "8080"); Collections.singletonMap("management.server.port", "8080")));
MapPropertySource otherSource = new MapPropertySource("other", other);
this.propertySources.addLast(otherSource);
this.postProcessor.postProcessEnvironment(this.environment, null); this.postProcessor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
assertThat(this.environment.getProperty("management.server.port")).isEqualTo(""); assertThat(this.environment.getProperty("management.server.port")).isEqualTo("");
@ -114,10 +112,8 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
@Test @Test
public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDifferentInProduction() { public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDifferentInProduction() {
addTestPropertySource("0", null); addTestPropertySource("0", null);
Map<String, Object> other = new HashMap<>(); this.propertySources.addLast(new MapPropertySource("other",
other.put("management.server.port", "8081"); Collections.singletonMap("management.server.port", "8081")));
MapPropertySource otherSource = new MapPropertySource("other", other);
this.propertySources.addLast(otherSource);
this.postProcessor.postProcessEnvironment(this.environment, null); this.postProcessor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
@ -126,10 +122,8 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
@Test @Test
public void postProcessWhenTestServerPortIsZeroAndManagementPortMinusOne() { public void postProcessWhenTestServerPortIsZeroAndManagementPortMinusOne() {
addTestPropertySource("0", null); addTestPropertySource("0", null);
Map<String, Object> other = new HashMap<>(); this.propertySources.addLast(new MapPropertySource("other",
other.put("management.server.port", "-1"); Collections.singletonMap("management.server.port", "-1")));
MapPropertySource otherSource = new MapPropertySource("other", other);
this.propertySources.addLast(otherSource);
this.postProcessor.postProcessEnvironment(this.environment, null); this.postProcessor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
assertThat(this.environment.getProperty("management.server.port")) assertThat(this.environment.getProperty("management.server.port"))

Loading…
Cancel
Save