Generate test suites

This commit is contained in:
Andrey Breslav
2012-06-13 16:07:27 +04:00
parent ca8ae7a4ae
commit 8b56d47af5
3 changed files with 67 additions and 26 deletions
@@ -37,6 +37,10 @@ public class Printer {
public void print(Object... objects) {
out.append(indent);
printWithNoIndent(objects);
}
public void printWithNoIndent(Object... objects) {
for (Object object : objects) {
out.append(object);
}
@@ -36,12 +36,14 @@ public class TestDataSource {
private final boolean recursive;
private final FileFilter filter;
private final String doTestMethodName;
private final String testClassName;
public TestDataSource(@NotNull File rootFile, boolean recursive, @NotNull FileFilter filter, String doTestMethodName) {
this.rootFile = rootFile;
this.recursive = recursive;
this.filter = filter;
this.doTestMethodName = doTestMethodName;
this.testClassName = StringUtil.capitalize(rootFile.getName());
}
public Collection<TestDataFile> getFiles() {
@@ -72,10 +74,14 @@ public class TestDataSource {
}
public void getAllTestsPresentCheck(@NotNull Printer p) {
p.println("allTestsPresent(new File(\"" + rootFile + "\"), " + recursive + ");");
p.println("allTestsPresent(" + getTestClassName() + ".class, new File(\"" + rootFile + "\"), " + recursive + ");");
}
public String getAllTestsPresentMethodName() {
return "allTestsPresentIn" + StringUtil.capitalize(rootFile.getName());
}
public String getTestClassName() {
return testClassName;
}
}
@@ -24,6 +24,7 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
/**
* @author abreslav
@@ -40,8 +41,8 @@ public class TestGenerator {
private final String baseDir;
private final String testDataFileExtension;
private final String testClassPackage;
private final String testClassName;
private final String suiteClassPackage;
private final String suiteClassName;
private final String baseTestClassPackage;
private final String baseTestClassName;
private final Collection<TestDataSource> testDataSources;
@@ -50,8 +51,8 @@ public class TestGenerator {
public TestGenerator(
@NotNull String baseDir,
@NotNull String testDataFileExtension,
@NotNull String testClassPackage,
@NotNull String testClassName,
@NotNull String suiteClassPackage,
@NotNull String suiteClassName,
@NotNull String baseTestClassPackage,
@NotNull String baseTestClassName,
@NotNull Collection<TestDataSource> testDataSources,
@@ -59,8 +60,8 @@ public class TestGenerator {
) {
this.baseDir = baseDir;
this.testDataFileExtension = testDataFileExtension;
this.testClassPackage = testClassPackage;
this.testClassName = testClassName;
this.suiteClassPackage = suiteClassPackage;
this.suiteClassName = suiteClassName;
this.baseTestClassPackage = baseTestClassPackage;
this.baseTestClassName = baseTestClassName;
this.testDataSources = testDataSources;
@@ -72,10 +73,12 @@ public class TestGenerator {
Printer p = new Printer(out);
p.print(FileUtil.loadFile(new File("injector-generator/copyright.txt")));
p.println("package ", testClassPackage, ";");
p.println("package ", suiteClassPackage, ";");
p.println();
p.println("import org.junit.Assert;");
p.println("import org.junit.Test;");
p.println("import org.junit.runner.RunWith;");
p.println("import org.junit.runners.Suite;");
p.println();
p.println("import java.io.File;");
@@ -88,24 +91,55 @@ public class TestGenerator {
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
p.println();
p.println("/* This class is generated by " + generatorName + ". DO NOT MODIFY MANUALLY */");
p.println("public class ", testClassName, " extends ", baseTestClassName, " {");
p.println("/* This class is generated by ", generatorName, ". DO NOT MODIFY MANUALLY */");
generateSuiteAnnotations(p);
p.println("public class ", suiteClassName, " {");
p.pushIndent();
for (TestDataSource testDataSource : testDataSources) {
generateTestClass(p, testDataSource);
p.println();
}
p.popIndent();
p.println("}");
String testSourceFilePath = baseDir + suiteClassPackage.replace(".", "/") + "/" + suiteClassName + ".java";
FileUtil.writeToFile(new File(testSourceFilePath), out.toString());
}
private void generateSuiteAnnotations(Printer p) {
p.println("@RunWith(Suite.class)");
p.println("@Suite.SuiteClasses({");
p.pushIndent();
for (Iterator<TestDataSource> iterator = testDataSources.iterator(); iterator.hasNext(); ) {
TestDataSource testDataSource = iterator.next();
p.print(suiteClassName, ".", testDataSource.getTestClassName(), ".class");
if (iterator.hasNext()) {
p.printWithNoIndent(",");
}
p.println();
}
p.popIndent();
p.println("})");
}
private void generateTestClass(Printer p, TestDataSource testDataSource) {
p.println("public static class ", testDataSource.getTestClassName(), " extends ", baseTestClassName, " {");
p.pushIndent();
Collection<TestDataFile> files = Lists.newArrayList();
for (TestDataSource testDataSource : testDataSources) {
files.addAll(testDataSource.getFiles());
files.addAll(testDataSource.getFiles());
p.println("@Test");
p.println("public void " + testDataSource.getAllTestsPresentMethodName() + "() throws Exception {");
p.pushIndent();
p.println("@Test");
p.println("public void " + testDataSource.getAllTestsPresentMethodName() + "() throws Exception {");
p.pushIndent();
testDataSource.getAllTestsPresentCheck(p);
testDataSource.getAllTestsPresentCheck(p);
p.popIndent();
p.println("}");
p.println();
}
p.popIndent();
p.println("}");
p.println();
for (TestDataFile file : files) {
p.println("@Test");
@@ -123,16 +157,13 @@ public class TestGenerator {
p.popIndent();
p.println("}");
String testSourceFilePath = baseDir + testClassPackage.replace(".", "/") + "/" + testClassName + ".java";
FileUtil.writeToFile(new File(testSourceFilePath), out.toString());
}
private void generateAllTestsPresent(Printer p) {
String[] methodText = new String[] {
"public static void allTestsPresent(File testDataDir, boolean recursive) {",
"public static void allTestsPresent(Class<?> clazz, File testDataDir, boolean recursive) {",
" Set<String> methodNames = new HashSet<String>();",
" for (Method method : " + testClassName + ".class.getDeclaredMethods()) {",
" for (Method method : clazz.getDeclaredMethods()) {",
" if (method.isAnnotationPresent(Test.class)) {",
" methodNames.add(method.getName().toLowerCase() + \"." + testDataFileExtension + "\");",
" }",
@@ -140,7 +171,7 @@ public class TestGenerator {
" for (File file : testDataDir.listFiles()) {",
" if (file.isDirectory()) {",
" if (recursive) {",
" allTestsPresent(file, recursive);",
" allTestsPresent(clazz, file, recursive);",
" }",
" }",
" else {",