From 3b16eed5cbda77c93f181042271056a3aa9e299d Mon Sep 17 00:00:00 2001 From: Shannon Pamperl Date: Sat, 24 Aug 2019 21:18:18 -0500 Subject: [PATCH] Revamp extension to explicitly declare input types correctly --- .../plugin/ContractVerifierExtension.java | 971 +++++++++++------- .../plugin/GenerateServerTestsTask.groovy | 2 +- ...ngCloudContractVerifierGradlePlugin.groovy | 11 - .../plugin/ContractVerifierSpec.groovy | 10 +- 4 files changed, 580 insertions(+), 414 deletions(-) diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.java b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.java index 050cec815a..70737be24f 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.java +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierExtension.java @@ -14,139 +14,134 @@ * limitations under the License. */ -package org.springframework.cloud.contract.verifier.plugin +package org.springframework.cloud.contract.verifier.plugin; -import org.gradle.api.tasks.Internal +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.gradle.api.Action; +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.ProjectLayout; +import org.gradle.api.model.ObjectFactory; +import org.gradle.api.provider.ListProperty; +import org.gradle.api.provider.MapProperty; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Internal; +import org.gradle.api.tasks.Optional; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; +import org.springframework.cloud.contract.verifier.config.TestFramework; +import org.springframework.cloud.contract.verifier.config.TestMode; +import org.springframework.util.Assert; -import javax.inject.Inject - -import groovy.transform.CompileStatic -import org.apache.commons.logging.Log -import org.apache.commons.logging.LogFactory -import org.gradle.api.file.DirectoryProperty -import org.gradle.api.model.ObjectFactory -import org.gradle.api.provider.ListProperty -import org.gradle.api.provider.MapProperty -import org.gradle.api.provider.Property -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.Optional - -import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties -import org.springframework.cloud.contract.verifier.config.TestFramework -import org.springframework.cloud.contract.verifier.config.TestMode +import javax.inject.Inject; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @author Marcin Grzejszczak * @author Anatoliy Balakirev + * @author Shannon Pamperl */ -@CompileStatic -class ContractVerifierExtension { +public class ContractVerifierExtension { - private static final Log log = LogFactory.getLog(ContractVerifierExtension) - - @Deprecated - void setTargetFramework(TestFramework targetFramework) { - log.warn("Please use the [testFramework] field. This one is deprecated") - this.testFramework.set(targetFramework) - } - - @Deprecated - TestFramework getTargetFramework() { - return getTestFramework().get() - } + private static final Log log = LogFactory.getLog(ContractVerifierExtension.class); /** * For which unit test library tests should be generated */ - Property testFramework + private Property testFramework; /** * Which mechanism should be used to invoke REST calls during tests */ - Property testMode + private Property testMode; /** * Base package for generated tests */ - Property basePackageForTests + private Property basePackageForTests; /** * Class which all generated tests should extend */ - Property baseClassForTests + private Property baseClassForTests; /** * Suffix for generated test classes, like Spec or Test */ - Property nameSuffixForTests + private Property nameSuffixForTests; /** * Rule class that should be added to generated tests */ - Property ruleClassForTests + private Property ruleClassForTests; /** * Patterns that should not be taken into account for processing */ - ListProperty excludedFiles + private ListProperty excludedFiles; /** * Patterns that should be taken into account for processing */ - ListProperty includedFiles + private ListProperty includedFiles; /** * Patterns for which generated tests should be @Ignored */ - ListProperty ignoredFiles + private ListProperty ignoredFiles; /** * Imports that should be added to generated tests */ - ListProperty imports + private ListProperty imports; /** * Static imports that should be added to generated tests */ - ListProperty staticImports + private ListProperty staticImports; /** * Directory containing contracts written using the GroovyDSL */ - DirectoryProperty contractsDslDir + private DirectoryProperty contractsDslDir; /** * Test source directory where tests generated from Groovy DSL should be placed */ - DirectoryProperty generatedTestSourcesDir + private DirectoryProperty generatedTestSourcesDir; /** * Test resource directory where tests generated from Groovy DSL should be referenced */ - DirectoryProperty generatedTestResourcesDir + private DirectoryProperty generatedTestResourcesDir; /** * Dir where the generated stubs from Groovy DSL should be placed. * You can then mention them in your packaging task to create jar with stubs */ - DirectoryProperty stubsOutputDir + private DirectoryProperty stubsOutputDir; /** * Suffix for the generated Stubs Jar task */ - Property stubsSuffix + private Property stubsSuffix; /** * Incubating feature. You can check the size of JSON arrays. If not turned on * explicitly will be disabled. */ - Property assertJsonSize + private Property assertJsonSize; /** * When enabled, this flag will tell stub runner to throw an exception when no stubs / * contracts were found. */ - Property failOnNoContracts + private Property failOnNoContracts; /** * If set to true then if any contracts that are in progress are found, will break the @@ -154,14 +149,14 @@ class ContractVerifierExtension { * contracts in progress and take into consideration that you might be causing false * positive test execution results on the consumer side. */ - Property failOnInProgress + private Property failOnInProgress; - ContractRepository contractRepository + private ContractRepository contractRepository; /** * Dependency that contains packaged contracts */ - Dependency contractDependency + private Dependency contractDependency; /** * The path in the JAR with all the contracts where contracts for this particular service lay. @@ -170,12 +165,12 @@ class ContractVerifierExtension { * If {@code groupid} is {@code com.example} and {@code artifactid} is {@code service} then the resolved path will be * {@code /com/example/artifactid} */ - Property contractsPath + private Property contractsPath; /** * Picks the mode in which stubs will be found and registered */ - Property contractsMode + private Property contractsMode; /** * A package that contains all the base clases for generated tests. If your contract resides in a location @@ -184,7 +179,7 @@ class ContractVerifierExtension { * have the package {@code com.example.contracts.base} and name {@code ExampleV1Base}. As you can see * it will take the two last folders to and attach {@code Base} to its name. */ - Property packageWithBaseClasses + private Property packageWithBaseClasses; /** * A way to override any base class mappings. The keys are regular expressions on the package name @@ -197,14 +192,14 @@ class ContractVerifierExtension { * When a contract's package matches the provided regular expression then extending class will be the one * provided in the map - in this case {@code com.example.SomeBaseClass} */ - MapProperty baseClassMappings + private BaseClassMapping baseClassMappings; /** * If set to true then the {@code target} or {@code build} folders are getting * excluded from any operations. This is used out of the box when working with * common repo with contracts. */ - Property excludeBuildFolders + private Property excludeBuildFolders; /** * If set to {@code true} will not assert whether the downloaded stubs / contract @@ -213,404 +208,584 @@ class ContractVerifierExtension { * @deprecated - with 2.1.0 this option is redundant */ @Deprecated - Property contractsSnapshotCheckSkip + private Property contractsSnapshotCheckSkip; /** * If set to {@code false} will NOT delete stubs from a temporary * folder after running tests */ - Property deleteStubsAfterTest + private Property deleteStubsAfterTest; /** * If {@code true} then will convert contracts to a YAML representation */ - Property convertToYaml + private Property convertToYaml; /** * Map of properties that can be passed to custom {@link org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder} */ - MapProperty contractsProperties - - void contractDependency(@DelegatesTo(Dependency) Closure closure) { - closure.delegate = contractDependency - closure.call() - } - - void baseClassMappings(@DelegatesTo(BaseClassMapping) Closure closure) { - closure.delegate = new BaseClassMapping(baseClassMappings) - closure.call() - } - - void contractRepository(@DelegatesTo(ContractRepository) Closure closure) { - closure.delegate = contractRepository - closure.call() - } - - void contractsProperties(Map map) { - contractsProperties.set(map) - } - - void setContractsProperties(Map map) { - contractsProperties.set(map) - } + private MapProperty contractsProperties; /** * Is set to true will not provide the default publication task */ - Property disableStubPublication - - // Added for backward compatibility only. Use setter of dedicated enum type - @Deprecated - void setTestMode(String testMode) { - if (testMode != null) { - this.testMode.set(TestMode.valueOf(testMode.toUpperCase())) - } - } - - // Added for backward compatibility only. Use setter of dedicated enum type - @Deprecated - void setTestFramework(String testFramework) { - if (testFramework != null) { - this.testFramework.set(TestFramework.valueOf(testFramework.toUpperCase())) - } - } - - // Added for backward compatibility only. Use setter of dedicated enum type - @Deprecated - void setContractsMode(String contractsMode) { - if (contractsMode != null) { - this.contractsMode.set(StubRunnerProperties.StubsMode.valueOf(contractsMode.toUpperCase())) - } - } - - void setBasePackageForTests(String basePackageForTests) { - this.basePackageForTests.set(basePackageForTests) - } - - void setBaseClassForTests(String baseClassForTests) { - this.baseClassForTests.set(baseClassForTests) - } - - void setNameSuffixForTests(String nameSuffixForTests) { - this.nameSuffixForTests.set(nameSuffixForTests) - } - - void setRuleClassForTests(String ruleClassForTests) { - this.ruleClassForTests.set(ruleClassForTests) - } - - void setContractsDslDir(String contractsDslDir) { - this.contractsDslDir.set(new File(contractsDslDir)) - } - - void setGeneratedTestSourcesDir(String generatedTestSourcesDir) { - this.generatedTestSourcesDir.set(new File(generatedTestSourcesDir)) - } - - void setGeneratedTestResourcesDir(String generatedTestResourcesDir) { - this.generatedTestResourcesDir.set(new File(generatedTestResourcesDir)) - } - - void setStubsOutputDir(String stubsOutputDir) { - this.stubsOutputDir.set(new File(stubsOutputDir)) - } - - void setStubsSuffix(String stubsSuffix) { - this.stubsSuffix.set(stubsSuffix) - } - - void setContractsPath(String contractsPath) { - this.contractsPath.set(contractsPath) - } - - void setPackageWithBaseClasses(String packageWithBaseClasses) { - this.packageWithBaseClasses.set(packageWithBaseClasses) - } - - void setBasePackageForTests(GString basePackageForTests) { - this.basePackageForTests.set(basePackageForTests.toString()) - } - - void setBaseClassForTests(GString baseClassForTests) { - this.baseClassForTests.set(baseClassForTests.toString()) - } - - void setNameSuffixForTests(GString nameSuffixForTests) { - this.nameSuffixForTests.set(nameSuffixForTests.toString()) - } - - void setRuleClassForTests(GString ruleClassForTests) { - this.ruleClassForTests.set(ruleClassForTests.toString()) - } - - void setContractsDslDir(GString contractsDslDir) { - this.contractsDslDir.set(new File(contractsDslDir.toString())) - } - - void setGeneratedTestSourcesDir(GString generatedTestSourcesDir) { - this.generatedTestSourcesDir.set(new File(generatedTestSourcesDir.toString())) - } - - void setGeneratedTestResourcesDir(GString generatedTestResourcesDir) { - this.generatedTestResourcesDir.set(new File(generatedTestResourcesDir.toString())) - } - - void setStubsOutputDir(GString stubsOutputDir) { - this.stubsOutputDir.set(new File(stubsOutputDir.toString())) - } - - void setStubsSuffix(GString stubsSuffix) { - this.stubsSuffix.set(stubsSuffix.toString()) - } - - void setContractsPath(GString contractsPath) { - this.contractsPath.set(contractsPath.toString()) - } - - void setPackageWithBaseClasses(GString packageWithBaseClasses) { - this.packageWithBaseClasses.set(packageWithBaseClasses.toString()) - } - + private Property disableStubPublication; @Inject - ContractVerifierExtension(ObjectFactory objects) { - this.testFramework = objects.property(TestFramework).convention(TestFramework.JUNIT) - this.testMode = objects.property(TestMode).convention(TestMode.MOCKMVC) - this.basePackageForTests = objects.property(String) - this.baseClassForTests = objects.property(String) - this.nameSuffixForTests = objects.property(String) - this.ruleClassForTests = objects.property(String) - this.excludedFiles = objects.listProperty(String).convention([]) - this.includedFiles = objects.listProperty(String).convention([]) - this.ignoredFiles = objects.listProperty(String).convention([]) - this.imports = objects.listProperty(String).convention([]) - this.staticImports = objects.listProperty(String).convention([]) - this.contractsDslDir = objects.directoryProperty() - this.generatedTestSourcesDir = objects.directoryProperty() - this.generatedTestResourcesDir = objects.directoryProperty() - this.stubsOutputDir = objects.directoryProperty() - this.stubsSuffix = objects.property(String).convention("stubs") - this.assertJsonSize = objects.property(Boolean).convention(false) - this.failOnNoContracts = objects.property(Boolean).convention(true) - this.failOnInProgress = objects.property(Boolean).convention(true) - this.contractRepository = new ContractRepository(objects) - this.contractDependency = new Dependency(objects) - this.contractsPath = objects.property(String) - this.contractsMode = objects.property(StubRunnerProperties.StubsMode).convention(StubRunnerProperties.StubsMode.CLASSPATH) - this.packageWithBaseClasses = objects.property(String) - this.baseClassMappings = objects.mapProperty(String, String).convention([:]) - this.excludeBuildFolders = objects.property(Boolean).convention(false) - this.contractsSnapshotCheckSkip = objects.property(Boolean).convention(false) - this.deleteStubsAfterTest = objects.property(Boolean).convention(true) - this.convertToYaml = objects.property(Boolean).convention(false) - this.contractsProperties = objects.mapProperty(String, String).convention([:]) - this.disableStubPublication = objects.property(Boolean).convention(false) + public ContractVerifierExtension(ProjectLayout layout, ObjectFactory objects) { + this.testFramework = objects.property(TestFramework.class).convention(TestFramework.JUNIT); + this.testMode = objects.property(TestMode.class).convention(TestMode.MOCKMVC); + this.basePackageForTests = objects.property(String.class); + this.baseClassForTests = objects.property(String.class); + this.nameSuffixForTests = objects.property(String.class); + this.ruleClassForTests = objects.property(String.class); + this.excludedFiles = objects.listProperty(String.class).convention(new ArrayList<>()); + this.includedFiles = objects.listProperty(String.class).convention(new ArrayList<>()); + this.ignoredFiles = objects.listProperty(String.class).convention(new ArrayList<>()); + this.imports = objects.listProperty(String.class).convention(new ArrayList<>()); + this.staticImports = objects.listProperty(String.class).convention(new ArrayList<>()); + this.contractsDslDir = objects.directoryProperty().convention(layout.getProjectDirectory().dir("src/test/resources/contracts")); + this.generatedTestSourcesDir = objects.directoryProperty().convention(layout.getBuildDirectory().dir("generated-test-sources/contracts")); + this.generatedTestResourcesDir = objects.directoryProperty().convention(layout.getBuildDirectory().dir("generated-test-resources/contracts")); + this.stubsOutputDir = objects.directoryProperty().convention(layout.getBuildDirectory().dir("stubs")); + this.stubsSuffix = objects.property(String.class).convention("stubs"); + this.assertJsonSize = objects.property(Boolean.class).convention(false); + this.failOnNoContracts = objects.property(Boolean.class).convention(true); + this.failOnInProgress = objects.property(Boolean.class).convention(true); + this.contractRepository = objects.newInstance(ContractRepository.class); + this.contractDependency = objects.newInstance(Dependency.class); + this.contractsPath = objects.property(String.class); + this.contractsMode = objects.property(StubRunnerProperties.StubsMode.class).convention(StubRunnerProperties.StubsMode.CLASSPATH); + this.packageWithBaseClasses = objects.property(String.class); + this.baseClassMappings = objects.newInstance(BaseClassMapping.class); + this.excludeBuildFolders = objects.property(Boolean.class).convention(false); + this.contractsSnapshotCheckSkip = objects.property(Boolean.class).convention(false); + this.deleteStubsAfterTest = objects.property(Boolean.class).convention(true); + this.convertToYaml = objects.property(Boolean.class).convention(false); + this.contractsProperties = objects.mapProperty(String.class, String.class).convention(new HashMap<>()); + this.disableStubPublication = objects.property(Boolean.class).convention(false); } - static class Dependency { - @Input - @Optional - Property groupId - @Input - @Optional - Property artifactId - @Input - @Optional - Property version - @Input - @Optional - Property classifier - @Input - @Optional - Property stringNotation + @Deprecated + public void setTargetFramework(TestFramework targetFramework) { + log.warn("Please use the [testFramework] field. This one is deprecated"); + this.testFramework.set(targetFramework); + } + + @Deprecated + public TestFramework getTargetFramework() { + return getTestFramework().get(); + } + + public Property getTestFramework() { + return testFramework; + } + + public void setTestFramework(TestFramework testFramework) { + this.testFramework.set(testFramework); + } + + public void setTestFramework(String testFramework) { + if (testFramework != null) { + this.testFramework.set(TestFramework.valueOf(testFramework.toUpperCase())); + } + } + + public Property getTestMode() { + return testMode; + } + + public void setTestMode(TestMode testMode) { + this.testMode.set(testMode); + } + + public void setTestMode(String testMode) { + if (testMode != null) { + this.testMode.set(TestMode.valueOf(testMode.toUpperCase())); + } + } + + public Property getBasePackageForTests() { + return basePackageForTests; + } + + public void setBasePackageForTests(String basePackageForTests) { + this.basePackageForTests.set(basePackageForTests); + } + + public Property getBaseClassForTests() { + return baseClassForTests; + } + + public void setBaseClassForTests(String baseClassForTests) { + this.baseClassForTests.set(baseClassForTests); + } + + public Property getNameSuffixForTests() { + return nameSuffixForTests; + } + + public void setNameSuffixForTests(String nameSuffixForTests) { + this.nameSuffixForTests.set(nameSuffixForTests); + } + + public Property getRuleClassForTests() { + return ruleClassForTests; + } + + public void setRuleClassForTests(String ruleClassForTests) { + this.ruleClassForTests.set(ruleClassForTests); + } + + public ListProperty getExcludedFiles() { + return excludedFiles; + } + + public void setExcludedFiles(List excludedFiles) { + this.excludedFiles.set(excludedFiles); + } + + public ListProperty getIncludedFiles() { + return includedFiles; + } + + public void setIncludedFiles(List includedFiles) { + this.includedFiles.set(includedFiles); + } + + public ListProperty getIgnoredFiles() { + return ignoredFiles; + } + + public void setIgnoredFiles(List ignoredFiles) { + this.ignoredFiles.set(ignoredFiles); + } + + public ListProperty getImports() { + return imports; + } + + public void setImports(String[] imports) { + this.imports.set(new ArrayList<>(Arrays.asList(imports))); + } + + public void setInputs(List imports) { + this.imports.set(imports); + } + + public ListProperty getStaticImports() { + return staticImports; + } + + public void setStaticImports(String[] staticImports) { + this.staticImports.set(new ArrayList<>(Arrays.asList(staticImports))); + } + + public void setStaticImports(List staticImports) { + this.staticImports.set(staticImports); + } + + public DirectoryProperty getContractsDslDir() { + return contractsDslDir; + } + + public void setContractsDslDir(File contractsDslDir) { + this.contractsDslDir.set(contractsDslDir); + } + + public DirectoryProperty getGeneratedTestSourcesDir() { + return generatedTestSourcesDir; + } + + public void setGeneratedTestSourcesDir(File generatedTestSourcesDir) { + this.generatedTestSourcesDir.set(generatedTestSourcesDir); + } + + public DirectoryProperty getGeneratedTestResourcesDir() { + return generatedTestResourcesDir; + } + + public void setGeneratedTestResourcesDir(File generatedTestResourcesDir) { + this.generatedTestResourcesDir.set(generatedTestResourcesDir); + } + + public DirectoryProperty getStubsOutputDir() { + return stubsOutputDir; + } + + public void setStubsOutputDir(File stubsOutputDir) { + this.stubsOutputDir.set(stubsOutputDir); + } + + public Property getStubsSuffix() { + return stubsSuffix; + } + + public void setStubsSuffix(String stubsSuffix) { + this.stubsSuffix.set(stubsSuffix); + } + + public Property getAssertJsonSize() { + return assertJsonSize; + } + + public void setAssertJsonSize(boolean assertJsonSize) { + this.assertJsonSize.set(assertJsonSize); + } + + public Property getFailOnNoContracts() { + return failOnNoContracts; + } + + public void setFailOnNoContracts(boolean failOnNoContracts) { + this.failOnNoContracts.set(failOnNoContracts); + } + + public Property getFailOnInProgress() { + return failOnInProgress; + } + + public void setFailOnInProgress(boolean failOnInProgress) { + this.failOnInProgress.set(failOnInProgress); + } + + public ContractRepository getContractRepository() { + return contractRepository; + } + + public void contractRepository(Action action) { + action.execute(contractRepository); + } + + public Dependency getContractDependency() { + return contractDependency; + } + + public void contractDependency(Action action) { + action.execute(contractDependency); + } + + public Property getContractsPath() { + return contractsPath; + } + + public void setContractsPath(String contractsPath) { + this.contractsPath.set(contractsPath); + } + + public Property getContractsMode() { + return contractsMode; + } + + public void setContractsMode(StubRunnerProperties.StubsMode contractsMode) { + this.contractsMode.set(contractsMode); + } + + public void setContractsMode(String contractsMode) { + if (contractsMode != null) { + this.contractsMode.set(StubRunnerProperties.StubsMode.valueOf(contractsMode.toUpperCase())); + } + } + + public Property getPackageWithBaseClasses() { + return packageWithBaseClasses; + } + + public void setPackageWithBaseClasses(String packageWithBaseClasses) { + this.packageWithBaseClasses.set(packageWithBaseClasses); + } + + public BaseClassMapping getBaseClassMappings() { + return baseClassMappings; + } + + public void setBaseClassMappings(Map baseClassMappings) { + this.baseClassMappings.getBaseClassMappings().set(baseClassMappings); + } + + public void baseClassMappings(Action action) { + action.execute(baseClassMappings); + } + + public Property getExcludeBuildFolders() { + return excludeBuildFolders; + } + + public void setExcludeBuildFolders(boolean excludeBuildFolders) { + this.excludeBuildFolders.set(excludeBuildFolders); + } + + public Property getContractsSnapshotCheckSkip() { + return contractsSnapshotCheckSkip; + } + + public void setContractsSnapshotCheckSkip(boolean contractsSnapshotCheckSkip) { + this.contractsSnapshotCheckSkip.set(contractsSnapshotCheckSkip); + } + + public Property getDeleteStubsAfterTest() { + return deleteStubsAfterTest; + } + + public void setDeleteStubsAfterTest(boolean deleteStubsAfterTest) { + this.deleteStubsAfterTest.set(deleteStubsAfterTest); + } + + public Property getConvertToYaml() { + return convertToYaml; + } + + public void setConvertToYaml(boolean convertToYaml) { + this.convertToYaml.set(convertToYaml); + } + + public MapProperty getContractsProperties() { + return contractsProperties; + } + + public void setContractsProperties(Map contractsProperties) { + this.contractsProperties.set(contractsProperties); + } + + // use standard setter instead + @Deprecated + public void contractsProperties(Map map) { + contractsProperties.set(map); + } + + public Property getDisableStubPublication() { + return disableStubPublication; + } + + public void setDisableStubPublication(boolean disableStubPublication) { + this.disableStubPublication.set(disableStubPublication); + } + + public static class Dependency { + private Property groupId; + private Property artifactId; + private Property version; + private Property classifier; + private Property stringNotation; @Inject - Dependency(ObjectFactory objects) { - groupId = objects.property(String) - artifactId = objects.property(String) - version = objects.property(String) - classifier = objects.property(String) - stringNotation = objects.property(String) + public Dependency(ObjectFactory objects) { + groupId = objects.property(String.class); + artifactId = objects.property(String.class); + version = objects.property(String.class); + classifier = objects.property(String.class); + stringNotation = objects.property(String.class); + } + + @Input + @Optional + public Property getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId.set(groupId); + } + + @Input + @Optional + public Property getArtifactId() { + return artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId.set(artifactId); + } + + @Input + @Optional + public Property getVersion() { + return version; + } + + public void setVersion(String version) { + this.version.set(version); + } + + @Input + @Optional + public Property getClassifier() { + return classifier; + } + + public void setClassifier(String classifier) { + this.classifier.set(classifier); + } + + @Input + @Optional + public Property getStringNotation() { + return stringNotation; + } + + public void setStringNotation(String stringNotation) { + this.stringNotation.set(stringNotation); } @Override - String toString() { + public String toString() { return "Dependency{" + "groupId=" + groupId.getOrNull() + ", artifactId=" + artifactId.getOrNull() + ", classifier=" + classifier.getOrNull() + ", version=" + version.getOrNull() + ", stringNotation=" + stringNotation.getOrNull() + - '}' - } - - void setGroupId(String groupId) { - this.groupId.set(groupId) - } - - void setArtifactId(String artifactId) { - this.artifactId.set(artifactId) - } - - void setVersion(String version) { - this.version.set(version) - } - - void setClassifier(String classifier) { - this.classifier.set(classifier) - } - - void setStringNotation(String stringNotation) { - this.stringNotation.set(stringNotation) - } - - void setGroupId(GString groupId) { - this.groupId.set(groupId.toString()) - } - - void setArtifactId(GString artifactId) { - this.artifactId.set(artifactId.toString()) - } - - void setVersion(GString version) { - this.version.set(version.toString()) - } - - void setClassifier(GString classifier) { - this.classifier.set(classifier.toString()) - } - - void setStringNotation(GString stringNotation) { - this.stringNotation.set(stringNotation.toString()) + '}'; } } - static class BaseClassMapping { - private final MapProperty delegate + public static class BaseClassMapping { + private final MapProperty baseClassMappings; - private BaseClassMapping(MapProperty delegate) { - this.delegate = delegate + @Inject + public BaseClassMapping(ObjectFactory objects) { + this.baseClassMappings = objects.mapProperty(String.class, String.class).convention(new HashMap<>()); } - void baseClassMapping(String packageRegex, String fqnBaseClass) { - delegate.put(packageRegex, fqnBaseClass) + public MapProperty getBaseClassMappings() { + return baseClassMappings; } - void baseClassMapping(Map mapping) { - delegate.putAll(mapping) + public void baseClassMapping(String packageRegex, String fqnBaseClass) { + baseClassMappings.put(packageRegex, fqnBaseClass); + } + + public void baseClassMapping(Map mapping) { + baseClassMappings.putAll(mapping); } } // This class is used as an input to the tasks, so all fields are marked as `@Input` to allow incremental build - static class ContractRepository { - @Input - @Optional - Property repositoryUrl - @Input - @Optional - Property username - @Input - @Optional - Property password - @Input - @Optional - Property proxyPort - @Input - @Optional - Property proxyHost + public static class ContractRepository { + private Property repositoryUrl; + private Property username; + private Property password; + private Property proxyPort; + private Property proxyHost; /** + * If set to true then will cache the folder where non snapshot contract artifacts got downloaded. + * * Not used any more, as we switched to Gradle's incremental build. */ - @Internal - @Deprecated - boolean cacheDownloadedContracts + private Property cacheDownloadedContracts; @Inject - ContractRepository(ObjectFactory objects) { - this.repositoryUrl = objects.property(String) - this.username = objects.property(String) - this.password = objects.property(String) - this.proxyHost = objects.property(String) - this.proxyPort = objects.property(Integer) + public ContractRepository(ObjectFactory objects) { + this.repositoryUrl = objects.property(String.class); + this.username = objects.property(String.class); + this.password = objects.property(String.class); + this.proxyHost = objects.property(String.class); + this.proxyPort = objects.property(Integer.class); + this.cacheDownloadedContracts = objects.property(Boolean.class).convention(true); + } + + @Input + @Optional + public Property getRepositoryUrl() { + return repositoryUrl; + } + + public void setRepositoryUrl(String repositoryUrl) { + this.repositoryUrl.set(repositoryUrl); + } + + // favor property assignment + @Deprecated + public void repositoryUrl(String repositoryUrl) { + this.repositoryUrl.set(repositoryUrl); + } + + @Input + @Optional + public Property getUsername() { + return username; + } + + public void setUsername(String username) { + this.username.set(username); + } + + // favor property assignment + @Deprecated + public void username(String username) { + this.username.set(username); + } + + @Input + @Optional + public Property getPassword() { + return password; + } + + public void setPassword(String password) { + this.password.set(password); + } + + // favor property assignment + @Deprecated + public void password(String password) { + this.password.set(password); + } + + @Input + @Optional + public Property getProxyHost() { + return proxyHost; + } + + public void setProxyHost(String proxyHost) { + this.proxyHost.set(proxyHost); + } + + // favor property assignment + @Deprecated + public void proxyHost(String proxyHost) { + this.proxyHost.set(proxyHost); + } + + @Input + @Optional + public Property getProxyPort() { + return proxyPort; + } + + // favor unwrapped int + @Deprecated + public void setProxyPort(Integer proxyPort) { + this.proxyPort.set(proxyPort); + } + + public void setProxyPort(int proxyPort) { + Assert.state(0 < proxyPort && proxyPort <= 65536, "Proxy port should be between 1 and 65536"); + this.proxyPort.set(proxyPort); + } + + @Internal + @Deprecated + public Property getCacheDownloadedContracts() { + return cacheDownloadedContracts; + } + + @Deprecated + public void setCacheDownloadedContracts(boolean cacheDownloadedContracts) { + this.cacheDownloadedContracts.set(cacheDownloadedContracts); } @Override - String toString() { + public String toString() { return "ContractRepository{" + "repositoryUrl=" + repositoryUrl.getOrNull() + ", username=" + username.getOrNull() + ", password=" + password.getOrNull() + ", proxyPort=" + proxyPort.getOrNull() + ", proxyHost=" + proxyHost.getOrNull() + - '}' - } - - void setRepositoryUrl(String repositoryUrl) { - this.repositoryUrl.set(repositoryUrl) - } - - void setUsername(String username) { - this.username.set(username) - } - - void setPassword(String password) { - this.password.set(password) - } - - void setProxyHost(String proxyHost) { - this.proxyHost.set(proxyHost) - } - - void setRepositoryUrl(GString repositoryUrl) { - this.repositoryUrl.set(repositoryUrl.toString()) - } - - void setUsername(GString username) { - this.username.set(username.toString()) - } - - void setPassword(GString password) { - this.password.set(password.toString()) - } - - void setProxyHost(GString proxyHost) { - this.proxyHost.set(proxyHost.toString()) - } - - void setProxyPort(Integer proxyPort) { - this.proxyPort.set(proxyPort) - } - - void repositoryUrl(String repositoryUrl) { - this.repositoryUrl.set(repositoryUrl) - } - - void username(String username) { - this.username.set(username) - } - - void password(String password) { - this.password.set(password) - } - - void proxyHost(String proxyHost) { - this.proxyHost.set(proxyHost) - } - - void repositoryUrl(GString repositoryUrl) { - this.repositoryUrl.set(repositoryUrl.toString()) - } - - void username(GString username) { - this.username.set(username.toString()) - } - - void password(GString password) { - this.password.set(password.toString()) - } - - void proxyHost(GString proxyHost) { - this.proxyHost.set(proxyHost.toString()) + ", cacheDownloadedContracts=" + cacheDownloadedContracts.get() + + '}'; } } } \ No newline at end of file diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/GenerateServerTestsTask.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/GenerateServerTestsTask.groovy index 5085546c5f..c18eadaf43 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/GenerateServerTestsTask.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/GenerateServerTestsTask.groovy @@ -151,7 +151,7 @@ class GenerateServerTestsTask extends DefaultTask { staticImports: extension.staticImports, testMode: extension.testMode, testFramework: extension.testFramework, - baseClassMappings: extension.baseClassMappings, + baseClassMappings: extension.baseClassMappings.getBaseClassMappings(), assertJsonSize: extension.assertJsonSize, failOnInProgress: extension.failOnInProgress, diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/SpringCloudContractVerifierGradlePlugin.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/SpringCloudContractVerifierGradlePlugin.groovy index 16b23e1ed5..32025e9c82 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/SpringCloudContractVerifierGradlePlugin.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/main/groovy/org/springframework/cloud/contract/verifier/plugin/SpringCloudContractVerifierGradlePlugin.groovy @@ -55,7 +55,6 @@ class SpringCloudContractVerifierGradlePlugin implements Plugin { this.project = project project.plugins.apply(GroovyPlugin) ContractVerifierExtension extension = project.extensions.create(EXTENSION_NAME, ContractVerifierExtension) - setConfigurationDefaults(extension) TaskProvider copyContracts = createAndConfigureCopyContractsTask(extension) TaskProvider generateClientStubs = createAndConfigureGenerateClientStubs(extension, copyContracts) @@ -101,16 +100,6 @@ class SpringCloudContractVerifierGradlePlugin implements Plugin { } } - private void setConfigurationDefaults(ContractVerifierExtension extension) { - extension.with { - it.contractsDslDir.convention(project.layout.projectDirectory.dir("src/test/resources/contracts")) - it.generatedTestSourcesDir.convention(project.layout.getBuildDirectory().dir("generated-test-sources/contracts")) - it.generatedTestResourcesDir.convention(project.layout.getBuildDirectory().dir("generated-test-resources/contracts")) - it.stubsOutputDir.convention(project.layout.getBuildDirectory().dir("stubs")) - } - } - - private void createGenerateTestsTask(ContractVerifierExtension extension, TaskProvider copyContracts) { TaskProvider task = project.tasks.register(GenerateServerTestsTask.TASK_NAME, GenerateServerTestsTask) task.configure { diff --git a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierSpec.groovy b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierSpec.groovy index 58064d307d..626da379fe 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierSpec.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/groovy/org/springframework/cloud/contract/verifier/plugin/ContractVerifierSpec.groovy @@ -1,5 +1,7 @@ package org.springframework.cloud.contract.verifier.plugin +import org.gradle.api.Project +import org.gradle.api.file.ProjectLayout import org.gradle.api.internal.project.DefaultProject import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.GroovyPlugin @@ -89,16 +91,16 @@ class ContractVerifierSpec extends Specification { def "should compile"() { given: - ObjectFactory objectFactory = Stub(ObjectFactory) - ContractVerifierExtension extension = new ContractVerifierExtension(objectFactory) + project.plugins.apply(SpringCloudContractVerifierGradlePlugin) + ContractVerifierExtension extension = project.getExtensions().findByType(ContractVerifierExtension) extension.with { // tag::package_with_base_classes[] - packageWithBaseClasses.set('com.example.base') + packageWithBaseClasses = 'com.example.base' // end::package_with_base_classes[] // tag::base_class_mappings[] - baseClassForTests.set("com.example.FooBase") + baseClassForTests = "com.example.FooBase" baseClassMappings { baseClassMapping('.*/com/.*', 'com.example.ComBase') baseClassMapping('.*/bar/.*': 'com.example.BarBase')