jdk/test/jdk/lambda/FDTest.java
changeset 29784 b9220497eb43
parent 29783 db33e568f107
parent 29770 6415d011ad02
child 29785 da950f343762
equal deleted inserted replaced
29783:db33e568f107 29784:b9220497eb43
     1 /*
       
     2  * Copyright (c) 2012, 2013, 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  */
       
    23 
       
    24 
       
    25 import shapegen.*;
       
    26 
       
    27 import com.sun.source.util.JavacTask;
       
    28 import com.sun.tools.javac.util.Pair;
       
    29 
       
    30 import java.net.URI;
       
    31 import java.util.Arrays;
       
    32 import java.util.ArrayList;
       
    33 import java.util.Collection;
       
    34 import java.util.List;
       
    35 
       
    36 import javax.tools.Diagnostic;
       
    37 import javax.tools.JavaCompiler;
       
    38 import javax.tools.JavaFileObject;
       
    39 import javax.tools.SimpleJavaFileObject;
       
    40 import javax.tools.StandardJavaFileManager;
       
    41 import javax.tools.ToolProvider;
       
    42 
       
    43 import org.testng.annotations.Test;
       
    44 import org.testng.annotations.BeforeSuite;
       
    45 import org.testng.annotations.DataProvider;
       
    46 import static org.testng.Assert.*;
       
    47 
       
    48 public class FDTest {
       
    49 
       
    50     public enum TestKind {
       
    51         POSITIVE,
       
    52         NEGATIVE;
       
    53 
       
    54         Collection<Hierarchy> getHierarchy(HierarchyGenerator hg) {
       
    55             return this == POSITIVE ?
       
    56                     hg.getOK() : hg.getErr();
       
    57         }
       
    58     }
       
    59 
       
    60     public static JavaCompiler comp;
       
    61     public static StandardJavaFileManager fm;
       
    62 
       
    63     @BeforeSuite
       
    64     static void init() {
       
    65         // create default shared JavaCompiler - reused across multiple
       
    66         // compilations
       
    67 
       
    68         comp = ToolProvider.getSystemJavaCompiler();
       
    69         fm = comp.getStandardFileManager(null, null, null);
       
    70     }
       
    71 
       
    72     public static void main(String[] args) throws Exception {
       
    73         init();
       
    74 
       
    75         for (Pair<TestKind,Hierarchy> fdtest : generateCases()) {
       
    76             runTest(fdtest.fst, fdtest.snd, comp, fm);
       
    77         }
       
    78     }
       
    79 
       
    80     @Test(dataProvider = "fdCases")
       
    81     public void testOneCase(TestKind tk, Hierarchy hs)
       
    82             throws Exception {
       
    83         FDTest.runTest(tk, hs, comp, fm);
       
    84     }
       
    85 
       
    86     @DataProvider(name = "fdCases")
       
    87     public Object[][] caseGenerator() {
       
    88         List<Pair<TestKind, Hierarchy>> cases = generateCases();
       
    89         Object[][] fdCases = new Object[cases.size()][];
       
    90         for (int i = 0; i < cases.size(); ++i) {
       
    91             fdCases[i] = new Object[2];
       
    92             fdCases[i][0] = cases.get(i).fst;
       
    93             fdCases[i][1] = cases.get(i).snd;
       
    94         }
       
    95         return fdCases;
       
    96     }
       
    97 
       
    98     public static List<Pair<TestKind, Hierarchy>> generateCases() {
       
    99         ArrayList<Pair<TestKind,Hierarchy>> list = new ArrayList<>();
       
   100         HierarchyGenerator hg = new HierarchyGenerator();
       
   101         for (TestKind tk : TestKind.values()) {
       
   102             for (Hierarchy hs : tk.getHierarchy(hg)) {
       
   103                 list.add(new Pair<>(tk, hs));
       
   104             }
       
   105         }
       
   106         return list;
       
   107     }
       
   108 
       
   109     public static void runTest(TestKind tk, Hierarchy hs,
       
   110             JavaCompiler comp, StandardJavaFileManager fm) throws Exception {
       
   111         new FDTest(tk, hs).run(comp, fm);
       
   112     }
       
   113 
       
   114     TestKind tk;
       
   115     Hierarchy hs;
       
   116     DefenderTestSource source;
       
   117     DiagnosticChecker diagChecker;
       
   118 
       
   119     public FDTest() {}
       
   120 
       
   121     FDTest(TestKind tk, Hierarchy hs) {
       
   122         this.tk = tk;
       
   123         this.hs = hs;
       
   124         this.source = new DefenderTestSource();
       
   125         this.diagChecker = new DiagnosticChecker();
       
   126     }
       
   127 
       
   128     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
       
   129         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
       
   130                 null, null, Arrays.asList(source));
       
   131         try {
       
   132             ct.analyze();
       
   133         } catch (Throwable ex) {
       
   134             fail("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
       
   135         }
       
   136         check();
       
   137     }
       
   138 
       
   139     void check() {
       
   140         boolean errorExpected = tk == TestKind.NEGATIVE;
       
   141         if (errorExpected != diagChecker.errorFound) {
       
   142             fail("problem in source: \n" +
       
   143                  "\nerror found = " + diagChecker.errorFound +
       
   144                  "\nerror expected = " + errorExpected +
       
   145                  "\n" + dumpHierarchy() +
       
   146                  "\n" + source.getCharContent(true));
       
   147         }
       
   148     }
       
   149 
       
   150     String dumpHierarchy() {
       
   151         StringBuilder buf = new StringBuilder();
       
   152         buf.append("root = " + hs.root + "\n");
       
   153         for (ClassCase cc : hs.all) {
       
   154             buf.append("  class name = " + cc.getName() + "\n");
       
   155             buf.append("    class OK = " + cc.get_OK() + "\n");
       
   156             buf.append("    prov = " + cc.get_mprov() + "\n");
       
   157 
       
   158         }
       
   159         return buf.toString();
       
   160     }
       
   161 
       
   162     class DefenderTestSource extends SimpleJavaFileObject {
       
   163 
       
   164         String source;
       
   165 
       
   166         public DefenderTestSource() {
       
   167             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
   168             StringBuilder buf = new StringBuilder();
       
   169             List<ClassCase> defaultRef = new ArrayList<>();
       
   170             for (ClassCase cc : hs.all) {
       
   171                 Hierarchy.genClassDef(buf, cc, null, defaultRef);
       
   172             }
       
   173             source = buf.toString();
       
   174         }
       
   175 
       
   176         @Override
       
   177         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
   178             return source;
       
   179         }
       
   180     }
       
   181 
       
   182     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
       
   183 
       
   184         boolean errorFound;
       
   185 
       
   186         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   187             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
       
   188                 errorFound = true;
       
   189             }
       
   190         }
       
   191     }
       
   192 }