aboutsummaryrefslogtreecommitdiff
path: root/test/core
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2009-11-28 22:50:44 +0100
committerRoel Spilker <r.spilker@gmail.com>2009-11-28 22:50:44 +0100
commit6d9bc684692a6bbef5a960a113af834ce8a16447 (patch)
tree852399c38bd9387e5da10ae3655504092f3ea975 /test/core
parentc1ec2b5edbe6172bb0a031700bf928cd2950bac0 (diff)
downloadlombok-6d9bc684692a6bbef5a960a113af834ce8a16447.tar.gz
lombok-6d9bc684692a6bbef5a960a113af834ce8a16447.tar.bz2
lombok-6d9bc684692a6bbef5a960a113af834ce8a16447.zip
Use new DirectoryRunner to run tests on all files in a directory
Diffstat (limited to 'test/core')
-rw-r--r--test/core/src/lombok/DirectoryRunner.java77
-rw-r--r--test/core/src/lombok/ReflectionFileTester.java60
-rw-r--r--test/core/src/lombok/TestViaDelombok.java30
3 files changed, 104 insertions, 63 deletions
diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java
new file mode 100644
index 00000000..9905df04
--- /dev/null
+++ b/test/core/src/lombok/DirectoryRunner.java
@@ -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));
+ }
+}
diff --git a/test/core/src/lombok/ReflectionFileTester.java b/test/core/src/lombok/ReflectionFileTester.java
deleted file mode 100644
index 32de9c54..00000000
--- a/test/core/src/lombok/ReflectionFileTester.java
+++ /dev/null
@@ -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);
- }
-}
diff --git a/test/core/src/lombok/TestViaDelombok.java b/test/core/src/lombok/TestViaDelombok.java
index a3e97098..63a8e1f8 100644
--- a/test/core/src/lombok/TestViaDelombok.java
+++ b/test/core/src/lombok/TestViaDelombok.java
@@ -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 ")) {