Path: blob/master/test/langtools/jdk/javadoc/tool/OptionSyntaxTest.java
40957 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 816614426* @summary support new-style options27* @modules jdk.compiler/com.sun.tools.javac.api28* @modules jdk.compiler/com.sun.tools.javac.main29* @modules jdk.javadoc/jdk.javadoc.internal.api30* @modules jdk.javadoc/jdk.javadoc.internal.tool31* @library /tools/lib32* @build toolbox.JavacTask toolbox.JavadocTask toolbox.ModuleBuilder toolbox.TestRunner toolbox.ToolBox33* @run main OptionSyntaxTest34*/35import java.io.IOException;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.ArrayList;39import java.util.Arrays;40import java.util.HashSet;41import java.util.List;42import java.util.Locale;43import java.util.Set;4445import javax.lang.model.SourceVersion;4647import jdk.javadoc.doclet.Doclet;48import jdk.javadoc.doclet.DocletEnvironment;49import jdk.javadoc.doclet.Reporter;5051import toolbox.JavadocTask;52import toolbox.ModuleBuilder;53import toolbox.Task;54import toolbox.TestRunner;55import toolbox.ToolBox;565758public class OptionSyntaxTest extends TestRunner {59public static class TestDoclet implements Doclet {60@Override61public boolean run(DocletEnvironment root) {62System.out.println("TestDoclet.run");63return true;64}6566@Override67public String getName() {68return "Test";69}7071@Override72public Set<Option> getSupportedOptions() {73return options;74}7576@Override77public SourceVersion getSupportedSourceVersion() {78return SourceVersion.latest();79}8081@Override82public void init(Locale locale, Reporter reporter) {83}8485private final Set<Doclet.Option> options = new HashSet<>(Arrays.asList(86new DOption("-old", 0),87new DOption("-oldWithArg", 1),88new DOption("-oldWithArgs", 2),89new DOption("--new", 0),90new DOption("--newWithArg", 1),91new DOption("--newWithArgs", 2)92));9394}9596static class DOption implements Doclet.Option {97private final List<String> names = new ArrayList<>();98private final int argCount;99100DOption(String name, int argCount) {101this.names.add(name);102this.argCount = argCount;103}104105@Override106public int getArgumentCount() {107return argCount;108}109110@Override111public String getDescription() {112return "description[" + names.get(0) + "]";113}114115@Override116public Kind getKind() {117return Doclet.Option.Kind.STANDARD;118}119120@Override121public List<String> getNames() {122return names;123}124125@Override126public String getParameters() {127return argCount > 0 ? "parameters[" + names.get(0) + "," + argCount + "]" : null;128}129130@Override131public boolean process(String option, List<String> arguments) {132List<String> args = new ArrayList<>();133for (int i = 0; i < argCount && i < arguments.size(); i++) {134args.add(arguments.get(i));135}136System.out.println("process " + option + " " + args);137return args.stream().filter(s -> s.startsWith("arg")).count() == argCount;138}139}140141public static void main(String... args) throws Exception {142OptionSyntaxTest t = new OptionSyntaxTest();143t.runTests();144}145146private final ToolBox tb = new ToolBox();147private final Path src = Paths.get("src");148private final Path modules = Paths.get("modules");149150OptionSyntaxTest() throws IOException {151super(System.err);152initModules();153}154155void initModules() throws IOException {156new ModuleBuilder(tb, "m1")157.exports("p1")158.classes("package p1; public class C1 { }")159.write(src);160161new ModuleBuilder(tb, "m2")162.exports("p2")163.classes("package p2; public class C2 { }")164.build(modules);165166}167168@Test169public void testBasic() {170new JavadocTask(tb, Task.Mode.CMDLINE)171.options("-docletpath", System.getProperty("test.classes"),172"-doclet", TestDoclet.class.getName(),173"-sourcepath", "src/m1",174"p1")175.run()176.writeAll();177}178179@Test180public void testNewSourcePath() {181new JavadocTask(tb, Task.Mode.CMDLINE)182.options("-docletpath", System.getProperty("test.classes"),183"-doclet", TestDoclet.class.getName(),184"--source-path", "src/m1",185"p1")186.run()187.writeAll();188}189190@Test191public void testNewSourcePathEquals() {192new JavadocTask(tb, Task.Mode.CMDLINE)193.options("-docletpath", System.getProperty("test.classes"),194"-doclet", TestDoclet.class.getName(),195"--source-path=src/m1",196"p1")197.run()198.writeAll();199}200201@Test202public void testOldDocletArgs() {203new JavadocTask(tb, Task.Mode.CMDLINE)204.options("-docletpath", System.getProperty("test.classes"),205"-doclet", TestDoclet.class.getName(),206"-sourcepath", "src/m1",207"-old",208"-oldWithArg", "arg",209"-oldWithArgs", "arg1", "arg2",210"p1")211.run()212.writeAll();213}214215@Test216public void testNewDocletArgs() {217new JavadocTask(tb, Task.Mode.CMDLINE)218.options("-docletpath", System.getProperty("test.classes"),219"-doclet", TestDoclet.class.getName(),220"-sourcepath", "src/m1",221"--new",222"--newWithArg", "arg",223"--newWithArgs", "arg1", "arg2",224"p1")225.run()226.writeAll();227}228229@Test230public void testNewDocletArgsEquals() {231new JavadocTask(tb, Task.Mode.CMDLINE)232.options("-docletpath", System.getProperty("test.classes"),233"-doclet", TestDoclet.class.getName(),234"-sourcepath", "src/m1",235"--new", "--newWithArg=arg",236"p1")237.run()238.writeAll();239}240241@Test242public void testNewDocletArgsMissingArgs() throws Exception {243String log = new JavadocTask(tb, Task.Mode.CMDLINE)244.options("-docletpath", System.getProperty("test.classes"),245"-doclet", TestDoclet.class.getName(),246"-sourcepath", "src/m1",247"--newWithArg")248.run(Task.Expect.FAIL)249.writeAll()250.getOutput(Task.OutputKind.DIRECT);251if (!log.contains("option --newWithArg requires an argument"))252throw new Exception("expected output not found");253}254255@Test256public void testNewDocletArgsExtraArgs() throws Exception {257String log = new JavadocTask(tb, Task.Mode.CMDLINE)258.options("-docletpath", System.getProperty("test.classes"),259"-doclet", TestDoclet.class.getName(),260"-sourcepath", "src/m1",261"--new=arg",262"p1")263.run(Task.Expect.FAIL)264.writeAll()265.getOutput(Task.OutputKind.DIRECT);266if (!log.contains("option --new does not require an argument"))267throw new Exception("expected output not found");268}269270@Test271public void testNewDocletArgsExtraArgs2() throws Exception {272String log = new JavadocTask(tb, Task.Mode.CMDLINE)273.options("-docletpath", System.getProperty("test.classes"),274"-doclet", TestDoclet.class.getName(),275"-sourcepath", "src/m1",276"--newWithArgs=arg1 arg2",277"p1")278.run(Task.Expect.FAIL)279.writeAll()280.getOutput(Task.OutputKind.DIRECT);281if (!log.contains("cannot use '=' syntax for options that require multiple arguments"))282throw new Exception("expected output not found");283}284285}286287288