test/tools/javac/lambda/FunctionalInterfaceConversionTest.java

changeset 1482
954541f13717
parent 1434
34d1ebaf4645
child 1520
5c956be64b9e
equal deleted inserted replaced
1481:d07340b61e6a 1482:954541f13717
1 /* 1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
25 * @test 25 * @test
26 * @bug 8003280 8004102 26 * @bug 8003280 8004102
27 * @summary Add lambda tests 27 * @summary Add lambda tests
28 * perform several automated checks in lambda conversion, esp. around accessibility 28 * perform several automated checks in lambda conversion, esp. around accessibility
29 * @author Maurizio Cimadamore 29 * @author Maurizio Cimadamore
30 * @library ../lib
31 * @build JavacTestingAbstractThreadedTest
30 * @run main FunctionalInterfaceConversionTest 32 * @run main FunctionalInterfaceConversionTest
31 */ 33 */
32 34
33 import com.sun.source.util.JavacTask; 35 import java.io.IOException;
34 import java.net.URI; 36 import java.net.URI;
35 import java.util.Arrays; 37 import java.util.Arrays;
36 import javax.tools.Diagnostic; 38 import javax.tools.Diagnostic;
37 import javax.tools.JavaCompiler; 39 import javax.tools.JavaCompiler;
38 import javax.tools.JavaFileObject; 40 import javax.tools.JavaFileObject;
39 import javax.tools.SimpleJavaFileObject; 41 import javax.tools.SimpleJavaFileObject;
40 import javax.tools.StandardJavaFileManager;
41 import javax.tools.ToolProvider; 42 import javax.tools.ToolProvider;
42 43 import com.sun.source.util.JavacTask;
43 public class FunctionalInterfaceConversionTest { 44
45 public class FunctionalInterfaceConversionTest
46 extends JavacTestingAbstractThreadedTest
47 implements Runnable {
44 48
45 enum PackageKind { 49 enum PackageKind {
46 NO_PKG(""), 50 NO_PKG(""),
47 PKG_A("a"); 51 PKG_A("a");
48 52
137 replaceAll("#T", thrownType.typeStr); 141 replaceAll("#T", thrownType.typeStr);
138 } 142 }
139 } 143 }
140 144
141 public static void main(String[] args) throws Exception { 145 public static void main(String[] args) throws Exception {
142 final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
143 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
144 for (PackageKind samPkg : PackageKind.values()) { 146 for (PackageKind samPkg : PackageKind.values()) {
145 for (ModifierKind modKind : ModifierKind.values()) { 147 for (ModifierKind modKind : ModifierKind.values()) {
146 for (SamKind samKind : SamKind.values()) { 148 for (SamKind samKind : SamKind.values()) {
147 for (MethodKind samMeth : MethodKind.values()) { 149 for (MethodKind samMeth : MethodKind.values()) {
148 for (MethodKind clientMeth : MethodKind.values()) { 150 for (MethodKind clientMeth : MethodKind.values()) {
149 for (TypeKind retType : TypeKind.values()) { 151 for (TypeKind retType : TypeKind.values()) {
150 for (TypeKind argType : TypeKind.values()) { 152 for (TypeKind argType : TypeKind.values()) {
151 for (TypeKind thrownType : TypeKind.values()) { 153 for (TypeKind thrownType : TypeKind.values()) {
152 for (ExprKind exprKind : ExprKind.values()) { 154 for (ExprKind exprKind : ExprKind.values()) {
153 new FunctionalInterfaceConversionTest(samPkg, modKind, samKind, 155 pool.execute(
154 samMeth, clientMeth, retType, argType, thrownType, exprKind).test(comp, fm); 156 new FunctionalInterfaceConversionTest(
157 samPkg, modKind, samKind,
158 samMeth, clientMeth, retType,
159 argType, thrownType, exprKind));
155 } 160 }
156 } 161 }
157 } 162 }
158 } 163 }
159 } 164 }
160 } 165 }
161 } 166 }
162 } 167 }
163 } 168 }
169
170 checkAfterExec(false);
164 } 171 }
165 172
166 PackageKind samPkg; 173 PackageKind samPkg;
167 ModifierKind modKind; 174 ModifierKind modKind;
168 SamKind samKind; 175 SamKind samKind;
173 TypeKind thrownType; 180 TypeKind thrownType;
174 ExprKind exprKind; 181 ExprKind exprKind;
175 DiagnosticChecker dc; 182 DiagnosticChecker dc;
176 183
177 SourceFile samSourceFile = new SourceFile("Sam.java", "#P \n #C") { 184 SourceFile samSourceFile = new SourceFile("Sam.java", "#P \n #C") {
185 @Override
178 public String toString() { 186 public String toString() {
179 return template.replaceAll("#P", samPkg.getPkgDecl()). 187 return template.replaceAll("#P", samPkg.getPkgDecl()).
180 replaceAll("#C", samKind.getSam(samMeth.getMethod(retType, argType, thrownType))); 188 replaceAll("#C", samKind.getSam(
189 samMeth.getMethod(retType, argType, thrownType)));
181 } 190 }
182 }; 191 };
183 192
184 SourceFile pkgClassSourceFile = new SourceFile("PackageClass.java", 193 SourceFile pkgClassSourceFile =
185 "#P\n #M class PackageClass extends Exception { }") { 194 new SourceFile("PackageClass.java",
195 "#P\n #M class PackageClass extends Exception { }") {
196 @Override
186 public String toString() { 197 public String toString() {
187 return template.replaceAll("#P", samPkg.getPkgDecl()). 198 return template.replaceAll("#P", samPkg.getPkgDecl()).
188 replaceAll("#M", modKind.modifier_str); 199 replaceAll("#M", modKind.modifier_str);
189 } 200 }
190 }; 201 };
191 202
192 SourceFile clientSourceFile = new SourceFile("Client.java", 203 SourceFile clientSourceFile =
193 "#I\n abstract class Client { \n" + 204 new SourceFile("Client.java",
194 " Sam s = #E;\n" + 205 "#I\n abstract class Client { \n" +
195 " #M \n }") { 206 " Sam s = #E;\n" +
207 " #M \n }") {
208 @Override
196 public String toString() { 209 public String toString() {
197 return template.replaceAll("#I", samPkg.getImportStat()) 210 return template.replaceAll("#I", samPkg.getImportStat())
198 .replaceAll("#E", exprKind.exprStr) 211 .replaceAll("#E", exprKind.exprStr)
199 .replaceAll("#M", clientMeth.getMethod(retType, argType, thrownType)); 212 .replaceAll("#M", clientMeth.getMethod(retType, argType, thrownType));
200 } 213 }
201 }; 214 };
202 215
203 FunctionalInterfaceConversionTest(PackageKind samPkg, ModifierKind modKind, SamKind samKind, 216 FunctionalInterfaceConversionTest(PackageKind samPkg, ModifierKind modKind,
204 MethodKind samMeth, MethodKind clientMeth, TypeKind retType, TypeKind argType, 217 SamKind samKind, MethodKind samMeth, MethodKind clientMeth,
205 TypeKind thrownType, ExprKind exprKind) { 218 TypeKind retType, TypeKind argType, TypeKind thrownType,
219 ExprKind exprKind) {
206 this.samPkg = samPkg; 220 this.samPkg = samPkg;
207 this.modKind = modKind; 221 this.modKind = modKind;
208 this.samKind = samKind; 222 this.samKind = samKind;
209 this.samMeth = samMeth; 223 this.samMeth = samMeth;
210 this.clientMeth = clientMeth; 224 this.clientMeth = clientMeth;
213 this.thrownType = thrownType; 227 this.thrownType = thrownType;
214 this.exprKind = exprKind; 228 this.exprKind = exprKind;
215 this.dc = new DiagnosticChecker(); 229 this.dc = new DiagnosticChecker();
216 } 230 }
217 231
218 void test(JavaCompiler comp, StandardJavaFileManager fm) throws Exception { 232 @Override
219 JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, 233 public void run() {
220 null, null, Arrays.asList(samSourceFile, pkgClassSourceFile, clientSourceFile)); 234 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
221 ct.analyze(); 235
236 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), dc, null, null,
237 Arrays.asList(samSourceFile, pkgClassSourceFile, clientSourceFile));
238 try {
239 ct.analyze();
240 } catch (IOException ex) {
241 throw new AssertionError("Test failing with cause", ex.getCause());
242 }
222 if (dc.errorFound == checkSamConversion()) { 243 if (dc.errorFound == checkSamConversion()) {
223 throw new AssertionError(samSourceFile + "\n\n" + pkgClassSourceFile + "\n\n" + clientSourceFile); 244 throw new AssertionError(samSourceFile + "\n\n" +
245 pkgClassSourceFile + "\n\n" + clientSourceFile);
224 } 246 }
225 } 247 }
226 248
227 boolean checkSamConversion() { 249 boolean checkSamConversion() {
228 if (samKind != SamKind.INTERFACE) { 250 if (samKind != SamKind.INTERFACE) {
262 @Override 284 @Override
263 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 285 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
264 return toString(); 286 return toString();
265 } 287 }
266 288
289 @Override
267 public abstract String toString(); 290 public abstract String toString();
268 } 291 }
269 292
270 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { 293 static class DiagnosticChecker
294 implements javax.tools.DiagnosticListener<JavaFileObject> {
271 295
272 boolean errorFound = false; 296 boolean errorFound = false;
273 297
298 @Override
274 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 299 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
275 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { 300 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
276 errorFound = true; 301 errorFound = true;
277 } 302 }
278 } 303 }

mercurial