ReleaseOptionUnsupported.java revision 4171:77a2d6c1f321
1/*
2 * Copyright (c) 2017, 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 8178152
27 * @summary Verify unsupported modules and module options handling.
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 *          jdk.compiler/com.sun.tools.javac.jvm
32 *          jdk.jdeps/com.sun.tools.classfile
33 *          jdk.jdeps/com.sun.tools.javap
34 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.JavapTask toolbox.TestRunner
35 * @run main ReleaseOptionUnsupported
36 */
37
38import java.io.IOException;
39import java.nio.file.Path;
40import java.nio.file.Paths;
41import java.util.Arrays;
42import java.util.List;
43
44import com.sun.tools.javac.jvm.Target;
45import toolbox.JavacTask;
46import toolbox.Task;
47import toolbox.Task.Expect;
48import toolbox.TestRunner;
49import toolbox.ToolBox;
50
51public class ReleaseOptionUnsupported extends TestRunner {
52
53    private final ToolBox tb = new ToolBox();
54
55    public ReleaseOptionUnsupported() {
56        super(System.err);
57    }
58
59    public static void main(String... args) throws Exception {
60        new ReleaseOptionUnsupported().runTests();
61    }
62
63    @Test
64    public void testUnsafe(Path base) throws IOException {
65        Path src = base.resolve("src");
66        tb.writeJavaFiles(src,
67                          "module m { requires jdk.unsupported; }",
68                          "package test; public class Test { sun.misc.Unsafe unsafe; } ");
69        Path classes = base.resolve("classes");
70        tb.createDirectories(classes);
71
72        List<String> log;
73        List<String> expected = Arrays.asList(
74                "Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
75                "1 warning"
76        );
77
78        log = new JavacTask(tb)
79                .options("-XDrawDiagnostics")
80                .outdir(classes)
81                .files(tb.findJavaFiles(src))
82                .run(Expect.SUCCESS)
83                .writeAll()
84                .getOutputLines(Task.OutputKind.DIRECT);
85
86        if (!expected.equals(log)) {
87            throw new AssertionError("Unexpected output: " + log);
88        }
89
90        log = new JavacTask(tb)
91                .options("-XDrawDiagnostics",
92                         "--release", Target.DEFAULT.multiReleaseValue())
93                .outdir(classes)
94                .files(tb.findJavaFiles(src))
95                .run(Expect.SUCCESS)
96                .writeAll()
97                .getOutputLines(Task.OutputKind.DIRECT);
98
99        if (!expected.equals(log)) {
100            throw new AssertionError("Unexpected output: " + log);
101        }
102    }
103
104    @Test
105    public void testUnsafeUnnamed(Path base) throws IOException {
106        Path src = base.resolve("src");
107        tb.writeJavaFiles(src,
108                          "package test; public class Test { sun.misc.Unsafe unsafe; } ");
109        Path classes = base.resolve("classes");
110        tb.createDirectories(classes);
111
112        List<String> log;
113        List<String> expected = Arrays.asList(
114                "Test.java:1:43: compiler.warn.sun.proprietary: sun.misc.Unsafe",
115                "1 warning"
116        );
117
118        log = new JavacTask(tb)
119                .options("-XDrawDiagnostics")
120                .outdir(classes)
121                .files(tb.findJavaFiles(src))
122                .run(Expect.SUCCESS)
123                .writeAll()
124                .getOutputLines(Task.OutputKind.DIRECT);
125
126        if (!expected.equals(log)) {
127            throw new AssertionError("Unexpected output: " + log);
128        }
129
130        log = new JavacTask(tb)
131                .options("-XDrawDiagnostics",
132                         "--release", Target.DEFAULT.multiReleaseValue())
133                .outdir(classes)
134                .files(tb.findJavaFiles(src))
135                .run(Expect.SUCCESS)
136                .writeAll()
137                .getOutputLines(Task.OutputKind.DIRECT);
138
139        if (!expected.equals(log)) {
140            throw new AssertionError("Unexpected output: " + log);
141        }
142    }
143
144    @Test
145    public void testAddExports(Path base) throws IOException {
146        Path src = base.resolve("src");
147        tb.writeJavaFiles(src,
148                          "module m { }",
149                          "package test; public class Test { jdk.internal.misc.Unsafe unsafe; } ");
150        Path classes = base.resolve("classes");
151        tb.createDirectories(classes);
152
153        new JavacTask(tb)
154                .options("-XDrawDiagnostics",
155                         "--add-exports", "java.base/jdk.internal.misc=m")
156                .outdir(classes)
157                .files(tb.findJavaFiles(src))
158                .run(Expect.SUCCESS)
159                .writeAll()
160                .getOutputLines(Task.OutputKind.DIRECT);
161
162        List<String> log;
163        List<String> expected;
164
165        log = new JavacTask(tb)
166                .options("-XDrawDiagnostics",
167                         "--add-exports", "java.base/jdk.internal.misc=m",
168                         "--release", Target.DEFAULT.multiReleaseValue())
169                .outdir(classes)
170                .files(tb.findJavaFiles(src))
171                .run(Expect.FAIL)
172                .writeAll()
173                .getOutputLines(Task.OutputKind.DIRECT);
174
175        expected = Arrays.asList(
176                "- compiler.err.add.exports.with.release: java.base",
177                "1 error"
178        );
179
180        if (!expected.equals(log)) {
181            throw new AssertionError("Unexpected output: " + log);
182        }
183
184        //OK to add exports a package of a non-system module:
185        tb.writeJavaFiles(src,
186                          "package test; public class Test { } ");
187        tb.createDirectories(classes);
188
189        new JavacTask(tb)
190                .options("-XDrawDiagnostics",
191                         "--add-exports", "m/test=ALL-UNNAMED",
192                         "--release", Target.DEFAULT.multiReleaseValue())
193                .outdir(classes)
194                .files(tb.findJavaFiles(src))
195                .run(Expect.SUCCESS)
196                .writeAll()
197                .getOutputLines(Task.OutputKind.DIRECT);
198    }
199
200    @Test
201    public void testAddReads(Path base) throws IOException {
202        Path src = base.resolve("src");
203        tb.writeJavaFiles(src,
204                          "module m { }",
205                          "package test; public class Test { } ");
206        Path classes = base.resolve("classes");
207        tb.createDirectories(classes);
208
209        new JavacTask(tb)
210                .options("-XDrawDiagnostics",
211                         "--add-reads", "java.base=m")
212                .outdir(classes)
213                .files(tb.findJavaFiles(src))
214                .run(Expect.SUCCESS)
215                .writeAll()
216                .getOutputLines(Task.OutputKind.DIRECT);
217
218        List<String> log;
219        List<String> expected;
220
221        log = new JavacTask(tb)
222                .options("-XDrawDiagnostics",
223                         "--add-reads", "java.base=m",
224                         "--release", Target.DEFAULT.multiReleaseValue())
225                .outdir(classes)
226                .files(tb.findJavaFiles(src))
227                .run(Expect.FAIL)
228                .writeAll()
229                .getOutputLines(Task.OutputKind.DIRECT);
230
231        expected = Arrays.asList(
232                "- compiler.err.add.reads.with.release: java.base",
233                "1 error"
234        );
235
236        if (!expected.equals(log)) {
237            throw new AssertionError("Unexpected output: " + log);
238        }
239
240        //OK to add reads a package of a non-system module:
241        tb.createDirectories(classes);
242
243        new JavacTask(tb)
244                .options("-XDrawDiagnostics",
245                         "--add-reads", "m=java.base",
246                         "--release", Target.DEFAULT.multiReleaseValue())
247                .outdir(classes)
248                .files(tb.findJavaFiles(src))
249                .run(Expect.SUCCESS)
250                .writeAll()
251                .getOutputLines(Task.OutputKind.DIRECT);
252    }
253
254    @Test
255    public void testPatchModule(Path base) throws IOException {
256        Path src = base.resolve("src");
257        tb.writeJavaFiles(src,
258                          "module m { }",
259                          "package test; public class Test { } ");
260        Path classes = base.resolve("classes");
261        tb.createDirectories(classes);
262        Path patch = base.resolve("patch");
263        tb.createDirectories(patch);
264
265        new JavacTask(tb)
266                .options("-XDrawDiagnostics",
267                         "--patch-module", "java.base=" + patch)
268                .outdir(classes)
269                .files(tb.findJavaFiles(src))
270                .run(Expect.SUCCESS)
271                .writeAll()
272                .getOutputLines(Task.OutputKind.DIRECT);
273
274        List<String> log;
275        List<String> expected;
276
277        log = new JavacTask(tb)
278                .options("-XDrawDiagnostics",
279                         "--patch-module", "java.base=" + patch,
280                         "--release", Target.DEFAULT.multiReleaseValue())
281                .outdir(classes)
282                .files(tb.findJavaFiles(src))
283                .run(Expect.FAIL)
284                .writeAll()
285                .getOutputLines(Task.OutputKind.DIRECT);
286
287        expected = Arrays.asList(
288                "- compiler.err.patch.module.with.release: java.base",
289                "1 error"
290        );
291
292        if (!expected.equals(log)) {
293            throw new AssertionError("Unexpected output: " + log);
294        }
295
296        //OK to patch a non-system module:
297        tb.createDirectories(classes);
298
299        new JavacTask(tb)
300                .options("-XDrawDiagnostics",
301                         "--patch-module", "m=" + patch,
302                         "--release", Target.DEFAULT.multiReleaseValue())
303                .outdir(classes)
304                .files(tb.findJavaFiles(src))
305                .run(Expect.SUCCESS)
306                .writeAll()
307                .getOutputLines(Task.OutputKind.DIRECT);
308    }
309
310    protected void runTests() throws Exception {
311        runTests(m -> new Object[] { Paths.get(m.getName()) });
312    }
313}
314