langtools/test/tools/javac/modules/ModuleInfoTest.java
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
child 37758 3ecf9b414e05
equal deleted inserted replaced
36777:28d33fb9097f 36778:e04318f39f92
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @summary tests for modfule declarations
    26  * @summary tests for module declarations
    27  * @library /tools/lib
    27  * @library /tools/lib
    28  * @modules
    28  * @modules
    29  *      jdk.compiler/com.sun.tools.javac.api
    29  *      jdk.compiler/com.sun.tools.javac.api
    30  *      jdk.compiler/com.sun.tools.javac.main
    30  *      jdk.compiler/com.sun.tools.javac.main
    31  *      jdk.jdeps/com.sun.tools.javap
    31  *      jdk.jdeps/com.sun.tools.javap
    32  * @build ToolBox ModuleTestBase
    32  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
    33  * @run main ModuleInfoTest
    33  * @run main ModuleInfoTest
    34  */
    34  */
    35 
    35 
    36 import java.nio.file.Files;
    36 import java.nio.file.Files;
    37 import java.nio.file.Path;
    37 import java.nio.file.Path;
       
    38 
       
    39 import toolbox.JavacTask;
       
    40 import toolbox.Task;
       
    41 import toolbox.ToolBox;
    38 
    42 
    39 public class ModuleInfoTest extends ModuleTestBase {
    43 public class ModuleInfoTest extends ModuleTestBase {
    40 
    44 
    41     public static void main(String... args) throws Exception {
    45     public static void main(String... args) throws Exception {
    42         ModuleInfoTest t = new ModuleInfoTest();
    46         ModuleInfoTest t = new ModuleInfoTest();
    48      */
    52      */
    49     @Test
    53     @Test
    50     void testModuleDeclNotInModuleJava(Path base) throws Exception {
    54     void testModuleDeclNotInModuleJava(Path base) throws Exception {
    51         Path src = base.resolve("src");
    55         Path src = base.resolve("src");
    52         tb.writeFile(src.resolve("M.java"), "module M { }");
    56         tb.writeFile(src.resolve("M.java"), "module M { }");
    53         String log = tb.new JavacTask()
    57         String log = new JavacTask(tb)
    54                 .options("-XDrawDiagnostics")
    58                 .options("-XDrawDiagnostics")
    55                 .files(findJavaFiles(src))
    59                 .files(findJavaFiles(src))
    56                 .run(ToolBox.Expect.FAIL)
    60                 .run(Task.Expect.FAIL)
    57                 .writeAll()
    61                 .writeAll()
    58                 .getOutput(ToolBox.OutputKind.DIRECT);
    62                 .getOutput(Task.OutputKind.DIRECT);
    59 
    63 
    60         if (!log.contains("M.java:1:1: compiler.err.module.decl.sb.in.module-info.java"))
    64         if (!log.contains("M.java:1:1: compiler.err.module.decl.sb.in.module-info.java"))
    61             throw new Exception("expected output not found");
    65             throw new Exception("expected output not found");
    62     }
    66     }
    63 
    67 
    66      */
    70      */
    67     @Test
    71     @Test
    68     void testNotModuleDeclInModuleJava_1(Path base) throws Exception {
    72     void testNotModuleDeclInModuleJava_1(Path base) throws Exception {
    69         Path src = base.resolve("src");
    73         Path src = base.resolve("src");
    70         tb.writeFile(src.resolve("module-info.java"), "class C { }");
    74         tb.writeFile(src.resolve("module-info.java"), "class C { }");
    71         tb.new JavacTask()
    75         new JavacTask(tb)
    72                 .options("-XDrawDiagnostics")
    76                 .options("-XDrawDiagnostics")
    73                 .files(findJavaFiles(src))
    77                 .files(findJavaFiles(src))
    74                 .run()
    78                 .run()
    75                 .writeAll();
    79                 .writeAll();
    76     }
    80     }
    80      */
    84      */
    81     @Test
    85     @Test
    82     void testNotModuleDeclInModuleJava_2(Path base) throws Exception {
    86     void testNotModuleDeclInModuleJava_2(Path base) throws Exception {
    83         Path src = base.resolve("src");
    87         Path src = base.resolve("src");
    84         tb.writeFile(src.resolve("module-info.java"), "public class C { }");
    88         tb.writeFile(src.resolve("module-info.java"), "public class C { }");
    85         String log = tb.new JavacTask()
    89         String log = new JavacTask(tb)
    86                 .options("-XDrawDiagnostics")
    90                 .options("-XDrawDiagnostics")
    87                 .files(findJavaFiles(src))
    91                 .files(findJavaFiles(src))
    88                 .run(ToolBox.Expect.FAIL)
    92                 .run(Task.Expect.FAIL)
    89                 .writeAll()
    93                 .writeAll()
    90                 .getOutput(ToolBox.OutputKind.DIRECT);
    94                 .getOutput(Task.OutputKind.DIRECT);
    91 
    95 
    92         if (!log.contains("module-info.java:1:8: compiler.err.class.public.should.be.in.file: C"))
    96         if (!log.contains("module-info.java:1:8: compiler.err.class.public.should.be.in.file: C"))
    93             throw new Exception("expected output not found");
    97             throw new Exception("expected output not found");
    94     }
    98     }
    95 
    99 
    98      */
   102      */
    99     @Test
   103     @Test
   100     void testSingleModuleDecl(Path base) throws Exception {
   104     void testSingleModuleDecl(Path base) throws Exception {
   101         Path src = base.resolve("src");
   105         Path src = base.resolve("src");
   102         tb.writeJavaFiles(src, "module M1 { } /*...*/ module M2 { }");
   106         tb.writeJavaFiles(src, "module M1 { } /*...*/ module M2 { }");
   103         String log = tb.new JavacTask()
   107         String log = new JavacTask(tb)
   104                 .options("-XDrawDiagnostics")
   108                 .options("-XDrawDiagnostics")
   105                 .files(findJavaFiles(src))
   109                 .files(findJavaFiles(src))
   106                 .run(ToolBox.Expect.FAIL)
   110                 .run(Task.Expect.FAIL)
   107                 .writeAll()
   111                 .writeAll()
   108                 .getOutput(ToolBox.OutputKind.DIRECT);
   112                 .getOutput(Task.OutputKind.DIRECT);
   109 
   113 
   110         if (!log.contains("module-info.java:1:14: compiler.err.expected: token.end-of-input"))
   114         if (!log.contains("module-info.java:1:14: compiler.err.expected: token.end-of-input"))
   111             throw new Exception("expected output not found");
   115             throw new Exception("expected output not found");
   112     }
   116     }
   113 
   117 
   116      */
   120      */
   117     @Test
   121     @Test
   118     void testRequiresNotFound(Path base) throws Exception {
   122     void testRequiresNotFound(Path base) throws Exception {
   119         Path src = base.resolve("src");
   123         Path src = base.resolve("src");
   120         tb.writeJavaFiles(src, "module M1 { requires M2; }");
   124         tb.writeJavaFiles(src, "module M1 { requires M2; }");
   121         String log = tb.new JavacTask()
   125         String log = new JavacTask(tb)
   122                 .options("-XDrawDiagnostics")
   126                 .options("-XDrawDiagnostics")
   123                 .files(findJavaFiles(src))
   127                 .files(findJavaFiles(src))
   124                 .run(ToolBox.Expect.FAIL)
   128                 .run(Task.Expect.FAIL)
   125                 .writeAll()
   129                 .writeAll()
   126                 .getOutput(ToolBox.OutputKind.DIRECT);
   130                 .getOutput(Task.OutputKind.DIRECT);
   127 
   131 
   128         if (!log.contains("module-info.java:1:22: compiler.err.module.not.found: M2"))
   132         if (!log.contains("module-info.java:1:22: compiler.err.module.not.found: M2"))
   129             throw new Exception("expected output not found");
   133             throw new Exception("expected output not found");
   130     }
   134     }
   131 
   135 
   134      */
   138      */
   135     @Test
   139     @Test
   136     void testExportsNotFound(Path base) throws Exception {
   140     void testExportsNotFound(Path base) throws Exception {
   137         Path src = base.resolve("src");
   141         Path src = base.resolve("src");
   138         tb.writeJavaFiles(src, "module M1 { exports p to M2; }");
   142         tb.writeJavaFiles(src, "module M1 { exports p to M2; }");
   139         String log = tb.new JavacTask()
   143         String log = new JavacTask(tb)
   140                 .options("-XDrawDiagnostics")
   144                 .options("-XDrawDiagnostics")
   141                 .files(findJavaFiles(src))
   145                 .files(findJavaFiles(src))
   142                 .run(ToolBox.Expect.FAIL)
   146                 .run(Task.Expect.FAIL)
   143                 .writeAll()
   147                 .writeAll()
   144                 .getOutput(ToolBox.OutputKind.DIRECT);
   148                 .getOutput(Task.OutputKind.DIRECT);
   145 
   149 
   146         if (!log.contains("module-info.java:1:26: compiler.err.module.not.found: M2"))
   150         if (!log.contains("module-info.java:1:26: compiler.err.module.not.found: M2"))
   147             throw new Exception("expected output not found");
   151             throw new Exception("expected output not found");
   148     }
   152     }
   149 
   153 
   152      */
   156      */
   153     @Test
   157     @Test
   154     void testRequiresSelf(Path base) throws Exception {
   158     void testRequiresSelf(Path base) throws Exception {
   155         Path src = base.resolve("src");
   159         Path src = base.resolve("src");
   156         tb.writeJavaFiles(src, "module M { requires M; }");
   160         tb.writeJavaFiles(src, "module M { requires M; }");
   157         String log = tb.new JavacTask()
   161         String log = new JavacTask(tb)
   158                 .options("-XDrawDiagnostics")
   162                 .options("-XDrawDiagnostics")
   159                 .files(findJavaFiles(src))
   163                 .files(findJavaFiles(src))
   160                 .run(ToolBox.Expect.FAIL)
   164                 .run(Task.Expect.FAIL)
   161                 .writeAll()
   165                 .writeAll()
   162                 .getOutput(ToolBox.OutputKind.DIRECT);
   166                 .getOutput(Task.OutputKind.DIRECT);
   163 
   167 
   164         if (!log.contains("module-info.java:1:21: compiler.err.cyclic.requires: M"))
   168         if (!log.contains("module-info.java:1:21: compiler.err.cyclic.requires: M"))
   165             throw new Exception("expected output not found");
   169             throw new Exception("expected output not found");
   166     }
   170     }
   167 
   171 
   179         tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }");
   183         tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }");
   180 
   184 
   181         Path classes = base.resolve("classes");
   185         Path classes = base.resolve("classes");
   182         Files.createDirectories(classes);
   186         Files.createDirectories(classes);
   183 
   187 
   184         String log = tb.new JavacTask()
   188         String log = new JavacTask(tb)
   185                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   189                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   186                 .outdir(classes)
   190                 .outdir(classes)
   187                 .files(findJavaFiles(src))
   191                 .files(findJavaFiles(src))
   188                 .run(ToolBox.Expect.FAIL)
   192                 .run(Task.Expect.FAIL)
   189                 .writeAll()
   193                 .writeAll()
   190                 .getOutput(ToolBox.OutputKind.DIRECT);
   194                 .getOutput(Task.OutputKind.DIRECT);
   191 
   195 
   192         if (!log.contains("module-info.java:1:22: compiler.err.cyclic.requires: m3"))
   196         if (!log.contains("module-info.java:1:22: compiler.err.cyclic.requires: m3"))
   193             throw new Exception("expected output not found");
   197             throw new Exception("expected output not found");
   194     }
   198     }
   195 
   199 
   207         tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }");
   211         tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }");
   208 
   212 
   209         Path classes = base.resolve("classes");
   213         Path classes = base.resolve("classes");
   210         Files.createDirectories(classes);
   214         Files.createDirectories(classes);
   211 
   215 
   212         String log = tb.new JavacTask()
   216         String log = new JavacTask(tb)
   213                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   217                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   214                 .outdir(classes)
   218                 .outdir(classes)
   215                 .files(findJavaFiles(src))
   219                 .files(findJavaFiles(src))
   216                 .run(ToolBox.Expect.FAIL)
   220                 .run(Task.Expect.FAIL)
   217                 .writeAll()
   221                 .writeAll()
   218                 .getOutput(ToolBox.OutputKind.DIRECT);
   222                 .getOutput(Task.OutputKind.DIRECT);
   219 
   223 
   220         if (!log.contains("module-info.java:1:29: compiler.err.cyclic.requires: m3"))
   224         if (!log.contains("module-info.java:1:29: compiler.err.cyclic.requires: m3"))
   221             throw new Exception("expected output not found");
   225             throw new Exception("expected output not found");
   222     }
   226     }
   223 
   227 
   233         tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m1; requires m1; }");
   237         tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m1; requires m1; }");
   234 
   238 
   235         Path classes = base.resolve("classes");
   239         Path classes = base.resolve("classes");
   236         Files.createDirectories(classes);
   240         Files.createDirectories(classes);
   237 
   241 
   238         String log = tb.new JavacTask()
   242         String log = new JavacTask(tb)
   239                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   243                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   240                 .outdir(classes)
   244                 .outdir(classes)
   241                 .files(findJavaFiles(src))
   245                 .files(findJavaFiles(src))
   242                 .run(ToolBox.Expect.FAIL)
   246                 .run(Task.Expect.FAIL)
   243                 .writeAll()
   247                 .writeAll()
   244                 .getOutput(ToolBox.OutputKind.DIRECT);
   248                 .getOutput(Task.OutputKind.DIRECT);
   245 
   249 
   246         if (!log.contains("module-info.java:1:35: compiler.err.duplicate.requires: m1"))
   250         if (!log.contains("module-info.java:1:35: compiler.err.duplicate.requires: m1"))
   247             throw new Exception("expected output not found");
   251             throw new Exception("expected output not found");
   248     }
   252     }
   249 
   253 
   256         tb.writeJavaFiles(src, "module m1 { exports p; exports p; }");
   260         tb.writeJavaFiles(src, "module m1 { exports p; exports p; }");
   257 
   261 
   258         Path classes = base.resolve("classes");
   262         Path classes = base.resolve("classes");
   259         Files.createDirectories(classes);
   263         Files.createDirectories(classes);
   260 
   264 
   261         String log = tb.new JavacTask()
   265         String log = new JavacTask(tb)
   262                 .options("-XDrawDiagnostics")
   266                 .options("-XDrawDiagnostics")
   263                 .outdir(classes)
   267                 .outdir(classes)
   264                 .files(findJavaFiles(src))
   268                 .files(findJavaFiles(src))
   265                 .run(ToolBox.Expect.FAIL)
   269                 .run(Task.Expect.FAIL)
   266                 .writeAll()
   270                 .writeAll()
   267                 .getOutput(ToolBox.OutputKind.DIRECT);
   271                 .getOutput(Task.OutputKind.DIRECT);
   268 
   272 
   269         if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p"))
   273         if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p"))
   270             throw new Exception("expected output not found");
   274             throw new Exception("expected output not found");
   271     }
   275     }
   272 
   276 
   280         tb.writeJavaFiles(src.resolve("m2"), "module m2 { }");
   284         tb.writeJavaFiles(src.resolve("m2"), "module m2 { }");
   281 
   285 
   282         Path classes = base.resolve("classes");
   286         Path classes = base.resolve("classes");
   283         Files.createDirectories(classes);
   287         Files.createDirectories(classes);
   284 
   288 
   285         String log = tb.new JavacTask()
   289         String log = new JavacTask(tb)
   286                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   290                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   287                 .outdir(classes)
   291                 .outdir(classes)
   288                 .files(findJavaFiles(src))
   292                 .files(findJavaFiles(src))
   289                 .run(ToolBox.Expect.FAIL)
   293                 .run(Task.Expect.FAIL)
   290                 .writeAll()
   294                 .writeAll()
   291                 .getOutput(ToolBox.OutputKind.DIRECT);
   295                 .getOutput(Task.OutputKind.DIRECT);
   292 
   296 
   293         if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p"))
   297         if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p"))
   294             throw new Exception("expected output not found");
   298             throw new Exception("expected output not found");
   295     }
   299     }
   296 
   300 
   306         tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { exports p to m1, m1; }");
   310         tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { exports p to m1, m1; }");
   307 
   311 
   308         Path classes = base.resolve("classes");
   312         Path classes = base.resolve("classes");
   309         Files.createDirectories(classes);
   313         Files.createDirectories(classes);
   310 
   314 
   311         String log = tb.new JavacTask()
   315         String log = new JavacTask(tb)
   312                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   316                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
   313                 .outdir(classes)
   317                 .outdir(classes)
   314                 .files(findJavaFiles(src))
   318                 .files(findJavaFiles(src))
   315                 .run(ToolBox.Expect.FAIL)
   319                 .run(Task.Expect.FAIL)
   316                 .writeAll()
   320                 .writeAll()
   317                 .getOutput(ToolBox.OutputKind.DIRECT);
   321                 .getOutput(Task.OutputKind.DIRECT);
   318 
   322 
   319         if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1"))
   323         if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1"))
   320             throw new Exception("expected output not found");
   324             throw new Exception("expected output not found");
   321     }
   325     }
   322 }
   326 }