basePackageForTests can be configured automatically

without this change we always set the default value of the package with generated tests, unless a value overrides it.
with this change the flow for setting the generated tests package name will look like this:

- pick basePackageForTests
- if basePackageForTests wasn't set pick the package from baseClassForTests
- if baseClassForTests wasn't set pick packageWithBaseClasses
- if nothing got set pick the default org.springframework.cloud.contract.verifier.tests

The rationale behind this change is such that often in the base class you would like to have package scoped methods and the fact that the tests are always generated in the default location requires to make these methods public or always override this property.

fixes #260
This commit is contained in:
Marcin Grzejszczak
2017-08-24 12:08:02 +02:00
parent a1839450fb
commit 3b88fea8bd
7 changed files with 170 additions and 43 deletions

View File

@@ -16,9 +16,14 @@
package org.springframework.cloud.contract.verifier
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger
import com.google.common.collect.ListMultimap
import groovy.transform.PackageScope
import org.apache.commons.lang3.StringUtils
import org.springframework.cloud.contract.spec.ContractVerifierException
import org.springframework.cloud.contract.verifier.builder.JavaTestGenerator
import org.springframework.cloud.contract.verifier.builder.SingleTestGenerator
@@ -27,20 +32,21 @@ import org.springframework.cloud.contract.verifier.file.ContractFileScanner
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.core.io.support.SpringFactoriesLoader
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger
import static org.springframework.cloud.contract.verifier.util.NamesUtil.*
import static org.springframework.cloud.contract.verifier.util.NamesUtil.afterLast
import static org.springframework.cloud.contract.verifier.util.NamesUtil.beforeLast
import static org.springframework.cloud.contract.verifier.util.NamesUtil.convertIllegalPackageChars
import static org.springframework.cloud.contract.verifier.util.NamesUtil.directoryToPackage
import static org.springframework.cloud.contract.verifier.util.NamesUtil.toLastDot
/**
* @author Jakub Kubrynski, codearte.io
*/
class TestGenerator {
private final ContractVerifierConfigProperties configProperties
private final String DEFAULT_CLASS_PREFIX = "ContractVerifier"
private static final String DEFAULT_CLASS_PREFIX = "ContractVerifier"
private static final String DEFAULT_TEST_PACKAGE = "org.springframework.cloud.contract.verifier.tests"
private final ContractVerifierConfigProperties configProperties
private AtomicInteger counter = new AtomicInteger()
private SingleTestGenerator generator
private FileSaver saver
@@ -73,10 +79,21 @@ class TestGenerator {
}
int generate() {
generateTestClasses(configProperties.basePackageForTests)
generateTestClasses(basePackageName())
return counter.get()
}
private String basePackageName() {
if (configProperties.basePackageForTests) {
return configProperties.basePackageForTests
} else if (configProperties.baseClassForTests) {
return toLastDot(configProperties.baseClassForTests)
} else if (configProperties.packageWithBaseClasses) {
return configProperties.packageWithBaseClasses
}
return DEFAULT_TEST_PACKAGE
}
@PackageScope
void generateTestClasses(final String basePackageName) {
ListMultimap<Path, ContractMetadata> contracts = contractFileScanner.findContracts()

View File

@@ -572,6 +572,98 @@ class SingleTestGeneratorSpec extends Specification {
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from basePackageForTests when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
basePackageForTests: "a.b", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from baseClassForTests's package when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
baseClassForTests: "a.b.SomeClass", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from packageWithBaseClasses when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
packageWithBaseClasses: "a.b", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a default folder when no property was passed for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "org/springframework/cloud/contract/verifier/tests/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}