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 10b20997e8..9f97d871bc 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 @@ -19,10 +19,12 @@ import java.util.Objects; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.convert.ConversionService; 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; +import org.springframework.util.ClassUtils; /** * {@link EnvironmentPostProcessor} implementation to start the management context on a @@ -44,15 +46,18 @@ class SpringBootTestRandomPortEnvironmentPostProcessor SpringApplication application) { MapPropertySource source = (MapPropertySource) environment.getPropertySources() .get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); - if (source == null || isTestServerPortFixed(source) + if (source == null + || isTestServerPortFixed(source, environment.getConversionService()) || isTestManagementPortConfigured(source)) { return; } - String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null); - if (managementPort == null || managementPort.equals("-1")) { + Integer managementPort = getPropertyAsInteger(environment, + MANAGEMENT_PORT_PROPERTY, null); + if (managementPort == null || managementPort.equals(-1)) { return; } - String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080"); + Integer serverPort = getPropertyAsInteger(environment, SERVER_PORT_PROPERTY, + 8080); if (!managementPort.equals(serverPort)) { source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0"); } @@ -61,21 +66,36 @@ class SpringBootTestRandomPortEnvironmentPostProcessor } } - private boolean isTestServerPortFixed(MapPropertySource source) { - return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY)); + private boolean isTestServerPortFixed(MapPropertySource source, + ConversionService conversionService) { + return !Integer.valueOf(0).equals( + getPropertyAsInteger(source, SERVER_PORT_PROPERTY, conversionService)); } private boolean isTestManagementPortConfigured(PropertySource source) { return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null; } - private String getProperty(ConfigurableEnvironment environment, String property, - String defaultValue) { + private Integer getPropertyAsInteger(ConfigurableEnvironment environment, + String property, Integer defaultValue) { return environment.getPropertySources().stream() .filter((source) -> !source.getName().equals( TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME)) - .map((source) -> (String) source.getProperty(property)) + .map((source) -> getPropertyAsInteger(source, property, + environment.getConversionService())) .filter(Objects::nonNull).findFirst().orElse(defaultValue); } + private Integer getPropertyAsInteger(PropertySource source, String property, + ConversionService conversionService) { + Object value = source.getProperty(property); + if (value == null) { + return null; + } + if (ClassUtils.isAssignableValue(Integer.class, value)) { + return (Integer) value; + } + return conversionService.convert(value, Integer.class); + } + } 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 e115cec57a..b91bcee970 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 @@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}. * * @author Madhura Bhave + * @author Andy Wilkinson */ public class SpringBootTestRandomPortEnvironmentPostProcessorTests { @@ -130,6 +131,16 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests { .isEqualTo("-1"); } + @Test + public void postProcessWhenTestServerPortIsZeroAndManagementPortIsAnInteger() { + addTestPropertySource("0", null); + 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"); + } + private void addTestPropertySource(String serverPort, String managementPort) { Map source = new HashMap<>(); source.put("server.port", serverPort);