diff options
Diffstat (limited to 'test/core')
-rw-r--r-- | test/core/src/lombok/AbstractRunTests.java | 77 | ||||
-rw-r--r-- | test/core/src/lombok/DirectoryRunner.java | 44 | ||||
-rw-r--r-- | test/core/src/lombok/LombokTestSource.java | 116 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaDelombok.java | 24 | ||||
-rw-r--r-- | test/core/src/lombok/RunTestsViaEcj.java | 10 | ||||
-rw-r--r-- | test/core/src/lombok/core/TestSingulars.java | 20 |
6 files changed, 196 insertions, 95 deletions
diff --git a/test/core/src/lombok/AbstractRunTests.java b/test/core/src/lombok/AbstractRunTests.java index 4e1c83dd..3d672bc4 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,51 +55,51 @@ 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, String platform, int version) 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; + AssertionError directiveFailure = null; + LombokTestSource sourceDirectives = null; + try { + sourceDirectives = LombokTestSource.readDirectives(file); + if (sourceDirectives.isIgnore()) return null; + if (!sourceDirectives.versionWithinLimit(version)) return null; + if (!sourceDirectives.runOnPlatform(platform)) return null; + } catch (AssertionError ae) { + directiveFailure = ae; + } 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; + if (!expected.versionWithinLimit(version)) 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(); + final LombokTestSource sourceDirectives_ = sourceDirectives; + final AssertionError directiveFailure_ = directiveFailure; + return new FileTester() { + @Override public void runTest() throws Throwable { + if (directiveFailure_ != null) throw directiveFailure_; + 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() { - try { - Class.forName("java.lang.AutoCloseable"); - } catch (ClassNotFoundException e) { - return 6; - } - - try { - Class.forName("java.util.stream.Stream"); - } catch (ClassNotFoundException e) { - return 7; - } - - 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; @@ -242,7 +243,7 @@ public abstract class AbstractRunTests { int size = Math.min(expectedLines.length, actualLines.length); if (size == 0 && expectedLines.length + actualLines.length > 0) { - Assert.fail("Missing / empty expected file."); + Assert.fail("Missing / empty expected file: " + name); } for (int i = 0; i < size; i++) { diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 9062f523..72f01de1 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,26 @@ 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, "javac", params.getVersion()); case ECJ: - return new RunTestsViaEcj().compareFile(params, file); + return new RunTestsViaEcj().createTester(params, file, "ecj", params.getVersion()); 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 c686bf19..31f7db3e 100644 --- a/test/core/src/lombok/LombokTestSource.java +++ b/test/core/src/lombok/LombokTestSource.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 The Project Lombok Authors. + * Copyright (C) 2014-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 @@ -26,8 +26,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -50,10 +52,18 @@ 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; + private final List<String> platforms; + public boolean runOnPlatform(String platform) { + if (platforms == null || platforms.isEmpty()) return true; + for (String pl : platforms) if (pl.equalsIgnoreCase(platform)) return true; + return false; + } + public boolean versionWithinLimit(int version) { return version >= versionLowerLimit && version <= versionUpperLimit; } @@ -74,6 +84,10 @@ public class LombokTestSource { return ignore; } + public boolean forceUnchanged() { + return unchanged; + } + public boolean isSkipCompareContent() { return skipCompareContent; } @@ -123,7 +137,8 @@ public class LombokTestSource { } private static final Pattern IGNORE_PATTERN = Pattern.compile("^\\s*ignore\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); - private static final Pattern SKIP_COMPARE_CONTENT_PATTERN = Pattern.compile("^\\s*skip[- ]?compare[- ]?content\\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[- ]?contents?\\s*(?:[-:].*)?$", Pattern.CASE_INSENSITIVE); private LombokTestSource(File file, String content, List<CompilerMessageMatcher> messages, List<String> directives) { this.file = file; @@ -135,8 +150,10 @@ 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>(); + String[] platformLimit = null; for (String directive : directives) { directive = directive.trim(); @@ -146,11 +163,24 @@ public class LombokTestSource { continue; } + if (UNCHANGED_PATTERN.matcher(directive).matches()) { + unchanged = true; + continue; + } + if (SKIP_COMPARE_CONTENT_PATTERN.matcher(directive).matches()) { skipCompareContent = true; continue; } + if (lc.startsWith("platform ")) { + String platformDesc = lc.substring("platform ".length()); + int idx = platformDesc.indexOf(':'); + if (idx != -1) platformDesc = platformDesc.substring(0, idx).trim(); + platformLimit = platformDesc.split("\\s*,\\s*"); + continue; + } + if (lc.startsWith("version ")) { int[] limits = parseVersionLimit(lc.substring(7).trim()); if (limits == null) { @@ -193,6 +223,8 @@ public class LombokTestSource { this.versionUpperLimit = versionUpper; this.ignore = ignore; this.skipCompareContent = skipCompareContent; + this.unchanged = unchanged; + this.platforms = platformLimit == null ? null : Arrays.asList(platformLimit); 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 + ")"); @@ -207,19 +239,27 @@ public class LombokTestSource { List<String> directives = new ArrayList<String>(); { - @Cleanup val rawIn = new FileInputStream(file); - BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, "UTF-8")); - for (String i = in.readLine(); i != null; i = in.readLine()) { - if (i.isEmpty()) continue; - - if (i.startsWith("//")) { - directives.add(i.substring(2)); - } else { - break; + InputStream rawIn = new FileInputStream(file); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, "UTF-8")); + try { + for (String i = in.readLine(); i != null; i = in.readLine()) { + if (i.isEmpty()) continue; + + if (i.startsWith("//")) { + directives.add(i.substring(2)); + } else { + break; + } + } + } + finally { + in.close(); } } - in.close(); - rawIn.close(); + finally { + rawIn.close(); + } } return new LombokTestSource(file, "", null, directives); @@ -235,25 +275,33 @@ public class LombokTestSource { File sourceFile = new File(sourceFolder, fileName); if (sourceFile.exists()) { - @Cleanup val rawIn = new FileInputStream(sourceFile); - BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, encoding)); - for (String i = in.readLine(); i != null; i = in.readLine()) { - if (content != null) { - content.append(i).append("\n"); - continue; + InputStream rawIn = new FileInputStream(sourceFile); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(rawIn, encoding)); + try { + for (String i = in.readLine(); i != null; i = in.readLine()) { + if (content != null) { + content.append(i).append("\n"); + continue; + } + + if (i.isEmpty()) continue; + + if (i.startsWith("//")) { + directives.add(i.substring(2)); + } else { + content = new StringBuilder(); + content.append(i).append("\n"); + } + } } - - if (i.isEmpty()) continue; - - if (i.startsWith("//")) { - directives.add(i.substring(2)); - } else { - content = new StringBuilder(); - content.append(i).append("\n"); + finally { + in.close(); } } - in.close(); - rawIn.close(); + finally { + rawIn.close(); + } } if (content == null) content = new StringBuilder(); @@ -262,9 +310,13 @@ public class LombokTestSource { if (messagesFolder != null) { File messagesFile = new File(messagesFolder, fileName + ".messages"); try { - @Cleanup val rawIn = new FileInputStream(messagesFile); - messages = CompilerMessageMatcher.readAll(rawIn); - rawIn.close(); + InputStream rawIn = new FileInputStream(messagesFile); + try { + messages = CompilerMessageMatcher.readAll(rawIn); + } + finally { + rawIn.close(); + } } catch (FileNotFoundException e) { messages = null; } 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..6ed1e950 100644 --- a/test/core/src/lombok/RunTestsViaEcj.java +++ b/test/core/src/lombok/RunTestsViaEcj.java @@ -24,7 +24,6 @@ package lombok; import java.io.File; import java.io.StringWriter; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -93,7 +92,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,23 +124,26 @@ 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) { List<String> classpath = new ArrayList<String>(); - classpath.addAll(Arrays.asList(System.getProperty("sun.boot.class.path").split(File.pathSeparator))); for (Iterator<String> i = classpath.iterator(); i.hasNext();) { if (FileSystem.getClasspath(i.next(), "UTF-8", null) == null) { i.remove(); } } - classpath.add("bin"); + if (new File("bin").exists()) classpath.add("bin"); classpath.add("dist/lombok.jar"); + classpath.add("lib/oracleJDK8Environment/rt.jar"); classpath.add("lib/test/commons-logging-commons-logging.jar"); classpath.add("lib/test/org.slf4j-slf4j-api.jar"); classpath.add("lib/test/org.slf4j-slf4j-ext.jar"); classpath.add("lib/test/log4j-log4j.jar"); classpath.add("lib/test/org.apache.logging.log4j-log4j-api.jar"); + classpath.add("lib/test/org.jboss.logging-jboss-logging.jar"); classpath.add("lib/test/com.google.guava-guava.jar"); classpath.add("lib/test/com.google.code.findbugs-findbugs.jar"); return new FileSystem(classpath.toArray(new String[0]), new String[] {file.getAbsolutePath()}, "UTF-8"); diff --git a/test/core/src/lombok/core/TestSingulars.java b/test/core/src/lombok/core/TestSingulars.java index 1134af08..4560615d 100644 --- a/test/core/src/lombok/core/TestSingulars.java +++ b/test/core/src/lombok/core/TestSingulars.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 The Project Lombok Authors. + * Copyright (C) 2015-2017 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 @@ -22,31 +22,35 @@ package lombok.core; import static lombok.core.handlers.Singulars.autoSingularize; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import org.junit.Test; public class TestSingulars { @Test public void testSingulars() { - assertEquals(null, autoSingularize("axes")); + assertNull("axes", autoSingularize("axes")); assertEquals("adjective", autoSingularize("adjectives")); assertEquals("bus", autoSingularize("buses")); assertEquals("octopus", autoSingularize("octopodes")); - assertEquals(null, autoSingularize("octopi")); + assertNull("octopi", autoSingularize("octopi")); assertEquals("elf", autoSingularize("elves")); assertEquals("jack", autoSingularize("jacks")); assertEquals("colloquy", autoSingularize("colloquies")); - assertEquals(null, autoSingularize("series")); + assertNull("series", autoSingularize("series")); assertEquals("man", autoSingularize("men")); - assertEquals(null, autoSingularize("highwaymen")); + assertNull("highwaymen", autoSingularize("highwaymen")); assertEquals("caveMan", autoSingularize("caveMen")); - assertEquals(null, autoSingularize("jackss")); - assertEquals(null, autoSingularize("virus")); + assertNull("jackss", autoSingularize("jackss")); + assertNull("virus", autoSingularize("virus")); assertEquals("quiz", autoSingularize("quizzes")); assertEquals("database", autoSingularize("databases")); assertEquals("dataBase", autoSingularize("dataBases")); assertEquals("Query", autoSingularize("Queries")); assertEquals("Movie", autoSingularize("Movies")); + assertEquals("cafe", autoSingularize("cafes")); + assertNull("caves", autoSingularize("caves")); + assertEquals("leaf", autoSingularize("leaves")); + assertEquals("autosave", autoSingularize("autosaves")); } } |