Merge pull request #168 from mzielinski/master
#166 - Directory structure of dsl groovy scripts should be used in generated specifications
This commit is contained in:
@@ -8,6 +8,9 @@ import java.nio.charset.StandardCharsets
|
||||
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
|
||||
*/
|
||||
@@ -63,14 +66,20 @@ class TestGenerator {
|
||||
}
|
||||
if (filesToClass.size()) {
|
||||
def className = afterLast(includedDirectoryRelativePath, File.separator) + configProperties.targetFramework.classNameSuffix
|
||||
def classBytes = generator.buildClass(filesToClass, className, packageNameForClass).getBytes(StandardCharsets.UTF_8)
|
||||
saver.saveClassFile(className, packageNameForClass, classBytes)
|
||||
def packageName = buildPackage(packageNameForClass, includedDirectoryRelativePath)
|
||||
def classBytes = generator.buildClass(filesToClass, className, packageName).getBytes(StandardCharsets.UTF_8)
|
||||
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 !directory.empty ? "$packageNameForClass.${directoryToPackage(directory)}" : packageNameForClass
|
||||
}
|
||||
|
||||
private static String normalizePath(String path) {
|
||||
return FilenameUtils.separatorsToUnix(path)
|
||||
}
|
||||
}
|
||||
@@ -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 ''
|
||||
}
|
||||
|
||||
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.replace('.' as char, File.separatorChar)
|
||||
}
|
||||
|
||||
static String directoryToPackage(String directory) {
|
||||
return directory.replace(File.separator, '.')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
3 * classGenerator.buildClass(_, _, _) >> "qwerty"
|
||||
6 * 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"
|
||||
3 * classGenerator.buildClass(_, 'exceptionsSpec', _) >> "qwerty"
|
||||
}
|
||||
|
||||
def "should ignore file"() {
|
||||
@@ -45,4 +46,18 @@ 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') >> "spec"
|
||||
1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v1') >> "spec1"
|
||||
1 * classGenerator.buildClass(_, 'exceptionsSpec', 'com.ofg.v2') >> "spec2"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user