diff --git a/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/DslToWireMockConverter.groovy b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/DslToWireMockConverter.groovy index 89b2b381ce..0279bbcebe 100644 --- a/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/DslToWireMockConverter.groovy +++ b/accurest-converters/src/main/groovy/io/codearte/accurest/wiremock/DslToWireMockConverter.groovy @@ -2,7 +2,7 @@ package io.codearte.accurest.wiremock import groovy.transform.CompileStatic import io.codearte.accurest.dsl.GroovyDsl -import org.codehaus.groovy.control.CompilerConfiguration +import io.codearte.accurest.util.AccurestDslConverter @CompileStatic abstract class DslToWireMockConverter implements SingleFileConverter { @@ -18,6 +18,6 @@ abstract class DslToWireMockConverter implements SingleFileConverter { } protected GroovyDsl createGroovyDSLfromStringContent(String groovyDslAsString) { - return (GroovyDsl) new GroovyShell(this.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate("$groovyDslAsString") + return AccurestDslConverter.convert(groovyDslAsString) } } diff --git a/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WireMockToDslConverterSpec.groovy b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WireMockToDslConverterSpec.groovy index 80e8592276..5e71f81605 100755 --- a/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WireMockToDslConverterSpec.groovy +++ b/accurest-converters/src/test/groovy/io/codearte/accurest/wiremock/WireMockToDslConverterSpec.groovy @@ -2,6 +2,7 @@ package io.codearte.accurest.wiremock import com.github.tomakehurst.wiremock.stubbing.StubMapping import io.codearte.accurest.dsl.GroovyDsl +import io.codearte.accurest.util.AccurestDslConverter import spock.lang.Specification class WireMockToDslConverterSpec extends Specification { @@ -67,7 +68,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - new GroovyShell(this.class.classLoader).evaluate( + AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") == expectedGroovyDsl @@ -121,7 +122,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - new GroovyShell(this.class.classLoader).evaluate( + AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") == expectedGroovyDsl @@ -172,7 +173,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - new GroovyShell(this.class.classLoader).evaluate( + AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") == expectedGroovyDsl @@ -226,7 +227,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - new GroovyShell(this.class.classLoader).evaluate( + AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") == expectedGroovyDsl @@ -291,7 +292,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - new GroovyShell(this.class.classLoader).evaluate( + AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") == expectedGroovyDsl @@ -329,7 +330,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") @@ -369,7 +370,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") @@ -410,7 +411,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") @@ -450,7 +451,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") @@ -490,7 +491,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") @@ -528,7 +529,7 @@ class WireMockToDslConverterSpec extends Specification { when: String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub) then: - GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate( + GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert( """ io.codearte.accurest.dsl.GroovyDsl.make { $groovyDsl }""") diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy index 678331c5eb..ee8a8e64b1 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy @@ -10,7 +10,7 @@ import io.codearte.accurest.config.TestFramework import io.codearte.accurest.config.TestMode import io.codearte.accurest.dsl.GroovyDsl import io.codearte.accurest.file.Contract -import org.codehaus.groovy.control.CompilerConfiguration +import io.codearte.accurest.util.AccurestDslConverter import static io.codearte.accurest.builder.ClassBuilder.createClass import static io.codearte.accurest.builder.MethodBuilder.createTestMethod @@ -58,7 +58,7 @@ class SingleTestGenerator { Map contracts = listOfFiles.collectEntries { File stubsFile = it.path.toFile() log.debug("Stub content from file [${stubsFile.text}]") - GroovyDsl stubContent = new GroovyShell(delegate.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate(stubsFile) + GroovyDsl stubContent = AccurestDslConverter.convert(stubsFile) TestType testType = (stubContent.input || stubContent.outputMessage) ? TestType.MESSAGING : TestType.HTTP return [(new ParsedDsl(it, stubContent, stubsFile)) : testType] } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/file/ContractFileScanner.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/file/ContractFileScanner.groovy index 5e125f0f31..cc00799c08 100755 --- a/accurest-core/src/main/groovy/io/codearte/accurest/file/ContractFileScanner.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/file/ContractFileScanner.groovy @@ -68,7 +68,7 @@ class ContractFileScanner { } private boolean hasScenarioFilenamePattern(Path path) { - SCENARIO_STEP_FILENAME_PATTERN.matcher(path.fileName.toString()).matches() + return SCENARIO_STEP_FILENAME_PATTERN.matcher(path.fileName.toString()).matches() } boolean matchesPattern(File file, Set excludeMatchers) { @@ -81,6 +81,6 @@ class ContractFileScanner { } private boolean isContractFile(File file) { - file.isFile() && FilenameUtils.getExtension(file.toString()).equalsIgnoreCase("groovy") + return file.isFile() && FilenameUtils.getExtension(file.toString()).equalsIgnoreCase("groovy") } } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/util/AccurestDslConverter.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/util/AccurestDslConverter.groovy new file mode 100644 index 0000000000..5f865ba24b --- /dev/null +++ b/accurest-core/src/main/groovy/io/codearte/accurest/util/AccurestDslConverter.groovy @@ -0,0 +1,26 @@ +package io.codearte.accurest.util + +import groovy.transform.CompileStatic +import io.codearte.accurest.dsl.GroovyDsl +import org.codehaus.groovy.control.CompilerConfiguration + +/** + * Converts to Groovy DSL + * + * @author Marcin Grzejszczak + */ +@CompileStatic +class AccurestDslConverter { + + static GroovyDsl convert(String dsl) { + return groovyShell().evaluate(dsl) as GroovyDsl + } + + static GroovyDsl convert(File dsl) { + return groovyShell().evaluate(dsl) as GroovyDsl + } + + private static GroovyShell groovyShell() { + return new GroovyShell(AccurestDslConverter.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding: 'UTF-8')) + } +} diff --git a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRepository.groovy b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRepository.groovy index 49a06075be..78527dbd6c 100644 --- a/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRepository.groovy +++ b/stub-runner/stub-runner/src/main/groovy/io/codearte/accurest/stubrunner/StubRepository.groovy @@ -4,8 +4,7 @@ import groovy.transform.CompileStatic import groovy.transform.PackageScope import groovy.util.logging.Slf4j import io.codearte.accurest.dsl.GroovyDsl -import org.codehaus.groovy.control.CompilerConfiguration - +import io.codearte.accurest.util.AccurestDslConverter /** * Wraps the folder with WireMock mappings. */ @@ -68,8 +67,7 @@ class StubRepository { descriptorsDirectory.eachFileRecurse { File file -> if (isAccurestDescriptor(file)) { try { - mappingDescriptors << - ((new GroovyShell(delegate.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate(file)) as GroovyDsl) + mappingDescriptors << AccurestDslConverter.convert(file) } catch (Exception e) { log.warn("Exception occurred while trying to parse file [$file]", e) }