From 04721e13b14c5eaf69537a4ef78ab343ab2eaf1b Mon Sep 17 00:00:00 2001 From: mzielinski Date: Tue, 3 Nov 2015 23:07:26 +0100 Subject: [PATCH 1/3] #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 From 9b6451cdbee5b3b8ec986a9dd060dd71f9c0125c Mon Sep 17 00:00:00 2001 From: Maciej Zielinski Date: Wed, 4 Nov 2015 09:11:02 +0100 Subject: [PATCH 2/3] #168 add intends in "should create class with full package" test --- .../io/codearte/accurest/GeneratorScannerSpec.groovy | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 523dd5ba4f..fb573f7a1b 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy @@ -49,14 +49,14 @@ class GeneratorScannerSpec extends Specification { 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)) + 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") + testGenerator.generateTestClasses("com.ofg") then: - 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v1') >> "spec1" - 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v2') >> "spec2" + 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v1') >> "spec1" + 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v2') >> "spec2" } } From d4c59421660fdd15ae8f8ff967f7478080ea8044 Mon Sep 17 00:00:00 2001 From: Maciej Zielinski Date: Wed, 4 Nov 2015 10:02:19 +0100 Subject: [PATCH 3/3] #168 fix bug when there is not subdirectories --- .../main/groovy/io/codearte/accurest/TestGenerator.groovy | 4 ++-- .../main/groovy/io/codearte/accurest/util/NamesUtil.groovy | 2 +- .../io/codearte/accurest/GeneratorScannerSpec.groovy | 7 ++++--- .../directory/with/stubs/package/exceptions/test.groovy | 0 4 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 accurest-core/src/test/resources/directory/with/stubs/package/exceptions/test.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 719b5cadca..339def4b8c 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/TestGenerator.groovy @@ -76,8 +76,8 @@ class TestGenerator { private static String buildPackage(final String packageNameForClass, final String includedDirectoryRelativePath) { String directory = beforeLast(includedDirectoryRelativePath, File.separator) - return "$packageNameForClass.${directoryToPackage(directory)}" - } + return !directory.empty ? "$packageNameForClass.${directoryToPackage(directory)}" : packageNameForClass + } private static String normalizePath(String path) { return FilenameUtils.separatorsToUnix(path) 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 5b19c72645..23e8c54479 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 @@ -9,7 +9,7 @@ class NamesUtil { if (string?.indexOf(separator) > -1) { return string.substring(0, string.lastIndexOf(separator)) } - return string + return '' } static String afterLast(String string, String 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 fb573f7a1b..f11187e3d2 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/GeneratorScannerSpec.groovy @@ -7,7 +7,7 @@ class GeneratorScannerSpec extends Specification { private SingleTestGenerator classGenerator = Mock(SingleTestGenerator) - def "should find all .json files and generate 3 classes for them"() { + def "should find all .json files and generate 6 classes for them"() { given: File resource = new File(this.getClass().getResource("/directory/with/stubs/stubsRepositoryIndicator").toURI()) AccurestConfigProperties properties = new AccurestConfigProperties() @@ -16,7 +16,7 @@ class GeneratorScannerSpec extends Specification { when: testGenerator.generateTestClasses("com.ofg") then: - 5 * classGenerator.buildClass(_, _, _) >> "qwerty" + 6 * classGenerator.buildClass(_, _, _) >> "qwerty" } def "should filter other directory"() { @@ -30,7 +30,7 @@ class GeneratorScannerSpec extends Specification { testGenerator.generateTestClasses("com.ofg") then: 1 * classGenerator.buildClass(_, 'differentSpec', _) >> "qwerty" - 2 * classGenerator.buildClass(_, 'exceptionsSpec', _) >> "qwerty" + 3 * classGenerator.buildClass(_, 'exceptionsSpec', _) >> "qwerty" } def "should ignore file"() { @@ -55,6 +55,7 @@ class GeneratorScannerSpec extends Specification { when: testGenerator.generateTestClasses("com.ofg") then: + 1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg') >> "spec" 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/exceptions/test.groovy b/accurest-core/src/test/resources/directory/with/stubs/package/exceptions/test.groovy new file mode 100644 index 0000000000..e69de29bb2