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