aboutsummaryrefslogtreecommitdiff
path: root/test/core/src/lombok/AbstractRunTests.java
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2012-10-25 00:07:44 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2012-10-25 00:07:44 +0200
commitd2808d407a65da9fe1e290a83df0f0da107f0cf8 (patch)
tree80e577330ebef88dbd91c1de59951cbff94af9e8 /test/core/src/lombok/AbstractRunTests.java
parent2d216118986115bf309c050cd841bbe8b62b80b3 (diff)
downloadlombok-d2808d407a65da9fe1e290a83df0f0da107f0cf8.tar.gz
lombok-d2808d407a65da9fe1e290a83df0f0da107f0cf8.tar.bz2
lombok-d2808d407a65da9fe1e290a83df0f0da107f0cf8.zip
The testrunner now uses a different mechanism to verify correctness of produced errors and warnings (i.e. we intentionally compile code with errors in them to verify that the appropriate error or warning message is emitted when lombok is active during a compilation run of either javac or ecj) - instead of string comparisons, it's a little more complex. This to enable testing of both javac6 and javac7, even if they produce (slightly) different error output.
Updated all message files in the 'expected output' directories to represent this change.
Diffstat (limited to 'test/core/src/lombok/AbstractRunTests.java')
-rw-r--r--test/core/src/lombok/AbstractRunTests.java64
1 files changed, 58 insertions, 6 deletions
diff --git a/test/core/src/lombok/AbstractRunTests.java b/test/core/src/lombok/AbstractRunTests.java
index 02c4803a..676426f4 100644
--- a/test/core/src/lombok/AbstractRunTests.java
+++ b/test/core/src/lombok/AbstractRunTests.java
@@ -25,13 +25,19 @@ import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
import java.util.List;
public abstract class AbstractRunTests {
@@ -43,11 +49,23 @@ public abstract class AbstractRunTests {
}
public boolean compareFile(DirectoryRunner.TestParams params, File file) throws Throwable {
- StringBuilder messages = new StringBuilder();
+ LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>();
StringWriter writer = new StringWriter();
transformCode(messages, writer, file);
String expectedFile = readFile(params.getAfterDirectory(), file, false);
- String expectedMessages = readFile(params.getMessagesDirectory(), file, true);
+ List<CompilerMessageMatcher> expectedMessages = Collections.emptyList();
+ if (params.getMessagesDirectory() != null) {
+ try {
+ InputStream in = new FileInputStream(new File(params.getMessagesDirectory(), file.getName() + ".messages"));
+ try {
+ expectedMessages = CompilerMessageMatcher.readAll(in);
+ } finally {
+ in.close();
+ }
+ } catch (FileNotFoundException ex) {
+ // That's okay - then we expect no messages, and expectedMessages already gets initialized to the empty list.
+ }
+ }
StringReader r = new StringReader(expectedFile);
BufferedReader br = new BufferedReader(r);
@@ -58,13 +76,13 @@ public abstract class AbstractRunTests {
expectedFile,
writer.toString(),
expectedMessages,
- messages.toString(),
+ messages,
params.printErrors());
return true;
}
- protected abstract void transformCode(StringBuilder message, StringWriter result, File file) throws Throwable;
+ protected abstract void transformCode(Collection<CompilerMessage> messages, StringWriter result, File file) throws Throwable;
protected String readFile(File file) throws IOException {
BufferedReader reader;
@@ -107,7 +125,19 @@ public abstract class AbstractRunTests {
}
}
- private void compare(String name, String expectedFile, String actualFile, String expectedMessages, String actualMessages, boolean printErrors) throws Throwable {
+ private static void dumpToFile(File file, Collection<CompilerMessage> content) throws IOException {
+ FileOutputStream fos = new FileOutputStream(file);
+ try {
+ for (CompilerMessage message : content) {
+ fos.write(message.asCompilerMessageMatcher().toString().getBytes("UTF-8"));
+ fos.write('\n');
+ }
+ } finally {
+ fos.close();
+ }
+ }
+
+ private void compare(String name, String expectedFile, String actualFile, List<CompilerMessageMatcher> expectedMessages, LinkedHashSet<CompilerMessage> actualMessages, boolean printErrors) throws Throwable {
try {
compareContent(name, expectedFile, actualFile);
} catch (Throwable e) {
@@ -129,8 +159,9 @@ public abstract class AbstractRunTests {
}
throw e;
}
+
try {
- compareContent(name, expectedMessages, actualMessages);
+ compareMessages(name, expectedMessages, actualMessages);
} catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
@@ -148,6 +179,27 @@ public abstract class AbstractRunTests {
}
}
+ private static void compareMessages(String name, List<CompilerMessageMatcher> expected, LinkedHashSet<CompilerMessage> actual) {
+ Iterator<CompilerMessageMatcher> expectedIterator = expected.iterator();
+ Iterator<CompilerMessage> actualIterator = actual.iterator();
+
+ while (true) {
+ boolean exHasNext = expectedIterator.hasNext();
+ boolean acHasNext = actualIterator.hasNext();
+ if (!exHasNext && !acHasNext) break;
+ if (exHasNext && acHasNext) {
+ CompilerMessageMatcher cmm = expectedIterator.next();
+ CompilerMessage cm = actualIterator.next();
+ if (cmm.matches(cm)) continue;
+ fail(String.format("[%s] Expected message '%s' but got message '%s'", name, cmm, cm));
+ throw new AssertionError("fail should have aborted already.");
+ }
+ if (exHasNext) fail(String.format("[%s] Expected message '%s' but ran out of actual messages", name, expectedIterator.next()));
+ if (acHasNext) fail(String.format("[%s] Unexpected message: %s", name, actualIterator.next()));
+ throw new AssertionError("fail should have aborted already.");
+ }
+ }
+
private static void compareContent(String name, String expectedFile, String actualFile) {
String[] expectedLines = expectedFile.split("(\\r?\\n)");
String[] actualLines = actualFile.split("(\\r?\\n)");