From 59e585a0c68959eb72be34524bdad19df5dc8a4d Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 21 Jul 2010 23:52:28 +0200 Subject: refactored the tests to prepare running ecj as well as delombok. --- test/core/src/lombok/AbstractRunTests.java | 136 ++++++++++++++++++++++++++ test/core/src/lombok/DirectoryRunner.java | 72 ++++++++++---- test/core/src/lombok/RunTestsViaDelombok.java | 128 ++---------------------- test/core/src/lombok/RunTestsViaEcj.java | 56 +++++++++++ 4 files changed, 254 insertions(+), 138 deletions(-) create mode 100644 test/core/src/lombok/AbstractRunTests.java create mode 100644 test/core/src/lombok/RunTestsViaEcj.java (limited to 'test/core') diff --git a/test/core/src/lombok/AbstractRunTests.java b/test/core/src/lombok/AbstractRunTests.java new file mode 100644 index 00000000..ad9c45af --- /dev/null +++ b/test/core/src/lombok/AbstractRunTests.java @@ -0,0 +1,136 @@ +/* + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +public abstract class AbstractRunTests { + protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + public void compareFile(DirectoryRunner.TestParams params, File file) throws Throwable { + StringBuilder messages = new StringBuilder(); + StringWriter result = new StringWriter(); + transformCode(messages, result, file); + compare( + file.getName(), + readFile(params.getAfterDirectory(), file, false), + result.toString(), + readFile(params.getMessagesDirectory(), file, true), + messages.toString(), + params.printErrors()); + } + + protected abstract void transformCode(StringBuilder message, StringWriter result, File file) throws Throwable; + + protected String readFile(File file) throws IOException { + BufferedReader reader; + try { + reader = new BufferedReader(new FileReader(file)); + } catch (FileNotFoundException e) { + return ""; + } + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + result.append(LINE_SEPARATOR); + } + reader.close(); + return result.toString(); + } + + private String readFile(File dir, File file, boolean messages) throws IOException { + if (dir == null) return ""; + return readFile(new File(dir, file.getName() + (messages ? ".messages" : ""))); + } + + private void compare(String name, String expectedFile, String actualFile, String expectedMessages, String actualMessages, boolean printErrors) 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; + } + try { + compareContent(name, expectedMessages, actualMessages); + } + catch (Throwable e) { + if (printErrors) { + System.out.println("***** " + name + " *****"); + System.out.println(e.getMessage()); + System.out.println("**** Expected ******"); + System.out.println(expectedMessages); + System.out.println("**** Actual ******"); + System.out.println(actualMessages); + 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 ")) { + actualLines[0] = ""; + } + expectedLines = removeBlanks(expectedLines); + actualLines = removeBlanks(actualLines); + int size = Math.min(expectedLines.length, actualLines.length); + for (int i = 0; i < size; i++) { + String expected = expectedLines[i]; + String actual = actualLines[i]; + assertEquals(String.format("Difference in %s on line %d", name, i + 1), expected, actual); + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %d in generated %s: %s", size + 1, name, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %d in generated %s: %s", size + 1, name, actualLines[size])); + } + } + + private static String[] removeBlanks(String[] in) { + List out = new ArrayList(); + for (String s : in) { + if (!s.trim().isEmpty()) out.add(s); + } + return out.toArray(new String[0]); + } +} diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index db474ff0..89fe6ab0 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -1,3 +1,24 @@ +/* + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok; import java.io.BufferedReader; @@ -6,7 +27,6 @@ import java.io.FileFilter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.lang.reflect.Method; import java.util.Map; import java.util.TreeMap; @@ -16,6 +36,17 @@ import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; public class DirectoryRunner extends Runner { + public enum Compiler { + DELOMBOK, JAVAC, ECJ; + } + + public interface TestParams { + Compiler getCompiler(); + boolean printErrors(); + File getBeforeDirectory(); + File getAfterDirectory(); + File getMessagesDirectory(); + } private static final FileFilter JAVA_FILE_FILTER = new FileFilter() { @Override public boolean accept(File file) { @@ -26,11 +57,13 @@ public class DirectoryRunner extends Runner { private final Description description; private final Map tests = new TreeMap(); private final Throwable failure; - private File beforeDirectory; - private File afterDirectory; + private final TestParams params; - public DirectoryRunner(Class testClass) { + public DirectoryRunner(Class testClass) throws Exception { description = Description.createSuiteDescription(testClass); + + this.params = (TestParams) testClass.newInstance(); + Throwable error = null; try { addTests(testClass); @@ -40,26 +73,20 @@ public class DirectoryRunner extends Runner { } 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(JAVA_FILE_FILTER)) { + for (File file : params.getBeforeDirectory().listFiles(JAVA_FILE_FILTER)) { 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) { @@ -83,16 +110,25 @@ public class DirectoryRunner extends Runner { notifier.fireTestFinished(testDescription); } } - + private boolean runTest(String fileName) throws Throwable { - File file = new File(beforeDirectory, fileName); + File file = new File(params.getBeforeDirectory(), fileName); if (mustIgnore(file)) { return false; } - RunTestsViaDelombok.compareFile(beforeDirectory, afterDirectory, file); + switch (params.getCompiler()) { + case DELOMBOK: + new RunTestsViaDelombok().compareFile(params, file); + break; + case ECJ: + new RunTestsViaEcj().compareFile(params, file); + break; + case JAVAC: + throw new UnsupportedOperationException(); + } return true; } - + private boolean mustIgnore(File file) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java index 37ff8f69..7f99b99e 100644 --- a/test/core/src/lombok/RunTestsViaDelombok.java +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -21,17 +21,8 @@ */ package lombok; -import static org.junit.Assert.*; - -import java.io.BufferedReader; import java.io.File; -import java.io.FileFilter; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; import java.io.StringWriter; -import java.util.ArrayList; -import java.util.List; import java.util.Locale; import javax.tools.Diagnostic; @@ -40,124 +31,21 @@ import javax.tools.JavaFileObject; import lombok.delombok.Delombok; -public class RunTestsViaDelombok { - private static final FileFilter JAVA_FILE_FILTER = new FileFilter() { - @Override public boolean accept(File file) { - return file.isFile() && file.getName().endsWith(".java"); - } - }; - - private static Delombok delombok = new Delombok(); - private static volatile boolean printErrors = false; - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); +public class RunTestsViaDelombok extends AbstractRunTests { + private Delombok delombok = new Delombok(); - public static void runComparison(File beforeDir, File afterDir) throws Throwable { - File[] listFiles = beforeDir.listFiles(JAVA_FILE_FILTER); - - for (File file : listFiles) { - compareFile(beforeDir, afterDir, file); - } - } - - public static void compareFile(File beforeDir, File afterDir, File file) throws Throwable { + @Override + public void transformCode(final StringBuilder messages, StringWriter result, File file) throws Throwable { delombok.setVerbose(false); delombok.setForceProcess(true); delombok.setCharset("UTF-8"); - - final StringBuilder messages = new StringBuilder(); + delombok.setDiagnosticsListener(new DiagnosticListener() { @Override public void report(Diagnostic d) { messages.append(String.format("%d:%d %s %s\n", d.getLineNumber(), d.getColumnNumber(), d.getKind(), d.getMessage(Locale.ENGLISH))); } }); - StringWriter writer = new StringWriter(); - delombok.delombok(file.getAbsolutePath(), writer); - compare(file.getName(), readFile(afterDir, file, false), writer.toString(), readFile(beforeDir, file, true), messages.toString()); - } - - public static void printErrors(boolean print) { - printErrors = print; - } - - private static void compare(String name, String expectedFile, String actualFile, String expectedMessages, String actualMessages) 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; - } - try { - compareContent(name, expectedMessages, actualMessages); - } - catch (Throwable e) { - if (printErrors) { - System.out.println("***** " + name + " *****"); - System.out.println(e.getMessage()); - System.out.println("**** Expected ******"); - System.out.println(expectedMessages); - System.out.println("**** Actual ******"); - System.out.println(actualMessages); - 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 ")) { - actualLines[0] = ""; - } - expectedLines = removeBlanks(expectedLines); - actualLines = removeBlanks(actualLines); - int size = Math.min(expectedLines.length, actualLines.length); - for (int i = 0; i < size; i++) { - String expected = expectedLines[i]; - String actual = actualLines[i]; - assertEquals(String.format("Difference in %s on line %d", name, i + 1), expected, actual); - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %d in generated %s: %s", size + 1, name, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %d in generated %s: %s", size + 1, name, actualLines[size])); - } - } - - private static String readFile(File afterDir, File file, boolean messages) throws IOException { - BufferedReader reader; - try { - reader = new BufferedReader(new FileReader(new File(afterDir, file.getName() + (messages ? ".messages" : "")))); - } catch (FileNotFoundException e) { - return ""; - } - StringBuilder result = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - result.append(LINE_SEPARATOR); - } - reader.close(); - return result.toString(); - } - - private static String[] removeBlanks(String[] in) { - List out = new ArrayList(); - for (String s : in) { - if (!s.trim().isEmpty()) out.add(s); - } - return out.toArray(new String[0]); + delombok.delombok(file.getAbsolutePath(), result); } -} \ No newline at end of file +} diff --git a/test/core/src/lombok/RunTestsViaEcj.java b/test/core/src/lombok/RunTestsViaEcj.java new file mode 100644 index 00000000..3f7d6e81 --- /dev/null +++ b/test/core/src/lombok/RunTestsViaEcj.java @@ -0,0 +1,56 @@ +package lombok; + +import java.io.File; +import java.io.StringWriter; +import java.util.Locale; + +import lombok.eclipse.TransformEclipseAST; + +import org.eclipse.jdt.core.compiler.CategorizedProblem; +import org.eclipse.jdt.internal.compiler.CompilationResult; +import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.batch.CompilationUnit; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; +import org.eclipse.jdt.internal.compiler.parser.Parser; +import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; +import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; + +public class RunTestsViaEcj extends AbstractRunTests { + protected static CompilerOptions ecjCompilerOptions() { + CompilerOptions options = new CompilerOptions(); + options.complianceLevel = ClassFileConstants.JDK1_6; + options.sourceLevel = ClassFileConstants.JDK1_6; + options.targetJDK = ClassFileConstants.JDK1_6; + options.parseLiteralExpressionsAsConstants = true; + return options; + } + + @Override + public void transformCode(final StringBuilder messages, StringWriter result, File file) throws Throwable { + ProblemReporter problemReporter = new ProblemReporter(new IErrorHandlingPolicy() { + public boolean proceedOnErrors() { + return true; + } + + public boolean stopOnFirstError() { + return false; + } + }, ecjCompilerOptions(), new DefaultProblemFactory(Locale.ENGLISH)); + + Parser parser = new Parser(problemReporter, true); + String source = readFile(file); + CompilationUnit sourceUnit = new CompilationUnit(source.toCharArray(), file.getName(), "UTF-8"); + CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); + CompilationUnitDeclaration cud = parser.parse(sourceUnit, compilationResult); + + TransformEclipseAST.transform(parser, cud); + + for (CategorizedProblem p : compilationResult.getErrors()) { + messages.append(p.toString()).append("\n"); + } + + result.append(cud.toString()); + } +} -- cgit