Introduced configuration

This commit is contained in:
Jakub Kubrynski
2014-12-07 00:00:06 +01:00
parent aaa51302fd
commit c6a75b7dcf
6 changed files with 97 additions and 31 deletions

View File

@@ -1,19 +0,0 @@
package eu.coderate.accurest
import static eu.coderate.accurest.builder.ClassBuilder.createClass
import static eu.coderate.accurest.builder.MethodBuilder.createVoidMethod
class Generator {
public static void main(String[] args) {
print createClass('MyTest', 'io.test', "com.test.BaseClass")
.addImport('org.junit.Test')
.addImport('org.junit.Rule')
.addStaticImport('org.hamcrest.Matchers.*')
.addStaticImport('com.jayway.restassured.RestAssured.*')
.addStaticImport('com.jayway.restassured.matcher.RestAssuredMatchers.*')
.addRule('com.test.MyRule')
.addMethod(createVoidMethod('shouldInvokeService'))
.build()
}
}

View File

@@ -0,0 +1,49 @@
package eu.coderate.accurest
import eu.coderate.accurest.util.NamesUtil
import static eu.coderate.accurest.builder.ClassBuilder.createClass
import static eu.coderate.accurest.builder.MethodBuilder.createVoidMethod
/**
* @author Jakub Kubrynski
*/
class TestGenerator {
private final String mocksBaseDirectory
private final String basePackageForTests
private final String baseClassForTests
private final String ruleClassForTests
TestGenerator(String mocksBaseDirectory, String basePackageForTests, String baseClassForTests, String ruleClassForTests) {
this.mocksBaseDirectory = getClass().getClassLoader().getResource(mocksBaseDirectory).path
this.basePackageForTests = basePackageForTests
this.baseClassForTests = baseClassForTests
this.ruleClassForTests = ruleClassForTests
}
public String generate() {
StringBuilder builder = new StringBuilder()
List<String> files = new File(mocksBaseDirectory).list()
files.each {
builder << addClass(NamesUtil.capitalize(NamesUtil.toLastDot(it)))
}
return builder
}
private String addClass(String className) {
createClass(className, basePackageForTests, baseClassForTests)
.addImport('org.junit.Test')
.addImport('org.junit.Rule')
.addStaticImport('org.hamcrest.Matchers.*')
.addStaticImport('com.jayway.restassured.RestAssured.*')
.addStaticImport('com.jayway.restassured.matcher.RestAssuredMatchers.*')
.addRule('com.test.MyRule')
.addMethod(createVoidMethod('shouldInvokeService'))
.build()
}
public static void main(String[] args) {
print new TestGenerator("wiremocks", 'io.test', 'com.test.BaseClass', "").generate()
}
}

View File

@@ -19,7 +19,7 @@ class ClassBuilder {
if (baseClass) {
imports << baseClass
}
this.baseClass = NamesUtil.getName(baseClass)
this.baseClass = NamesUtil.afterLastDot(baseClass)
this.packageName = packageName
this.className = className
}
@@ -49,7 +49,7 @@ class ClassBuilder {
ClassBuilder addRule(String ruleClass) {
imports << ruleClass
rules << NamesUtil.getName(ruleClass)
rules << NamesUtil.afterLastDot(ruleClass)
return this
}
@@ -78,7 +78,7 @@ class ClassBuilder {
clazz.startBlock()
rules.sort().each {
clazz.addLine("@Rule")
clazz.addLine("public " + it + " " + NamesUtil.fieldName(it) + " = new $it();")
clazz.addLine("public " + it + " " + NamesUtil.uncapitalize(it) + " = new $it();")
}
clazz.endBlock()
clazz.addEmptyLine()

View File

@@ -4,19 +4,33 @@ package eu.coderate.accurest.util
* @author Jakub Kubrynski
*/
class NamesUtil {
static String getName(String qualifiedName) {
if (qualifiedName?.indexOf('.') > -1) {
return qualifiedName.substring(qualifiedName.lastIndexOf('.')+1)
static String afterLastDot(String string) {
if (string?.indexOf('.') > -1) {
return string.substring(string.lastIndexOf('.')+1)
}
return qualifiedName
return string
}
static String fieldName(String className) {
if (className) {
static String uncapitalize(String className) {
if (!className) {
return className
}
String name = getName(className)
String firstChar = name.charAt(0).toLowerCase() as String
return firstChar + name.substring(1)
String firstChar = className.charAt(0).toLowerCase() as String
return firstChar + className.substring(1)
}
static String capitalize(String className) {
if (!className) {
return className
}
String firstChar = className.charAt(0).toUpperCase() as String
return firstChar + className.substring(1)
}
static String toLastDot(String string) {
if (string?.indexOf('.') > -1) {
return string.substring(0, string.lastIndexOf('.'))
}
return string
}
}

View File

@@ -0,0 +1,13 @@
{
"request": {
"method": "GET",
"url": "/some/thing"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": {
"Content-Type": "text/plain"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"request": {
"method": "GET",
"url": "/some/thing2"
},
"response": {
"status": 400
}
}