BadOptionsTest.java revision 3778:f6ae0686d664
1/*
2 * Copyright (c) 2002, 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 8169676
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;
40import java.util.ArrayList;
41import java.util.Arrays;
42import java.util.HashSet;
43import java.util.List;
44import java.util.ListIterator;
45import java.util.Locale;
46import java.util.Objects;
47import java.util.Set;
48
49import javax.lang.model.SourceVersion;
50
51import jdk.javadoc.doclet.Doclet;
52import jdk.javadoc.doclet.DocletEnvironment;
53import jdk.javadoc.doclet.Reporter;
54
55import toolbox.JavadocTask;
56import toolbox.ModuleBuilder;
57import toolbox.Task;
58import toolbox.TestRunner;
59import toolbox.ToolBox;
60
61/*
62 * This is primarily a test of the error reporting mechanisms
63 * for bad options provided by javac and utilized by javadoc.
64 * It is not an exhaustive test of all bad option forms detected
65 * by javac/javadoc.
66 */
67public class BadOptionsTest extends TestRunner {
68
69    public static void main(String... args) throws Exception {
70        BadOptionsTest t = new BadOptionsTest();
71        t.runTests();
72    }
73
74    private final ToolBox tb = new ToolBox();
75    private final Path src = Paths.get("src");
76
77    BadOptionsTest() throws IOException {
78        super(System.err);
79        init();
80    }
81
82    void init() throws IOException {
83        tb.writeJavaFiles(src,
84                "public class C { }");
85
86    }
87
88    @Test
89    public void testAddModulesEmptyArg() {
90        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
91                .options("-Xold",
92                        "--add-modules", "")
93                .files(src.resolve("C.java"))
94                .run(Task.Expect.FAIL)
95                .writeAll();
96        checkFound(result.getOutput(Task.OutputKind.DIRECT),
97                "javadoc: error - no value for --add-modules option");
98        checkNotFound(result, "Exception", "at jdk.javadoc/");
99    }
100
101    @Test
102    public void testAddModulesBadName() {
103        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
104                .options("-Xold", "-quiet",
105                        "--add-modules", "123")
106                .files(src.resolve("C.java"))
107                .run(Task.Expect.FAIL)
108                .writeAll();
109        checkFound(result.getOutput(Task.OutputKind.DIRECT),
110                "error: bad name in value for --add-modules option: '123'");
111        checkNotFound(result, "Exception", "at jdk.javadoc/");
112    }
113
114    @Test
115    public void testAddExportsEmptyArg() {
116        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
117                .options("-Xold",
118                        "--add-exports", "")
119                .files(src.resolve("C.java"))
120                .run(Task.Expect.FAIL)
121                .writeAll();
122        checkFound(result.getOutput(Task.OutputKind.DIRECT),
123                "javadoc: error - no value for --add-exports option");
124        checkNotFound(result, "Exception", "at jdk.javadoc/");
125    }
126
127    @Test
128    public void testAddExportsBadArg() {
129        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
130                .options("-Xold",
131                        "--add-exports", "m/p")
132                .files(src.resolve("C.java"))
133                .run(Task.Expect.FAIL)
134                .writeAll();
135        checkFound(result.getOutput(Task.OutputKind.DIRECT),
136                "javadoc: error - bad value for --add-exports option");
137        checkNotFound(result, "Exception", "at jdk.javadoc/");
138    }
139
140    @Test
141    public void testAddExportsBadName() {
142        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
143                .options("-Xold",
144                        "--add-exports", "m!/p1=m2")
145                .files(src.resolve("C.java"))
146                .run()
147                .writeAll();
148        checkFound(result.getOutput(Task.OutputKind.DIRECT),
149                "warning: bad name in value for --add-exports option: 'm!'");
150        checkNotFound(result, "Exception", "at jdk.javadoc/");
151    }
152
153    private void checkFound(String log, String... expect) {
154        for (String e : expect) {
155            if (!log.contains(e)) {
156                error("Expected string not found: '" + e + "'");
157            }
158        }
159    }
160
161    private void checkNotFound(Task.Result result, String... unexpected) {
162        for (Task.OutputKind k : Task.OutputKind.values()) {
163            String r = result.getOutput(k);
164            for (String u : unexpected) {
165                if (r.contains(u)) {
166                    error("Unexpected string found: '" + u + "'");
167                }
168            }
169        }
170    }
171}
172