test/tools/javac/api/TestJavacTask_Multiple.java

changeset 930
cb119107aeea
child 1013
8eb952f43b11
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestJavacTask_Multiple.java	Mon Mar 14 11:48:41 2011 -0700
     1.3 @@ -0,0 +1,118 @@
     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_Multiple {
    1.40 +    public static void main(String... args) throws Exception {
    1.41 +        new TestJavacTask_Multiple().run();
    1.42 +    }
    1.43 +
    1.44 +    final int MAX_TASKS = 3;
    1.45 +
    1.46 +    enum TestKind {
    1.47 +        CALL {
    1.48 +            int test(CompilationTask t) {
    1.49 +                boolean ok = t.call();
    1.50 +                if (!ok)
    1.51 +                    throw new Error("compilation failed");
    1.52 +                return 1;
    1.53 +            }
    1.54 +        },
    1.55 +        PARSE {
    1.56 +            int test(CompilationTask t) {
    1.57 +                try {
    1.58 +                    ((JavacTask) t).parse();
    1.59 +                return 1;
    1.60 +                } catch (IOException ex) {
    1.61 +                    throw new Error(ex);
    1.62 +                }
    1.63 +            }
    1.64 +
    1.65 +        };
    1.66 +        abstract int test(CompilationTask t);
    1.67 +    }
    1.68 +
    1.69 +    int count;
    1.70 +
    1.71 +    void run() throws Exception {
    1.72 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    1.73 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    1.74 +        for (TestKind tk: TestKind.values()) {
    1.75 +            test(comp, fm, tk);
    1.76 +        }
    1.77 +
    1.78 +        int expect = TestKind.values().length * MAX_TASKS;
    1.79 +        if (count != expect) {
    1.80 +            throw new Exception("Unexpected number of tests completed: " + count
    1.81 +                    + ", expected: " + expect);
    1.82 +        }
    1.83 +
    1.84 +    }
    1.85 +
    1.86 +    void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
    1.87 +        System.err.println("test " + tk);
    1.88 +        File testSrc = new File(System.getProperty("test.src"));
    1.89 +        String thisClassName = TestJavacTask_Multiple.class.getName();
    1.90 +        Iterable<? extends JavaFileObject> files =
    1.91 +                fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    1.92 +
    1.93 +        List<CompilationTask> tasks = new ArrayList<CompilationTask>();
    1.94 +        for (int i = 1; i <= MAX_TASKS; i++) {
    1.95 +            File tmpDir = new File(tk + "_" + i);
    1.96 +            tmpDir.mkdirs();
    1.97 +            List<String> options = Arrays.asList( "-d", tmpDir.getPath() );
    1.98 +            CompilationTask t = comp.getTask(null, fm, null, options, null, files);
    1.99 +            ((JavacTask) t).setTaskListener(createTaskListener(tk, i));
   1.100 +            tasks.add(t);
   1.101 +        }
   1.102 +
   1.103 +        for (CompilationTask t: tasks)
   1.104 +            count += tk.test(t);
   1.105 +
   1.106 +        System.err.println();
   1.107 +    }
   1.108 +
   1.109 +    TaskListener createTaskListener(final TestKind tk, final int i) {
   1.110 +        return new TaskListener() {
   1.111 +
   1.112 +            public void started(TaskEvent e) {
   1.113 +                System.err.println(tk + "." + i + ": " + e + " started");
   1.114 +            }
   1.115 +
   1.116 +            public void finished(TaskEvent e) {
   1.117 +                System.err.println(tk + "." + i + ": " + e + " finished");
   1.118 +            }
   1.119 +        };
   1.120 +    }
   1.121 +}

mercurial