PluginsInModulesTest.java revision 3573:c4a18ee691c4
1/*
2 * Copyright (c) 2015, 2016, 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 * @summary Verify that plugins inside modules works
27 * @library /tools/lib
28 * @modules
29 *      jdk.compiler/com.sun.tools.javac.api
30 *      jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
32 * @run main PluginsInModulesTest
33 */
34
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.util.Arrays;
38import java.util.List;
39
40import toolbox.JavacTask;
41import toolbox.Task;
42import toolbox.ToolBox;
43
44public class PluginsInModulesTest extends ModuleTestBase {
45
46    public static void main(String... args) throws Exception {
47        new PluginsInModulesTest().runTests();
48    }
49
50    private static final String pluginModule1 =
51            "module pluginMod1 {\n" +
52            "    requires jdk.compiler;\n" +
53            "\n" +
54            "    provides com.sun.source.util.Plugin\n" +
55            "      with mypkg1.SimplePlugin1;\n" +
56            "}";
57
58    private static final String plugin1 =
59            "package mypkg1;\n" +
60            "import com.sun.source.util.JavacTask;\n" +
61            "import com.sun.source.util.Plugin;\n" +
62            "import com.sun.source.util.TaskEvent;\n" +
63            "import com.sun.source.util.TaskListener;\n" +
64            "\n" +
65            "public class SimplePlugin1 implements Plugin {\n" +
66            "\n" +
67            "    @Override\n" +
68            "    public String getName() {\n" +
69            "        return \"simpleplugin1\";\n" +
70            "    }\n" +
71            "\n" +
72            "    @Override\n" +
73            "    public void init(JavacTask task, String... args) {\n" +
74            "        task.addTaskListener(new PostAnalyzeTaskListener());\n" +
75            "    }\n" +
76            "\n" +
77            "    private static class PostAnalyzeTaskListener implements TaskListener {\n" +
78            "        @Override\n" +
79            "        public void started(TaskEvent taskEvent) { \n" +
80            "            if (taskEvent.getKind().equals(TaskEvent.Kind.COMPILATION)) {\n" +
81            "                System.out.println(\"simpleplugin1 started for event \" + taskEvent.getKind());\n" +
82            "            }\n" +
83            "        }\n" +
84            "\n" +
85            "        @Override\n" +
86            "        public void finished(TaskEvent taskEvent) {\n" +
87            "            if (taskEvent.getKind().equals(TaskEvent.Kind.COMPILATION)) {\n" +
88            "                System.out.println(\"simpleplugin1 finished for event \" + taskEvent.getKind());\n" +
89            "            }\n" +
90            "        }\n" +
91            "    }\n" +
92            "}";
93
94    private static final String testClass = "class Test { }";
95
96    void initialization(Path base) throws Exception {
97        moduleSrc = base.resolve("plugin_mods_src");
98        Path pluginMod1 = moduleSrc.resolve("pluginMod1");
99
100        processorCompiledModules = base.resolve("mods");
101
102        Files.createDirectories(processorCompiledModules);
103
104        tb.writeJavaFiles(
105                pluginMod1,
106                pluginModule1,
107                plugin1);
108
109        String log = new JavacTask(tb)
110                .options("--module-source-path", moduleSrc.toString())
111                .outdir(processorCompiledModules)
112                .files(findJavaFiles(moduleSrc))
113                .run()
114                .writeAll()
115                .getOutput(Task.OutputKind.DIRECT);
116
117        if (!log.isEmpty()) {
118            throw new AssertionError("Unexpected output: " + log);
119        }
120
121        classes = base.resolve("classes");
122        Files.createDirectories(classes);
123    }
124
125    Path processorCompiledModules;
126    Path moduleSrc;
127    Path classes;
128
129    @Test
130    public void testUseOnlyOneProcessor(Path base) throws Exception {
131        initialization(base);
132        List<String> log = new JavacTask(tb)
133                .options("--processor-module-path", processorCompiledModules.toString(),
134                        "-Xplugin:simpleplugin1")
135                .outdir(classes)
136                .sources(testClass)
137                .run()
138                .writeAll()
139                .getOutputLines(Task.OutputKind.STDOUT);
140        if (!log.equals(Arrays.asList("simpleplugin1 started for event COMPILATION",
141                                      "simpleplugin1 finished for event COMPILATION"))) {
142            throw new AssertionError("Unexpected output: " + log);
143        }
144    }
145}
146