Wed, 23 Jan 2013 20:57:40 +0000
8006694: temporarily workaround combo tests are causing time out in several platforms
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com
1 /*
2 * Copyright (c) 2011, 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 */
24 /*
25 * @test
26 * @bug 7046778 8006694
27 * @summary Project Coin: problem with diamond and member inner classes
28 * temporarily workaround combo tests are causing time out in several platforms
29 * @library ../../../lib
30 * @build JavacTestingAbstractThreadedTest
31 * @run main/othervm DiamondAndInnerClassTest
32 */
34 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
35 // see JDK-8006746
37 import com.sun.source.util.JavacTask;
38 import java.net.URI;
39 import java.util.Arrays;
40 import javax.tools.Diagnostic;
41 import javax.tools.JavaFileObject;
42 import javax.tools.SimpleJavaFileObject;
44 public class DiamondAndInnerClassTest
45 extends JavacTestingAbstractThreadedTest
46 implements Runnable {
48 enum TypeArgumentKind {
49 NONE(""),
50 STRING("<String>"),
51 INTEGER("<Integer>"),
52 DIAMOND("<>");
54 String typeargStr;
56 private TypeArgumentKind(String typeargStr) {
57 this.typeargStr = typeargStr;
58 }
60 boolean compatible(TypeArgumentKind that) {
61 switch (this) {
62 case NONE: return true;
63 case STRING: return that != INTEGER;
64 case INTEGER: return that != STRING;
65 default: throw new AssertionError("Unexpected decl kind: " + this);
66 }
67 }
69 boolean compatible(ArgumentKind that) {
70 switch (this) {
71 case NONE: return true;
72 case STRING: return that == ArgumentKind.STRING;
73 case INTEGER: return that == ArgumentKind.INTEGER;
74 default: throw new AssertionError("Unexpected decl kind: " + this);
75 }
76 }
77 }
79 enum ArgumentKind {
80 OBJECT("(Object)null"),
81 STRING("(String)null"),
82 INTEGER("(Integer)null");
84 String argStr;
86 private ArgumentKind(String argStr) {
87 this.argStr = argStr;
88 }
89 }
91 enum TypeQualifierArity {
92 ONE(1, "A1#TA1"),
93 TWO(2, "A1#TA1.A2#TA2"),
94 THREE(3, "A1#TA1.A2#TA2.A3#TA3");
96 int n;
97 String qualifierStr;
99 private TypeQualifierArity(int n, String qualifierStr) {
100 this.n = n;
101 this.qualifierStr = qualifierStr;
102 }
104 String getType(TypeArgumentKind... typeArgumentKinds) {
105 String res = qualifierStr;
106 for (int i = 1 ; i <= typeArgumentKinds.length ; i++) {
107 res = res.replace("#TA" + i, typeArgumentKinds[i-1].typeargStr);
108 }
109 return res;
110 }
112 boolean matches(InnerClassDeclArity innerClassDeclArity) {
113 return n ==innerClassDeclArity.n;
114 }
115 }
117 enum InnerClassDeclArity {
118 ONE(1, "class A1<X> { A1(X x1) { } #B }"),
119 TWO(2, "class A1<X1> { class A2<X2> { A2(X1 x1, X2 x2) { } #B } }"),
120 THREE(3, "class A1<X1> { class A2<X2> { class A3<X3> { A3(X1 x1, X2 x2, X3 x3) { } #B } } }");
122 int n;
123 String classDeclStr;
125 private InnerClassDeclArity(int n, String classDeclStr) {
126 this.n = n;
127 this.classDeclStr = classDeclStr;
128 }
129 }
131 enum ArgumentListArity {
132 ONE(1, "(#A1)"),
133 TWO(2, "(#A1,#A2)"),
134 THREE(3, "(#A1,#A2,#A3)");
136 int n;
137 String argListStr;
139 private ArgumentListArity(int n, String argListStr) {
140 this.n = n;
141 this.argListStr = argListStr;
142 }
144 String getArgs(ArgumentKind... argumentKinds) {
145 String res = argListStr;
146 for (int i = 1 ; i <= argumentKinds.length ; i++) {
147 res = res.replace("#A" + i, argumentKinds[i-1].argStr);
148 }
149 return res;
150 }
152 boolean matches(InnerClassDeclArity innerClassDeclArity) {
153 return n ==innerClassDeclArity.n;
154 }
155 }
157 public static void main(String... args) throws Exception {
158 for (InnerClassDeclArity innerClassDeclArity : InnerClassDeclArity.values()) {
159 for (TypeQualifierArity declType : TypeQualifierArity.values()) {
160 if (!declType.matches(innerClassDeclArity)) continue;
161 for (TypeQualifierArity newClassType : TypeQualifierArity.values()) {
162 if (!newClassType.matches(innerClassDeclArity)) continue;
163 for (ArgumentListArity argList : ArgumentListArity.values()) {
164 if (!argList.matches(innerClassDeclArity)) continue;
165 for (TypeArgumentKind taDecl1 : TypeArgumentKind.values()) {
166 boolean isDeclRaw = taDecl1 == TypeArgumentKind.NONE;
167 //no diamond on decl site
168 if (taDecl1 == TypeArgumentKind.DIAMOND) continue;
169 for (TypeArgumentKind taSite1 : TypeArgumentKind.values()) {
170 boolean isSiteRaw =
171 taSite1 == TypeArgumentKind.NONE;
172 //diamond only allowed on the last type qualifier
173 if (taSite1 == TypeArgumentKind.DIAMOND &&
174 innerClassDeclArity !=
175 InnerClassDeclArity.ONE)
176 continue;
177 for (ArgumentKind arg1 : ArgumentKind.values()) {
178 if (innerClassDeclArity == innerClassDeclArity.ONE) {
179 pool.execute(
180 new DiamondAndInnerClassTest(
181 innerClassDeclArity, declType,
182 newClassType, argList,
183 new TypeArgumentKind[] {taDecl1},
184 new TypeArgumentKind[] {taSite1},
185 new ArgumentKind[] {arg1}));
186 continue;
187 }
188 for (TypeArgumentKind taDecl2 : TypeArgumentKind.values()) {
189 //no rare types
190 if (isDeclRaw != (taDecl2 == TypeArgumentKind.NONE))
191 continue;
192 //no diamond on decl site
193 if (taDecl2 == TypeArgumentKind.DIAMOND)
194 continue;
195 for (TypeArgumentKind taSite2 : TypeArgumentKind.values()) {
196 //no rare types
197 if (isSiteRaw != (taSite2 == TypeArgumentKind.NONE))
198 continue;
199 //diamond only allowed on the last type qualifier
200 if (taSite2 == TypeArgumentKind.DIAMOND &&
201 innerClassDeclArity != InnerClassDeclArity.TWO)
202 continue;
203 for (ArgumentKind arg2 : ArgumentKind.values()) {
204 if (innerClassDeclArity == innerClassDeclArity.TWO) {
205 pool.execute(
206 new DiamondAndInnerClassTest(
207 innerClassDeclArity,
208 declType,
209 newClassType,
210 argList,
211 new TypeArgumentKind[] {taDecl1, taDecl2},
212 new TypeArgumentKind[] {taSite1, taSite2},
213 new ArgumentKind[] {arg1, arg2}));
214 continue;
215 }
216 for (TypeArgumentKind taDecl3 : TypeArgumentKind.values()) {
217 //no rare types
218 if (isDeclRaw != (taDecl3 == TypeArgumentKind.NONE))
219 continue;
220 //no diamond on decl site
221 if (taDecl3 == TypeArgumentKind.DIAMOND)
222 continue;
223 for (TypeArgumentKind taSite3 : TypeArgumentKind.values()) {
224 //no rare types
225 if (isSiteRaw != (taSite3 == TypeArgumentKind.NONE))
226 continue;
227 //diamond only allowed on the last type qualifier
228 if (taSite3 == TypeArgumentKind.DIAMOND &&
229 innerClassDeclArity != InnerClassDeclArity.THREE)
230 continue;
231 for (ArgumentKind arg3 : ArgumentKind.values()) {
232 if (innerClassDeclArity ==
233 innerClassDeclArity.THREE) {
234 pool.execute(
235 new DiamondAndInnerClassTest(
236 innerClassDeclArity,
237 declType,
238 newClassType,
239 argList,
240 new TypeArgumentKind[] {taDecl1, taDecl2, taDecl3},
241 new TypeArgumentKind[] {taSite1, taSite2, taSite3},
242 new ArgumentKind[] {arg1, arg2, arg3}));
243 continue;
244 }
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252 }
253 }
254 }
255 }
256 }
257 }
259 checkAfterExec();
260 }
262 InnerClassDeclArity innerClassDeclArity;
263 TypeQualifierArity declType;
264 TypeQualifierArity siteType;
265 ArgumentListArity argList;
266 TypeArgumentKind[] declTypeArgumentKinds;
267 TypeArgumentKind[] siteTypeArgumentKinds;
268 ArgumentKind[] argumentKinds;
269 JavaSource source;
270 DiagnosticChecker diagChecker;
272 DiamondAndInnerClassTest(InnerClassDeclArity innerClassDeclArity,
273 TypeQualifierArity declType, TypeQualifierArity siteType,
274 ArgumentListArity argList, TypeArgumentKind[] declTypeArgumentKinds,
275 TypeArgumentKind[] siteTypeArgumentKinds, ArgumentKind[] argumentKinds) {
276 this.innerClassDeclArity = innerClassDeclArity;
277 this.declType = declType;
278 this.siteType = siteType;
279 this.argList = argList;
280 this.declTypeArgumentKinds = declTypeArgumentKinds;
281 this.siteTypeArgumentKinds = siteTypeArgumentKinds;
282 this.argumentKinds = argumentKinds;
283 this.source = new JavaSource();
284 this.diagChecker = new DiagnosticChecker();
285 }
287 class JavaSource extends SimpleJavaFileObject {
289 String bodyTemplate = "#D res = new #S#AL;";
291 String source;
293 public JavaSource() {
294 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
295 source = innerClassDeclArity.classDeclStr.replace("#B", bodyTemplate)
296 .replace("#D", declType.getType(declTypeArgumentKinds))
297 .replace("#S", siteType.getType(siteTypeArgumentKinds))
298 .replace("#AL", argList.getArgs(argumentKinds));
299 }
301 @Override
302 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
303 return source;
304 }
305 }
307 @Override
308 public void run() {
309 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
310 null, null, Arrays.asList(source));
311 try {
312 ct.analyze();
313 } catch (Throwable ex) {
314 throw new AssertionError("Error thrown when compiling the following code:\n" +
315 source.getCharContent(true));
316 }
317 check();
318 }
320 void check() {
321 checkCount.incrementAndGet();
323 boolean errorExpected = false;
325 TypeArgumentKind[] expectedArgKinds =
326 new TypeArgumentKind[innerClassDeclArity.n];
328 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
329 if (!declTypeArgumentKinds[i].compatible(siteTypeArgumentKinds[i])) {
330 errorExpected = true;
331 break;
332 }
333 expectedArgKinds[i] = siteTypeArgumentKinds[i] ==
334 TypeArgumentKind.DIAMOND ?
335 declTypeArgumentKinds[i] : siteTypeArgumentKinds[i];
336 }
338 if (!errorExpected) {
339 for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
340 if (!expectedArgKinds[i].compatible(argumentKinds[i])) {
341 errorExpected = true;
342 break;
343 }
344 }
345 }
347 if (errorExpected != diagChecker.errorFound) {
348 throw new Error("invalid diagnostics for source:\n" +
349 source.getCharContent(true) +
350 "\nFound error: " + diagChecker.errorFound +
351 "\nExpected error: " + errorExpected);
352 }
353 }
355 static class DiagnosticChecker
356 implements javax.tools.DiagnosticListener<JavaFileObject> {
358 boolean errorFound;
360 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
361 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
362 errorFound = true;
363 }
364 }
365 }
367 }