AnnotationProcessorsInModulesTest.java revision 3294:9adfb22ff08f
1321936Shselasky/*
2321936Shselasky * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3321936Shselasky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4321936Shselasky *
5321936Shselasky * This code is free software; you can redistribute it and/or modify it
6321936Shselasky * under the terms of the GNU General Public License version 2 only, as
7321936Shselasky * published by the Free Software Foundation.
8321936Shselasky *
9321936Shselasky * This code is distributed in the hope that it will be useful, but WITHOUT
10321936Shselasky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11321936Shselasky * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12321936Shselasky * version 2 for more details (a copy is included in the LICENSE file that
13321936Shselasky * accompanied this code).
14321936Shselasky *
15321936Shselasky * You should have received a copy of the GNU General Public License version
16321936Shselasky * 2 along with this work; if not, write to the Free Software Foundation,
17321936Shselasky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18321936Shselasky *
19321936Shselasky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20321936Shselasky * or visit www.oracle.com if you need additional information or have any
21321936Shselasky * questions.
22321936Shselasky */
23321936Shselasky
24321936Shselasky/**
25321936Shselasky * @test
26321936Shselasky * @summary Verify that annotation processors inside modules works
27321936Shselasky * @library /tools/lib
28321936Shselasky * @modules
29321936Shselasky *      jdk.compiler/com.sun.tools.javac.api
30321936Shselasky *      jdk.compiler/com.sun.tools.javac.main
31321936Shselasky *      jdk.jdeps/com.sun.tools.javap
32321936Shselasky * @build ToolBox ModuleTestBase
33321936Shselasky * @run main AnnotationProcessorsInModulesTest
34321936Shselasky */
35321936Shselasky
36321936Shselaskyimport java.nio.file.Files;
37321936Shselaskyimport java.nio.file.Path;
38321936Shselaskyimport java.util.Arrays;
39321936Shselaskyimport java.util.List;
40321936Shselasky
41321936Shselaskypublic class AnnotationProcessorsInModulesTest extends ModuleTestBase {
42321936Shselasky
43321936Shselasky    public static void main(String... args) throws Exception {
44321936Shselasky        new AnnotationProcessorsInModulesTest().runTests();
45321936Shselasky    }
46321936Shselasky
47321936Shselasky    private static final String annotationProcessorModule1 =
48321936Shselasky            "module anno_proc1 {\n" +
49321936Shselasky            "    requires java.compiler;\n" +
50321936Shselasky            "\n" +
51321936Shselasky            "    provides javax.annotation.processing.Processor\n" +
52321936Shselasky            "      with mypkg1.MyProcessor1;\n" +
53321936Shselasky            "}";
54321936Shselasky
55321936Shselasky    private static final String annotationProcessorModule2 =
56321936Shselasky            "module anno_proc2 {\n" +
57321936Shselasky            "    requires java.compiler;\n" +
58321936Shselasky            "\n" +
59321936Shselasky            "    provides javax.annotation.processing.Processor\n" +
60321936Shselasky            "      with mypkg2.MyProcessor2;\n" +
61321936Shselasky            "}";
62321936Shselasky
63321936Shselasky    private static final String annotationProcessor1 =
64321936Shselasky            "package mypkg1;\n" +
65321936Shselasky            "\n" +
66321936Shselasky            "import javax.annotation.processing.AbstractProcessor;\n" +
67321936Shselasky            "import javax.annotation.processing.RoundEnvironment;\n" +
68321936Shselasky            "import javax.annotation.processing.SupportedAnnotationTypes;\n" +
69321936Shselasky            "import javax.lang.model.SourceVersion;\n" +
70321936Shselasky            "import javax.lang.model.element.*;\n" +
71321936Shselasky            "\n" +
72321936Shselasky            "import java.util.*;\n" +
73321936Shselasky            "\n" +
74321936Shselasky            "@SupportedAnnotationTypes(\"*\")\n" +
75321936Shselasky            "public final class MyProcessor1 extends AbstractProcessor {\n" +
76321936Shselasky            "\n" +
77321936Shselasky            "    @Override\n" +
78321936Shselasky            "    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
79321936Shselasky            "        return false;\n" +
80321936Shselasky            "    }\n" +
81321936Shselasky            "\n" +
82321936Shselasky            "    @Override\n" +
83321936Shselasky            "    public SourceVersion getSupportedSourceVersion() {\n" +
84321936Shselasky            "        System.out.println(\"the annotation processor 1 is working!\");\n" +
85321936Shselasky            "        return SourceVersion.latest();\n" +
86321936Shselasky            "    }\n" +
87321936Shselasky            "}";
88321936Shselasky
89321936Shselasky    private static final String annotationProcessor2 =
90321936Shselasky            "package mypkg2;\n" +
91321936Shselasky            "\n" +
92321936Shselasky            "import javax.annotation.processing.AbstractProcessor;\n" +
93321936Shselasky            "import javax.annotation.processing.RoundEnvironment;\n" +
94321936Shselasky            "import javax.annotation.processing.SupportedAnnotationTypes;\n" +
95321936Shselasky            "import javax.lang.model.SourceVersion;\n" +
96321936Shselasky            "import javax.lang.model.element.*;\n" +
97321936Shselasky            "\n" +
98321936Shselasky            "import java.util.*;\n" +
99321936Shselasky            "\n" +
100321936Shselasky            "@SupportedAnnotationTypes(\"*\")\n" +
101321936Shselasky            "public final class MyProcessor2 extends AbstractProcessor {\n" +
102321936Shselasky            "\n" +
103321936Shselasky            "    @Override\n" +
104321936Shselasky            "    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
105321936Shselasky            "        return false;\n" +
106321936Shselasky            "    }\n" +
107321936Shselasky            "\n" +
108321936Shselasky            "    @Override\n" +
109321936Shselasky            "    public SourceVersion getSupportedSourceVersion() {\n" +
110321936Shselasky            "        System.out.println(\"the annotation processor 2 is working!\");\n" +
111321936Shselasky            "        return SourceVersion.latest();\n" +
112321936Shselasky            "    }\n" +
113321936Shselasky            "}";
114321936Shselasky
115321936Shselasky    private static final String testClass = "class Test{}";
116321936Shselasky
117321936Shselasky    void initialization(Path base) throws Exception {
118321936Shselasky        moduleSrc = base.resolve("anno_proc_src");
119321936Shselasky        Path anno_proc1 = moduleSrc.resolve("anno_proc1");
120321936Shselasky        Path anno_proc2 = moduleSrc.resolve("anno_proc2");
121321936Shselasky
122321936Shselasky        processorCompiledModules = base.resolve("mods");
123321936Shselasky
124321936Shselasky        Files.createDirectories(processorCompiledModules);
125321936Shselasky
126321936Shselasky        tb.writeJavaFiles(
127321936Shselasky                anno_proc1,
128321936Shselasky                annotationProcessorModule1,
129321936Shselasky                annotationProcessor1);
130321936Shselasky
131321936Shselasky        tb.writeJavaFiles(
132321936Shselasky                anno_proc2,
133321936Shselasky                annotationProcessorModule2,
134321936Shselasky                annotationProcessor2);
135321936Shselasky
136321936Shselasky        String log = tb.new JavacTask()
137321936Shselasky                .options("-modulesourcepath", moduleSrc.toString())
138321936Shselasky                .outdir(processorCompiledModules)
139321936Shselasky                .files(findJavaFiles(moduleSrc))
140321936Shselasky                .run()
141321936Shselasky                .writeAll()
142321936Shselasky                .getOutput(ToolBox.OutputKind.DIRECT);
143321936Shselasky
144321936Shselasky        if (!log.isEmpty()) {
145321936Shselasky            throw new AssertionError("Unexpected output: " + log);
146321936Shselasky        }
147321936Shselasky
148321936Shselasky        classes = base.resolve("classes");
149321936Shselasky        Files.createDirectories(classes);
150321936Shselasky    }
151321936Shselasky
152321936Shselasky    Path processorCompiledModules;
153321936Shselasky    Path moduleSrc;
154321936Shselasky    Path classes;
155321936Shselasky
156321936Shselasky    @Test
157321936Shselasky    void testUseOnlyOneProcessor(Path base) throws Exception {
158321936Shselasky        initialization(base);
159321936Shselasky        String log = tb.new JavacTask()
160321936Shselasky                .options("-processormodulepath", processorCompiledModules.toString(),
161321936Shselasky                        "-processor", "mypkg2.MyProcessor2")
162321936Shselasky                .outdir(classes)
163321936Shselasky                .sources(testClass)
164321936Shselasky                .run()
165321936Shselasky                .writeAll()
166321936Shselasky                .getOutput(ToolBox.OutputKind.STDOUT);
167321936Shselasky        if (!log.trim().equals("the annotation processor 2 is working!")) {
168321936Shselasky            throw new AssertionError("Unexpected output: " + log);
169321936Shselasky        }
170321936Shselasky    }
171321936Shselasky
172321936Shselasky    @Test
173321936Shselasky    void testAnnotationProcessorExecutionOrder(Path base) throws Exception {
174321936Shselasky        initialization(base);
175321936Shselasky        List<String> log = tb.new JavacTask()
176321936Shselasky                .options("-processormodulepath", processorCompiledModules.toString(),
177321936Shselasky                        "-processor", "mypkg1.MyProcessor1,mypkg2.MyProcessor2")
178321936Shselasky                .outdir(classes)
179321936Shselasky                .sources(testClass)
180321936Shselasky                .run()
181321936Shselasky                .writeAll()
182321936Shselasky                .getOutputLines(ToolBox.OutputKind.STDOUT);
183321936Shselasky        if (!log.equals(Arrays.asList("the annotation processor 1 is working!",
184321936Shselasky                                      "the annotation processor 2 is working!"))) {
185321936Shselasky            throw new AssertionError("Unexpected output: " + log);
186321936Shselasky        }
187321936Shselasky
188321936Shselasky        log = tb.new JavacTask()
189321936Shselasky                .options("-processormodulepath", processorCompiledModules.toString(),
190321936Shselasky                        "-processor", "mypkg2.MyProcessor2,mypkg1.MyProcessor1")
191321936Shselasky                .outdir(classes)
192321936Shselasky                .sources(testClass)
193321936Shselasky                .run()
194321936Shselasky                .writeAll()
195321936Shselasky                .getOutputLines(ToolBox.OutputKind.STDOUT);
196321936Shselasky        if (!log.equals(Arrays.asList("the annotation processor 2 is working!",
197321936Shselasky                                      "the annotation processor 1 is working!"))) {
198321936Shselasky            throw new AssertionError("Unexpected output: " + log);
199321936Shselasky        }
200321936Shselasky    }
201321936Shselasky
202321936Shselasky    @Test
203321936Shselasky    void testErrorOutputIfOneProcessorNameIsIncorrect(Path base) throws Exception {
204321936Shselasky        initialization(base);
205321936Shselasky        String log = tb.new JavacTask()
206321936Shselasky                .options("-XDrawDiagnostics", "-processormodulepath", processorCompiledModules.toString(),
207321936Shselasky                         "-processor", "mypkg2.MyProcessor2,noPackage.noProcessor,mypkg1.MyProcessor1")
208321936Shselasky                .outdir(classes)
209321936Shselasky                .sources(testClass)
210321936Shselasky                .run(ToolBox.Expect.FAIL)
211321936Shselasky                .writeAll()
212321936Shselasky                .getOutputLines(ToolBox.OutputKind.STDOUT, ToolBox.OutputKind.DIRECT).toString();
213321936Shselasky        if (!log.trim().equals("[the annotation processor 2 is working!, - compiler.err.proc.processor.not.found: noPackage.noProcessor, 1 error]")) {
214321936Shselasky            throw new AssertionError("Unexpected output: " + log);
215321936Shselasky        }
216321936Shselasky    }
217321936Shselasky
218321936Shselasky    @Test
219321936Shselasky    void testOptionsExclusion(Path base) throws Exception {
220321936Shselasky        initialization(base);
221321936Shselasky        List<String> log = tb.new JavacTask()
222321936Shselasky                .options("-XDrawDiagnostics", "-processormodulepath", processorCompiledModules.toString(),
223321936Shselasky                        "-processorpath", processorCompiledModules.toString())
224321936Shselasky                .outdir(classes)
225321936Shselasky                .sources(testClass)
226321936Shselasky                .run(ToolBox.Expect.FAIL)
227321936Shselasky                .writeAll()
228321936Shselasky                .getOutputLines(ToolBox.OutputKind.DIRECT);
229321936Shselasky        if (!log.equals(Arrays.asList("- compiler.err.processorpath.no.processormodulepath",
230321936Shselasky                                      "1 error"))) {
231321936Shselasky            throw new AssertionError("Unexpected output: " + log);
232321936Shselasky        }
233321936Shselasky    }
234321936Shselasky}
235321936Shselasky