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