Converting the test to a combo-framework test. JDK-8226585-branch
authorjlahoda
Tue, 24 Sep 2019 15:40:26 +0200
branchJDK-8226585-branch
changeset 58290 d885633d9de4
parent 58109 ee07de0d2c16
child 58342 ebb1ff3d6707
Converting the test to a combo-framework test.
src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java
src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java
test/langtools/tools/javac/lib/combo/ComboInstance.java
test/langtools/tools/javac/preview/PreviewErrors.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java	Tue Sep 24 15:40:26 2019 +0200
@@ -257,6 +257,7 @@
                 ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
                 Types.instance(this).newRound();
                 Check.instance(this).newRound();
+                Check.instance(this).clear(); //clear mandatory warning handlers
                 Modules.instance(this).newRound();
                 Annotate.instance(this).newRound();
                 CompileStates.instance(this).clear();
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Tue Sep 24 15:40:26 2019 +0200
@@ -465,6 +465,14 @@
         localClassNameIndexes.clear();
     }
 
+    public void clear() {
+        deprecationHandler.clear();
+        removalHandler.clear();
+        uncheckedHandler.clear();
+        sunApiHandler.clear();
+        previewHandler.clear();
+    }
+
     public void putCompiled(ClassSymbol csym) {
         compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym);
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java	Tue Sep 24 15:40:26 2019 +0200
@@ -44,6 +44,7 @@
 import java.nio.file.Path;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Objects;
@@ -173,6 +174,10 @@
     private long lastUsedTime = System.currentTimeMillis();
     protected long deferredCloseTimeout = 0;
 
