test/langtools/tools/javac/options/smokeTests/OptionSmokeTest.java
changeset 51182 b0fcf59be391
parent 49822 53aae0c219e6
child 52323 c306abfeae0d
equal deleted inserted replaced
51181:01b8120f867a 51182:b0fcf59be391
    25  * @test
    25  * @test
    26  * @bug 8196433
    26  * @bug 8196433
    27  * @summary use the new error diagnostic approach at javac.Main
    27  * @summary use the new error diagnostic approach at javac.Main
    28  * @library /tools/lib
    28  * @library /tools/lib
    29  * @modules jdk.compiler/com.sun.tools.javac.api
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.code
    30  *          jdk.compiler/com.sun.tools.javac.main
    31  *          jdk.compiler/com.sun.tools.javac.main
    31  *          jdk.compiler/com.sun.tools.javac.util
    32  *          jdk.compiler/com.sun.tools.javac.util
    32  *          jdk.jdeps/com.sun.tools.javap
    33  *          jdk.jdeps/com.sun.tools.javap
    33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
    34  * @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
    34  * @run main OptionSmokeTest
    35  * @run main OptionSmokeTest
    36 
    37 
    37 import java.nio.file.Path;
    38 import java.nio.file.Path;
    38 import java.nio.file.Paths;
    39 import java.nio.file.Paths;
    39 
    40 
    40 import com.sun.tools.javac.util.Assert;
    41 import com.sun.tools.javac.util.Assert;
       
    42 import com.sun.tools.javac.code.Source;
    41 
    43 
    42 import toolbox.TestRunner;
    44 import toolbox.TestRunner;
    43 import toolbox.ToolBox;
    45 import toolbox.ToolBox;
    44 import toolbox.JavacTask;
    46 import toolbox.JavacTask;
    45 import toolbox.Task;
    47 import toolbox.Task;
    63         new OptionSmokeTest().runTests();
    65         new OptionSmokeTest().runTests();
    64     }
    66     }
    65 
    67 
    66     @Test
    68     @Test
    67     public void optionA1(Path base) throws Exception {
    69     public void optionA1(Path base) throws Exception {
    68         doTest(base,
    70         doTest(base, "error: -A requires an argument; use '-Akey' or '-Akey=value'", "-A");
    69                 "error: -A requires an argument; use '-Akey' or '-Akey=value'",
       
    70                 "-A");
       
    71     }
    71     }
    72 
    72 
    73     @Test
    73     @Test
    74     public void optionA2(Path base) throws Exception {
    74     public void optionA2(Path base) throws Exception {
    75         doTest(base,
    75         doTest(base,
    83     }
    83     }
    84 
    84 
    85     @Test
    85     @Test
    86     public void profileAndBSP(Path base) throws Exception {
    86     public void profileAndBSP(Path base) throws Exception {
    87         doTest(base, "error: profile and bootclasspath options cannot be used together",
    87         doTest(base, "error: profile and bootclasspath options cannot be used together",
    88                 "-profile compact1 -bootclasspath . -target 8 -source 8");
    88                 String.format("-profile compact1 -bootclasspath . -target %s -source %s", Source.DEFAULT.name, Source.DEFAULT.name));
    89     }
    89     }
    90 
    90 
    91     @Test
    91     @Test
    92     public void invalidProfile(Path base) throws Exception {
    92     public void invalidProfile(Path base) throws Exception {
    93         doTest(base, "error: invalid profile: noProfile",
    93         doTest(base, "error: invalid profile: noProfile", "-profile noProfile");
    94                 "-profile noProfile");
       
    95     }
    94     }
    96 
    95 
    97     @Test
    96     @Test
    98     public void invalidTarget(Path base) throws Exception {
    97     public void invalidTarget(Path base) throws Exception {
    99         doTest(base, "error: invalid target release: 999999",
    98         doTest(base, "error: invalid target release: 999999", "-target 999999");
   100                 "-target 999999");
       
   101     }
    99     }
   102 
   100 
   103     @Test
   101     @Test
   104     public void optionNotAvailableWithTarget(Path base) throws Exception {
   102     public void optionNotAvailableWithTarget(Path base) throws Exception {
   105         doTest(base, "error: option -profile not allowed with target 11",
   103         doTest(base, String.format("error: option -profile not allowed with target %s", Source.DEFAULT.name),
   106                 "-profile compact1 -target 11");
   104                 String.format("-profile compact1 -target %s", Source.DEFAULT.name));
   107     }
   105     }
   108 
   106 
   109     @Test
   107     @Test
   110     public void optionTooMany(Path base) throws Exception {
   108     public void optionTooMany(Path base) throws Exception {
   111         doTest(base, "error: option --default-module-for-created-files can only be specified once",
   109         doTest(base, "error: option --default-module-for-created-files can only be specified once",
   112                 "--default-module-for-created-files=m1x --default-module-for-created-files=m1x");
   110                 "--default-module-for-created-files=m1x --default-module-for-created-files=m1x");
   113     }
   111     }
   114 
   112 
   115     @Test
   113     @Test
   116     public void noSrcFiles(Path base) throws Exception {
   114     public void noSrcFiles(Path base) throws Exception {
   117         doTestNoSource(base, "error: no source files", "-target 11");
   115         doTestNoSource(base, "error: no source files", String.format("-target %s", Source.DEFAULT.name));
   118     }
   116     }
   119 
   117 
   120     @Test
   118     @Test
   121     public void requiresArg(Path base) throws Exception {
   119     public void requiresArg(Path base) throws Exception {
   122         doTestNoSource(base, "error: -target requires an argument", "-target");
   120         doTestNoSource(base, "error: -target requires an argument", "-target");
   133                 "--source-path . --module-source-path .");
   131                 "--source-path . --module-source-path .");
   134     }
   132     }
   135 
   133 
   136     @Test
   134     @Test
   137     public void sourceAndTargetMismatch(Path base) throws Exception {
   135     public void sourceAndTargetMismatch(Path base) throws Exception {
   138         doTest(base, "warning: source release 11 requires target release 11",
   136         doTest(base, String.format("warning: source release %s requires target release %s", Source.DEFAULT.name, Source.DEFAULT.name),
   139                 "-source 11 -target 10");
   137                 String.format("-source %s -target %s", Source.DEFAULT.name, Source.MIN.name));
   140     }
   138     }
   141 
   139 
   142     @Test
   140     @Test
   143     public void targetConflictsWithDefaultSource(Path base) throws Exception {
   141     public void targetConflictsWithDefaultSource(Path base) throws Exception {
   144         doTest(base, "warning: target release 10 conflicts with default source release 11",
   142         doTest(base, String.format("warning: target release %s conflicts with default source release %s", Source.MIN.name, Source.DEFAULT.name),
   145                 "-target 10");
   143                 String.format("-target %s", Source.MIN.name));
   146     }
   144     }
   147 
   145 
   148     @Test
   146     @Test
   149     public void profileNotValidForTarget(Path base) throws Exception {
   147     public void profileNotValidForTarget(Path base) throws Exception {
   150         doTest(base, "warning: profile compact2 is not valid for target release 1.7",
   148         doTest(base, String.format("warning: profile compact2 is not valid for target release %s", Source.MIN.name),
   151                 "-profile compact2 -target 7 -source 7");
   149                 String.format("-profile compact2 -target %s -source %s", Source.MIN.name, Source.MIN.name));
   152     }
   150     }
   153 
   151 
   154     @Test
   152     @Test
   155     public void fileNotFound(Path base) throws Exception {
   153     public void fileNotFound(Path base) throws Exception {
   156         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   154         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   157                 .files("notExistent/T.java")
   155                 .files("notExistent/T.java")
   158                 .run(Task.Expect.FAIL)
   156                 .run(Task.Expect.FAIL)
   159                 .writeAll()
   157                 .writeAll()
   160                 .getOutput(Task.OutputKind.DIRECT);
   158                 .getOutput(Task.OutputKind.DIRECT);
   161         Assert.check(log.startsWith("error: file not found: notExistent" + fileSeparator + "T.java"),
   159         Assert.check(log.startsWith(String.format("error: file not found: notExistent%sT.java", fileSeparator)),
   162                 "real value of log:" + log);
   160                 String.format("real value of log:%s", log));
   163     }
   161     }
   164 
   162 
   165     static final String fileSeparator = System.getProperty("file.separator");
   163     static final String fileSeparator = System.getProperty("file.separator");
   166 
   164 
   167     @Test
   165     @Test
   168     public void notADirectory(Path base) throws Exception {
   166     public void notADirectory(Path base) throws Exception {
   169         doTest(base, "error: not a directory: notADirectory" + fileSeparator + "src" + fileSeparator + "Dummy.java",
   167         doTest(base, String.format("error: not a directory: notADirectory%ssrc%sDummy.java", fileSeparator, fileSeparator),
   170                 "-d notADirectory" + fileSeparator + "src" + fileSeparator + "Dummy.java");
   168                 String.format("-d notADirectory%ssrc%sDummy.java", fileSeparator, fileSeparator));
   171     }
   169     }
   172 
   170 
   173     @Test
   171     @Test
   174     public void notAFile(Path base) throws Exception {
   172     public void notAFile(Path base) throws Exception {
   175         // looks like a java file, it is a directory
   173         // looks like a java file, it is a directory
   178         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   176         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   179                 .spaceSeparatedOptions("-XDsourcefile " + dir)
   177                 .spaceSeparatedOptions("-XDsourcefile " + dir)
   180                 .run(Task.Expect.FAIL)
   178                 .run(Task.Expect.FAIL)
   181                 .writeAll()
   179                 .writeAll()
   182                 .getOutput(Task.OutputKind.DIRECT);
   180                 .getOutput(Task.OutputKind.DIRECT);
   183         Assert.check(log.startsWith("error: not a file: notAFile" + fileSeparator + "dir.java"));
   181         Assert.check(log.startsWith(String.format("error: not a file: notAFile%sdir.java", fileSeparator)));
   184     }
   182     }
   185 
   183 
   186     @Test
   184     @Test
   187     public void badValueForOption(Path base) throws Exception {
   185     public void badValueForOption(Path base) throws Exception {
   188         doTestNoSource(base, "error: bad value for --patch-module option: \'notExistent\'",
   186         doTestNoSource(base, "error: bad value for --patch-module option: \'notExistent\'",
   198     @Test
   196     @Test
   199     public void unmatchedQuoteInEnvVar(Path base) throws Exception {
   197     public void unmatchedQuoteInEnvVar(Path base) throws Exception {
   200         Path src = base.resolve("src");
   198         Path src = base.resolve("src");
   201         tb.writeJavaFiles(src, "class Dummy {}");
   199         tb.writeJavaFiles(src, "class Dummy {}");
   202         String log = new JavacTask(tb, Task.Mode.EXEC)
   200         String log = new JavacTask(tb, Task.Mode.EXEC)
   203                 .envVar("JDK_JAVAC_OPTIONS", "--add-exports jdk.compiler" + fileSeparator + "com.sun.tools.javac.jvm=\"ALL-UNNAMED")
   201                 .envVar("JDK_JAVAC_OPTIONS",
       
   202                         String.format("--add-exports jdk.compiler%scom.sun.tools.javac.jvm=\"ALL-UNNAMED", fileSeparator))
   204                 .files(findJavaFiles(src))
   203                 .files(findJavaFiles(src))
   205                 .run(Task.Expect.FAIL)
   204                 .run(Task.Expect.FAIL)
   206                 .writeAll()
   205                 .writeAll()
   207                 .getOutput(Task.OutputKind.STDERR);
   206                 .getOutput(Task.OutputKind.STDERR);
   208         Assert.check(log.startsWith("error: unmatched quote in environment variable JDK_JAVAC_OPTIONS"));
   207         Assert.check(log.startsWith("error: unmatched quote in environment variable JDK_JAVAC_OPTIONS"));
   209     }
   208     }
   210 
   209 
   211     @Test
   210     @Test
   212     public void optionCantBeUsedWithRelease(Path base) throws Exception {
   211     public void optionCantBeUsedWithRelease(Path base) throws Exception {
   213         doTestNoSource(base, "error: option -source cannot be used together with --release",
   212         doTestNoSource(base, "error: option -source cannot be used together with --release",
   214                 "--release 7 -source 7");
   213                 String.format("--release %s -source %s", Source.DEFAULT.name, Source.DEFAULT.name));
   215     }
   214     }
   216 
   215 
   217     @Test
   216     @Test
   218     public void releaseVersionNotSupported(Path base) throws Exception {
   217     public void releaseVersionNotSupported(Path base) throws Exception {
   219         doTestNoSource(base, "error: release version 99999999 not supported",
   218         doTestNoSource(base, "error: release version 99999999 not supported",
   222 
   221 
   223     // taken from former test: tools/javac/options/release/ReleaseOptionClashes
   222     // taken from former test: tools/javac/options/release/ReleaseOptionClashes
   224     @Test
   223     @Test
   225     public void releaseAndBootclasspath(Path base) throws Exception {
   224     public void releaseAndBootclasspath(Path base) throws Exception {
   226         doTestNoSource(base, "error: option --boot-class-path cannot be used together with --release",
   225         doTestNoSource(base, "error: option --boot-class-path cannot be used together with --release",
   227                 "--release 7 -bootclasspath any");
   226                 String.format("--release %s -bootclasspath any", Source.DEFAULT.name));
   228         doTestNoSource(base, "error: option -Xbootclasspath: cannot be used together with --release",
   227         doTestNoSource(base, "error: option -Xbootclasspath: cannot be used together with --release",
   229                 "--release 7 -Xbootclasspath:any");
   228                 String.format("--release %s -Xbootclasspath:any", Source.DEFAULT.name));
   230         doTestNoSource(base, "error: option -Xbootclasspath/p: cannot be used together with --release",
   229         doTestNoSource(base, "error: option -Xbootclasspath/p: cannot be used together with --release",
   231                 "--release 7 -Xbootclasspath/p:any");
   230                 String.format("--release %s -Xbootclasspath/p:any", Source.DEFAULT.name));
   232         doTestNoSource(base, "error: option -endorseddirs cannot be used together with --release",
   231         doTestNoSource(base, "error: option -endorseddirs cannot be used together with --release",
   233                 "--release 7 -endorseddirs any");
   232                 String.format("--release %s -endorseddirs any", Source.DEFAULT.name));
   234         doTestNoSource(base, "error: option -extdirs cannot be used together with --release",
   233         doTestNoSource(base, "error: option -extdirs cannot be used together with --release",
   235                 "--release 7 -extdirs any");
   234                 String.format("--release %s -extdirs any", Source.DEFAULT.name));
   236         doTestNoSource(base, "error: option -source cannot be used together with --release",
   235         doTestNoSource(base, "error: option -source cannot be used together with --release",
   237                 "--release 7 -source 8");
   236                 String.format("--release %s -source %s", Source.MIN.name, Source.DEFAULT.name));
   238         doTestNoSource(base, "error: option -target cannot be used together with --release",
   237         doTestNoSource(base, "error: option -target cannot be used together with --release",
   239                 "--release 7 -target 8");
   238                 String.format("--release %s -target %s", Source.MIN.name, Source.DEFAULT.name));
   240         doTestNoSource(base, "error: option --system cannot be used together with --release",
   239         doTestNoSource(base, "error: option --system cannot be used together with --release",
   241                 "--release 9 --system none");
   240                 String.format("--release %s --system none", Source.DEFAULT.name));
   242         doTestNoSource(base, "error: option --upgrade-module-path cannot be used together with --release",
   241         doTestNoSource(base, "error: option --upgrade-module-path cannot be used together with --release",
   243                 "--release 9 --upgrade-module-path any");
   242                 String.format("--release %s --upgrade-module-path any", Source.DEFAULT.name));
   244     }
   243     }
   245 
   244 
   246     void doTest(Path base, String output, String options) throws Exception {
   245     void doTest(Path base, String output, String options) throws Exception {
   247         Path src = base.resolve("src");
   246         Path src = base.resolve("src");
   248         tb.writeJavaFiles(src, "class Dummy { }");
   247         tb.writeJavaFiles(src, "class Dummy { }");
   250                 .spaceSeparatedOptions(options)
   249                 .spaceSeparatedOptions(options)
   251                 .files(findJavaFiles(src))
   250                 .files(findJavaFiles(src))
   252                 .run(Task.Expect.FAIL)
   251                 .run(Task.Expect.FAIL)
   253                 .writeAll()
   252                 .writeAll()
   254                 .getOutput(Task.OutputKind.DIRECT);
   253                 .getOutput(Task.OutputKind.DIRECT);
   255         Assert.check(log.startsWith(output), "expected:\n" + output + '\n' + "found:\n" + log);
   254         Assert.check(log.startsWith(output), String.format("expected:\n%s\nfound:\n%s", output, log));
   256     }
   255     }
   257 
   256 
   258     void doTestNoSource(Path base, String output, String options) throws Exception {
   257     void doTestNoSource(Path base, String output, String options) throws Exception {
   259         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   258         String log = new JavacTask(tb, Task.Mode.CMDLINE)
   260                 .spaceSeparatedOptions(options)
   259                 .spaceSeparatedOptions(options)
   261                 .run(Task.Expect.FAIL)
   260                 .run(Task.Expect.FAIL)
   262                 .writeAll()
   261                 .writeAll()
   263                 .getOutput(Task.OutputKind.DIRECT);
   262                 .getOutput(Task.OutputKind.DIRECT);
   264         Assert.check(log.startsWith(output), "expected:\n" + output + '\n' + "found:\n" + log);
   263         Assert.check(log.startsWith(output), String.format("expected:\n%s\nfound:\n%s", output, log));
   265     }
   264     }
   266 }
   265 }