langtools/test/tools/javac/file/MultiReleaseJar/MutliReleaseModuleInfoTest.java
changeset 41155 93dad90c22c1
child 45688 abb7c4afc7a8
equal deleted inserted replaced
41154:ed8625a2f0f1 41155:93dad90c22c1
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8156568
       
    27  * @summary Update javac to support compiling against a modular multi-release JAR
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
       
    32  * @run main MutliReleaseModuleInfoTest
       
    33  */
       
    34 
       
    35 import java.nio.file.Paths;
       
    36 import java.util.Set;
       
    37 import javax.annotation.processing.AbstractProcessor;
       
    38 import javax.annotation.processing.RoundEnvironment;
       
    39 import javax.annotation.processing.SupportedAnnotationTypes;
       
    40 import javax.lang.model.SourceVersion;
       
    41 import javax.lang.model.element.ModuleElement;
       
    42 import javax.lang.model.element.ModuleElement.RequiresDirective;
       
    43 import javax.lang.model.element.TypeElement;
       
    44 import javax.lang.model.util.ElementFilter;
       
    45 
       
    46 import toolbox.JarTask;
       
    47 import toolbox.JavacTask;
       
    48 import toolbox.Task;
       
    49 import toolbox.ToolBox;
       
    50 
       
    51 
       
    52 public class MutliReleaseModuleInfoTest {
       
    53 
       
    54     private final String service_mi =
       
    55             "module service {\n" +
       
    56             "}\n";
       
    57 
       
    58     private final String service =
       
    59             "package service;\n" +
       
    60             "public class Service {\n" +
       
    61             "}\n";
       
    62 
       
    63     private final String service_mi9 =
       
    64             "module service {\n" +
       
    65             "    requires java.desktop;\n" +
       
    66             "}\n";
       
    67 
       
    68     private final String service9 =
       
    69             "package service;\n" +
       
    70             "public class Service {\n" +
       
    71             "}\n";
       
    72 
       
    73     private final String client_mi =
       
    74             "module client {\n" +
       
    75             "    requires service;\n" +
       
    76             "}\n";
       
    77 
       
    78     private final String manifest =
       
    79         "Manifest-Version: 1.0\n" +
       
    80         "Multi-Release: true\n";
       
    81 
       
    82     private final ToolBox tb = new ToolBox();
       
    83 
       
    84     public static void main(String [] args) throws Exception {
       
    85         new MutliReleaseModuleInfoTest().runTest();
       
    86     }
       
    87 
       
    88     private void runTest() throws Exception {
       
    89         tb.createDirectories("classes", "classes/META-INF/versions/9");
       
    90         new JavacTask(tb)
       
    91                 .outdir("classes")
       
    92                 .sources(service_mi, service)
       
    93                 .run();
       
    94         new JavacTask(tb)
       
    95                 .outdir("classes/META-INF/versions/9")
       
    96                 .sources(service_mi9, service9)
       
    97                 .run();
       
    98         new JarTask(tb, "multi-release.jar")
       
    99                 .manifest(manifest)
       
   100                 .baseDir("classes")
       
   101                 .files("module-info.class", "service/Service.class",
       
   102                        "META-INF/versions/9/module-info.class",
       
   103                        "META-INF/versions/9/service/Service.class")
       
   104                 .run();
       
   105         tb.cleanDirectory(Paths.get("classes"));
       
   106 
       
   107         tb.writeFile("module-info.java", client_mi);
       
   108         Task.Result result = new JavacTask(tb)
       
   109                 .outdir("classes")
       
   110                 .options("-processor", VerifyRequires.class.getName(),
       
   111                                "--module-path", "multi-release.jar")
       
   112                 .files("module-info.java")
       
   113                 .run();
       
   114         result.writeAll();
       
   115         tb.deleteFiles("module-info.java");
       
   116 
       
   117         tb.deleteFiles(
       
   118                 "multi-release.jar",
       
   119                 "classes/module-info.class",
       
   120                 "classes"
       
   121         );
       
   122     }
       
   123 
       
   124     @SupportedAnnotationTypes("*")
       
   125     public static final class VerifyRequires extends AbstractProcessor {
       
   126 
       
   127         @Override
       
   128         public boolean process(Set<? extends TypeElement> u, RoundEnvironment r) {
       
   129             ModuleElement sm = processingEnv.getElementUtils().getModuleElement("service");
       
   130             if (sm == null) {
       
   131                 throw new AssertionError("Cannot find the service module!");
       
   132             }
       
   133             boolean foundjd = false;
       
   134             for (RequiresDirective rd : ElementFilter.requiresIn(sm.getDirectives())) {
       
   135                 foundjd |= rd.getDependency().getSimpleName().contentEquals("java.desktop");
       
   136             }
       
   137             if (!foundjd) {
       
   138                 throw new AssertionError("Missing dependency on java desktop module!");
       
   139             }
       
   140             return false;
       
   141         }
       
   142 
       
   143         @Override
       
   144         public SourceVersion getSupportedSourceVersion() {
       
   145             return SourceVersion.latest();
       
   146         }
       
   147     }
       
   148 }