|
|
|
|
@ -34,14 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -34,14 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests for {@link SpringBootTestRandomPortApplicationListener}. |
|
|
|
|
* Tests for {@link SpringBootTestRandomPortContextCustomizer}. |
|
|
|
|
* |
|
|
|
|
* @author Madhura Bhave |
|
|
|
|
* @author Andy Wilkinson |
|
|
|
|
*/ |
|
|
|
|
class SpringBootTestRandomPortApplicationListenerTests { |
|
|
|
|
class SpringBootTestRandomPortContextCustomizerTests { |
|
|
|
|
|
|
|
|
|
private final SpringBootTestRandomPortApplicationListener listener = new SpringBootTestRandomPortApplicationListener(); |
|
|
|
|
private final SpringBootTestRandomPortContextCustomizer customizer = new SpringBootTestRandomPortContextCustomizer(); |
|
|
|
|
|
|
|
|
|
private MockEnvironment environment; |
|
|
|
|
|
|
|
|
|
@ -56,7 +56,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -56,7 +56,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
@Test |
|
|
|
|
void postProcessWhenServerAndManagementPortIsZeroInTestPropertySource() { |
|
|
|
|
addTestPropertySource("0", "0"); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -67,7 +67,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -67,7 +67,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
Map<String, Object> source = new HashMap<>(); |
|
|
|
|
source.put("management.server.port", "0"); |
|
|
|
|
this.propertySources.addLast(new MapPropertySource("other", source)); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -77,7 +77,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -77,7 +77,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
addTestPropertySource("8080", "8081"); |
|
|
|
|
this.environment.setProperty("server.port", "8080"); |
|
|
|
|
this.environment.setProperty("management.server.port", "8081"); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("8080"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("8081"); |
|
|
|
|
} |
|
|
|
|
@ -85,7 +85,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -85,7 +85,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
@Test |
|
|
|
|
void postProcessWhenTestServerPortIsZeroAndTestManagementPortIsNotNull() { |
|
|
|
|
addTestPropertySource("0", "8080"); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("8080"); |
|
|
|
|
} |
|
|
|
|
@ -93,7 +93,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -93,7 +93,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
@Test |
|
|
|
|
void postProcessWhenTestServerPortIsZeroAndManagementPortIsNull() { |
|
|
|
|
addTestPropertySource("0", null); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isNull(); |
|
|
|
|
} |
|
|
|
|
@ -106,7 +106,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -106,7 +106,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
other.put("management.server.port", "8081"); |
|
|
|
|
MapPropertySource otherSource = new MapPropertySource("other", other); |
|
|
|
|
this.propertySources.addLast(otherSource); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEmpty(); |
|
|
|
|
} |
|
|
|
|
@ -118,7 +118,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -118,7 +118,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
addTestPropertySource("0", null); |
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "8080"))); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEmpty(); |
|
|
|
|
} |
|
|
|
|
@ -128,7 +128,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -128,7 +128,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
addTestPropertySource("0", null); |
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "8081"))); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -138,7 +138,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -138,7 +138,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
addTestPropertySource("0", null); |
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "-1"))); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("-1"); |
|
|
|
|
} |
|
|
|
|
@ -148,7 +148,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -148,7 +148,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
addTestPropertySource("0", null); |
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", 8081))); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -162,7 +162,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -162,7 +162,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
testPropertySource.getSource().put("port", "9090"); |
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "${port}"))); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -173,7 +173,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -173,7 +173,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
this.propertySources |
|
|
|
|
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "${port}"))); |
|
|
|
|
assertThatExceptionOfType(PlaceholderResolutionException.class) |
|
|
|
|
.isThrownBy(() -> this.listener.postProcessEnvironment(this.environment)) |
|
|
|
|
.isThrownBy(() -> this.customizer.postProcessEnvironment(this.environment)) |
|
|
|
|
.withMessage("Could not resolve placeholder 'port' in value \"${port}\""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -188,7 +188,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -188,7 +188,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
source.put("server.port", "${port}"); |
|
|
|
|
source.put("management.server.port", "9090"); |
|
|
|
|
this.propertySources.addLast(new MapPropertySource("other", source)); |
|
|
|
|
this.listener.postProcessEnvironment(this.environment); |
|
|
|
|
this.customizer.postProcessEnvironment(this.environment); |
|
|
|
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0"); |
|
|
|
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0"); |
|
|
|
|
} |
|
|
|
|
@ -201,7 +201,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
@@ -201,7 +201,7 @@ class SpringBootTestRandomPortApplicationListenerTests {
|
|
|
|
|
source.put("management.server.port", "9090"); |
|
|
|
|
this.propertySources.addLast(new MapPropertySource("other", source)); |
|
|
|
|
assertThatExceptionOfType(PlaceholderResolutionException.class) |
|
|
|
|
.isThrownBy(() -> this.listener.postProcessEnvironment(this.environment)) |
|
|
|
|
.isThrownBy(() -> this.customizer.postProcessEnvironment(this.environment)) |
|
|
|
|
.withMessage("Could not resolve placeholder 'port' in value \"${port}\""); |
|
|
|
|
} |
|
|
|
|
|