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