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 Test the --limit-modules option
27 * @library /tools/lib
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 *          jdk.compiler/com.sun.tools.javac.main
30 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
31 * @run main LimitModulesTest
32 */
33
34
35import java.nio.file.Path;
36
37import toolbox.JavacTask;
38import toolbox.Task;
39
40public class LimitModulesTest extends ModuleTestBase {
41    public static void main(String... args) throws Exception {
42        new LimitModulesTest().runTests();
43    }
44
45    @Test
46    public void testEmpty(Path base) throws Exception {
47        Path src = base.resolve("src");
48        tb.writeJavaFiles(src, "class Dummy { }");
49        Path classes = base.resolve("classes");
50        tb.createDirectories(classes);
51
52        String log = new JavacTask(tb, Task.Mode.CMDLINE)
53                .options("--module-source-path", src.toString(),
54                         "--limit-modules", "")
55                .outdir(classes)
56                .files(findJavaFiles(src))
57                .run(Task.Expect.FAIL)
58                .writeAll()
59                .getOutput(Task.OutputKind.DIRECT);
60
61        if (!log.contains("javac: no value for --limit-modules option"))
62            throw new Exception("expected output not found");
63
64        log = new JavacTask(tb, Task.Mode.CMDLINE)
65                .options("--module-source-path", src.toString(),
66                         "--limit-modules=")
67                .outdir(classes)
68                .files(findJavaFiles(src))
69                .run(Task.Expect.FAIL)
70                .writeAll()
71                .getOutput(Task.OutputKind.DIRECT);
72
73        if (!log.contains("javac: no value for --limit-modules option"))
74            throw new Exception("expected output not found");
75    }
76
77    @Test
78    public void testEmptyItem(Path base) throws Exception {
79        Path src = base.resolve("src");
80        Path src_m1 = src.resolve("m1x");
81        tb.writeJavaFiles(src_m1,
82                          "module m1x { }");
83        Path src_m2 = src.resolve("m2x");
84        tb.writeJavaFiles(src_m2,
85                          "module m2x { }");
86        Path classes = base.resolve("classes");
87        tb.createDirectories(classes);
88
89        new JavacTask(tb)
90                .options("--module-source-path", src.toString(),
91                         "--limit-modules", ",m1x")
92                .outdir(classes)
93                .files(findJavaFiles(src))
94                .run()
95                .writeAll();
96
97        new JavacTask(tb)
98                .options("--module-source-path", src.toString(),
99                         "--limit-modules", "m1x,,m2x")
100                .outdir(classes)
101                .files(findJavaFiles(src))
102                .run()
103                .writeAll();
104
105        new JavacTask(tb)
106                .options("--module-source-path", src.toString(),
107                         "--limit-modules", "m1x,")
108                .outdir(classes)
109                .files(findJavaFiles(src))
110                .run()
111                .writeAll();
112    }
113
114    @Test
115    public void testEmptyList(Path base) throws Exception {
116        Path src = base.resolve("src");
117        tb.writeJavaFiles(src, "class Dummy { }");
118        Path classes = base.resolve("classes");
119        tb.createDirectories(classes);
120
121        String log = new JavacTask(tb, Task.Mode.CMDLINE)
122                .options("--module-source-path", src.toString(),
123                         "--limit-modules", ",")
124                .outdir(classes)
125                .files(findJavaFiles(src))
126                .run(Task.Expect.FAIL)
127                .writeAll()
128                .getOutput(Task.OutputKind.DIRECT);
129
130        if (!log.contains("javac: bad value for --limit-modules option"))
131            throw new Exception("expected output not found");
132    }
133
134    @Test
135    public void testInvalidName(Path base) throws Exception {
136        Path src = base.resolve("src");
137        tb.writeJavaFiles(src, "class Dummy { }");
138        Path classes = base.resolve("classes");
139        tb.createDirectories(classes);
140
141        String log = new JavacTask(tb)
142                .options("-XDrawDiagnostics",
143                         "--limit-modules", "BadModule!")
144                .outdir(classes)
145                .files(findJavaFiles(src))
146                .run(Task.Expect.FAIL)
147                .writeAll()
148                .getOutput(Task.OutputKind.DIRECT);
149
150        if (!log.contains("- compiler.err.bad.name.for.option: --limit-modules, BadModule!"))
151            throw new Exception("expected output not found");
152    }
153
154    @Test
155    public void testLastOneWins(Path base) throws Exception {
156        Path src = base.resolve("src");
157        tb.writeJavaFiles(src,
158                          "package p; class C { com.sun.tools.javac.Main main; }");
159        Path classes = base.resolve("classes");
160        tb.createDirectories(classes);
161
162        System.err.println("case 1:");
163        new JavacTask(tb)
164                .options("-XDrawDiagnostics",
165                         "--limit-modules", "java.base",
166                         "--limit-modules", "jdk.compiler")
167                .outdir(classes)
168                .files(findJavaFiles(src))
169                .run()
170                .writeAll();
171
172        System.err.println("case 2:");
173        String log = new JavacTask(tb)
174                .options("-XDrawDiagnostics",
175                         "--limit-modules", "jdk.compiler",
176                         "--limit-modules", "java.base")
177                .outdir(classes)
178                .files(findJavaFiles(src))
179                .run(Task.Expect.FAIL)
180                .writeAll()
181                .getOutput(Task.OutputKind.DIRECT);
182
183        if (!log.contains("C.java:1:35: compiler.err.package.not.visible: com.sun.tools.javac, (compiler.misc.not.def.access.does.not.read.from.unnamed: com.sun.tools.javac, jdk.compiler)"))
184            throw new Exception("expected output not found");
185    }
186}
187
188