1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/TestJavacTask_Lock.java Mon Mar 14 11:48:41 2011 -0700 1.3 @@ -0,0 +1,111 @@ 1.4 +/* 1.5 + * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7026509 1.30 + * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations 1.31 + */ 1.32 + 1.33 +import java.io.*; 1.34 +import java.util.*; 1.35 +import javax.tools.*; 1.36 +import javax.tools.JavaCompiler.CompilationTask; 1.37 +import com.sun.source.util.*; 1.38 + 1.39 +public class TestJavacTask_Lock { 1.40 + public static void main(String... args) throws Exception { 1.41 + new TestJavacTask_Lock().run(); 1.42 + } 1.43 + 1.44 + enum MethodKind { 1.45 + CALL { 1.46 + int test(CompilationTask t) { 1.47 + boolean ok = t.call(); 1.48 + if (!ok) 1.49 + throw new Error("compilation failed"); 1.50 + return 1; 1.51 + } 1.52 + }, 1.53 + PARSE { 1.54 + int test(CompilationTask t) { 1.55 + try { 1.56 + ((JavacTask) t).parse(); 1.57 + return 1; 1.58 + } catch (IOException ex) { 1.59 + throw new Error(ex); 1.60 + } 1.61 + } 1.62 + 1.63 + }; 1.64 + abstract int test(CompilationTask t); 1.65 + } 1.66 + 1.67 + JavaCompiler comp; 1.68 + StandardJavaFileManager fm; 1.69 + 1.70 + void run() throws Exception { 1.71 + comp = ToolProvider.getSystemJavaCompiler(); 1.72 + fm = comp.getStandardFileManager(null, null, null); 1.73 + 1.74 + for (MethodKind first: MethodKind.values()) { 1.75 + for (MethodKind second: MethodKind.values()) { 1.76 + test(first, second); 1.77 + } 1.78 + } 1.79 + 1.80 + if (errors > 0) 1.81 + throw new Exception(errors + " errors found"); 1.82 + } 1.83 + 1.84 + void test(MethodKind first, MethodKind second) { 1.85 + System.err.println("test: " + first + ", " + second); 1.86 + File testSrc = new File(System.getProperty("test.src")); 1.87 + String thisClassName = TestJavacTask_Lock.class.getName(); 1.88 + Iterable<? extends JavaFileObject> files = 1.89 + fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java")); 1.90 + File tmpDir = new File(first + "_" + second); 1.91 + tmpDir.mkdirs(); 1.92 + List<String> options = Arrays.asList( "-d", tmpDir.getPath() ); 1.93 + CompilationTask t = comp.getTask(null, fm, null, options, null, files); 1.94 + 1.95 + try { 1.96 + first.test(t); 1.97 + second.test(t); 1.98 + error("No exception thrown"); 1.99 + } catch (IllegalStateException e) { 1.100 + System.err.println("Expected exception caught: " + e); 1.101 + } catch (Exception e) { 1.102 + error("Unexpected exception caught: " + e); 1.103 + e.printStackTrace(System.err); 1.104 + } 1.105 + 1.106 + } 1.107 + 1.108 + void error(String msg) { 1.109 + System.err.println("Error: " + msg); 1.110 + errors++; 1.111 + } 1.112 + 1.113 + int errors; 1.114 +}