Skip to content

Commit b473f29

Browse files
committed
Polish
1 parent b4638b8 commit b473f29

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.env.EnvironmentPostProcessor;
2222
import org.springframework.core.env.ConfigurableEnvironment;
2323
import org.springframework.core.env.MapPropertySource;
24+
import org.springframework.core.env.PropertySource;
2425
import org.springframework.test.context.support.TestPropertySourceUtils;
2526

2627
/**
@@ -29,9 +30,9 @@
2930
* different port.
3031
*
3132
* @author Madhura Bhave
32-
* @since 2.1.0
33+
* @author Andy Wilkinson
3334
*/
34-
public class SpringBootTestRandomPortEnvironmentPostProcessor
35+
class SpringBootTestRandomPortEnvironmentPostProcessor
3536
implements EnvironmentPostProcessor {
3637

3738
private static final String MANAGEMENT_PORT_PROPERTY = "management.server.port";
@@ -43,27 +44,29 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
4344
SpringApplication application) {
4445
MapPropertySource source = (MapPropertySource) environment.getPropertySources()
4546
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
46-
if (isTestServerPortRandom(source)) {
47-
if (source.getProperty(MANAGEMENT_PORT_PROPERTY) == null) {
48-
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY,
49-
null);
50-
if (managementPort != null && !managementPort.equals("-1")) {
51-
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY,
52-
"8080");
53-
if (!managementPort.equals(serverPort)) {
54-
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
55-
}
56-
else {
57-
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "");
58-
}
59-
}
60-
}
47+
if (source == null || isTestServerPortFixed(source)
48+
|| isTestManagementPortConfigured(source)) {
49+
return;
6150
}
51+
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null);
52+
if (managementPort == null || managementPort.equals("-1")) {
53+
return;
54+
}
55+
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080");
56+
if (!managementPort.equals(serverPort)) {
57+
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0");
58+
}
59+
else {
60+
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "");
61+
}
62+
}
6263

64+
private boolean isTestServerPortFixed(MapPropertySource source) {
65+
return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY));
6366
}
6467

65-
private boolean isTestServerPortRandom(MapPropertySource source) {
66-
return (source != null && "0".equals(source.getProperty(SERVER_PORT_PROPERTY)));
68+
private boolean isTestManagementPortConfigured(PropertySource<?> source) {
69+
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null;
6770
}
6871

6972
private String getProperty(ConfigurableEnvironment environment, String property,

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.boot.test.web;
1717

18+
import java.util.Collections;
1819
import java.util.HashMap;
1920
import java.util.Map;
2021

@@ -99,13 +100,10 @@ public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndSame
99100
@Test
100101
public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDefaultSameInProduction() {
101102
// mgmt port is 8080 which means it's on the same port as main server since that
102-
// is
103-
// null in app properties
103+
// is null in app properties
104104
addTestPropertySource("0", null);
105-
Map<String, Object> other = new HashMap<>();
106-
other.put("management.server.port", "8080");
107-
MapPropertySource otherSource = new MapPropertySource("other", other);
108-
this.propertySources.addLast(otherSource);
105+
this.propertySources.addLast(new MapPropertySource("other",
106+
Collections.singletonMap("management.server.port", "8080")));
109107
this.postProcessor.postProcessEnvironment(this.environment, null);
110108
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
111109
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("");
@@ -114,10 +112,8 @@ public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDefa
114112
@Test
115113
public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDifferentInProduction() {
116114
addTestPropertySource("0", null);
117-
Map<String, Object> other = new HashMap<>();
118-
other.put("management.server.port", "8081");
119-
MapPropertySource otherSource = new MapPropertySource("other", other);
120-
this.propertySources.addLast(otherSource);
115+
this.propertySources.addLast(new MapPropertySource("other",
116+
Collections.singletonMap("management.server.port", "8081")));
121117
this.postProcessor.postProcessEnvironment(this.environment, null);
122118
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
123119
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
@@ -126,10 +122,8 @@ public void postProcessWhenTestServerPortIsZeroAndManagementPortIsNotNullAndDiff
126122
@Test
127123
public void postProcessWhenTestServerPortIsZeroAndManagementPortMinusOne() {
128124
addTestPropertySource("0", null);
129-
Map<String, Object> other = new HashMap<>();
130-
other.put("management.server.port", "-1");
131-
MapPropertySource otherSource = new MapPropertySource("other", other);
132-
this.propertySources.addLast(otherSource);
125+
this.propertySources.addLast(new MapPropertySource("other",
126+
Collections.singletonMap("management.server.port", "-1")));
133127
this.postProcessor.postProcessEnvironment(this.environment, null);
134128
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
135129
assertThat(this.environment.getProperty("management.server.port"))

0 commit comments

Comments
 (0)