< prev index next >

test/langtools/tools/javac/launcher/SourceLauncherTest.java

Print this page




 275 
 276     /*
 277      * Negative tests: such as cannot find or execute main method.
 278      */
 279 
 280     @Test
 281     public void testHelloWorldWithShebangJava(Path base) throws IOException {
 282         tb.writeJavaFiles(base,
 283             "#!/usr/bin/java --source " + thisVersion + "\n" +
 284             "import java.util.Arrays;\n" +
 285             "class HelloWorld {\n" +
 286             "    public static void main(String... args) {\n" +
 287             "        System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
 288             "    }\n" +
 289             "}");
 290         Path file = base.resolve("HelloWorld.java");
 291         testError(file,
 292             file + ":1: error: illegal character: '#'\n" +
 293             "#!/usr/bin/java --source " + thisVersion + "\n" +
 294             "^\n" +
 295             file + ":1: error: class, interface, or enum expected\n" +
 296             "#!/usr/bin/java --source " + thisVersion + "\n" +
 297             "  ^\n" +
 298             "2 errors\n",
 299             "error: compilation failed");
 300     }
 301 
 302     @Test
 303     public void testNoClass(Path base) throws IOException {
 304         Files.createDirectories(base);
 305         Path file = base.resolve("NoClass.java");
 306         Files.write(file, List.of("package p;"));
 307         testError(file, "", "error: no class declared in source file");
 308     }
 309 
 310     @Test
 311     public void testLoadClass(Path base) throws IOException {
 312         Path src1 = base.resolve("src1");
 313         Path file1 = src1.resolve("LoadClass.java");
 314         tb.writeJavaFiles(src1,
 315                 "class LoadClass {\n"


 481     // For any source file that is invoked through the OS shebang mechanism, invalid shebang
 482     // lines will be caught and handled by the OS, before the launcher is even invoked.
 483     // However, if such a file is passed directly to the launcher, perhaps using the --source
 484     // option, a well-formed shebang line will be removed but a badly-formed one will be not be
 485     // removed and will cause compilation errors.
 486     @Test
 487     public void testBadShebang(Path base) throws IOException {
 488         tb.writeJavaFiles(base,
 489             "#/usr/bin/java --source " + thisVersion + "\n" +
 490             "import java.util.Arrays;\n" +
 491             "class HelloWorld {\n" +
 492             "    public static void main(String... args) {\n" +
 493             "        System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
 494             "    }\n" +
 495             "}");
 496         Path file = base.resolve("HelloWorld.java");
 497         testError(file,
 498             file + ":1: error: illegal character: '#'\n" +
 499             "#/usr/bin/java --source " + thisVersion + "\n" +
 500             "^\n" +
 501             file + ":1: error: class, interface, or enum expected\n" +
 502             "#/usr/bin/java --source " + thisVersion + "\n" +
 503             "  ^\n" +
 504             "2 errors\n",
 505             "error: compilation failed");
 506     }
 507 
 508     @Test
 509     public void testBadSourceOpt(Path base) throws IOException {
 510         Files.createDirectories(base);
 511         Path file = base.resolve("DummyClass.java");
 512         Files.write(file, List.of("class DummyClass { }"));
 513         Properties sysProps = System.getProperties();
 514         Properties p = new Properties(sysProps);
 515         p.setProperty("jdk.internal.javac.source", "<BAD>");
 516         System.setProperties(p);
 517         try {
 518             testError(file, "", "error: invalid value for --source option: <BAD>");
 519         } finally {
 520             System.setProperties(sysProps);
 521         }




 275 
 276     /*
 277      * Negative tests: such as cannot find or execute main method.
 278      */
 279 
 280     @Test
 281     public void testHelloWorldWithShebangJava(Path base) throws IOException {
 282         tb.writeJavaFiles(base,
 283             "#!/usr/bin/java --source " + thisVersion + "\n" +
 284             "import java.util.Arrays;\n" +
 285             "class HelloWorld {\n" +
 286             "    public static void main(String... args) {\n" +
 287             "        System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
 288             "    }\n" +
 289             "}");
 290         Path file = base.resolve("HelloWorld.java");
 291         testError(file,
 292             file + ":1: error: illegal character: '#'\n" +
 293             "#!/usr/bin/java --source " + thisVersion + "\n" +
 294             "^\n" +
 295             file + ":1: error: class, interface, enum, or record expected\n" +
 296             "#!/usr/bin/java --source " + thisVersion + "\n" +
 297             "  ^\n" +
 298             "2 errors\n",
 299             "error: compilation failed");
 300     }
 301 
 302     @Test
 303     public void testNoClass(Path base) throws IOException {
 304         Files.createDirectories(base);
 305         Path file = base.resolve("NoClass.java");
 306         Files.write(file, List.of("package p;"));
 307         testError(file, "", "error: no class declared in source file");
 308     }
 309 
 310     @Test
 311     public void testLoadClass(Path base) throws IOException {
 312         Path src1 = base.resolve("src1");
 313         Path file1 = src1.resolve("LoadClass.java");
 314         tb.writeJavaFiles(src1,
 315                 "class LoadClass {\n"


 481     // For any source file that is invoked through the OS shebang mechanism, invalid shebang
 482     // lines will be caught and handled by the OS, before the launcher is even invoked.
 483     // However, if such a file is passed directly to the launcher, perhaps using the --source
 484     // option, a well-formed shebang line will be removed but a badly-formed one will be not be
 485     // removed and will cause compilation errors.
 486     @Test
 487     public void testBadShebang(Path base) throws IOException {
 488         tb.writeJavaFiles(base,
 489             "#/usr/bin/java --source " + thisVersion + "\n" +
 490             "import java.util.Arrays;\n" +
 491             "class HelloWorld {\n" +
 492             "    public static void main(String... args) {\n" +
 493             "        System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
 494             "    }\n" +
 495             "}");
 496         Path file = base.resolve("HelloWorld.java");
 497         testError(file,
 498             file + ":1: error: illegal character: '#'\n" +
 499             "#/usr/bin/java --source " + thisVersion + "\n" +
 500             "^\n" +
 501             file + ":1: error: class, interface, enum, or record expected\n" +
 502             "#/usr/bin/java --source " + thisVersion + "\n" +
 503             "  ^\n" +
 504             "2 errors\n",
 505             "error: compilation failed");
 506     }
 507 
 508     @Test
 509     public void testBadSourceOpt(Path base) throws IOException {
 510         Files.createDirectories(base);
 511         Path file = base.resolve("DummyClass.java");
 512         Files.write(file, List.of("class DummyClass { }"));
 513         Properties sysProps = System.getProperties();
 514         Properties p = new Properties(sysProps);
 515         p.setProperty("jdk.internal.javac.source", "<BAD>");
 516         System.setProperties(p);
 517         try {
 518             testError(file, "", "error: invalid value for --source option: <BAD>");
 519         } finally {
 520             System.setProperties(sysProps);
 521         }


< prev index next >