test/tools/javac/defaultMethods/fd/FDTest.java

Sat, 17 Nov 2012 19:01:03 +0000

author
mcimadamore
date
Sat, 17 Nov 2012 19:01:03 +0000
changeset 1415
01c9d4161882
parent 1393
d7d932236fee
permissions
-rw-r--r--

8003280: Add lambda tests
Summary: Turn on lambda expression, method reference and default method support
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @summary Automatic test for checking correctness of default resolution
    27  */
    29 import shapegen.*;
    31 import com.sun.source.util.JavacTask;
    33 import java.net.URI;
    34 import java.util.Arrays;
    35 import java.util.ArrayList;
    36 import java.util.Collection;
    37 import java.util.List;
    39 import javax.tools.Diagnostic;
    40 import javax.tools.JavaCompiler;
    41 import javax.tools.JavaFileObject;
    42 import javax.tools.SimpleJavaFileObject;
    43 import javax.tools.StandardJavaFileManager;
    44 import javax.tools.ToolProvider;
    46 public class FDTest {
    48     enum TestKind {
    49         POSITIVE,
    50         NEGATIVE;
    52         Collection<Hierarchy> getHierarchy(HierarchyGenerator hg) {
    53             return this == POSITIVE ?
    54                     hg.getOK() : hg.getErr();
    55         }
    56     }
    58     public static void main(String[] args) throws Exception {
    59         //create default shared JavaCompiler - reused across multiple compilations
    60         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    61         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    63         HierarchyGenerator hg = new HierarchyGenerator();
    64         for (TestKind tk : TestKind.values()) {
    65             for (Hierarchy hs : tk.getHierarchy(hg)) {
    66                 new FDTest(tk, hs).run(comp, fm);
    67             }
    68         }
    69     }
    71     TestKind tk;
    72     Hierarchy hs;
    73     DefenderTestSource source;
    74     DiagnosticChecker diagChecker;
    76     FDTest(TestKind tk, Hierarchy hs) {
    77         this.tk = tk;
    78         this.hs = hs;
    79         this.source = new DefenderTestSource();
    80         this.diagChecker = new DiagnosticChecker();
    81     }
    83     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    84         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
    85                 null, null, Arrays.asList(source));
    86         try {
    87             ct.analyze();
    88         } catch (Throwable ex) {
    89             throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
    90         }
    91         check();
    92     }
    94     void check() {
    95         boolean errorExpected = tk == TestKind.NEGATIVE;
    96         if (errorExpected != diagChecker.errorFound) {
    97             throw new AssertionError("problem in source: \n" +
    98                     "\nerror found = " + diagChecker.errorFound +
    99                     "\nerror expected = " + errorExpected +
   100                     "\n" + dumpHierarchy() +
   101                     "\n" + source.getCharContent(true));
   102         }
   103     }
   105     String dumpHierarchy() {
   106         StringBuilder buf = new StringBuilder();
   107         buf.append("root = " + hs.root + "\n");
   108         for (ClassCase cc : hs.all) {
   109             buf.append("  class name = " + cc.getName() + "\n");
   110             buf.append("    class OK = " + cc.get_OK() + "\n");
   111             buf.append("    prov = " + cc.get_mprov() + "\n");
   113         }
   114         return buf.toString();
   115     }
   117     class DefenderTestSource extends SimpleJavaFileObject {
   119         String source;
   121         public DefenderTestSource() {
   122             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   123             StringBuilder buf = new StringBuilder();
   124             List<ClassCase> defaultRef = new ArrayList<>();
   125             for (ClassCase cc : hs.all) {
   126                 hs.genClassDef(buf, cc, null, defaultRef);
   127             }
   128             source = buf.toString();
   129         }
   131         @Override
   132         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   133             return source;
   134         }
   135     }
   137     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   139         boolean errorFound;
   141         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   142             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   143                 errorFound = true;
   144             }
   145         }
   146     }
   147 }

mercurial