test/tools/javac/api/TestJavacTask_Multiple.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 1013
8eb952f43b11
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

jjg@930 1 /*
katleman@1013 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
jjg@930 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@930 4 *
jjg@930 5 * This code is free software; you can redistribute it and/or modify it
jjg@930 6 * under the terms of the GNU General Public License version 2 only, as
jjg@930 7 * published by the Free Software Foundation.
jjg@930 8 *
jjg@930 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@930 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@930 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@930 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@930 13 * accompanied this code).
jjg@930 14 *
jjg@930 15 * You should have received a copy of the GNU General Public License version
jjg@930 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@930 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@930 18 *
jjg@930 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@930 20 * or visit www.oracle.com if you need additional information or have any
jjg@930 21 * questions.
jjg@930 22 */
jjg@930 23
jjg@930 24 /*
jjg@930 25 * @test
jjg@930 26 * @bug 7026509
jjg@930 27 * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg@930 28 */
jjg@930 29
jjg@930 30 import java.io.*;
jjg@930 31 import java.util.*;
jjg@930 32 import javax.tools.*;
jjg@930 33 import javax.tools.JavaCompiler.CompilationTask;
jjg@930 34 import com.sun.source.util.*;
jjg@930 35
jjg@930 36 public class TestJavacTask_Multiple {
jjg@930 37 public static void main(String... args) throws Exception {
jjg@930 38 new TestJavacTask_Multiple().run();
jjg@930 39 }
jjg@930 40
jjg@930 41 final int MAX_TASKS = 3;
jjg@930 42
jjg@930 43 enum TestKind {
jjg@930 44 CALL {
jjg@930 45 int test(CompilationTask t) {
jjg@930 46 boolean ok = t.call();
jjg@930 47 if (!ok)
jjg@930 48 throw new Error("compilation failed");
jjg@930 49 return 1;
jjg@930 50 }
jjg@930 51 },
jjg@930 52 PARSE {
jjg@930 53 int test(CompilationTask t) {
jjg@930 54 try {
jjg@930 55 ((JavacTask) t).parse();
jjg@930 56 return 1;
jjg@930 57 } catch (IOException ex) {
jjg@930 58 throw new Error(ex);
jjg@930 59 }
jjg@930 60 }
jjg@930 61
jjg@930 62 };
jjg@930 63 abstract int test(CompilationTask t);
jjg@930 64 }
jjg@930 65
jjg@930 66 int count;
jjg@930 67
jjg@930 68 void run() throws Exception {
jjg@930 69 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
jjg@930 70 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
jjg@930 71 for (TestKind tk: TestKind.values()) {
jjg@930 72 test(comp, fm, tk);
jjg@930 73 }
jjg@930 74
jjg@930 75 int expect = TestKind.values().length * MAX_TASKS;
jjg@930 76 if (count != expect) {
jjg@930 77 throw new Exception("Unexpected number of tests completed: " + count
jjg@930 78 + ", expected: " + expect);
jjg@930 79 }
jjg@930 80
jjg@930 81 }
jjg@930 82
jjg@930 83 void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
jjg@930 84 System.err.println("test " + tk);
jjg@930 85 File testSrc = new File(System.getProperty("test.src"));
jjg@930 86 String thisClassName = TestJavacTask_Multiple.class.getName();
jjg@930 87 Iterable<? extends JavaFileObject> files =
jjg@930 88 fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
jjg@930 89
jjg@930 90 List<CompilationTask> tasks = new ArrayList<CompilationTask>();
jjg@930 91 for (int i = 1; i <= MAX_TASKS; i++) {
jjg@930 92 File tmpDir = new File(tk + "_" + i);
jjg@930 93 tmpDir.mkdirs();
jjg@930 94 List<String> options = Arrays.asList( "-d", tmpDir.getPath() );
jjg@930 95 CompilationTask t = comp.getTask(null, fm, null, options, null, files);
jjg@930 96 ((JavacTask) t).setTaskListener(createTaskListener(tk, i));
jjg@930 97 tasks.add(t);
jjg@930 98 }
jjg@930 99
jjg@930 100 for (CompilationTask t: tasks)
jjg@930 101 count += tk.test(t);
jjg@930 102
jjg@930 103 System.err.println();
jjg@930 104 }
jjg@930 105
jjg@930 106 TaskListener createTaskListener(final TestKind tk, final int i) {
jjg@930 107 return new TaskListener() {
jjg@930 108
jjg@930 109 public void started(TaskEvent e) {
jjg@930 110 System.err.println(tk + "." + i + ": " + e + " started");
jjg@930 111 }
jjg@930 112
jjg@930 113 public void finished(TaskEvent e) {
jjg@930 114 System.err.println(tk + "." + i + ": " + e + " finished");
jjg@930 115 }
jjg@930 116 };
jjg@930 117 }
jjg@930 118 }

mercurial