+    public void clear() {
+        new HashSet<>(options.keySet()).forEach(k -> options.remove(k));
+    }
+
     protected ClassLoader getClassLoader(URL[] urls) {
         ClassLoader thisClassLoader = getClass().getClassLoader();
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java	Tue Sep 24 15:40:26 2019 +0200
@@ -186,18 +186,18 @@
     /**
      * The log to which to report warnings.
      */
-    private Log log;
+    private final Log log;
 
     /**
      * Whether or not to report individual warnings, or simply to report a
      * single aggregate warning at the end of the compilation.
      */
-    private boolean verbose;
+    private final boolean verbose;
 
     /**
      * The common prefix for all I18N message keys generated by this handler.
      */
-    private String prefix;
+    private final String prefix;
 
     /**
      * A set containing the names of the source files for which specific
@@ -262,4 +262,11 @@
         else
             log.note(file, new Note("compiler", msg, args));
     }
+
+    public void clear() {
+        sourcesWithReportedWarnings = null;
+        deferredDiagnosticKind = null;
+        deferredDiagnosticSource = null;
+        deferredDiagnosticArg = null;
+    }
 }
--- a/test/langtools/tools/javac/lib/combo/ComboInstance.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/test/langtools/tools/javac/lib/combo/ComboInstance.java	Tue Sep 24 15:40:26 2019 +0200
@@ -23,6 +23,7 @@
 
 package combo;
 
+import java.lang.reflect.Method;
 import javax.tools.StandardJavaFileManager;
 import java.util.Optional;
 
@@ -57,6 +58,14 @@
             env.info().lastError = Optional.of(ex);
         } finally {
             this.env = null;
+            try {
+                Class<?> fmClass = env.fileManager().getClass();
+                Method clear = fmClass.getMethod("clear");
+                clear.setAccessible(true);
+                clear.invoke(env.fileManager());
+            } catch (Exception ex) {
+                throw new IllegalStateException(ex);
+            }
         }
     }
 
@@ -125,4 +134,4 @@
             return success;
         }
     }
-}
\ No newline at end of file
+}
--- a/test/langtools/tools/javac/preview/PreviewErrors.java	Thu Sep 12 17:47:24 2019 +0200
+++ b/test/langtools/tools/javac/preview/PreviewErrors.java	Tue Sep 24 15:40:26 2019 +0200
@@ -25,59 +25,64 @@
  * @test
  * @bug 8226585
  * @summary Verify behavior w.r.t. preview feature API errors and warnings
- * @library /tools/lib
+ * @library /tools/lib /tools/javac/lib
  * @modules
  *      java.base/jdk.internal
  *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.file
  *      jdk.compiler/com.sun.tools.javac.main
+ *      jdk.compiler/com.sun.tools.javac.util
  * @build toolbox.ToolBox toolbox.JavacTask
+ * @build combo.ComboTestHelper
  * @compile --enable-preview -source ${jdk.version} PreviewErrors.java
  * @run main/othervm --enable-preview PreviewErrors
  */
 
-import toolbox.JavacTask;
-import toolbox.Task;
-import toolbox.TestRunner;
-import toolbox.ToolBox;
-
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.List;
+
+import combo.ComboInstance;
+import combo.ComboParameter;
+import combo.ComboTask;
+import combo.ComboTestHelper;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.tools.Diagnostic;
+
 import jdk.internal.PreviewFeature;
 
-public class PreviewErrors extends TestRunner {
+import toolbox.JavacTask;
+import toolbox.ToolBox;
+
+public class PreviewErrors extends ComboInstance<PreviewErrors> {
 
     protected ToolBox tb;
 
     PreviewErrors() {
-        super(System.err);
+        super();
         tb = new ToolBox();
     }
 
     public static void main(String... args) throws Exception {
-        PreviewErrors t = new PreviewErrors();
-        t.runTests();
+        new ComboTestHelper<PreviewErrors>()
+                .withDimension("ESSENTIAL", (x, essential) -> x.essential = essential, EssentialAPI.values())
+                .withDimension("PREVIEW", (x, preview) -> x.preview = preview, Preview.values())
+                .withDimension("LINT", (x, lint) -> x.lint = lint, Lint.values())
+                .withDimension("SUPPRESS", (x, suppress) -> x.suppress = suppress, Suppress.values())
+                .run(PreviewErrors::new);
     }
 
-    /**
-     * Run all methods annotated with @Test, and throw an exception if any
-     * errors are reported..
-     *
-     * @throws Exception if any errors occurred
-     */
-    protected void runTests() throws Exception {
-        runTests(m -> new Object[] { Paths.get(m.getName()) });
-    }
+    private EssentialAPI essential;
+    private Preview preview;
+    private Lint lint;
+    private Suppress suppress;
 
-    Path[] findJavaFiles(Path... paths) throws IOException {
-        return tb.findJavaFiles(paths);
-    }
-
-    @Test
-    public void essentialApi(Path base) throws Exception {
+    @Override
+    public void doWork() throws IOException {
+        Path base = Paths.get(".");
         Path src = base.resolve("src");
         Path srcJavaBase = src.resolve("java.base");
         Path classes = base.resolve("classes");
@@ -85,110 +90,95 @@
 
         Files.createDirectories(classesJavaBase);
 
-        Path srcTest = src.resolve("test");
-        Path classesTest = classes.resolve("test");
-
-        Files.createDirectories(classesTest);
+        tb.writeJavaFiles(srcJavaBase,
+                          """
+                          package java.lang;
+                          public class Extra {
+                              @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.${preview}
+                                                           ${essential})
+                              public static void test() { }
+                          }
+                          """.replace("${preview}", PreviewFeature.Feature.values()[0].name())
+                             .replace("${essential}", essential.expand(null)));
 
-        for (EssentialAPI essential : EssentialAPI.values()) {
-            tb.writeJavaFiles(srcJavaBase,
-                              """
-                              package java.lang;
-                              public class Extra {
-                                  @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.${preview}
-                                                               ${essential})
-                                  public static void test() { }
-                              }
-                              """.replace("${preview}", PreviewFeature.Feature.values()[0].name())
-                                 .replace("${essential}", essential.code()));
-
-            new JavacTask(tb)
-                    .outdir(classesJavaBase)
-                    .options("--patch-module", "java.base=" + srcJavaBase.toString())
-                    .files(findJavaFiles(srcJavaBase))
-                    .run()
-                    .writeAll();
+        new JavacTask(tb)
+                .outdir(classesJavaBase)
+                .options("--patch-module", "java.base=" + srcJavaBase.toString())
+                .files(tb.findJavaFiles(srcJavaBase))
+                .run()
+                .writeAll();
 
-            for (Preview preview : Preview.values()) {
-                for (Lint lint : Lint.values()) {
-                    for (Suppress suppress : Suppress.values()) {
-                        tb.writeJavaFiles(srcTest,
-                                          """
-                                          package test;
-                                          public class Test {
-                                              ${suppress}
-                                              public void test() {
-                                                  Extra.test();
-                                              }
-                                          }
-                                          """.replace("${suppress}", suppress.code()));
+        ComboTask task = newCompilationTask()
+                .withSourceFromTemplate("""
+                                        package test;
+                                        public class Test {
+                                            #{SUPPRESS}
+                                            public void test() {
+                                                Extra.test();
+                                            }
+                                        }
+                                        """)
+                .withOption("-XDrawDiagnostics")
+                .withOption("--patch-module")
+                .withOption("java.base=" + classesJavaBase.toString())
+                .withOption("-source")
+                .withOption(String.valueOf(Runtime.version().feature()));
+        if (preview.expand(null)!= null) {
+            task = task.withOption(preview.expand(null));
+        }
 
-                        List<String> options = new ArrayList<>();
+        if (lint.expand(null) != null) {
+            task = task.withOption(lint.expand(null));
+        }
 
-                        options.add("-XDrawDiagnostics");
-                        options.add("--patch-module");
-                        options.add("java.base=" + classesJavaBase.toString());
-                        options.add("-source");
-                        options.add(String.valueOf(Runtime.version().feature()));
-
-                        if (preview.opt() != null) {
-                            options.add(preview.opt());
+        task.generate(result -> {
+                Set<String> actual = Arrays.stream(Diagnostic.Kind.values())
+                                            .flatMap(kind -> result.diagnosticsForKind(kind).stream())
+                                            .map(d -> d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode())
+                                            .collect(Collectors.toSet());
+                Set<String> expected;
+                boolean ok;
+                if (essential == EssentialAPI.YES) {
+                    if (preview == Preview.YES) {
+                        if (lint == Lint.ENABLE_PREVIEW) {
+                            expected = Set.of("5:14:compiler.warn.is.preview");
+                        } else {
+                            expected = Set.of("-1:-1:compiler.note.preview.filename",
+                                              "-1:-1:compiler.note.preview.recompile");
                         }
-
-                        if (lint.opt() != null) {
-                            options.add(lint.opt());
-                        }
-                        List<String> output;
-                        List<String> expected;
-                        Task.Expect expect;
-
-                        if (essential == EssentialAPI.YES) {
-                            if (preview == Preview.YES) {
-                                if (lint == Lint.ENABLE_PREVIEW) {
-                                    expected = List.of("Test.java:5:14: compiler.warn.is.preview: test()",
-                                                       "1 warning");
-                                } else {
-                                    expected = List.of("- compiler.note.preview.filename: Test.java",
-                                                       "- compiler.note.preview.recompile");
-                                }
-                                expect = Task.Expect.SUCCESS;
-                            } else {
-                                expected = List.of("Test.java:5:14: compiler.err.is.preview: test()",
-                                                   "1 error");
-                                expect = Task.Expect.FAIL;
-                            }
-                        } else {
-                            if (suppress == Suppress.YES) {
-                                expected = List.of("");
-                            } else if ((preview == Preview.YES && (lint == Lint.NONE || lint == Lint.DISABLE_PREVIEW)) ||
-                                       (preview == Preview.NO && lint == Lint.DISABLE_PREVIEW)) {
-                                expected = List.of("- compiler.note.preview.filename: Test.java",
-                                                   "- compiler.note.preview.recompile");
-                            } else {
-                                expected = List.of("Test.java:5:14: compiler.warn.is.preview: test()",
-                                                   "1 warning");
-                            }
-                            expect = Task.Expect.SUCCESS;
-                        }
-
-                        output = new JavacTask(tb)
-                                .outdir(classesTest)
-                                .options(options)
-                                .files(findJavaFiles(srcTest))
-                                .run(expect)
-                                .writeAll()
-                                .getOutputLines(Task.OutputKind.DIRECT);
-
-                        if (!expected.equals(output)) {
-                            throw new IllegalStateException("Unexpected output for " + essential + ", " + preview + ", " + lint + ", " + suppress + ": " + output);
-                        }
+                        ok = true;
+                    } else {
+                        expected = Set.of("5:14:compiler.err.is.preview");
+                        ok = false;
+                    }
+                } else {
+                    if (suppress == Suppress.YES) {
+                        expected = Set.of();
+                    } else if ((preview == Preview.YES && (lint == Lint.NONE || lint == Lint.DISABLE_PREVIEW)) ||
+                               (preview == Preview.NO && lint == Lint.DISABLE_PREVIEW)) {
+                        expected = Set.of("-1:-1:compiler.note.preview.filename",
+                                          "-1:-1:compiler.note.preview.recompile");
+                    } else {
+                        expected = Set.of("5:14:compiler.warn.is.preview");
+                    }
+                    ok = true;
+                }
+                if (ok) {
+                    if (!result.get().iterator().hasNext()) {
+                        throw new IllegalStateException("Did not succeed as expected.");
+                    }
+                } else {
+                    if (result.get().iterator().hasNext()) {
+                        throw new IllegalStateException("Succeed unexpectedly.");
                     }
                 }
-            }
-        }
+                if (!expected.equals(actual)) {
+                    throw new IllegalStateException("Unexpected output for " + essential + ", " + preview + ", " + lint + ", " + suppress + ": " + actual.toString());
+                }
+            });
     }
 
-    public enum EssentialAPI {
+    public enum EssentialAPI implements ComboParameter {
         YES(", essentialAPI=true"),
         NO(", essentialAPI=false");
 
@@ -198,12 +188,12 @@
             this.code = code;
         }
 
-        public String code() {
+        public String expand(String optParameter) {
             return code;
         }
     }
 
-    public enum Preview {
+    public enum Preview implements ComboParameter {
         YES("--enable-preview"),
         NO(null);
 
@@ -213,12 +203,12 @@
             this.opt = opt;
         }
 
-        public String opt() {
+        public String expand(String optParameter) {
             return opt;
         }
     }
 
-    public enum Lint {
+    public enum Lint implements ComboParameter {
         NONE(null),
         ENABLE_PREVIEW("-Xlint:preview"),
         DISABLE_PREVIEW("-Xlint:-preview");
@@ -229,12 +219,12 @@
             this.opt = opt;
         }
 
-        public String opt() {
+        public String expand(String optParameter) {
             return opt;
         }
     }
 
-    public enum Suppress {
+    public enum Suppress implements ComboParameter {
         YES("@SuppressWarnings(\"preview\")"),
         NO("");
 
@@ -244,7 +234,7 @@
             this.code = code;
         }
 
-        public String code() {
+        public String expand(String optParameter) {
             return code;
         }
     }