langtools/test/tools/javac/modules/AutomaticModules.java
changeset 45682 fc3b228b9e2a
parent 44576 9e18c9ce29e7
equal deleted inserted replaced
45600:6589d4088eaa 45682:fc3b228b9e2a
    32  *      jdk.compiler/com.sun.tools.javac.main
    32  *      jdk.compiler/com.sun.tools.javac.main
    33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask ModuleTestBase
    33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask ModuleTestBase
    34  * @run main AutomaticModules
    34  * @run main AutomaticModules
    35  */
    35  */
    36 
    36 
       
    37 import java.io.File;
    37 import java.nio.file.Files;
    38 import java.nio.file.Files;
    38 import java.nio.file.Path;
    39 import java.nio.file.Path;
    39 import java.util.Arrays;
    40 import java.util.Arrays;
    40 import java.util.List;
    41 import java.util.List;
       
    42 import java.util.zip.ZipEntry;
       
    43 import java.util.zip.ZipOutputStream;
    41 
    44 
    42 import toolbox.JarTask;
    45 import toolbox.JarTask;
    43 import toolbox.JavacTask;
    46 import toolbox.JavacTask;
    44 import toolbox.Task;
    47 import toolbox.Task;
       
    48 import toolbox.Task.Mode;
    45 
    49 
    46 public class AutomaticModules extends ModuleTestBase {
    50 public class AutomaticModules extends ModuleTestBase {
    47 
    51 
    48     public static void main(String... args) throws Exception {
    52     public static void main(String... args) throws Exception {
    49         AutomaticModules t = new AutomaticModules();
    53         AutomaticModules t = new AutomaticModules();
   612         if (!expected.equals(log)) {
   616         if (!expected.equals(log)) {
   613             throw new Exception("expected output not found: " + log);
   617             throw new Exception("expected output not found: " + log);
   614         }
   618         }
   615     }
   619     }
   616 
   620 
       
   621     @Test
       
   622     public void testAutomaticModuleNameCorrect(Path base) throws Exception {
       
   623         Path modulePath = base.resolve("module-path");
       
   624 
       
   625         Files.createDirectories(modulePath);
       
   626 
       
   627         Path automaticSrc = base.resolve("automaticSrc");
       
   628         tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
       
   629         Path automaticClasses = base.resolve("automaticClasses");
       
   630         tb.createDirectories(automaticClasses);
       
   631 
       
   632         String automaticLog = new JavacTask(tb)
       
   633                                 .outdir(automaticClasses)
       
   634                                 .files(findJavaFiles(automaticSrc))
       
   635                                 .run()
       
   636                                 .writeAll()
       
   637                                 .getOutput(Task.OutputKind.DIRECT);
       
   638 
       
   639         if (!automaticLog.isEmpty())
       
   640             throw new Exception("expected output not found: " + automaticLog);
       
   641 
       
   642         Path automaticJar = modulePath.resolve("automatic-1.0.jar");
       
   643 
       
   644         new JarTask(tb, automaticJar)
       
   645           .baseDir(automaticClasses)
       
   646           .files("api/Api.class")
       
   647           .manifest("Automatic-Module-Name: custom.module.name\n\n")
       
   648           .run();
       
   649 
       
   650         Path src = base.resolve("src");
       
   651 
       
   652         tb.writeJavaFiles(src,
       
   653                           "module m { requires custom.module.name; }",
       
   654                           "package impl; public class Impl { api.Api a; }");
       
   655 
       
   656         Path classes = base.resolve("classes");
       
   657 
       
   658         Files.createDirectories(classes);
       
   659 
       
   660         new JavacTask(tb)
       
   661                 .options("--module-path", modulePath.toString(),
       
   662                          "-XDrawDiagnostics")
       
   663                 .outdir(classes)
       
   664                 .files(findJavaFiles(src))
       
   665                 .run(Task.Expect.SUCCESS)
       
   666                 .writeAll()
       
   667                 .getOutputLines(Task.OutputKind.DIRECT);
       
   668 
       
   669         tb.writeJavaFiles(src,
       
   670                           "module m { requires automatic; }");
       
   671 
       
   672         List<String> log = new JavacTask(tb)
       
   673                 .options("--module-path", modulePath.toString(),
       
   674                          "-XDrawDiagnostics")
       
   675                 .outdir(classes)
       
   676                 .files(findJavaFiles(src))
       
   677                 .run(Task.Expect.FAIL)
       
   678                 .writeAll()
       
   679                 .getOutputLines(Task.OutputKind.DIRECT);
       
   680 
       
   681         List<String> expected =
       
   682                 Arrays.asList("module-info.java:1:21: compiler.err.module.not.found: automatic",
       
   683                               "1 error");
       
   684 
       
   685         if (!expected.equals(log)) {
       
   686             throw new Exception("expected output not found: " + log);
       
   687         }
       
   688     }
       
   689 
       
   690     @Test
       
   691     public void testAutomaticModuleNameIncorrect(Path base) throws Exception {
       
   692         for (String name : new String[] {"", "999", "foo.class", "foo._"}) {
       
   693             if (Files.isDirectory(base)) {
       
   694                 tb.cleanDirectory(base);
       
   695             }
       
   696             Path modulePath = base.resolve("module-path");
       
   697 
       
   698             Files.createDirectories(modulePath);
       
   699 
       
   700             Path automaticSrc = base.resolve("automaticSrc");
       
   701             tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
       
   702             Path automaticClasses = base.resolve("automaticClasses");
       
   703             tb.createDirectories(automaticClasses);
       
   704 
       
   705             String automaticLog = new JavacTask(tb)
       
   706                                     .outdir(automaticClasses)
       
   707                                     .files(findJavaFiles(automaticSrc))
       
   708                                     .run()
       
   709                                     .writeAll()
       
   710                                     .getOutput(Task.OutputKind.DIRECT);
       
   711 
       
   712             if (!automaticLog.isEmpty())
       
   713                 throw new Exception("expected output not found: " + automaticLog);
       
   714 
       
   715             Path automaticJar = modulePath.resolve("automatic-1.0.jar");
       
   716 
       
   717             new JarTask(tb, automaticJar)
       
   718               .baseDir(automaticClasses)
       
   719               .files("api/Api.class")
       
   720               .manifest("Automatic-Module-Name: " + name + "\n\n")
       
   721               .run();
       
   722 
       
   723             Path src = base.resolve("src");
       
   724 
       
   725             tb.writeJavaFiles(src,
       
   726                               "package impl; public class Impl { api.Api a; }");
       
   727 
       
   728             Path classes = base.resolve("classes");
       
   729 
       
   730             Files.createDirectories(classes);
       
   731 
       
   732             List<String> log = new JavacTask(tb, Mode.CMDLINE)
       
   733                     .options("--module-path", modulePath.toString(),
       
   734                              "--add-modules", "ALL-MODULE-PATH",
       
   735                              "-XDrawDiagnostics")
       
   736                     .outdir(classes)
       
   737                     .files(findJavaFiles(src))
       
   738                     .run(Task.Expect.FAIL)
       
   739                     .writeAll()
       
   740                     .getOutputLines(Task.OutputKind.DIRECT);
       
   741 
       
   742             List<String> expected =
       
   743                     Arrays.asList("- compiler.err.locn.cant.get.module.name.for.jar: " +
       
   744                                       "testAutomaticModuleNameIncorrect/module-path/automatic-1.0.jar".replace("/", File.separator),
       
   745                                   "1 error");
       
   746 
       
   747             if (!expected.equals(log)) {
       
   748                 throw new Exception("expected output not found: " + log);
       
   749             }
       
   750         }
       
   751     }
       
   752 
       
   753     @Test
       
   754     public void testAutomaticModuleNameBroken(Path base) throws Exception {
       
   755         Path modulePath = base.resolve("module-path");
       
   756 
       
   757         Files.createDirectories(modulePath);
       
   758 
       
   759         Path automaticSrc = base.resolve("automaticSrc");
       
   760         tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
       
   761         Path automaticClasses = base.resolve("automaticClasses");
       
   762         tb.createDirectories(automaticClasses);
       
   763 
       
   764         String automaticLog = new JavacTask(tb)
       
   765                                 .outdir(automaticClasses)
       
   766                                 .files(findJavaFiles(automaticSrc))
       
   767                                 .run()
       
   768                                 .writeAll()
       
   769                                 .getOutput(Task.OutputKind.DIRECT);
       
   770 
       
   771         if (!automaticLog.isEmpty())
       
   772             throw new Exception("expected output not found: " + automaticLog);
       
   773 
       
   774         Path automaticJar = modulePath.resolve("automatic-1.0.jar");
       
   775 
       
   776         try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(automaticJar))) {
       
   777             out.putNextEntry(new ZipEntry("api/Api.class"));
       
   778             Files.copy(automaticClasses.resolve("api").resolve("Api.class"), out);
       
   779         }
       
   780 
       
   781         Path src = base.resolve("src");
       
   782 
       
   783         tb.writeJavaFiles(src,
       
   784                           "module m { requires automatic; }",
       
   785                           "package impl; public class Impl { api.Api a; }");
       
   786 
       
   787         Path classes = base.resolve("classes");
       
   788 
       
   789         Files.createDirectories(classes);
       
   790 
       
   791         new JavacTask(tb)
       
   792                 .options("--module-path", modulePath.toString(),
       
   793                          "-XDrawDiagnostics")
       
   794                 .outdir(classes)
       
   795                 .files(findJavaFiles(src))
       
   796                 .run(Task.Expect.SUCCESS)
       
   797                 .writeAll()
       
   798                 .getOutputLines(Task.OutputKind.DIRECT);
       
   799 
       
   800         tb.writeJavaFiles(src,
       
   801                           "module m { requires custom.module.name; }");
       
   802 
       
   803         List<String> log = new JavacTask(tb)
       
   804                 .options("--module-path", modulePath.toString(),
       
   805                          "-XDrawDiagnostics")
       
   806                 .outdir(classes)
       
   807                 .files(findJavaFiles(src))
       
   808                 .run(Task.Expect.FAIL)
       
   809                 .writeAll()
       
   810                 .getOutputLines(Task.OutputKind.DIRECT);
       
   811 
       
   812         List<String> expected =
       
   813                 Arrays.asList("module-info.java:1:34: compiler.err.module.not.found: custom.module.name",
       
   814                               "1 error");
       
   815 
       
   816         if (!expected.equals(log)) {
       
   817             throw new Exception("expected output not found: " + log);
       
   818         }
       
   819     }
       
   820 
   617 }
   821 }