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("--add-modules=")
92                .files(src.resolve("C.java"))
93                .run(Task.Expect.FAIL)
94                .writeAll();
95        checkFound(result.getOutput(Task.OutputKind.DIRECT),
96                "javadoc: error - no value for --add-modules option");
97        checkNotFound(result, "Exception", "at jdk.javadoc/");
98    }
99
100    @Test
101    public void testAddModulesBadName() {
102        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
103                .options("-quiet",
104                        "--add-modules", "123")
105                .files(src.resolve("C.java"))
106                .run(Task.Expect.FAIL)
107                .writeAll();
108        checkFound(result.getOutput(Task.OutputKind.DIRECT),
109                "error: bad name in value for --add-modules option: '123'");
110        checkNotFound(result, "Exception", "at jdk.javadoc/");
111    }
112
113    @Test
114    public void testAddExportsEmptyArg() {
115        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
116                .options("--add-exports=")
117                .files(src.resolve("C.java"))
118                .run(Task.Expect.FAIL)
119                .writeAll();
120        checkFound(result.getOutput(Task.OutputKind.DIRECT),
121                "javadoc: error - no value for --add-exports option");
122        checkNotFound(result, "Exception", "at jdk.javadoc/");
123    }
124
125    @Test
126    public void testAddExportsBadArg() {
127        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
128                .options("--add-exports=m/p")
129                .files(src.resolve("C.java"))
130                .run(Task.Expect.FAIL)
131                .writeAll();
132        checkFound(result.getOutput(Task.OutputKind.DIRECT),
133                "javadoc: error - bad value for --add-exports option");
134        checkNotFound(result, "Exception", "at jdk.javadoc/");
135    }
136
137    @Test
138    public void testAddExportsBadName() {
139        Task.Result result = new JavadocTask(tb, Task.Mode.CMDLINE)
140                .options("--add-exports", "m!/p1=m2")
141                .files(src.resolve("C.java"))
142                .run()
143                .writeAll();
144        checkFound(result.getOutput(Task.OutputKind.DIRECT),
145                "warning: bad name in value for --add-exports option: 'm!'");
146        checkNotFound(result, "Exception", "at jdk.javadoc/");
147    }
148
149    private void checkFound(String log, String... expect) {
150        for (String e : expect) {
151            if (!log.contains(e)) {
152                error("Expected string not found: '" + e + "'");
153            }
154        }
155    }
156
157    private void checkNotFound(Task.Result result, String... unexpected) {
158        for (Task.OutputKind k : Task.OutputKind.values()) {
159            String r = result.getOutput(k);
160            for (String u : unexpected) {
161                if (r.contains(u)) {
162                    error("Unexpected string found: '" + u + "'");
163                }
164            }
165        }
166    }
167}
168