Modules.java revision 3618:64182008b2d0
1/*
2 * Copyright (c) 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 * @bug 8159305
27 * @summary Tests primarily the module graph computations.
28 * @modules
29 *      jdk.javadoc/jdk.javadoc.internal.api
30 *      jdk.javadoc/jdk.javadoc.internal.tool
31 *      jdk.compiler/com.sun.tools.javac.api
32 *      jdk.compiler/com.sun.tools.javac.main
33 * @library /tools/lib
34 * @build toolbox.ToolBox toolbox.TestRunner
35 * @run main Modules
36 */
37
38import java.io.IOException;
39import java.nio.file.Files;
40import java.nio.file.Path;
41
42import toolbox.*;
43
44public class Modules extends ModuleTestBase {
45
46    public static void main(String... args) throws Exception {
47        new Modules().runTests();
48    }
49
50    @Test
51    public void testBasicMoption(Path base) throws Exception {
52        Files.createDirectory(base);
53        Path src = base.resolve("src");
54        ModuleBuilder mb = new ModuleBuilder(tb, "m1");
55        mb.comment("The first module.")
56                .exports("pub")
57                .classes("package pub; /** Klass A */ public class A {}")
58                .classes("package pro; /** Klass B */ public class B {}")
59                .write(src);
60        execTask("--module-source-path", src.toString(),
61                 "--module", "m1");
62        checkModulesSpecified("m1");
63        checkPackagesIncluded("pub");
64        checkTypesIncluded("pub.A");
65    }
66
67    @Test
68    public void testMultipleModulesOption1(Path base) throws Exception {
69        Path src = base.resolve("src");
70
71        ModuleBuilder mb1 = new ModuleBuilder(tb, "m1");
72        mb1.comment("The first module.")
73                .exports("m1pub")
74                .requires("m2")
75                .classes("package m1pub; /** Klass A */ public class A {}")
76                .classes("package m1pro; /** Klass B */ public class B {}")
77                .write(src);
78
79        ModuleBuilder mb2 = new ModuleBuilder(tb, "m2");
80        mb2.comment("The second module.")
81                .exports("m2pub")
82                .classes("package m2pub; /** Klass A */ public class A {}")
83                .classes("package m2pro; /** Klass B */ public class B {}")
84                .write(src);
85        execTask("--module-source-path", src.toString(),
86            "--module", "m1,m2");
87        checkModulesSpecified("m1", "m2");
88        checkPackagesIncluded("m1pub", "m2pub");
89        checkTypesIncluded("m1pub.A", "m2pub.A");
90
91    }
92
93    @Test
94    public void testMultipleModulesAggregatedModuleOption(Path base) throws Exception {
95        Path src = base.resolve("src");
96
97        ModuleBuilder mb1 = new ModuleBuilder(tb, "m1");
98        mb1.comment("The first module.")
99                .exports("m1pub")
100                .requires("m2")
101                .classes("package m1pub; /** Klass A */ public class A {}")
102                .classes("package m1pro; /** Klass B */ public class B {}")
103                .write(src);
104
105        ModuleBuilder mb2 = new ModuleBuilder(tb, "m2");
106        mb2.comment("The second module.")
107                .exports("m2pub")
108                .classes("package m2pub; /** Klass A */ public class A {}")
109                .classes("package m2pro; /** Klass B */ public class B {}")
110                .write(src);
111        execTask("--module-source-path", src.toString(),
112            "--module", "m1",
113            "--module", "m2");
114        checkModulesSpecified("m1", "m2");
115        checkPackagesIncluded("m1pub", "m2pub");
116        checkTypesIncluded("m1pub.A", "m2pub.A");
117
118    }
119
120   /**
121     * Tests diamond graph, inspired by javac diamond tests.
122     *
123     *
124     * Module M : test module, with variable requires
125     *
126     * Module N :
127     *     requires public O  --->   Module O:
128     *                                 requires J   ---->   Module J:
129     *                                 exports openO          exports openJ
130     *
131     *
132     * Module L :
133     *     requires public P  --->   Module P:
134     *                                 exports openP
135     *
136     *
137     */
138
139    @Test
140    public void testExpandRequiresNone(Path base) throws Exception {
141        Path src = base.resolve("src");
142
143        createAuxiliaryModules(src);
144
145        new ModuleBuilder(tb, "M")
146                .comment("The M module.")
147                .requires("N", src)
148                .requires("L", src)
149                .requires("O", src)
150                .exports("p")
151                .classes("package p; public class Main { openO.O o; openN.N n; openL.L l; }")
152                .write(src);
153
154        execTask("--module-source-path", src.toString(),
155                "--module", "M");
156
157        checkModulesSpecified("M");
158        checkModulesIncluded("M");
159        checkPackagesIncluded("p");
160        checkTypesIncluded("p.Main");
161        checkPackagesNotIncluded(".*open.*");
162    }
163
164    @Test
165    public void testExpandRequiresPublic(Path base) throws Exception {
166        Path src = base.resolve("src");
167
168        createAuxiliaryModules(src);
169
170        new ModuleBuilder(tb, "M")
171                .comment("The M module.")
172                .requiresPublic("N", src)
173                .requires("L", src)
174                .exports("p")
175                .classes("package p; public class Main { openO.O o; openN.N n; openL.L l; }")
176                .write(src);
177
178        execTask("--module-source-path", src.toString(),
179                "--module", "M",
180                "--expand-requires:public");
181
182        checkModulesSpecified("M", "N", "O");
183        checkModulesIncluded("M", "N", "O");
184        checkPackagesIncluded("p", "openN", "openO");
185        checkTypesIncluded("p.Main", "openN.N", "openO.O");
186    }
187
188    @Test
189    public void testExpandRequiresAll(Path base) throws Exception {
190        Path src = base.resolve("src");
191
192        createAuxiliaryModules(src);
193
194        new ModuleBuilder(tb, "M")
195                .comment("The M module.")
196                .requiresPublic("N", src)
197                .requires("L", src)
198                .requires("O", src)
199                .exports("p")
200                .classes("package p; public class Main { openO.O o; openN.N n; openL.L l; }")
201                .write(src);
202
203        execTask("--module-source-path", src.toString(),
204                "--module", "M",
205                "--expand-requires:all");
206
207        checkModulesSpecified("M", "java.base", "N", "L", "O");
208        checkModulesIncluded("M", "java.base", "N", "L", "O");
209        checkModulesNotIncluded("P", "J", "Q");
210        checkPackagesIncluded("p", "openN", "openL", "openO");
211        checkPackagesNotIncluded(".*openP.*", ".*openJ.*");
212        checkTypesIncluded("p.Main", "openN.N", "openL.L", "openO.O");
213        checkTypesNotIncluded(".*openP.*", ".*openJ.*");
214    }
215
216    @Test
217    public void testMissingModule(Path base) throws Exception {
218        Path src = base.resolve("src");
219
220        createAuxiliaryModules(src);
221
222        new ModuleBuilder(tb, "M")
223                .comment("The M module.")
224                .requiresPublic("N", src)
225                .requires("L", src)
226                .requires("O", src)
227                .exports("p")
228                .classes("package p; public class Main { openO.O o; openN.N n; openL.L l; }")
229                .write(src);
230
231        execNegativeTask("--module-source-path", src.toString(),
232                "--module", "MIA",
233                "--expand-requires:all");
234
235        assertErrorPresent("javadoc: error - module MIA not found.");
236    }
237
238    @Test
239    public void testMissingModuleMultiModuleCmdline(Path base) throws Exception {
240        Path src = base.resolve("src");
241
242        createAuxiliaryModules(src);
243
244        new ModuleBuilder(tb, "M")
245                .comment("The M module.")
246                .requiresPublic("N", src)
247                .requires("L", src)
248                .requires("O", src)
249                .exports("p")
250                .classes("package p; public class Main { openO.O o; openN.N n; openL.L l; }")
251                .write(src);
252
253        execNegativeTask("--module-source-path", src.toString(),
254                "--module", "M,N,L,MIA,O,P",
255                "--expand-requires:all");
256
257        assertErrorPresent("javadoc: error - module MIA not found");
258    }
259
260    void createAuxiliaryModules(Path src) throws IOException {
261
262        new ModuleBuilder(tb, "J")
263                .comment("The J module.")
264                .exports("openJ")
265                .classes("package openJ;  /** Klass J open. */ public class J { }")
266                .classes("package closedJ; /** Klass J closed. */ public class J  { }")
267                .write(src);
268
269        new ModuleBuilder(tb, "L")
270                .comment("The L module.")
271                .exports("openL")
272                .requiresPublic("P")
273                .classes("package openL; /** Klass L open */ public class L { }")
274                .classes("package closedL;  /** Klass L closed */ public class L { }")
275                .write(src);
276
277        new ModuleBuilder(tb, "N")
278                .comment("The N module.")
279                .exports("openN")
280                .requiresPublic("O")
281                .classes("package openN; /** Klass N open */ public class N  { }")
282                .classes("package closedN; /** Klass N closed */ public class N { }")
283                .write(src);
284
285        new ModuleBuilder(tb, "O")
286                .comment("The O module.")
287                .exports("openO")
288                .requires("J")
289                .classes("package openO; /** Klass O open. */ public class O { openJ.J j; }")
290                .classes("package closedO;  /** Klass O closed. */ public class O { }")
291                .write(src);
292
293        new ModuleBuilder(tb, "P")
294                .comment("The O module.")
295                .exports("openP")
296                .requires("J")
297                .classes("package openP; /** Klass O open. */ public class O { openJ.J j; }")
298                .classes("package closedP;  /** Klass O closed. */ public class O { }")
299                .write(src);
300
301        new ModuleBuilder(tb, "Q")
302                .comment("The Q module.")
303                .exports("openQ")
304                .requires("J")
305                .classes("package openQ; /** Klass Q open. */ public class Q { openJ.J j; }")
306                .classes("package closedQ;  /** Klass Q closed. */ public class Q { }")
307                .write(src);
308
309    }
310}
311