langtools/test/tools/javac/modules/ModuleInfoTest.java
changeset 38916 d118cb2cffab
parent 37758 3ecf9b414e05
child 40308 274367a99f98
equal deleted inserted replaced
38915:12b5c35d4fd7 38916:d118cb2cffab
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
       
    26  * @bug 8158123
    26  * @summary tests for module declarations
    27  * @summary tests for module declarations
    27  * @library /tools/lib
    28  * @library /tools/lib
    28  * @modules
    29  * @modules
    29  *      jdk.compiler/com.sun.tools.javac.api
    30  *      jdk.compiler/com.sun.tools.javac.api
    30  *      jdk.compiler/com.sun.tools.javac.main
    31  *      jdk.compiler/com.sun.tools.javac.main
    33  * @run main ModuleInfoTest
    34  * @run main ModuleInfoTest
    34  */
    35  */
    35 
    36 
    36 import java.nio.file.Files;
    37 import java.nio.file.Files;
    37 import java.nio.file.Path;
    38 import java.nio.file.Path;
       
    39 import java.util.Arrays;
    38 
    40 
    39 import toolbox.JavacTask;
    41 import toolbox.JavacTask;
    40 import toolbox.Task;
    42 import toolbox.Task;
    41 import toolbox.ToolBox;
    43 import toolbox.ToolBox;
    42 
    44 
   321                 .getOutput(Task.OutputKind.DIRECT);
   323                 .getOutput(Task.OutputKind.DIRECT);
   322 
   324 
   323         if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1"))
   325         if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1"))
   324             throw new Exception("expected output not found");
   326             throw new Exception("expected output not found");
   325     }
   327     }
       
   328 
       
   329     /**
       
   330      * Verify that annotations are not permitted at
       
   331      * any of the module names or the package names.
       
   332      */
       
   333     @Test
       
   334     public void testAnnotations(Path base) throws Exception {
       
   335         Path src = base.resolve("src");
       
   336         Path src_m1 = src.resolve("m1.sub");
       
   337         Path classes = base.resolve("classes");
       
   338         Files.createDirectories(classes);
       
   339 
       
   340         String code = "module @m1.@sub { " +
       
   341                 "requires @p1.@p2; " +
       
   342                 "exports @p1.@p2; " +
       
   343                 "exports @p1.@p2 to @m2.@sub; " +
       
   344                 "exports @p1.@p2 to @m2.@sub, @m3.@sub; " +
       
   345                 "uses @p1.@Interface; " +
       
   346                 "provides @p1.@Interface with @p2.@Concrete; " +
       
   347                 "}";
       
   348         String[] splittedCode = code.split("@");
       
   349         int length = splittedCode.length;
       
   350         String anno = "@Anno ";
       
   351 
       
   352         for (int i = 1; i < length; i++) {
       
   353             String preAnno = String.join("", Arrays.copyOfRange(splittedCode, 0, i));
       
   354             String postAnno = String.join("", Arrays.copyOfRange(splittedCode, i, length));
       
   355             String moduleInfo = preAnno + anno + postAnno;
       
   356             tb.writeFile(src_m1.resolve("module-info.java"), moduleInfo);
       
   357 
       
   358             String log = new JavacTask(tb)
       
   359                     .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
       
   360                     .outdir(classes)
       
   361                     .files(findJavaFiles(src))
       
   362                     .run(Task.Expect.FAIL)
       
   363                     .writeAll()
       
   364                     .getOutput(Task.OutputKind.DIRECT);
       
   365 
       
   366             if (!log.matches("(?s)^module\\-info\\.java:\\d+:\\d+: compiler\\.err\\.expected: token\\.identifier.*"))
       
   367                 throw new Exception("expected output not found");
       
   368         }
       
   369     }
   326 }
   370 }