Browse Source

Tolerate Integer values for port properties

Closes gh-14682
pull/14681/merge
Andy Wilkinson 7 years ago
parent
commit
da7daece08
  1. 38
      spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java
  2. 11
      spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java

38
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.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.convert.ConversionService;
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.core.env.PropertySource;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.ClassUtils;
/** /**
* {@link EnvironmentPostProcessor} implementation to start the management context on a * {@link EnvironmentPostProcessor} implementation to start the management context on a
@ -44,15 +46,18 @@ 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 (source == null || isTestServerPortFixed(source) if (source == null
|| isTestServerPortFixed(source, environment.getConversionService())
|| isTestManagementPortConfigured(source)) { || isTestManagementPortConfigured(source)) {
return; return;
} }
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null); Integer managementPort = getPropertyAsInteger(environment,
if (managementPort == null || managementPort.equals("-1")) { MANAGEMENT_PORT_PROPERTY, null);
if (managementPort == null || managementPort.equals(-1)) {
return; return;
} }
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080"); Integer serverPort = getPropertyAsInteger(environment, SERVER_PORT_PROPERTY,
8080);
if (!managementPort.equals(serverPort)) { if (!managementPort.equals(serverPort)) {
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0"); source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
} }
@ -61,21 +66,36 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
} }
} }
private boolean isTestServerPortFixed(MapPropertySource source) { private boolean isTestServerPortFixed(MapPropertySource source,
return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY)); ConversionService conversionService) {
return !Integer.valueOf(0).equals(
getPropertyAsInteger(source, SERVER_PORT_PROPERTY, conversionService));
} }
private boolean isTestManagementPortConfigured(PropertySource<?> source) { private boolean isTestManagementPortConfigured(PropertySource<?> source) {
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null; return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null;
} }
private String getProperty(ConfigurableEnvironment environment, String property, private Integer getPropertyAsInteger(ConfigurableEnvironment environment,
String defaultValue) { String property, Integer defaultValue) {
return environment.getPropertySources().stream() return environment.getPropertySources().stream()
.filter((source) -> !source.getName().equals( .filter((source) -> !source.getName().equals(
TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME)) 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); .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);
}
} }

11
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}. * Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
* *
* @author Madhura Bhave * @author Madhura Bhave
* @author Andy Wilkinson
*/ */
public class SpringBootTestRandomPortEnvironmentPostProcessorTests { public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
@ -130,6 +131,16 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
.isEqualTo("-1"); .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) { private void addTestPropertySource(String serverPort, String managementPort) {
Map<String, Object> source = new HashMap<>(); Map<String, Object> source = new HashMap<>();
source.put("server.port", serverPort); source.put("server.port", serverPort);

Loading…
Cancel
Save