1/*
2 * Copyright (c) 2016, 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 8153391
27 * @summary Verify javac behaves properly in absence of zip/jar FileSystemProvider
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 * @run main/othervm --limit-modules jdk.compiler LimitedImage
32 */
33
34import java.io.IOException;
35import java.nio.file.Path;
36import java.nio.file.Paths;
37import java.util.Arrays;
38import java.util.List;
39
40import toolbox.JavacTask;
41import toolbox.JarTask;
42import toolbox.Task.Expect;
43import toolbox.Task.Mode;
44import toolbox.Task.OutputKind;
45import toolbox.ToolBox;
46
47public class LimitedImage {
48    public static void main(String... args) throws IOException {
49        ToolBox tb = new ToolBox();
50
51        //showing help should be OK
52        new JavacTask(tb, Mode.CMDLINE)
53                .options("--help")
54                .run().writeAll();
55
56        Path testSource = Paths.get("Test.java");
57        tb.writeFile(testSource, "class Test {}");
58
59        //when zip/jar FS is not needed, compilation should succeed
60        new JavacTask(tb, Mode.CMDLINE)
61                .classpath()
62                .files(testSource)
63                .outdir(".")
64                .run()
65                .writeAll();
66
67        Path testJar = Paths.get("test.jar").toAbsolutePath();
68
69        new JarTask(tb, testJar).run();
70
71        List<String> actualOutput;
72        List<String> expectedOutput = Arrays.asList(
73                "- compiler.err.no.zipfs.for.archive: " + testJar.toString()
74        );
75
76        //check proper diagnostics when zip/jar FS not present:
77        System.err.println("Test " + testJar + " on classpath");
78        actualOutput = new JavacTask(tb, Mode.CMDLINE)
79                .classpath(testJar)
80                .options("-XDrawDiagnostics")
81                .files(testSource)
82                .outdir(".")
83                .run(Expect.FAIL)
84                .writeAll()
85                .getOutputLines(OutputKind.DIRECT);
86
87        if (!expectedOutput.equals(actualOutput)) {
88            throw new AssertionError("Unexpected output");
89        }
90
91        System.err.println("Test " + testJar + " on sourcepath");
92        actualOutput = new JavacTask(tb, Mode.CMDLINE)
93                .sourcepath(testJar)
94                .options("-XDrawDiagnostics")
95                .files(testSource)
96                .outdir(".")
97                .run(Expect.FAIL)
98                .writeAll()
99                .getOutputLines(OutputKind.DIRECT);
100
101        if (!expectedOutput.equals(actualOutput)) {
102            throw new AssertionError("Unexpected output");
103        }
104
105        System.err.println("Test " + testJar + " on modulepath");
106        actualOutput = new JavacTask(tb, Mode.CMDLINE)
107                .options("-XDrawDiagnostics",
108                         "--module-path", testJar.toString())
109                .files(testSource)
110                .outdir(".")
111                .run(Expect.FAIL)
112                .writeAll()
113                .getOutputLines(OutputKind.DIRECT);
114
115        if (!expectedOutput.equals(actualOutput)) {
116            throw new AssertionError("Unexpected output");
117        }
118
119        expectedOutput = Arrays.asList(
120                "- compiler.err.no.zipfs.for.archive: " + testJar.toString(),
121                "1 error"
122        );
123
124        System.err.println("Test directory containing " + testJar + " on modulepath");
125        actualOutput = new JavacTask(tb, Mode.CMDLINE)
126                .classpath()
127                .options("-XDrawDiagnostics",
128                         "--module-path", testJar.getParent().toString())
129                .files(testSource)
130                .outdir(".")
131                .run(Expect.FAIL)
132                .writeAll()
133                .getOutputLines(OutputKind.DIRECT);
134
135        if (!expectedOutput.equals(actualOutput)) {
136            throw new AssertionError("Unexpected output");
137        }
138    }
139
140}
141