|
1 /* |
|
2 * Copyright (c) 2012 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package org.openjdk.tests.vm; |
|
27 |
|
28 import java.util.*; |
|
29 |
|
30 import org.testng.ITestResult; |
|
31 import org.testng.annotations.Test; |
|
32 import org.testng.annotations.DataProvider; |
|
33 import org.testng.annotations.AfterMethod; |
|
34 import org.testng.annotations.AfterSuite; |
|
35 |
|
36 import org.openjdk.tests.separate.*; |
|
37 import org.openjdk.tests.separate.Compiler; |
|
38 |
|
39 import org.openjdk.tests.shapegen.Hierarchy; |
|
40 import org.openjdk.tests.shapegen.HierarchyGenerator; |
|
41 import org.openjdk.tests.shapegen.ClassCase; |
|
42 |
|
43 import static org.testng.Assert.*; |
|
44 import static org.openjdk.tests.separate.SourceModel.*; |
|
45 import static org.openjdk.tests.separate.SourceModel.Class; |
|
46 import static org.openjdk.tests.separate.SourceModel.Method; |
|
47 import static org.openjdk.tests.separate.SourceModel.Type; |
|
48 |
|
49 public class FDSeparateCompilationTest extends TestHarness { |
|
50 |
|
51 private static String EMPTY = "\"\""; |
|
52 |
|
53 public FDSeparateCompilationTest() { |
|
54 super(false, true); |
|
55 } |
|
56 |
|
57 @DataProvider(name = "allShapes", parallel = true) |
|
58 public Object[][] hierarchyGenerator() { |
|
59 ArrayList<Object[]> allCases = new ArrayList<>(); |
|
60 |
|
61 HierarchyGenerator hg = new HierarchyGenerator(); |
|
62 for (Object x : hg.getOK()) { |
|
63 allCases.add(new Object[]{x}); |
|
64 } |
|
65 for (Object x : hg.getErr()) { |
|
66 allCases.add(new Object[]{x}); |
|
67 } |
|
68 return allCases.toArray(new Object[0][]); |
|
69 } |
|
70 |
|
71 // The expected value obtained when invoking the method from the specified |
|
72 // class. If returns null, then an AbstractMethodError is expected. |
|
73 private static String getExpectedResult(ClassCase cc) { |
|
74 Set<ClassCase> provs = cc.get_mprov(); |
|
75 if (cc.get_mres() != null) { |
|
76 return cc.get_mres().getName(); |
|
77 } else if (provs != null && provs.size() == 1) { |
|
78 ClassCase cand = provs.iterator().next(); |
|
79 switch (cand.kind) { |
|
80 case CCONCRETE: |
|
81 case IDEFAULT: |
|
82 return cand.getName(); |
|
83 case CNONE: |
|
84 case IVAC: |
|
85 return getExpectedResult(cand); |
|
86 } |
|
87 } |
|
88 return null; |
|
89 } |
|
90 |
|
91 private static final ConcreteMethod canonicalMethod = new ConcreteMethod( |
|
92 "String", "m", "returns " + EMPTY + ";", AccessFlag.PUBLIC); |
|
93 |
|
94 @Test(groups = "vm", dataProvider = "allShapes") |
|
95 public void separateCompilationTest(Hierarchy hs) { |
|
96 ClassCase cc = hs.root; |
|
97 Type type = sourceTypeFrom(hs.root); |
|
98 |
|
99 Class specimen = null; |
|
100 if (type instanceof Class) { |
|
101 Class ctype = (Class)type; |
|
102 if (ctype.isAbstract()) { |
|
103 specimen = new Class("Test" + ctype.getName(), ctype); |
|
104 } else { |
|
105 specimen = ctype; |
|
106 } |
|
107 } else { |
|
108 specimen = new Class("Test" + type.getName(), (Interface)type); |
|
109 } |
|
110 |
|
111 String value = getExpectedResult(cc); |
|
112 if (value != null) { |
|
113 assertInvokeVirtualEquals(value, specimen, canonicalMethod, EMPTY); |
|
114 } else { |
|
115 assertThrows(AbstractMethodError.class, specimen, |
|
116 canonicalMethod, EMPTY); |
|
117 } |
|
118 } |
|
119 |
|
120 @AfterMethod |
|
121 public void printCaseError(ITestResult result) { |
|
122 if (result.getStatus() == ITestResult.FAILURE) { |
|
123 Hierarchy hs = (Hierarchy)result.getParameters()[0]; |
|
124 System.out.println("Separate compilation case " + hs); |
|
125 printCaseDetails(hs); |
|
126 } |
|
127 } |
|
128 |
|
129 @AfterSuite |
|
130 public void cleanupCompilerCache() { |
|
131 Compiler.purgeCache(); |
|
132 } |
|
133 |
|
134 private void printCaseDetails(Hierarchy hs) { |
|
135 String exp = getExpectedResult(hs.root); |
|
136 for (String s : hs.getDescription()) { |
|
137 System.out.println(" " + s); |
|
138 } |
|
139 if (exp != null) { |
|
140 System.out.println(" Expected \"" + exp + "\""); |
|
141 } else { |
|
142 System.out.println(" Expected AbstractMethodError"); |
|
143 } |
|
144 } |
|
145 |
|
146 private Type sourceTypeFrom(ClassCase cc) { |
|
147 Type type = null; |
|
148 |
|
149 if (cc.isInterface()) { |
|
150 Interface iface = new Interface(cc.getName()); |
|
151 for (ClassCase scc : cc.getInterfaces()) { |
|
152 Interface supertype = (Interface)sourceTypeFrom(scc); |
|
153 iface.addSuperType(supertype); |
|
154 } |
|
155 type = iface; |
|
156 } else { |
|
157 Class cls = new Class(cc.getName()); |
|
158 if (cc.hasSuperclass()) { |
|
159 Class superc = (Class)sourceTypeFrom(cc.getSuperclass()); |
|
160 cls.setSuperClass(superc); |
|
161 } |
|
162 for (ClassCase scc : cc.getInterfaces()) { |
|
163 Interface supertype = (Interface)sourceTypeFrom(scc); |
|
164 cls.addSuperType(supertype); |
|
165 } |
|
166 if (cc.isAbstract()) { |
|
167 cls.getAccessFlags().add(AccessFlag.ABSTRACT); |
|
168 } |
|
169 type = cls; |
|
170 } |
|
171 Method method = methodFrom(cc); |
|
172 if (method != null) { |
|
173 type.addMethod(method); |
|
174 } |
|
175 return type; |
|
176 } |
|
177 |
|
178 private Method methodFrom(ClassCase cc) { |
|
179 switch (cc.kind) { |
|
180 case IVAC: |
|
181 case CNONE: return null; |
|
182 case IPRESENT: |
|
183 case CABSTRACT: |
|
184 return new AbstractMethod("String", "m", AccessFlag.PUBLIC); |
|
185 case IDEFAULT: |
|
186 return new DefaultMethod( |
|
187 "String", "m", "return \"" + cc.getName() + "\";"); |
|
188 case CCONCRETE: |
|
189 return new ConcreteMethod( |
|
190 "String", "m", "return \"" + cc.getName() + "\";", |
|
191 AccessFlag.PUBLIC); |
|
192 default: |
|
193 fail("Unknown method type in class"); |
|
194 return null; |
|
195 } |
|
196 } |
|
197 } |