SingleModuleModeTest.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 tests for single module mode compilation
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 SingleModuleModeTest
34 */
35
36import java.nio.file.Files;
37import java.nio.file.Path;
38import java.util.Set;
39
40import javax.annotation.processing.AbstractProcessor;
41import javax.annotation.processing.RoundEnvironment;
42import javax.annotation.processing.SupportedAnnotationTypes;
43import javax.lang.model.SourceVersion;
44import javax.lang.model.element.TypeElement;
45
46public class SingleModuleModeTest extends ModuleTestBase{
47
48    public static void main(String... args) throws Exception {
49        new SingleModuleModeTest().run();
50    }
51
52    void run() throws Exception {
53        tb = new ToolBox();
54
55        runTests();
56    }
57
58    @Test
59    void testTooManyModules(Path base) throws Exception {
60        Path src = base.resolve("src");
61        tb.writeJavaFiles(src.resolve("m1"), "module m1 { }");
62        tb.writeJavaFiles(src.resolve("m2"), "module m2 { }");
63
64        String log = tb.new JavacTask()
65                .options("-XDrawDiagnostics")
66                .files(findJavaFiles(src))
67                .run(ToolBox.Expect.FAIL)
68                .writeAll()
69                .getOutput(ToolBox.OutputKind.DIRECT);
70
71        if (!log.contains("module-info.java:1:1: compiler.err.too.many.modules"))
72            throw new Exception("expected output not found");
73    }
74
75    @Test
76    void testImplicitModuleSource(Path base) throws Exception {
77        Path src = base.resolve("src");
78        tb.writeJavaFiles(src,
79                "module m { }",
80                "class C { }");
81
82        tb.new JavacTask()
83                .classpath(src)
84                .files(src.resolve("C.java"))
85                .run()
86                .writeAll();
87    }
88
89    @Test
90    void testImplicitModuleClass(Path base) throws Exception {
91        Path src = base.resolve("src");
92        tb.writeJavaFiles(src,
93                "module m { }",
94                "class C { }");
95        Path classes = base.resolve("classes");
96        Files.createDirectories(classes);
97
98        tb.new JavacTask()
99                .outdir(classes)
100                .files(src.resolve("module-info.java"))
101                .run()
102                .writeAll();
103
104        tb.new JavacTask()
105                .classpath(classes)
106                .files(src.resolve("C.java"))
107                .run()
108                .writeAll();
109    }
110
111    @Test
112    void testImplicitModuleClassAP(Path base) throws Exception {
113        Path src = base.resolve("src");
114        tb.writeJavaFiles(src,
115                "module m { uses java.lang.Runnable; }",
116                "class C { }");
117        Path classes = base.resolve("classes");
118        Files.createDirectories(classes);
119
120        tb.new JavacTask()
121                .outdir(classes)
122                .files(src.resolve("module-info.java"))
123                .run()
124                .writeAll();
125
126        tb.new JavacTask()
127                .options("-processor", VerifyUsesProvides.class.getName(),
128                         "-processorpath", System.getProperty("test.classes"))
129                .outdir(classes)
130                .classpath(classes)
131                .files(src.resolve("C.java"))
132                .run()
133                .writeAll();
134    }
135
136    @Test
137    void testImplicitModuleSourceAP(Path base) throws Exception {
138        Path src = base.resolve("src");
139        tb.writeJavaFiles(src,
140                "module m { uses java.lang.Runnable; }",
141                "class C { }");
142        Path classes = base.resolve("classes");
143        Files.createDirectories(classes);
144
145        tb.new JavacTask()
146                .options("-processor", VerifyUsesProvides.class.getName(),
147                         "-processorpath", System.getProperty("test.classes"))
148                .outdir(classes)
149                .sourcepath(src)
150                .classpath(classes)
151                .files(src.resolve("C.java"))
152                .run()
153                .writeAll();
154    }
155
156    @SupportedAnnotationTypes("*")
157    public static final class VerifyUsesProvides extends AbstractProcessor {
158
159        @Override
160        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
161            if (processingEnv.getElementUtils().getModuleElement("m") == null) {
162                throw new AssertionError();
163            }
164
165            return false;
166        }
167
168        @Override
169        public SourceVersion getSupportedSourceVersion() {
170            return SourceVersion.latest();
171        }
172
173    }
174}
175