From 4c073c8a524ef33498db277f00da8c5eee7dcb9c Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 23 Nov 2017 11:53:15 +0100 Subject: [PATCH] Fixing the way StubRunnerBuilder options are built without this change we're completely ignoring the option of using system properties to set the stub runner builder options with this change we set it up with defaults from system properties and then we override it with the user's code fixes #462 --- .../stubrunner/StubRunnerOptions.java | 20 +++++++++++ .../stubrunner/junit/StubRunnerRule.java | 21 +---------- .../StubRunnerOptionsBuilderSpec.groovy | 36 +++++++++++++++++++ .../verifier/MavenContractsDownloader.java | 1 + 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerOptions.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerOptions.java index 71a4b4bf3d..124967343c 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerOptions.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunnerOptions.java @@ -123,6 +123,26 @@ public class StubRunnerOptions { } } + public static StubRunnerOptions fromSystemProps() { + StubRunnerOptionsBuilder builder = new StubRunnerOptionsBuilder() + .withMinPort(Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000"))) + .withMaxPort(Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000"))) + .withStubRepositoryRoot(System.getProperty("stubrunner.repository.root", "")) + .withWorkOffline(Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false"))) + .withStubsClassifier(System.getProperty("stubrunner.classifier", "stubs")) + .withStubs(System.getProperty("stubrunner.ids", "")) + .withUsername(System.getProperty("stubrunner.username")) + .withPassword(System.getProperty("stubrunner.password")) + .withStubPerConsumer(Boolean.parseBoolean(System.getProperty("stubrunner.stubs-per-consumer", "false"))) + .withConsumerName(System.getProperty("stubrunner.consumer-name")) + .withMappingsOutputFolder(System.getProperty("stubrunner.mappings-output-folder")); + String proxyHost = System.getProperty("stubrunner.proxy.host"); + if (proxyHost != null) { + builder.withProxy(proxyHost, Integer.parseInt(System.getProperty("stubrunner.proxy.port"))); + } + return builder.build(); + } + public Integer getMinPortValue() { return this.minPortValue; } diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java index 71de3807a5..c70d3ebb9c 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java @@ -45,7 +45,7 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio private static final String DELIMITER = ":"; private static final String LATEST_VERSION = "+"; - StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder(defaultStubRunnerOptions()); + StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder(StubRunnerOptions.fromSystemProps()); BatchStubRunner stubFinder; MessageVerifier verifier = new ExceptionThrowingMessageVerifier(); StubRunnerRule delegate = this; @@ -70,25 +70,6 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio }; } - private StubRunnerOptions defaultStubRunnerOptions() { - StubRunnerOptionsBuilder builder = new StubRunnerOptionsBuilder() - .withMinPort(Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000"))) - .withMaxPort(Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000"))) - .withStubRepositoryRoot(System.getProperty("stubrunner.repository.root", "")) - .withWorkOffline(Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false"))) - .withStubsClassifier(System.getProperty("stubrunner.classifier", "stubs")) - .withStubs(System.getProperty("stubrunner.ids", "")) - .withUsername(System.getProperty("stubrunner.username")) - .withPassword(System.getProperty("stubrunner.password")) - .withStubPerConsumer(Boolean.parseBoolean(System.getProperty("stubrunner.stubsPerConsumer", "false"))) - .withConsumerName(System.getProperty("stubrunner.consumer-name")); - String proxyHost = System.getProperty("stubrunner.proxy.host"); - if (proxyHost != null) { - builder.withProxy(proxyHost, Integer.parseInt(System.getProperty("stubrunner.proxy.port"))); - } - return builder.build(); - } - @Override public StubRunnerRule messageVerifier(MessageVerifier messageVerifier) { verifier(messageVerifier); return this.delegate; diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerOptionsBuilderSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerOptionsBuilderSpec.groovy index 09415d716d..aa13265231 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerOptionsBuilderSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerOptionsBuilderSpec.groovy @@ -18,6 +18,7 @@ package org.springframework.cloud.contract.stubrunner import spock.lang.Issue import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties class StubRunnerOptionsBuilderSpec extends Specification { @@ -160,4 +161,39 @@ class StubRunnerOptionsBuilderSpec extends Specification { options.consumerName == "consumer" options.mappingsOutputFolder == "folder" } + + @Issue("#462") + @RestoreSystemProperties + def shouldSetAllPropsFromSystemProps() { + given: + System.setProperty("stubrunner.port.range.min", "1") + System.setProperty("stubrunner.port.range.max", "2") + System.setProperty("stubrunner.repository.root", "root") + System.setProperty("stubrunner.work-offline", "true") + System.setProperty("stubrunner.classifier", "classifier") + System.setProperty("stubrunner.ids", "a:b:c,foo:bar:baz:classifier") + System.setProperty("stubrunner.username", "foo") + System.setProperty("stubrunner.password", "bar") + System.setProperty("stubrunner.stubs-per-consumer", "true") + System.setProperty("stubrunner.consumer-name", "consumer") + System.setProperty("stubrunner.proxy.host", "host") + System.setProperty("stubrunner.proxy.port", "4") + System.setProperty("stubrunner.mappings-output-folder", "folder") + when: + StubRunnerOptions options = StubRunnerOptions.fromSystemProps() + then: + options.minPortValue == 1 + options.maxPortValue == 2 + options.stubRepositoryRoot == "root" + options.workOffline == true + options.stubsClassifier == "classifier" + options.dependencies == [new StubConfiguration("a:b:c"), new StubConfiguration("foo:bar:baz:classifier")] + options.username == "foo" + options.password == "bar" + options.proxyOptions.proxyHost == "host" + options.proxyOptions.proxyPort == 4 + options.stubsPerConsumer == true + options.consumerName == "consumer" + options.mappingsOutputFolder == "folder" + } } diff --git a/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/MavenContractsDownloader.java b/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/MavenContractsDownloader.java index 7c9b27b4ab..bbc174ce92 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/MavenContractsDownloader.java +++ b/spring-cloud-contract-tools/spring-cloud-contract-maven-plugin/src/main/java/org/springframework/cloud/contract/maven/verifier/MavenContractsDownloader.java @@ -114,6 +114,7 @@ class MavenContractsDownloader { StubRunnerOptions buildOptions() { return new StubRunnerOptionsBuilder() + .withOptions(StubRunnerOptions.fromSystemProps()) .withStubRepositoryRoot(this.contractsRepositoryUrl) .withWorkOffline(this.contractsWorkOffline) .build();