From 5707124350fca9701ca1b41be0ea2e49ccda1293 Mon Sep 17 00:00:00 2001 From: Matt Garner Date: Thu, 18 Apr 2019 15:30:03 +0100 Subject: [PATCH] #883/#1028.: Further DirtiesContext fix where the context is refreshed during the test phase (#1048) * #883: Register port now only handles auto port values. This is to avoid the httpsPortDynamic being set to true on the context initialisation, but then being updated to false when the context is refreshed. * #1028: Not registering the wiremock ports until they're available in the environment properties. --- .../wiremock/WireMockApplicationListener.java | 20 +++++++++++++------ .../wiremock/WireMockConfiguration.java | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index a8f3c96dee..460fcfa231 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -47,7 +47,15 @@ public class WireMockApplicationListener } private void registerPort(ConfigurableEnvironment environment) { - if (environment.getProperty("wiremock.server.port", Integer.class, 0) == 0) { + Integer httpPortProperty = environment.getProperty("wiremock.server.port", + Integer.class); + // If the httpPortProperty is not found it means the AutoConfigureWireMock hasn't + // been initialised. + if (httpPortProperty == null) { + return; + } + + if (httpPortProperty.equals(0)) { MutablePropertySources propertySources = environment.getPropertySources(); addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources @@ -56,8 +64,9 @@ public class WireMockApplicationListener SocketUtils.findAvailableTcpPort(10000, 12500)); source.put("wiremock.server.port-dynamic", true); } - if (environment.getProperty("wiremock.server.https-port", Integer.class, - 0) == 0) { + int httpsPortProperty = environment.getProperty("wiremock.server.https-port", + Integer.class, 0); + if (httpsPortProperty == 0) { MutablePropertySources propertySources = environment.getPropertySources(); addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources @@ -66,13 +75,12 @@ public class WireMockApplicationListener SocketUtils.findAvailableTcpPort(12500, 15000)); source.put("wiremock.server.https-port-dynamic", true); } - else if (environment.getProperty("wiremock.server.https-port", Integer.class, - 0) != -1) { + else if (httpsPortProperty == -1) { MutablePropertySources propertySources = environment.getPropertySources(); addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources .get("wiremock")).getSource(); - source.put("wiremock.server.https-port-dynamic", false); + source.put("wiremock.server.https-port-dynamic", true); } } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index ff5d170555..abebd1632a 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -272,7 +272,7 @@ class WireMockProperties { private boolean portDynamic = false; - private boolean httpsPortDynamic = true; + private boolean httpsPortDynamic = false; public int getPort() { return this.port;