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