TestJavacTask_Lock.java revision 2599:50b448c5be54
1/* 2 * Copyright (c) 2011, 2014, 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 */ 23 24/* 25 * @test 26 * @bug 7026509 27 * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations 28 */ 29 30import java.io.*; 31import java.util.*; 32import javax.tools.*; 33import javax.tools.JavaCompiler.CompilationTask; 34import com.sun.source.util.*; 35 36public class TestJavacTask_Lock { 37 public static void main(String... args) throws Exception { 38 new TestJavacTask_Lock().run(); 39 } 40 41 enum MethodKind { 42 CALL { 43 int test(CompilationTask t) { 44 boolean ok = t.call(); 45 if (!ok) 46 throw new Error("compilation failed"); 47 return 1; 48 } 49 }, 50 PARSE { 51 int test(CompilationTask t) { 52 try { 53 ((JavacTask) t).parse(); 54 return 1; 55 } catch (IOException ex) { 56 throw new Error(ex); 57 } 58 } 59 60 }; 61 abstract int test(CompilationTask t); 62 } 63 64 JavaCompiler comp; 65 StandardJavaFileManager fm; 66 67 void run() throws Exception { 68 comp = ToolProvider.getSystemJavaCompiler(); 69 fm = comp.getStandardFileManager(null, null, null); 70 71 for (MethodKind first: MethodKind.values()) { 72 for (MethodKind second: MethodKind.values()) { 73 test(first, second); 74 } 75 } 76 77 if (errors > 0) 78 throw new Exception(errors + " errors found"); 79 } 80 81 void test(MethodKind first, MethodKind second) { 82 System.err.println("test: " + first + ", " + second); 83 File testSrc = new File(System.getProperty("test.src")); 84 String thisClassName = TestJavacTask_Lock.class.getName(); 85 Iterable<? extends JavaFileObject> files = 86 fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java")); 87 File tmpDir = new File(first + "_" + second); 88 tmpDir.mkdirs(); 89 List<String> options = Arrays.asList( "-d", tmpDir.getPath() ); 90 CompilationTask t = comp.getTask(null, fm, null, options, null, files); 91 92 try { 93 first.test(t); 94 second.test(t); 95 error("No exception thrown"); 96 } catch (IllegalStateException e) { 97 e.printStackTrace(); 98 System.err.println("Expected exception caught: " + e); 99 } catch (Exception e) { 100 error("Unexpected exception caught: " + e); 101 e.printStackTrace(System.err); 102 } 103 104 } 105 106 void error(String msg) { 107 System.err.println("Error: " + msg); 108 errors++; 109 } 110 111 int errors; 112} 113