diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-11-17 02:15:26 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-11-17 02:15:26 +0100 |
commit | 0e34eec7668ee060fe65133243009dbb27e7c251 (patch) | |
tree | 598bd583cba8a5bdc289c08010bfd86828f3d062 /test/core/src | |
parent | b8a222006da0a535134c5e7e94282550bfe745b6 (diff) | |
parent | 8a504f8beec691b64d1c08f629520c2443821847 (diff) | |
download | lombok-0e34eec7668ee060fe65133243009dbb27e7c251.tar.gz lombok-0e34eec7668ee060fe65133243009dbb27e7c251.tar.bz2 lombok-0e34eec7668ee060fe65133243009dbb27e7c251.zip |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'test/core/src')
-rw-r--r-- | test/core/src/lombok/AbstractRunTests.java | 47 | ||||
-rw-r--r-- | test/core/src/lombok/DirectoryRunner.java | 43 | ||||
-rw-r--r-- | test/core/src/lombok/LombokTestSource.java | 13 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaDelombok.java | 24 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaEcj.java | 4 |
5 files changed, 98 insertions, 33 deletions
diff --git a/test/core/src/lombok/AbstractRunTests.java b/test/core/src/lombok/AbstractRunTests.java index 4e1c83dd..f564d0dc 100644 --- a/test/core/src/lombok/AbstractRunTests.java +++ b/test/core/src/lombok/AbstractRunTests.java @@ -39,6 +39,7 @@ import java.util.Map; import org.junit.Assert; +import lombok.DirectoryRunner.FileTester; import lombok.core.AST; import lombok.core.LombokConfiguration; import lombok.core.LombokImmutableList; @@ -54,32 +55,38 @@ public abstract class AbstractRunTests { this.dumpActualFilesHere = findPlaceToDumpActualFiles(); } - public boolean compareFile(DirectoryRunner.TestParams params, File file) throws Throwable { + public final FileTester createTester(final DirectoryRunner.TestParams params, final File file) throws IOException { ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys(); final LombokTestSource sourceDirectives = LombokTestSource.readDirectives(file); - if (sourceDirectives.isIgnore()) return false; - if (!sourceDirectives.versionWithinLimit(params.getVersion())) return false; - if (!sourceDirectives.versionWithinLimit(getClasspathVersion())) return false; + if (sourceDirectives.isIgnore()) return null; + if (!sourceDirectives.versionWithinLimit(params.getVersion())) return null; + if (!sourceDirectives.versionWithinLimit(getClasspathVersion())) return null; String fileName = file.getName(); - LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName); + final LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName); - if (expected.isIgnore()) return false; - if (!expected.versionWithinLimit(params.getVersion())) return false; + if (expected.isIgnore()) return null; + if (!expected.versionWithinLimit(params.getVersion())) return null; - LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>(); - StringWriter writer = new StringWriter(); - - LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() { - @Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) { - return sourceDirectives.getConfiguration(); + return new FileTester() { + @Override public void runTest() throws Throwable { + LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>(); + StringWriter writer = new StringWriter(); + + LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() { + @Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) { + return sourceDirectives.getConfiguration(); + } + }); + + boolean changed = transformCode(messages, writer, file, sourceDirectives.getSpecifiedEncoding(), sourceDirectives.getFormatPreferences()); + boolean forceUnchanged = sourceDirectives.forceUnchanged() || sourceDirectives.isSkipCompareContent(); + if (params.expectChanges() && !forceUnchanged && !changed) messages.add(new CompilerMessage(-1, -1, true, "not flagged modified")); + if (!params.expectChanges() && changed) messages.add(new CompilerMessage(-1, -1, true, "unexpected modification")); + + compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives.isSkipCompareContent() || expected.isSkipCompareContent()); } - }); - - transformCode(messages, writer, file, sourceDirectives.getSpecifiedEncoding(), sourceDirectives.getFormatPreferences()); - - compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives.isSkipCompareContent() || expected.isSkipCompareContent()); - return true; + }; } private static int getClasspathVersion() { @@ -98,7 +105,7 @@ public abstract class AbstractRunTests { return 8; } - protected abstract void transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable; + protected abstract boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable; protected String readFile(File file) throws IOException { BufferedReader reader; diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 9062f523..d4275b09 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2014 The Project Lombok Authors. + * Copyright (C) 2009-2015 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,7 @@ package lombok; import java.io.File; import java.io.FileFilter; +import java.io.IOException; import java.util.Map; import java.util.TreeMap; @@ -69,6 +70,8 @@ public class DirectoryRunner extends Runner { public boolean accept(File file) { return true; } + + public abstract boolean expectChanges(); } private static final FileFilter JAVA_FILE_FILTER = new FileFilter() { @@ -114,19 +117,29 @@ public class DirectoryRunner extends Runner { @Override public void run(RunNotifier notifier) { if (failure != null) { - notifier.fireTestStarted(description); - notifier.fireTestFailure(new Failure(description, failure)); - notifier.fireTestFinished(description); + reportInitializationFailure(notifier, description, failure); return; } for (Map.Entry<String, Description> entry : tests.entrySet()) { Description testDescription = entry.getValue(); + + FileTester tester; + try { + tester = createTester(entry.getKey()); + } catch (IOException e) { + reportInitializationFailure(notifier, testDescription, e); + continue; + } + + if (tester == null) { + notifier.fireTestIgnored(testDescription); + continue; + } + notifier.fireTestStarted(testDescription); try { - if (!runTest(entry.getKey())) { - notifier.fireTestIgnored(testDescription); - } + tester.runTest(); } catch (Throwable t) { notifier.fireTestFailure(new Failure(testDescription, t)); } @@ -134,17 +147,27 @@ public class DirectoryRunner extends Runner { } } - private boolean runTest(String fileName) throws Throwable { + private void reportInitializationFailure(RunNotifier notifier, Description description, Throwable throwable) { + notifier.fireTestStarted(description); + notifier.fireTestFailure(new Failure(description, throwable)); + notifier.fireTestFinished(description); + } + + private FileTester createTester(String fileName) throws IOException { File file = new File(params.getBeforeDirectory(), fileName); switch (params.getCompiler()) { case DELOMBOK: - return new RunTestsViaDelombok().compareFile(params, file); + return new RunTestsViaDelombok().createTester(params, file); case ECJ: - return new RunTestsViaEcj().compareFile(params, file); + return new RunTestsViaEcj().createTester(params, file); default: case JAVAC: throw new UnsupportedOperationException(); } } + + public interface FileTester { + void runTest() throws Throwable; + } } diff --git a/test/core/src/lombok/LombokTestSource.java b/test/core/src/lombok/LombokTestSource.java index 1f3f5e1a..cd7cd166 100644 --- a/test/core/src/lombok/LombokTestSource.java +++ b/test/core/src/lombok/LombokTestSource.java @@ -51,6 +51,7 @@ public class LombokTestSource { private final Map<String, String> formatPreferences; private final boolean ignore; private final boolean skipCompareContent; + private final boolean unchanged; private final int versionLowerLimit, versionUpperLimit; private final ConfigurationResolver configuration; private final String specifiedEncoding; @@ -75,6 +76,10 @@ public class LombokTestSource { return ignore; } + public boolean forceUnchanged() { + return unchanged; + } + public boolean isSkipCompareContent() { return skipCompareContent; } @@ -124,6 +129,7 @@ public class LombokTestSource { } private static final Pattern IGNORE_PATTERN = Pattern.compile("^\\s*ignore\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); + private static final Pattern UNCHANGED_PATTERN = Pattern.compile("^\\s*unchanged\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); private static final Pattern SKIP_COMPARE_CONTENT_PATTERN = Pattern.compile("^\\s*skip[- ]?compare[- ]?content\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); private LombokTestSource(File file, String content, List<CompilerMessageMatcher> messages, List<String> directives) { @@ -136,6 +142,7 @@ public class LombokTestSource { int versionUpper = Integer.MAX_VALUE; boolean ignore = false; boolean skipCompareContent = false; + boolean unchanged = false; String encoding = null; Map<String, String> formats = new HashMap<String, String>(); @@ -147,6 +154,11 @@ public class LombokTestSource { continue; } + if (UNCHANGED_PATTERN.matcher(directive).matches()) { + unchanged = true; + continue; + } + if (SKIP_COMPARE_CONTENT_PATTERN.matcher(directive).matches()) { skipCompareContent = true; continue; @@ -194,6 +206,7 @@ public class LombokTestSource { this.versionUpperLimit = versionUpper; this.ignore = ignore; this.skipCompareContent = skipCompareContent; + this.unchanged = unchanged; ConfigurationProblemReporter reporter = new ConfigurationProblemReporter() { @Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) { Assert.fail("Problem on directive line: " + problem + " at conf line #" + lineNumber + " (" + line + ")"); diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java index 8ec41ef1..0887de32 100644 --- a/test/core/src/lombok/RunTestsViaDelombok.java +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -21,8 +21,11 @@ */ package lombok; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.PrintStream; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.Locale; import java.util.Map; @@ -35,8 +38,10 @@ public class RunTestsViaDelombok extends AbstractRunTests { private Delombok delombok = new Delombok(); @Override - public void transformCode(Collection<CompilerMessage> messages, StringWriter result, final File file, String encoding, Map<String, String> formatPreferences) throws Throwable { - delombok.setVerbose(false); + public boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, final File file, String encoding, Map<String, String> formatPreferences) throws Throwable { + delombok.setVerbose(true); + ChangedChecker cc = new ChangedChecker(); + delombok.setFeedback(cc.feedback); delombok.setForceProcess(true); delombok.setCharset(encoding == null ? "UTF-8" : encoding); delombok.setFormatPreferences(formatPreferences); @@ -52,8 +57,23 @@ public class RunTestsViaDelombok extends AbstractRunTests { try { Locale.setDefault(Locale.ENGLISH); delombok.delombok(); + return cc.isChanged(); } finally { Locale.setDefault(originalLocale); } } + + static class ChangedChecker { + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + private final PrintStream feedback; + + ChangedChecker() throws UnsupportedEncodingException { + feedback = new PrintStream(bytes, true, "UTF-8"); + } + + boolean isChanged() throws UnsupportedEncodingException { + feedback.flush(); + return bytes.toString("UTF-8").endsWith("[delomboked]\n"); + } + } } diff --git a/test/core/src/lombok/RunTestsViaEcj.java b/test/core/src/lombok/RunTestsViaEcj.java index 272ed8b7..75200223 100644 --- a/test/core/src/lombok/RunTestsViaEcj.java +++ b/test/core/src/lombok/RunTestsViaEcj.java @@ -93,7 +93,7 @@ public class RunTestsViaEcj extends AbstractRunTests { } @Override - public void transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable { + public boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable { final AtomicReference<CompilationResult> compilationResult_ = new AtomicReference<CompilationResult>(); final AtomicReference<CompilationUnitDeclaration> compilationUnit_ = new AtomicReference<CompilationUnitDeclaration>(); ICompilerRequestor bitbucketRequestor = new ICompilerRequestor() { @@ -125,6 +125,8 @@ public class RunTestsViaEcj extends AbstractRunTests { if (cud == null) result.append("---- NO CompilationUnit provided by ecj ----"); else result.append(cud.toString()); + + return true; } private FileSystem createFileSystem(File file) { |