Use new DirectoryRunner to run tests on all files in a directory

This commit is contained in:
Roel Spilker committed 2009-11-28 22:50:44 +01:00
1 parent c1ec2b5edb
commit 6d9bc68469
5 files changed
+123 -98

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
package lombok;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
public class DirectoryRunner extends Runner {
private final Description description;
private final Map<String, Description> tests = new TreeMap<String, Description>();
private final Throwable failure;
private File beforeDirectory;
private File afterDirectory;
public DirectoryRunner(Class<?> testClass) {
description = Description.createSuiteDescription(testClass);
Throwable error = null;
try {
addTests(testClass);
}
catch (Throwable t) {
error = t;
}
this.failure = error;
}
private void addTests(Class<?> testClass) throws Exception {
Method beforeMethod = testClass.getDeclaredMethod("getBeforeDirectory");
beforeDirectory = (File) beforeMethod.invoke(null);
Method afterMethod = testClass.getDeclaredMethod("getAfterDirectory");
afterDirectory = (File) afterMethod.invoke(null);
for (File file : beforeDirectory.listFiles()) {
Description testDescription = Description.createTestDescription(testClass, file.getName());
description.addChild(testDescription);
tests.put(file.getName(), testDescription);
}
}
@Override
public Description getDescription() {
return description;
}
@Override
public void run(RunNotifier notifier) {
if (failure != null) {
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, failure));
notifier.fireTestFinished(description);
return;
}
for (Map.Entry<String, Description> entry : tests.entrySet()) {
Description testDescription = entry.getValue();
notifier.fireTestStarted(testDescription);
try {
runTest(entry.getKey());
}
catch (Throwable t) {
notifier.fireTestFailure(new Failure(testDescription, t));
}
notifier.fireTestFinished(testDescription);
}
}
private void runTest(String fileName) throws Throwable {
TestViaDelombok.compareFile(afterDirectory, new File(beforeDirectory, fileName));
}
}
@@ -1,60 +0,0 @@
package lombok;
import java.io.File;
import java.lang.reflect.Method;
import org.junit.Test;
public class ReflectionFileTester {
private final File before;
private final File after;
public ReflectionFileTester(String beforePath, String afterPath) {
before = new File(beforePath);
after = new File(afterPath);
if (!before.isDirectory()) {
throw new IllegalArgumentException(beforePath + " is not a directory");
}
if (!after.isDirectory()) {
throw new IllegalArgumentException(afterPath + " is not a directory");
}
}
public boolean verify(Class<?> clazz) {
boolean result = true;
for (File f : before.listFiles()) {
String fileName = f.getName();
if (!fileName.endsWith(".java")) {
continue;
}
String methodName = "test" + fileName.substring(0, fileName.length() - 5);
try {
Method method = clazz.getDeclaredMethod(methodName);
if (method.getAnnotation(Test.class) == null) {
result = false;
System.err.printf("Class %s method %s is not a @Test method\n", clazz.getName(), methodName);
}
}
catch (NoSuchMethodException e) {
result = false;
System.err.printf("Class %s has no method %s\n", clazz.getName(), methodName);
}
}
return result;
}
public void test() throws Exception {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace.length < 3) {
throw new Error("No stacktrace available");
}
String methodName = stackTrace[2].getMethodName();
if (!methodName.startsWith("test")) {
throw new IllegalStateException("test() should be called from a methos that starts with 'test'");
}
String fileName = methodName.substring(4).concat(".java");
File testFile = new File(before, fileName);
TestViaDelombok.compareFile(after, testFile);
}
}
+27 -3
View File
@@ -35,10 +35,11 @@ import lombok.delombok.Delombok;
public class TestViaDelombok {
private static Delombok delombok = new Delombok();
private static volatile boolean printErrors = false;
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void runComparison(File beforeDir, File afterDir) throws IOException {
public static void runComparison(File beforeDir, File afterDir) throws Throwable {
File[] listFiles = beforeDir.listFiles();
for (File file : listFiles) {
@@ -46,7 +47,7 @@ public class TestViaDelombok {
}
}
public static void compareFile(File afterDir, File file) throws IOException {
public static void compareFile(File afterDir, File file) throws Throwable {
delombok.setVerbose(false);
delombok.setForceProcess(true);
delombok.setCharset("UTF-8");
@@ -55,7 +56,30 @@ public class TestViaDelombok {
compare(file.getName(), readAfter(afterDir, file), writer.toString());
}
private static void compare(String name, String expectedFile, String actualFile) {
public static void printErrors(boolean print) {
printErrors = print;
}
private static void compare(String name, String expectedFile, String actualFile) throws Throwable {
try {
compareContent(name, expectedFile, actualFile);
}
catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
System.out.println(e.getMessage());
System.out.println("**** Expected ******");
System.out.println(expectedFile);
System.out.println("**** Actual ******");
System.out.println(actualFile);
System.out.println("*******************");
}
throw e;
}
}
private static void compareContent(String name, String expectedFile,
String actualFile) {
String[] expectedLines = expectedFile.split("(\\r?\\n)");
String[] actualLines = actualFile.split("(\\r?\\n)");
if (actualLines[0].startsWith("// Generated by delombok at ")) {
@@ -23,16 +23,20 @@ package lombok.delombok;
import java.io.File;
import lombok.DirectoryRunner;
import lombok.TestViaDelombok;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DirectoryRunner.class)
public class TestLombokFiles {
private static final File BEFORE_DIR = new File("test/lombok/resource/before");
private static final File AFTER_DIR = new File("test/lombok/resource/after");
@Test
public void testSources() throws Exception {
TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR);
public static File getBeforeDirectory() {
TestViaDelombok.printErrors(true);
return new File("test/lombok/resource/before");
}
public static File getAfterDirectory() {
return new File("test/lombok/resource/after");
}
}
@@ -21,39 +21,19 @@
*/
package lombok.delombok;
import lombok.ReflectionFileTester;
import java.io.File;
import org.junit.BeforeClass;
import org.junit.Test;
import lombok.DirectoryRunner;
import org.junit.runner.RunWith;
@RunWith(DirectoryRunner.class)
public class TestSourceFiles {
private static final String AFTER = "test/delombok/resource/after";
private static final String BEFORE = "test/delombok/resource/before";
private static final ReflectionFileTester tester = new ReflectionFileTester(BEFORE, AFTER);
@BeforeClass
public static void verify() {
tester.verify(TestSourceFiles.class);
public static File getBeforeDirectory() {
return new File("test/delombok/resource/before");
}
@Test
public void testAnnotation() throws Exception {
tester.test();
}
@Test
public void testCast() throws Exception {
tester.test();
}
@Test
public void testForLoop() throws Exception {
tester.test();
}
@Test
public void testWithComments() throws Exception {
tester.test();
public static File getAfterDirectory() {
return new File("test/delombok/resource/after");
}
}