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