Skip to content

Commit da7daec

Browse files
committed
Tolerate Integer values for port properties
Closes gh-14682
1 parent b473f29 commit da7daec

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessor.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
import org.springframework.boot.SpringApplication;
2121
import org.springframework.boot.env.EnvironmentPostProcessor;
22+
import org.springframework.core.convert.ConversionService;
2223
import org.springframework.core.env.ConfigurableEnvironment;
2324
import org.springframework.core.env.MapPropertySource;
2425
import org.springframework.core.env.PropertySource;
2526
import org.springframework.test.context.support.TestPropertySourceUtils;
27+
import org.springframework.util.ClassUtils;
2628

2729
/**
2830
* {@link EnvironmentPostProcessor} implementation to start the management context on a
@@ -44,15 +46,18 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
4446
SpringApplication application) {
4547
MapPropertySource source = (MapPropertySource) environment.getPropertySources()
4648
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
47-
if (source == null || isTestServerPortFixed(source)
49+
if (source == null
50+
|| isTestServerPortFixed(source, environment.getConversionService())
4851
|| isTestManagementPortConfigured(source)) {
4952
return;
5053
}
51-
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null);
52-
if (managementPort == null || managementPort.equals("-1")) {
54+
Integer managementPort = getPropertyAsInteger(environment,
55+
MANAGEMENT_PORT_PROPERTY, null);
56+
if (managementPort == null || managementPort.equals(-1)) {
5357
return;
5458
}
55-
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080");
59+
Integer serverPort = getPropertyAsInteger(environment, SERVER_PORT_PROPERTY,
60+
8080);
5661
if (!managementPort.equals(serverPort)) {
5762
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
5863
}
@@ -61,21 +66,36 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
6166
}
6267
}
6368

64-
private boolean isTestServerPortFixed(MapPropertySource source) {
65-
return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY));
69+
private boolean isTestServerPortFixed(MapPropertySource source,
70+
ConversionService conversionService) {
71+
return !Integer.valueOf(0).equals(
72+
getPropertyAsInteger(source, SERVER_PORT_PROPERTY, conversionService));
6673
}
6774

6875
private boolean isTestManagementPortConfigured(PropertySource<?> source) {
6976
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null;
7077
}
7178

72-
private String getProperty(ConfigurableEnvironment environment, String property,
73-
String defaultValue) {
79+
private Integer getPropertyAsInteger(ConfigurableEnvironment environment,
80+
String property, Integer defaultValue) {
7481
return environment.getPropertySources().stream()
7582
.filter((source) -> !source.getName().equals(
7683
TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME))
77-
.map((source) -> (String) source.getProperty(property))
84+
.map((source) -> getPropertyAsInteger(source, property,
85+
environment.getConversionService()))
7886
.filter(Objects::nonNull).findFirst().orElse(defaultValue);
7987
}
8088

89+
private Integer getPropertyAsInteger(PropertySource<?> source, String property,
90+
ConversionService conversionService) {
91+
Object value = source.getProperty(property);
92+
if (value == null) {
93+
return null;
94+
}
95+
if (ClassUtils.isAssignableValue(Integer.class, value)) {
96+
return (Integer) value;
97+
}
98+
return conversionService.convert(value, Integer.class);
99+
}
100+
81101
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/SpringBootTestRandomPortEnvironmentPostProcessorTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
3434
*
3535
* @author Madhura Bhave
36+
* @author Andy Wilkinson
3637
*/
3738
public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
3839

@@ -130,6 +131,16 @@ public void postProcessWhenTestServerPortIsZeroAndManagementPortMinusOne() {
130131
.isEqualTo("-1");
131132
}
132133

134+
@Test
135+
public void postProcessWhenTestServerPortIsZeroAndManagementPortIsAnInteger() {
136+
addTestPropertySource("0", null);
137+
this.propertySources.addLast(new MapPropertySource("other",
138+
Collections.singletonMap("management.server.port", 8081)));
139+
this.postProcessor.postProcessEnvironment(this.environment, null);
140+
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
141+
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
142+
}
143+
133144
private void addTestPropertySource(String serverPort, String managementPort) {
134145
Map<String, Object> source = new HashMap<>();
135146
source.put("server.port", serverPort);

0 commit comments

Comments
 (0)