From 04721e13b14c5eaf69537a4ef78ab343ab2eaf1b Mon Sep 17 00:00:00 2001 From: mzielinski Date: Tue, 3 Nov 2015 23:07:26 +0100 Subject: [PATCH] #166 directory structure of dsl groovy scripts should be used in generated specifications --- .../io/codearte/accurest/TestGenerator.groovy | 17 +++++++++++++---- .../io/codearte/accurest/util/NamesUtil.groovy | 11 +++++++++++ .../accurest/GeneratorScannerSpec.groovy | 16 +++++++++++++++- .../stubs/package/v1/exceptions/testv1.groovy | 0 .../stubs/package/v2/exceptions/testv2.groovy | 0 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 accurest-core/src/test/resources/directory/with/stubs/package/v1/exceptions/testv1.groovy create mode 100644 accurest-core/src/test/resources/directory/with/stubs/package/v2/exceptions/testv2.groovy diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy index fb612cebd1..a502448bc4 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy @@ -7,6 +7,9 @@ import org.codehaus.plexus.util.DirectoryScanner import java.util.concurrent.atomic.AtomicInteger import static io.codearte.accurest.util.NamesUtil.afterLast +import static io.codearte.accurest.util.NamesUtil.beforeLast +import static io.codearte.accurest.util.NamesUtil.directoryToPackage + /** * @author Jakub Kubrynski */ @@ -62,14 +65,20 @@ class TestGenerator { } if (filesToClass.size()) { def className = afterLast(includedDirectoryRelativePath, File.separator) + configProperties.targetFramework.classNameSuffix - def classBytes = generator.buildClass(filesToClass, className, packageNameForClass).bytes - saver.saveClassFile(className, packageNameForClass, classBytes) + def packageName = buildPackage(packageNameForClass, includedDirectoryRelativePath) + def classBytes = generator.buildClass(filesToClass, className, packageName).bytes + saver.saveClassFile(className, packageName, classBytes) counter.incrementAndGet() } } } - - private String normalizePath(String path) { + + private static String buildPackage(final String packageNameForClass, final String includedDirectoryRelativePath) { + String directory = beforeLast(includedDirectoryRelativePath, File.separator) + return "$packageNameForClass.${directoryToPackage(directory)}" + } + + private static String normalizePath(String path) { return FilenameUtils.separatorsToUnix(path) } } \ No newline at end of file diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/NamesUtil.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/NamesUtil.groovy index 4817cc4816..88172ca90b 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/util/NamesUtil.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/NamesUtil.groovy @@ -5,6 +5,13 @@ package io.codearte.accurest.util */ class NamesUtil { + static String beforeLast(String string, String separator) { + if (string?.indexOf(separator) > -1) { + return string.substring(0, string.lastIndexOf(separator)) + } + return string + } + static String afterLast(String string, String separator) { if (string?.indexOf(separator) > -1) { return string.substring(string.lastIndexOf(separator) + 1) @@ -42,4 +49,8 @@ class NamesUtil { static String packageToDirectory(String packageName) { return packageName.replaceAll('\\.', File.separator) } + + static String directoryToPackage(String directory) { + return directory.replace(File.separator, '.') + } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy index 6a478e3d92..523dd5ba4f 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy @@ -16,7 +16,7 @@ class GeneratorScannerSpec extends Specification { when: testGenerator.generateTestClasses("com.ofg") then: - 3 * classGenerator.buildClass(_, _, _) >> "qwerty" + 5 * classGenerator.buildClass(_, _, _) >> "qwerty" } def "should filter other directory"() { @@ -30,6 +30,7 @@ class GeneratorScannerSpec extends Specification { testGenerator.generateTestClasses("com.ofg") then: 1 * classGenerator.buildClass(_, 'differentSpec', _) >> "qwerty" + 2 * classGenerator.buildClass(_, 'exceptionsSpec', _) >> "qwerty" } def "should ignore file"() { @@ -45,4 +46,17 @@ class GeneratorScannerSpec extends Specification { then: 1 * classGenerator.buildClass({ it.size() == 1 }, 'otherSpec', _) >> "sample.groovy" } + + def "should create class with full package"() { + given: + AccurestConfigProperties properties = new AccurestConfigProperties() + properties.contractsDslDir = new File(this.getClass().getResource("/directory/with/stubs/package").toURI()) + TestGenerator testGenerator = new TestGenerator(properties, classGenerator, Stub(FileSaver)) + when: + testGenerator.generateTestClasses("com.ofg") + then: + 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v1') >> "spec1" + 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v2') >> "spec2" + } + } diff --git a/accurest-core/src/test/resources/directory/with/stubs/package/v1/exceptions/testv1.groovy b/accurest-core/src/test/resources/directory/with/stubs/package/v1/exceptions/testv1.groovy new file mode 100644 index 0000000000..e69de29bb2 diff --git a/accurest-core/src/test/resources/directory/with/stubs/package/v2/exceptions/testv2.groovy b/accurest-core/src/test/resources/directory/with/stubs/package/v2/exceptions/testv2.groovy new file mode 100644 index 0000000000..e69de29bb2