1 /*
   2  * Copyright (c) 2002, 2019, 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 8166144
  27  * @summary support new-style options
  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.ModuleBuilder toolbox.TestRunner toolbox.ToolBox
  34  * @build DOption
  35  * @run main OptionSyntaxTest
  36  */
  37 import java.io.IOException;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 import java.util.HashSet;
  42 import java.util.Locale;
  43 import java.util.Set;
  44 
  45 import javax.lang.model.SourceVersion;
  46 
  47 import jdk.javadoc.doclet.Doclet;
  48 import jdk.javadoc.doclet.DocletEnvironment;
  49 import jdk.javadoc.doclet.Reporter;
  50 
  51 import toolbox.JavadocTask;
  52 import toolbox.ModuleBuilder;
  53 import toolbox.Task;
  54 import toolbox.TestRunner;
  55 import toolbox.ToolBox;
  56 
  57 
  58 public class OptionSyntaxTest extends TestRunner {
  59     public static class TestDoclet implements Doclet {
  60         @Override
  61         public boolean run(DocletEnvironment root) {
  62             System.out.println("TestDoclet.run");
  63             return true;
  64         }
  65 
  66         @Override
  67         public String getName() {
  68             return "Test";
  69         }
  70 
  71         @Override
  72         public Set<Option> getSupportedOptions() {
  73             return options;
  74         }
  75 
  76         @Override
  77         public SourceVersion getSupportedSourceVersion() {
  78             return SourceVersion.latest();
  79         }
  80 
  81         @Override
  82         public void init(Locale locale, Reporter reporter) {
  83         }
  84 
  85         private final Set<Doclet.Option> options = new HashSet<>(Arrays.asList(
  86                 new DOption("-old", 0),
  87                 new DOption("-oldWithArg", 1),
  88                 new DOption("-oldWithArgs", 2),
  89                 new DOption("--new", 0),
  90                 new DOption("--newWithArg", 1),
  91                 new DOption("--newWithArgs", 2)
  92         ));
  93 
  94     }
  95 
  96 
  97     public static void main(String... args) throws Exception {
  98         OptionSyntaxTest t = new OptionSyntaxTest();
  99         t.runTests();
 100     }
 101 
 102     private final ToolBox tb = new ToolBox();
 103     private final Path src = Paths.get("src");
 104     private final Path modules = Paths.get("modules");
 105 
 106     OptionSyntaxTest() throws IOException {
 107         super(System.err);
 108         initModules();
 109     }
 110 
 111     void initModules() throws IOException {
 112         new ModuleBuilder(tb, "m1")
 113                 .exports("p1")
 114                 .classes("package p1; public class C1 { }")
 115                 .write(src);
 116 
 117         new ModuleBuilder(tb, "m2")
 118                 .exports("p2")
 119                 .classes("package p2; public class C2 { }")
 120                 .build(modules);
 121 
 122     }
 123 
 124     @Test
 125     public void testBasic() {
 126         new JavadocTask(tb, Task.Mode.CMDLINE)
 127                 .options("-docletpath", System.getProperty("test.classes"),
 128                        "-doclet", TestDoclet.class.getName(),
 129                        "-sourcepath", "src/m1",
 130                        "p1")
 131                 .run()
 132                 .writeAll();
 133     }
 134 
 135     @Test
 136     public void testNewSourcePath() {
 137         new JavadocTask(tb, Task.Mode.CMDLINE)
 138                 .options("-docletpath", System.getProperty("test.classes"),
 139                        "-doclet", TestDoclet.class.getName(),
 140                        "--source-path", "src/m1",
 141                        "p1")
 142                 .run()
 143                 .writeAll();
 144     }
 145 
 146     @Test
 147     public void testNewSourcePathEquals() {
 148         new JavadocTask(tb, Task.Mode.CMDLINE)
 149                 .options("-docletpath", System.getProperty("test.classes"),
 150                        "-doclet", TestDoclet.class.getName(),
 151                        "--source-path=src/m1",
 152                        "p1")
 153                 .run()
 154                 .writeAll();
 155     }
 156 
 157     @Test
 158     public void testOldDocletArgs() {
 159         new JavadocTask(tb, Task.Mode.CMDLINE)
 160                 .options("-docletpath", System.getProperty("test.classes"),
 161                        "-doclet", TestDoclet.class.getName(),
 162                        "-sourcepath", "src/m1",
 163                        "-old",
 164                        "-oldWithArg", "arg",
 165                        "-oldWithArgs", "arg1", "arg2",
 166                        "p1")
 167                 .run()
 168                 .writeAll();
 169     }
 170 
 171     @Test
 172     public void testNewDocletArgs() {
 173         new JavadocTask(tb, Task.Mode.CMDLINE)
 174                 .options("-docletpath", System.getProperty("test.classes"),
 175                        "-doclet", TestDoclet.class.getName(),
 176                        "-sourcepath", "src/m1",
 177                        "--new",
 178                        "--newWithArg", "arg",
 179                        "--newWithArgs", "arg1", "arg2",
 180                        "p1")
 181                 .run()
 182                 .writeAll();
 183     }
 184 
 185     @Test
 186     public void testNewDocletArgsEquals() {
 187         new JavadocTask(tb, Task.Mode.CMDLINE)
 188                 .options("-docletpath", System.getProperty("test.classes"),
 189                        "-doclet", TestDoclet.class.getName(),
 190                        "-sourcepath", "src/m1",
 191                        "--new", "--newWithArg=arg",
 192                        "p1")
 193                 .run()
 194                 .writeAll();
 195     }
 196 
 197     @Test
 198     public void testNewDocletArgsMissingArgs() throws Exception {
 199         String log = new JavadocTask(tb, Task.Mode.CMDLINE)
 200                 .options("-docletpath", System.getProperty("test.classes"),
 201                        "-doclet", TestDoclet.class.getName(),
 202                        "-sourcepath", "src/m1",
 203                        "--newWithArg")
 204                 .run(Task.Expect.FAIL)
 205                 .writeAll()
 206                 .getOutput(Task.OutputKind.DIRECT);
 207         if (!log.contains("option --newWithArg requires an argument"))
 208             throw new Exception("expected output not found");
 209     }
 210 
 211     @Test
 212     public void testNewDocletArgsExtraArgs() throws Exception {
 213         String log = new JavadocTask(tb, Task.Mode.CMDLINE)
 214                 .options("-docletpath", System.getProperty("test.classes"),
 215                        "-doclet", TestDoclet.class.getName(),
 216                        "-sourcepath", "src/m1",
 217                        "--new=arg",
 218                        "p1")
 219                 .run(Task.Expect.FAIL)
 220                 .writeAll()
 221                 .getOutput(Task.OutputKind.DIRECT);
 222         if (!log.contains("option --new does not require an argument"))
 223             throw new Exception("expected output not found");
 224     }
 225 
 226     @Test
 227     public void testNewDocletArgsExtraArgs2() throws Exception {
 228         String log = new JavadocTask(tb, Task.Mode.CMDLINE)
 229                 .options("-docletpath", System.getProperty("test.classes"),
 230                        "-doclet", TestDoclet.class.getName(),
 231                        "-sourcepath", "src/m1",
 232                        "--newWithArgs=arg1 arg2",
 233                        "p1")
 234                 .run(Task.Expect.FAIL)
 235                 .writeAll()
 236                 .getOutput(Task.OutputKind.DIRECT);
 237         if (!log.contains("cannot use '=' syntax for options that require multiple arguments"))
 238             throw new Exception("expected output not found");
 239     }
 240 
 241 }