From e22f5ec76eae63aa565e93c8c5b2c1f5dd27f6ab Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 3 Jan 2019 10:59:02 +0100 Subject: [PATCH] Fixed cloning of objects --- .../stubrunner/ScmStubDownloaderBuilder.java | 6 +- .../plugin/ContractVerifierExtension.groovy | 51 +++++- .../plugin/PublishStubsToScmTask.groovy | 13 +- .../ContractVerifierExtensionSpec.groovy | 153 ++++++++++++++++++ 4 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtensionSpec.groovy diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ScmStubDownloaderBuilder.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ScmStubDownloaderBuilder.java index 2650600d2c..e1425ef665 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ScmStubDownloaderBuilder.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ScmStubDownloaderBuilder.java @@ -143,9 +143,11 @@ class GitContractsRepo { else { if (log.isDebugEnabled()) { log.debug("The project has already been cloned to [" + file - + "]. Will reset any changes."); + + "]. Will reset any changes and pull the latest ones."); } - new GitRepo(file, properties).reset(file); + GitRepo gitRepo = new GitRepo(file, properties); + gitRepo.reset(file); + gitRepo.pull(file); } return file; } diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.groovy index 0976baa549..c8432ca5e0 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.groovy @@ -16,7 +16,6 @@ package org.springframework.cloud.contract.verifier.plugin -import groovy.transform.AutoClone import groovy.transform.ToString import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory @@ -24,12 +23,10 @@ import org.apache.commons.logging.LogFactory import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties import org.springframework.cloud.contract.verifier.config.TestFramework import org.springframework.cloud.contract.verifier.config.TestMode - /** * @author Marcin Grzejszczak */ @ToString -@AutoClone class ContractVerifierExtension { private static final Log log = LogFactory.getLog(ContractVerifierExtension) @@ -242,7 +239,51 @@ class ContractVerifierExtension { this.disableStubPublication = disableStubPublication } - @AutoClone + ContractVerifierExtension copy() { + return new ContractVerifierExtension( + testFramework: this.testFramework, + testMode: this.testMode, + basePackageForTests: this.basePackageForTests, + baseClassForTests: this.baseClassForTests, + nameSuffixForTests: this.nameSuffixForTests, + ruleClassForTests: this.ruleClassForTests, + excludedFiles: new ArrayList(this.excludedFiles), + includedFiles: new ArrayList(this.includedFiles), + ignoredFiles: new ArrayList(this.ignoredFiles), + imports: Arrays.asList(this.imports).toArray(), + staticImports: Arrays.asList(this.staticImports).toArray(), + contractsDslDir: this.contractsDslDir, + generatedTestSourcesDir: this.generatedTestSourcesDir, + generatedTestResourcesDir: this.generatedTestResourcesDir, + stubsOutputDir: this.stubsOutputDir, + stubsSuffix: this.stubsSuffix, + assertJsonSize: this.assertJsonSize, + contractRepository: new ContractRepository( + repositoryUrl: this.contractRepository.repositoryUrl, + username: this.contractRepository.username, + password: this.contractRepository.password, + proxyPort: this.contractRepository.proxyPort, + proxyHost: this.contractRepository.proxyHost, + cacheDownloadedContracts: this.contractRepository.cacheDownloadedContracts + ), + contractDependency: new Dependency( + groupId: this.contractDependency.groupId, + artifactId: this.contractDependency.artifactId, + classifier: this.contractDependency.classifier, + version: this.contractDependency.version, + stringNotation: this.contractDependency.stringNotation + ), + contractsPath: this.contractsPath, + contractsMode: this.contractsMode, + packageWithBaseClasses: this.packageWithBaseClasses, + baseClassMappings: new HashMap(this.baseClassMappings), + excludeBuildFolders: this.excludeBuildFolders, + deleteStubsAfterTest: this.deleteStubsAfterTest, + convertToYaml: this.convertToYaml, + contractsProperties: new HashMap(this.contractsProperties) + ) + } + @ToString(includeNames = true, includePackage = false) static class Dependency { String groupId @@ -272,7 +313,6 @@ class ContractVerifierExtension { } } - @AutoClone @ToString(includeNames = true, includePackage = false) static class BaseClassMapping { private final Map delegate @@ -290,7 +330,6 @@ class ContractVerifierExtension { } } - @AutoClone @ToString(includeNames = true, includePackage = false) static class ContractRepository { /** diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/PublishStubsToScmTask.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/PublishStubsToScmTask.groovy index 2a7fce0d34..00e86e41d5 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/PublishStubsToScmTask.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/PublishStubsToScmTask.groovy @@ -40,33 +40,34 @@ class PublishStubsToScmTask extends ConventionTask { @TaskAction void publishStubsToScm() { - if (!shouldRun()) { + ContractVerifierExtension clonedExtension = modifyExtension() + if (!shouldRun(clonedExtension)) { return } String projectName = project.group.toString() + ":" + project.name.toString() + ":" + this.project.version.toString() project.logger.info("Pushing Stubs to SCM for project [" + projectName + "]") - ContractVerifierExtension clonedExtension = modifyExtension() StubRunnerOptions options = getDownloader().options(clonedExtension) new ContractProjectUpdater(options).updateContractProject(projectName, getStubsOutputDir().toPath()) } private ContractVerifierExtension modifyExtension() { - ContractVerifierExtension clone = getConfigProperties().clone() + ContractVerifierExtension clone = getConfigProperties().copy() this.closureHolder.extensionClosure.delegate = clone this.closureHolder.extensionClosure.call(clone) return clone } - private boolean shouldRun() { - String contractRepoUrl = getConfigProperties().contractRepository.repositoryUrl ?: "" + private boolean shouldRun(ContractVerifierExtension clonedExtension) { + String contractRepoUrl = clonedExtension.contractRepository.repositoryUrl ?: "" if (!contractRepoUrl || !ScmStubDownloaderBuilder.isProtocolAccepted(contractRepoUrl)) { - project.logger.info("Skipping pushing stubs to scm since your contracts repository URL doesn't match any of the accepted protocols") + project.logger.warn("Skipping pushing stubs to scm since your contracts repository URL [${contractRepoUrl}] doesn't match any of the accepted protocols for SCM stub downloader") return false } return true } void customize(@DelegatesTo(ContractVerifierExtension) Closure closure) { + project.logger.debug("Storing the extension closure") this.closureHolder.extensionClosure = closure } diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtensionSpec.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtensionSpec.groovy new file mode 100644 index 0000000000..c2851ee513 --- /dev/null +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtensionSpec.groovy @@ -0,0 +1,153 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.cloud.contract.verifier.plugin + +import spock.lang.Specification + +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties +import org.springframework.cloud.contract.verifier.config.TestFramework +import org.springframework.cloud.contract.verifier.config.TestMode + +class ContractVerifierExtensionSpec extends Specification { + def "should make a copy of the object"() { + given: + ContractVerifierExtension original = new ContractVerifierExtension( + testFramework: TestFramework.JUNIT5, + testMode: TestMode.EXPLICIT, + basePackageForTests: "foo1", + baseClassForTests: "foo2", + nameSuffixForTests: "foo3", + ruleClassForTests: "foo4", + excludedFiles: ["foo5"], + includedFiles: ["foo6"], + ignoredFiles: ["foo7"], + imports: ["foo8"], + staticImports: ["foo9"], + contractsDslDir: new File("foo10"), + generatedTestSourcesDir: new File("foo11"), + generatedTestResourcesDir: new File("foo12"), + stubsOutputDir: new File("foo13"), + stubsSuffix: 'foo14', + assertJsonSize: false, + contractRepository: new ContractVerifierExtension.ContractRepository( + repositoryUrl: "foo15", + username: "foo16", + password: "foo17", + proxyPort: 18, + proxyHost: "foo19", + cacheDownloadedContracts: false + ), + contractDependency: new ContractVerifierExtension.Dependency( + groupId: "foo20", + artifactId: "foo21", + classifier: "foo22", + version: "foo23", + stringNotation: "foo24" + ), + contractsPath: "foo25", + contractsMode: StubRunnerProperties.StubsMode.CLASSPATH, + packageWithBaseClasses: "foo26", + baseClassMappings: [foo27: "foo28"], + excludeBuildFolders: false, + deleteStubsAfterTest: false, + convertToYaml: false, + contractsProperties: [foo29: "foo30"] + ) + when: + ContractVerifierExtension copy = original.copy() + original.with { + testFramework = TestFramework.CUSTOM + testMode = TestMode.MOCKMVC + basePackageForTests = "bar1" + baseClassForTests = "bar2" + nameSuffixForTests = "bar3" + ruleClassForTests = "bar4" + excludedFiles = ["bar5"] + includedFiles = ["bar6"] + ignoredFiles = ["bar7"] + imports = ["bar8"] + staticImports = ["bar9"] + contractsDslDir = new File("bar10") + generatedTestSourcesDir = new File("bar11") + generatedTestResourcesDir = new File("bar12") + stubsOutputDir = new File("bar13") + stubsSuffix = 'bar14' + assertJsonSize = true + contractRepository.with { + repositoryUrl = "bar15" + username = "bar16" + password = "bar17" + proxyPort = 28 + proxyHost = "bar19" + cacheDownloadedContracts = true + } + contractDependency.with { + groupId = "bar20" + artifactId = "bar21" + classifier = "bar22" + version = "bar23" + stringNotation = "bar24" + } + contractsPath = "bar25" + contractsMode = StubRunnerProperties.StubsMode.REMOTE + packageWithBaseClasses = "bar26" + baseClassMappings = [bar27 : "bar28"] + excludeBuildFolders = true + deleteStubsAfterTest = true + convertToYaml = true + contractsProperties = [bar29 : "bar30"] + } + then: + copy.testFramework == TestFramework.JUNIT5 + copy.testMode == TestMode.EXPLICIT + copy.basePackageForTests == "foo1" + copy.baseClassForTests == "foo2" + copy.nameSuffixForTests == "foo3" + copy.ruleClassForTests == "foo4" + copy.excludedFiles == ["foo5"] + copy.includedFiles == ["foo6"] + copy.ignoredFiles == ["foo7"] + copy.imports == ["foo8"] + copy.staticImports == ["foo9"] + copy.contractsDslDir == new File("foo10") + copy.generatedTestSourcesDir == new File("foo11") + copy.generatedTestResourcesDir == new File("foo12") + copy.stubsOutputDir == new File("foo13") + copy.stubsSuffix == 'foo14' + copy.assertJsonSize == false + copy.contractRepository.repositoryUrl == "foo15" + copy.contractRepository.username == "foo16" + copy.contractRepository.password == "foo17" + copy.contractRepository.proxyPort == 18 + copy.contractRepository.proxyHost == "foo19" + copy.contractRepository.cacheDownloadedContracts == false + copy.contractDependency.groupId == "foo20" + copy.contractDependency.artifactId == "foo21" + copy.contractDependency.classifier == "foo22" + copy.contractDependency.version == "foo23" + copy.contractDependency.stringNotation == "foo24" + copy.contractsPath == "foo25" + copy.contractsMode == StubRunnerProperties.StubsMode.CLASSPATH + copy.packageWithBaseClasses == "foo26" + copy.baseClassMappings == [foo27 : "foo28"] + copy.excludeBuildFolders == false + copy.deleteStubsAfterTest == false + copy.convertToYaml == false + copy.contractsProperties == [foo29 : "foo30"] + } +}