929N/A/*
1011N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
929N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
929N/A *
929N/A * This code is free software; you can redistribute it and/or modify it
929N/A * under the terms of the GNU General Public License version 2 only, as
929N/A * published by the Free Software Foundation.
929N/A *
929N/A * This code is distributed in the hope that it will be useful, but WITHOUT
929N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
929N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
929N/A * version 2 for more details (a copy is included in the LICENSE file that
929N/A * accompanied this code).
929N/A *
929N/A * You should have received a copy of the GNU General Public License version
929N/A * 2 along with this work; if not, write to the Free Software Foundation,
929N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
929N/A *
929N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
929N/A * or visit www.oracle.com if you need additional information or have any
929N/A * questions.
929N/A */
929N/A
929N/A/*
929N/A * @test
929N/A * @bug 7026509
929N/A * @summary Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
929N/A */
929N/A
929N/Aimport java.io.*;
929N/Aimport java.util.*;
929N/Aimport javax.tools.*;
929N/Aimport javax.tools.JavaCompiler.CompilationTask;
929N/Aimport com.sun.source.util.*;
929N/A
929N/Apublic class TestJavacTask_Multiple {
929N/A public static void main(String... args) throws Exception {
929N/A new TestJavacTask_Multiple().run();
929N/A }
929N/A
929N/A final int MAX_TASKS = 3;
929N/A
929N/A enum TestKind {
929N/A CALL {
929N/A int test(CompilationTask t) {
929N/A boolean ok = t.call();
929N/A if (!ok)
929N/A throw new Error("compilation failed");
929N/A return 1;
929N/A }
929N/A },
929N/A PARSE {
929N/A int test(CompilationTask t) {
929N/A try {
929N/A ((JavacTask) t).parse();
929N/A return 1;
929N/A } catch (IOException ex) {
929N/A throw new Error(ex);
929N/A }
929N/A }
929N/A
929N/A };
929N/A abstract int test(CompilationTask t);
929N/A }
929N/A
929N/A int count;
929N/A
929N/A void run() throws Exception {
929N/A JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
929N/A StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
929N/A for (TestKind tk: TestKind.values()) {
929N/A test(comp, fm, tk);
929N/A }
929N/A
929N/A int expect = TestKind.values().length * MAX_TASKS;
929N/A if (count != expect) {
929N/A throw new Exception("Unexpected number of tests completed: " + count
929N/A + ", expected: " + expect);
929N/A }
929N/A
929N/A }
929N/A
929N/A void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
929N/A System.err.println("test " + tk);
929N/A File testSrc = new File(System.getProperty("test.src"));
929N/A String thisClassName = TestJavacTask_Multiple.class.getName();
929N/A Iterable<? extends JavaFileObject> files =
929N/A fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
929N/A
929N/A List<CompilationTask> tasks = new ArrayList<CompilationTask>();
929N/A for (int i = 1; i <= MAX_TASKS; i++) {
929N/A File tmpDir = new File(tk + "_" + i);
929N/A tmpDir.mkdirs();
929N/A List<String> options = Arrays.asList( "-d", tmpDir.getPath() );
929N/A CompilationTask t = comp.getTask(null, fm, null, options, null, files);
929N/A ((JavacTask) t).setTaskListener(createTaskListener(tk, i));
929N/A tasks.add(t);
929N/A }
929N/A
929N/A for (CompilationTask t: tasks)
929N/A count += tk.test(t);
929N/A
929N/A System.err.println();
929N/A }
929N/A
929N/A TaskListener createTaskListener(final TestKind tk, final int i) {
929N/A return new TaskListener() {
929N/A
929N/A public void started(TaskEvent e) {
929N/A System.err.println(tk + "." + i + ": " + e + " started");
929N/A }
929N/A
929N/A public void finished(TaskEvent e) {
929N/A System.err.println(tk + "." + i + ": " + e + " finished");
929N/A }
929N/A };
929N/A }
929N/A}