1/*
2 * Copyright (c) 2002, 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 8169676 8175055
27 * @summary boolean result of Option.process is often ignored
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 * @modules jdk.compiler/com.sun.tools.javac.main
30 * @modules jdk.javadoc/jdk.javadoc.internal.api
31 * @modules jdk.javadoc/jdk.javadoc.internal.tool
32 * @library /tools/lib
33 * @build toolbox.JavacTask toolbox.JavadocTask toolbox.TestRunner toolbox.ToolBox
34 * @run main BadOptionsTest
35 */
36
37import java.io.IOException;
38import java.nio.file.Path;
39import java.nio.file.Paths;
40
41import toolbox.JavadocTask;
42import toolbox.ModuleBuilder;
43import toolbox.Task;
44import toolbox.TestRunner;
45import toolbox.ToolBox;
46
47/*
48 * This is primarily a test of the error reporting mechanisms
49 * for bad options provided by javac and utilized by javadoc.
50 * It is not an exhaustive test of all bad option forms detected
51 * by javac/javadoc.
52 */
53public class BadOptionsTest extends TestRunner {
54
55    public static void main(String... args) throws Exception {
56        BadOptionsTest t = new BadOptionsTest();
57        t.runTests();
58    }
59
60    private final ToolBox tb = new ToolBox();
61    private final Path src = Paths.get("src");
62
63    BadOptionsTest() throws IOException {
64        super(System.err);
65        init();
66    }
67
68    void init() throws IOException {
69        tb.writeJavaFiles(src,
70                "public class C { }");
71
72    }
73
74    @Test
75    public void testAddModulesEmptyArg() {
76        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
77                .options("--add-modules=")
78                .files(src.resolve("C.java"))
79                .run(Task.Expect.FAIL)
80                .writeAll();
81        checkFound(result.getOutput(Task.OutputKind.DIRECT),
82                "javadoc: error - no value for --add-modules option");
83        checkNotFound(result, "Exception", "at jdk.javadoc/");
84    }
85
86    @Test
87    public void testAddModulesBadName() {
88        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
89                .options("-quiet",
90                        "--add-modules", "123")
91                .files(src.resolve("C.java"))
92                .run(Task.Expect.FAIL)
93                .writeAll();
94        checkFound(result.getOutput(Task.OutputKind.DIRECT),
95                "error: bad name in value for --add-modules option: '123'");
96        checkNotFound(result, "Exception", "at jdk.javadoc/");
97    }
98
99    @Test
100    public void testAddExportsEmptyArg() {
101        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
102                .options("--add-exports=")
103                .files(src.resolve("C.java"))
104                .run(Task.Expect.FAIL)
105                .writeAll();
106        checkFound(result.getOutput(Task.OutputKind.DIRECT),
107                "javadoc: error - no value for --add-exports option");
108        checkNotFound(result, "Exception", "at jdk.javadoc/");
109    }
110
111    @Test
112    public void testAddExportsBadArg() {
113        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
114                .options("--add-exports=m/p")
115                .files(src.resolve("C.java"))
116                .run(Task.Expect.FAIL)
117                .writeAll();
118        checkFound(result.getOutput(Task.OutputKind.DIRECT),
119                "javadoc: error - bad value for --add-exports option");
120        checkNotFound(result, "Exception", "at jdk.javadoc/");
121    }
122
123    @Test
124    public void testAddExportsBadName() {
125        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
126                .options("--add-exports", "m!/p1=m2")
127                .files(src.resolve("C.java"))
128                .run()
129                .writeAll();
130        checkFound(result.getOutput(Task.OutputKind.DIRECT),
131                "warning: bad name in value for --add-exports option: 'm!'");
132        checkNotFound(result, "Exception", "at jdk.javadoc/");
133    }
134
135    @Test
136    public void testSourcePathAndModuleSourceConflict() throws IOException {
137        Path msrc = Paths.get("msrc");
138        new ModuleBuilder(tb, "m1")
139                .exports("p1")
140                .classes("package p1; public class C1 { }")
141                .write(msrc);
142        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
143                .options("-sourcepath", "src",
144                        "--module-source-path", msrc.getFileName().toString(),
145                        "--module", "m1")
146                .run(Task.Expect.FAIL)
147                .writeAll();
148        checkFound(result.getOutput(Task.OutputKind.DIRECT),
149                "javadoc: cannot specify both --source-path and --module-source-path");
150        checkFound(result.getOutput(Task.OutputKind.DIRECT),
151                "1 error");
152    }
153
154    private void checkFound(String log, String... expect) {
155        for (String e : expect) {
156            if (!log.contains(e)) {
157                error("Expected string not found: '" + e + "'");
158            }
159        }
160    }
161
162    private void checkNotFound(Task.Result result, String... unexpected) {
163        for (Task.OutputKind k : Task.OutputKind.values()) {
164            String r = result.getOutput(k);
165            for (String u : unexpected) {
166                if (r.contains(u)) {
167                    error("Unexpected string found: '" + u + "'");
168                }
169            }
170        }
171    }
172}
173