diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java index 282d9ffa34..10b20997e8 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java @@ -21,6 +21,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.test.context.support.TestPropertySourceUtils; /** @@ -29,9 +30,9 @@ import org.springframework.test.context.support.TestPropertySourceUtils; * different port. * * @author Madhura Bhave - * @since 2.1.0 + * @author Andy Wilkinson */ -public class SpringBootTestRandomPortEnvironmentPostProcessor +class SpringBootTestRandomPortEnvironmentPostProcessor implements EnvironmentPostProcessor { private static final String MANAGEMENT_PORT_PROPERTY = "management.server.port"; @@ -43,27 +44,29 @@ public class SpringBootTestRandomPortEnvironmentPostProcessor SpringApplication application) { MapPropertySource source = (MapPropertySource) environment.getPropertySources() .get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); - if (isTestServerPortRandom(source)) { - if (source.getProperty(MANAGEMENT_PORT_PROPERTY) == null) { - String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, - 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, ""); - } - } - } + if (source == null || isTestServerPortFixed(source) + || isTestManagementPortConfigured(source)) { + return; } + 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) { - return (source != null && "0".equals(source.getProperty(SERVER_PORT_PROPERTY))); + private boolean isTestManagementPortConfigured(PropertySource source) { + return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null; } private String getProperty(ConfigurableEnvironment environment, String property, diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java index f81c399fee..e115cec57a 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java @@ -15,6 +15,7 @@ */ package org.springframework.boot.test.web; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -99,13 +100,10 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests { @Test public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDefaultSameInProduction() { // mgmt port is 8080 which means it's on the same port as main server since that - // is - // null in app properties + // is null in app properties addTestPropertySource("0", null); - Map other = new HashMap<>(); - other.put("management.server.port", "8080"); - MapPropertySource otherSource = new MapPropertySource("other", other); - this.propertySources.addLast(otherSource); + this.propertySources.addLast(new MapPropertySource("other", + Collections.singletonMap("management.server.port", "8080"))); this.postProcessor.postProcessEnvironment(this.environment, null); assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("management.server.port")).isEqualTo(""); @@ -114,10 +112,8 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests { @Test public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDifferentInProduction() { addTestPropertySource("0", null); - Map other = new HashMap<>(); - other.put("management.server.port", "8081"); - MapPropertySource otherSource = new MapPropertySource("other", other); - this.propertySources.addLast(otherSource); + this.propertySources.addLast(new MapPropertySource("other", + Collections.singletonMap("management.server.port", "8081"))); this.postProcessor.postProcessEnvironment(this.environment, null); assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); @@ -126,10 +122,8 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests { @Test public void postProcessWhenTestServerPortIsZeroAndManagementPortMinusOne() { addTestPropertySource("0", null); - Map other = new HashMap<>(); - other.put("management.server.port", "-1"); - MapPropertySource otherSource = new MapPropertySource("other", other); - this.propertySources.addLast(otherSource); + this.propertySources.addLast(new MapPropertySource("other", + Collections.singletonMap("management.server.port", "-1"))); this.postProcessor.postProcessEnvironment(this.environment, null); assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); assertThat(this.environment.getProperty("management.server.port"))