mcimadamore@1393: /* mcimadamore@1393: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. mcimadamore@1393: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1393: * mcimadamore@1393: * This code is free software; you can redistribute it and/or modify it mcimadamore@1393: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1393: * published by the Free Software Foundation. mcimadamore@1393: * mcimadamore@1393: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1393: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1393: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1393: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1393: * accompanied this code). mcimadamore@1393: * mcimadamore@1393: * You should have received a copy of the GNU General Public License version mcimadamore@1393: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1393: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1393: * mcimadamore@1393: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1393: * or visit www.oracle.com if you need additional information or have any mcimadamore@1393: * questions. mcimadamore@1393: */ mcimadamore@1393: mcimadamore@1393: /* mcimadamore@1393: * @test mcimadamore@1393: * @summary Automatic test for checking correctness of default resolution mcimadamore@1393: */ mcimadamore@1393: mcimadamore@1393: import shapegen.*; mcimadamore@1393: mcimadamore@1393: import com.sun.source.util.JavacTask; mcimadamore@1393: mcimadamore@1393: import java.net.URI; mcimadamore@1393: import java.util.Arrays; mcimadamore@1393: import java.util.ArrayList; mcimadamore@1393: import java.util.Collection; mcimadamore@1393: import java.util.List; mcimadamore@1393: mcimadamore@1393: import javax.tools.Diagnostic; mcimadamore@1393: import javax.tools.JavaCompiler; mcimadamore@1393: import javax.tools.JavaFileObject; mcimadamore@1393: import javax.tools.SimpleJavaFileObject; mcimadamore@1393: import javax.tools.StandardJavaFileManager; mcimadamore@1393: import javax.tools.ToolProvider; mcimadamore@1393: mcimadamore@1393: public class FDTest { mcimadamore@1393: mcimadamore@1393: enum TestKind { mcimadamore@1393: POSITIVE, mcimadamore@1393: NEGATIVE; mcimadamore@1393: mcimadamore@1393: Collection getHierarchy(HierarchyGenerator hg) { mcimadamore@1393: return this == POSITIVE ? mcimadamore@1393: hg.getOK() : hg.getErr(); mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: public static void main(String[] args) throws Exception { mcimadamore@1393: //create default shared JavaCompiler - reused across multiple compilations mcimadamore@1393: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1393: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1393: mcimadamore@1393: HierarchyGenerator hg = new HierarchyGenerator(); mcimadamore@1393: for (TestKind tk : TestKind.values()) { mcimadamore@1393: for (Hierarchy hs : tk.getHierarchy(hg)) { mcimadamore@1393: new FDTest(tk, hs).run(comp, fm); mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: TestKind tk; mcimadamore@1393: Hierarchy hs; mcimadamore@1393: DefenderTestSource source; mcimadamore@1393: DiagnosticChecker diagChecker; mcimadamore@1393: mcimadamore@1393: FDTest(TestKind tk, Hierarchy hs) { mcimadamore@1393: this.tk = tk; mcimadamore@1393: this.hs = hs; mcimadamore@1393: this.source = new DefenderTestSource(); mcimadamore@1393: this.diagChecker = new DiagnosticChecker(); mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { mcimadamore@1393: JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, mcimadamore@1393: Arrays.asList("-XDallowDefaultMethods"), null, Arrays.asList(source)); mcimadamore@1393: try { mcimadamore@1393: ct.analyze(); mcimadamore@1393: } catch (Throwable ex) { mcimadamore@1393: throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true)); mcimadamore@1393: } mcimadamore@1393: check(); mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: void check() { mcimadamore@1393: boolean errorExpected = tk == TestKind.NEGATIVE; mcimadamore@1393: if (errorExpected != diagChecker.errorFound) { mcimadamore@1393: throw new AssertionError("problem in source: \n" + mcimadamore@1393: "\nerror found = " + diagChecker.errorFound + mcimadamore@1393: "\nerror expected = " + errorExpected + mcimadamore@1393: "\n" + dumpHierarchy() + mcimadamore@1393: "\n" + source.getCharContent(true)); mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: String dumpHierarchy() { mcimadamore@1393: StringBuilder buf = new StringBuilder(); mcimadamore@1393: buf.append("root = " + hs.root + "\n"); mcimadamore@1393: for (ClassCase cc : hs.all) { mcimadamore@1393: buf.append(" class name = " + cc.getName() + "\n"); mcimadamore@1393: buf.append(" class OK = " + cc.get_OK() + "\n"); mcimadamore@1393: buf.append(" prov = " + cc.get_mprov() + "\n"); mcimadamore@1393: mcimadamore@1393: } mcimadamore@1393: return buf.toString(); mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: class DefenderTestSource extends SimpleJavaFileObject { mcimadamore@1393: mcimadamore@1393: String source; mcimadamore@1393: mcimadamore@1393: public DefenderTestSource() { mcimadamore@1393: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1393: StringBuilder buf = new StringBuilder(); mcimadamore@1393: List defaultRef = new ArrayList<>(); mcimadamore@1393: for (ClassCase cc : hs.all) { mcimadamore@1393: hs.genClassDef(buf, cc, null, defaultRef); mcimadamore@1393: } mcimadamore@1393: source = buf.toString(); mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: @Override mcimadamore@1393: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1393: return source; mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: mcimadamore@1393: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@1393: mcimadamore@1393: boolean errorFound; mcimadamore@1393: mcimadamore@1393: public void report(Diagnostic diagnostic) { mcimadamore@1393: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { mcimadamore@1393: errorFound = true; mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: } mcimadamore@1393: }