From 46056aa7837acc7100ebc286d08a34c612588763 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Sun, 16 Dec 2018 23:28:02 +0100 Subject: [PATCH] Added a test that ensures that invalid class name gets converted --- .../cloud/contract/verifier/TestGenerator.groovy | 4 ++-- .../verifier/GeneratorScannerSpec.groovy | 11 +++++++++++ .../with/name-with-hyphen/car-rental/diff.groovy | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-contract-verifier/src/test/resources/directory/with/name-with-hyphen/car-rental/diff.groovy diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy index ca01686508..4b7beb8cd8 100755 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy @@ -105,7 +105,7 @@ class TestGenerator { void generateTestClasses(final String basePackageName) { ListMultimap contracts = contractFileScanner.findContracts() contracts.asMap().entrySet().each { - Map.Entry> entry -> processIncludedDirectory(relativizeContractPath(entry), entry.getValue(), basePackageName) + Map.Entry> entry -> processIncludedDirectory(relativizeContractPath(entry), (Collection) entry.getValue(), basePackageName) } } @@ -129,7 +129,7 @@ class TestGenerator { Path dir = saver.generateTestBaseDir(basePackageNameForClass, convertIllegalPackageChars(includedDirectoryRelativePath.toString())) Path classPath = saver.pathToClass(dir, convertedClassName) def classBytes = generator.buildClass(configProperties, contracts, includedDirectoryRelativePath, - new SingleTestGenerator.GeneratedClassData(className, packageName, classPath)).getBytes(StandardCharsets.UTF_8) + new SingleTestGenerator.GeneratedClassData(convertedClassName, packageName, classPath)).getBytes(StandardCharsets.UTF_8) saver.saveClassFile(classPath, classBytes) counter.incrementAndGet() } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/GeneratorScannerSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/GeneratorScannerSpec.groovy index a5d185a206..26059fc4c2 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/GeneratorScannerSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/GeneratorScannerSpec.groovy @@ -53,4 +53,15 @@ class GeneratorScannerSpec extends Specification { 1 * classGenerator.buildClass(_, _, _, { SingleTestGenerator.GeneratedClassData it -> it.className == 'exceptionsSpec' && it.classPackage == 'org.springframework.cloud.contract.verifier.v2'} ) >> "spec2" } + def "should create class with name with hyphen"() { + given: + ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(testFramework: SPOCK) + properties.contractsDslDir = new File(this.getClass().getResource("/directory/with/name-with-hyphen").toURI()) + TestGenerator testGenerator = new TestGenerator(properties, classGenerator, Stub(FileSaver)) + when: + testGenerator.generateTestClasses("org.springframework.cloud.contract.verifier") + then: + 1 * classGenerator.buildClass(_, _, _, { SingleTestGenerator.GeneratedClassData it -> it.className == 'car_rentalSpec' && it.classPackage == 'org.springframework.cloud.contract.verifier'} ) >> "spec" + } + } diff --git a/spring-cloud-contract-verifier/src/test/resources/directory/with/name-with-hyphen/car-rental/diff.groovy b/spring-cloud-contract-verifier/src/test/resources/directory/with/name-with-hyphen/car-rental/diff.groovy new file mode 100644 index 0000000000..14129e130c --- /dev/null +++ b/spring-cloud-contract-verifier/src/test/resources/directory/with/name-with-hyphen/car-rental/diff.groovy @@ -0,0 +1,16 @@ +/* + * Copyright 2013-2017 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. + */ +