Fri, 27 May 2011 15:02:39 -0700
7045697: JDK7 THIRD PARTY README update
Reviewed-by: lana
1 /*
2 * Copyright (c) 2011, 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 * @bug 7046348
27 * @summary Regression: javac complains of missing classfile for a seemingly unrelated interface
28 */
30 import java.io.File;
31 import java.net.URI;
32 import java.util.Arrays;
34 import javax.tools.Diagnostic;
35 import javax.tools.DiagnosticListener;
36 import javax.tools.JavaCompiler;
37 import javax.tools.JavaCompiler.CompilationTask;
38 import javax.tools.JavaFileObject;
39 import javax.tools.SimpleJavaFileObject;
40 import javax.tools.ToolProvider;
42 public class EagerInterfaceCompletionTest {
44 JavaCompiler javacTool;
45 File testDir;
46 HierarchyKind hierarchyKind;
47 TestKind testKind;
48 ActionKind actionKind;
50 EagerInterfaceCompletionTest(JavaCompiler javacTool, File testDir,
51 HierarchyKind hierarchyKind, TestKind testKind, ActionKind actionKind) {
52 this.javacTool = javacTool;
53 this.hierarchyKind = hierarchyKind;
54 this.testDir = testDir;
55 this.testKind = testKind;
56 this.actionKind = actionKind;
57 }
59 void test() {
60 testDir.mkdirs();
61 compile(null, hierarchyKind.source);
62 actionKind.doAction(this);
63 DiagnosticChecker dc = new DiagnosticChecker();
64 compile(dc, testKind.source);
65 if (testKind.completionFailure(actionKind, hierarchyKind) != dc.errorFound) {
66 if (dc.errorFound) {
67 error("Unexpected completion failure" +
68 "\nhierarhcyKind " + hierarchyKind +
69 "\ntestKind " + testKind +
70 "\nactionKind " + actionKind);
71 } else {
72 error("Missing completion failure " +
73 "\nhierarhcyKind " + hierarchyKind +
74 "\ntestKind " + testKind +
75 "\nactionKind " + actionKind);
76 }
77 }
78 }
80 void compile(DiagnosticChecker dc, JavaSource... sources) {
81 try {
82 CompilationTask ct = javacTool.getTask(null, null, dc,
83 Arrays.asList("-d", testDir.getAbsolutePath(), "-cp", testDir.getAbsolutePath()),
84 null, Arrays.asList(sources));
85 ct.call();
86 }
87 catch (Exception e) {
88 e.printStackTrace();
89 error("Internal compilation error");
90 }
91 }
93 void removeClass(String classToRemoveStr) {
94 File classToRemove = new File(testDir, classToRemoveStr);
95 if (!classToRemove.exists()) {
96 error("Expected file " + classToRemove + " does not exists in folder " + testDir);
97 }
98 classToRemove.delete();
99 };
101 void error(String msg) {
102 System.err.println(msg);
103 nerrors++;
104 }
106 class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
108 boolean errorFound = false;
110 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
111 errorFound = true;
112 }
113 }
115 //global declarations
117 enum HierarchyKind {
118 INTERFACE("interface A { boolean f = false; void m(); }\n" +
119 "class B implements A { public void m() {} }"),
120 CLASS("class A { boolean f; void m() {} }\n" +
121 "class B extends A { void m() {} }"),
122 ABSTRACT_CLASS("abstract class A { boolean f; abstract void m(); }\n" +
123 "class B extends A { void m() {} }");
125 JavaSource source;
127 private HierarchyKind(String code) {
128 this.source = new JavaSource("Test1.java", code);
129 }
130 }
132 enum ActionKind {
133 REMOVE_A("A.class"),
134 REMOVE_B("B.class");
136 String classFile;
138 private ActionKind(String classFile) {
139 this.classFile = classFile;
140 }
142 void doAction(EagerInterfaceCompletionTest test) {
143 test.removeClass(classFile);
144 };
145 }
147 enum TestKind {
148 ACCESS_ONLY("class C { B b; }"),
149 SUPER("class C extends B {}"),
150 METHOD("class C { void test(B b) { b.m(); } }"),
151 FIELD("class C { void test(B b) { boolean b2 = b.f; } }"),
152 CONSTR("class C { void test() { new B(); } }");
154 JavaSource source;
156 private TestKind(final String code) {
157 this.source = new JavaSource("Test2.java", code);
158 }
160 boolean completionFailure(ActionKind ak, HierarchyKind hk) {
161 switch (this) {
162 case ACCESS_ONLY:
163 case CONSTR: return ak == ActionKind.REMOVE_B;
164 case FIELD:
165 case SUPER: return true;
166 case METHOD: return hk != HierarchyKind.INTERFACE || ak == ActionKind.REMOVE_B;
167 default: throw new AssertionError("Unexpected test kind " + this);
168 }
169 }
170 }
172 public static void main(String[] args) throws Exception {
173 String SCRATCH_DIR = System.getProperty("user.dir");
174 JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
175 int n = 0;
176 for (HierarchyKind hierarchyKind : HierarchyKind.values()) {
177 for (TestKind testKind : TestKind.values()) {
178 for (ActionKind actionKind : ActionKind.values()) {
179 File testDir = new File(SCRATCH_DIR, "test"+n);
180 new EagerInterfaceCompletionTest(javacTool, testDir, hierarchyKind, testKind, actionKind).test();
181 n++;
182 }
183 }
184 }
185 if (nerrors > 0) {
186 throw new AssertionError("Some errors have been detected");
187 }
188 }
190 static class JavaSource extends SimpleJavaFileObject {
192 String source;
194 public JavaSource(String filename, String source) {
195 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
196 this.source = source;
197 }
199 @Override
200 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
201 return source;
202 }
203 }
205 static int nerrors = 0;
206 }