test/hotspot/jtreg/runtime/appcds/MultiReleaseJars.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     1 /*
       
     2  * Copyright (c) 2016, 2018, 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 /*
       
    26  * @test MultiReleaseJars
       
    27  * @summary Test multi-release jar with AppCDS.
       
    28  * @requires vm.cds
       
    29  * @library /test/lib
       
    30  * @modules java.base/jdk.internal.misc
       
    31  *          jdk.jartool/sun.tools.jar
       
    32  * @run main/othervm/timeout=2400 MultiReleaseJars
       
    33  */
       
    34 
       
    35 import java.io.File;
       
    36 import java.io.FileOutputStream;
       
    37 import java.io.PrintStream;
       
    38 import java.io.IOException;
       
    39 import jdk.test.lib.process.OutputAnalyzer;
       
    40 
       
    41 public class MultiReleaseJars {
       
    42 
       
    43     static final int MAJOR_VERSION = Runtime.version().major();
       
    44     static final String MAJOR_VERSION_STRING = String.valueOf(MAJOR_VERSION);
       
    45 
       
    46     static String[] getMain() {
       
    47         String[] sts = {
       
    48             "package version;",
       
    49             "public class Main {",
       
    50             "    public static void main(String[] args) {",
       
    51             "        Version version = new Version();",
       
    52             "        System.out.println(\"I am running on version \" + version.getVersion());",
       
    53             "    }",
       
    54             "}"
       
    55         };
       
    56         return sts;
       
    57     }
       
    58 
       
    59     static String[] getVersion(int version) {
       
    60         String[] sts = {
       
    61             "package version;",
       
    62             "public class Version {",
       
    63             "    public int getVersion(){ return " + version + "; }",
       
    64             "}"
       
    65         };
       
    66         return sts;
       
    67     }
       
    68 
       
    69     static void writeFile(File file, String... contents) throws Exception {
       
    70         if (contents == null) {
       
    71             throw new java.lang.RuntimeException("No input for writing to file" + file);
       
    72         }
       
    73         try (
       
    74              FileOutputStream fos = new FileOutputStream(file);
       
    75              PrintStream ps = new PrintStream(fos)
       
    76         ) {
       
    77             for (String str : contents) {
       
    78                 ps.println(str);
       
    79             }
       
    80         }
       
    81     }
       
    82 
       
    83     /* version.jar entries and files:
       
    84      * META-INF/
       
    85      * META-INF/MANIFEST.MF
       
    86      * version/
       
    87      * version/Main.class
       
    88      * version/Version.class
       
    89      * META-INF/versions/
       
    90      * META-INF/versions/<major-version>/
       
    91      * META-INF/versions/<major-version>/version/
       
    92      * META-INF/versions/<major-version>/version/Version.class
       
    93      */
       
    94     static void createClassFilesAndJar() throws Exception {
       
    95         String tempDir = System.getProperty("test.classes");
       
    96         File baseDir = new File(tempDir + File.separator + "base");
       
    97         File vDir    = new File(tempDir + File.separator + MAJOR_VERSION_STRING);
       
    98 
       
    99         baseDir.mkdirs();
       
   100         vDir.mkdirs();
       
   101 
       
   102         File fileMain = TestCommon.getOutputSourceFile("Main.java");
       
   103         writeFile(fileMain, getMain());
       
   104 
       
   105         File fileVersion = TestCommon.getOutputSourceFile("Version.java");
       
   106         writeFile(fileVersion, getVersion(7));
       
   107         JarBuilder.compile(baseDir.getAbsolutePath(), fileVersion.getAbsolutePath(), "--release", "7");
       
   108         JarBuilder.compile(baseDir.getAbsolutePath(), fileMain.getAbsolutePath(),
       
   109             "-cp", baseDir.getAbsolutePath(), "--release", MAJOR_VERSION_STRING);
       
   110 
       
   111         String[] meta = {
       
   112             "Multi-Release: true",
       
   113             "Main-Class: version.Main"
       
   114         };
       
   115         File metainf = new File(tempDir, "mf.txt");
       
   116         writeFile(metainf, meta);
       
   117 
       
   118         fileVersion = TestCommon.getOutputSourceFile("Version.java");
       
   119         writeFile(fileVersion, getVersion(MAJOR_VERSION));
       
   120         JarBuilder.compile(vDir.getAbsolutePath(), fileVersion.getAbsolutePath(), "--release", MAJOR_VERSION_STRING);
       
   121 
       
   122         JarBuilder.build("version", baseDir, metainf.getAbsolutePath(),
       
   123             "--release", MAJOR_VERSION_STRING, "-C", vDir.getAbsolutePath(), ".");
       
   124 
       
   125         // the following jar file is for testing case-insensitive "Multi-Release"
       
   126         // attibute name
       
   127         String[] meta2 = {
       
   128             "multi-Release: true",
       
   129             "Main-Class: version.Main"
       
   130         };
       
   131         metainf = new File(tempDir, "mf2.txt");
       
   132         writeFile(metainf, meta2);
       
   133         JarBuilder.build("version2", baseDir, metainf.getAbsolutePath(),
       
   134             "--release", MAJOR_VERSION_STRING, "-C", vDir.getAbsolutePath(), ".");
       
   135     }
       
   136 
       
   137     static void checkExecOutput(OutputAnalyzer output, String expectedOutput) throws Exception {
       
   138         try {
       
   139             TestCommon.checkExec(output, expectedOutput);
       
   140         } catch (java.lang.RuntimeException re) {
       
   141             String cause = re.getMessage();
       
   142             if (!expectedOutput.equals(cause)) {
       
   143                 throw re;
       
   144             }
       
   145         }
       
   146     }
       
   147 
       
   148     public static void main(String... args) throws Exception {
       
   149         // create version.jar which contains Main.class and Version.class.
       
   150         // Version.class has two versions: 8 and the current version.
       
   151         createClassFilesAndJar();
       
   152 
       
   153         String mainClass          = "version.Main";
       
   154         String loadInfo           = "[class,load] version.Version source: shared objects file";
       
   155         String appClasses[]       = {"version/Main", "version/Version"};
       
   156         String appJar             = TestCommon.getTestJar("version.jar");
       
   157         String appJar2            = TestCommon.getTestJar("version2.jar");
       
   158         String enableMultiRelease = "-Djdk.util.jar.enableMultiRelease=true";
       
   159         String jarVersion         = null;
       
   160         String expectedOutput     = null;
       
   161 
       
   162         // 1. default to highest version
       
   163         //    if META-INF/versions exists, no other commandline options like -Djdk.util.jar.version and
       
   164         //    -Djdk.util.jar.enableMultiRelease passed to vm
       
   165         OutputAnalyzer output = TestCommon.dump(appJar, appClasses);
       
   166         output.shouldContain("Loading classes to share: done.");
       
   167         output.shouldHaveExitValue(0);
       
   168 
       
   169         output = TestCommon.exec(appJar, mainClass);
       
   170         checkExecOutput(output, "I am running on version " + MAJOR_VERSION_STRING);
       
   171 
       
   172         // 2. Test versions 7 and the current major version.
       
   173         //    -Djdk.util.jar.enableMultiRelease=true (or force), default is true.
       
   174         //    a) -Djdk.util.jar.version=7 does not exist in jar.
       
   175         //        It will fallback to the root version which is also 7 in this test.
       
   176         //    b) -Djdk.util.jar.version=MAJOR_VERSION exists in the jar.
       
   177         for (int i : new int[] {7, MAJOR_VERSION}) {
       
   178             jarVersion = "-Djdk.util.jar.version=" + i;
       
   179             expectedOutput = "I am running on version " + i;
       
   180             output = TestCommon.dump(appJar, appClasses, enableMultiRelease, jarVersion);
       
   181             output.shouldContain("Loading classes to share: done.");
       
   182             output.shouldHaveExitValue(0);
       
   183 
       
   184             output = TestCommon.exec(appJar, mainClass);
       
   185             checkExecOutput(output, expectedOutput);
       
   186         }
       
   187 
       
   188         // 3. For unsupported version, 5 and current major version + 1, the multiversion
       
   189         // will be turned off, so it will use the default (root) version.
       
   190         for (int i : new int[] {5, MAJOR_VERSION + 1}) {
       
   191             jarVersion = "-Djdk.util.jar.version=" + i;
       
   192             output = TestCommon.dump(appJar, appClasses, enableMultiRelease, jarVersion);
       
   193             output.shouldHaveExitValue(0);
       
   194             // With the fix for 8172218, multi-release jar is being handled in
       
   195             // jdk corelib which doesn't emit the following warning message.
       
   196             //output.shouldContain("JDK" + i + " is not supported in multiple version jars");
       
   197 
       
   198             output = TestCommon.exec(appJar, mainClass);
       
   199             if (i == 5)
       
   200                 checkExecOutput(output, "I am running on version 7");
       
   201             else
       
   202                 checkExecOutput(output, "I am running on version " + MAJOR_VERSION_STRING);
       
   203         }
       
   204 
       
   205         // 4. If explicitly disabled from command line for multiversion jar, it will use default
       
   206         //    version at root regardless multiversion versions exists.
       
   207         //    -Djdk.util.jar.enableMultiRelease=false (not 'true' or 'force')
       
   208         for (int i = 6; i < MAJOR_VERSION + 1; i++) {
       
   209             jarVersion = "-Djdk.util.jar.version=" + i;
       
   210             output = TestCommon.dump(appJar, appClasses, "-Djdk.util.jar.enableMultiRelease=false", jarVersion);
       
   211             output.shouldHaveExitValue(0);
       
   212 
       
   213             output = TestCommon.exec(appJar, mainClass);
       
   214             expectedOutput = "I am running on version 7";
       
   215             checkExecOutput(output, expectedOutput);
       
   216         }
       
   217 
       
   218         // 5. Sanity test with -Xbootclasspath/a
       
   219         //    AppCDS behaves the same as the non-AppCDS case. A multi-release
       
   220         //    jar file in the -Xbootclasspath/a will be ignored.
       
   221         output = TestCommon.dump(appJar, appClasses, "-Xbootclasspath/a:" + appJar, enableMultiRelease, jarVersion);
       
   222         output.shouldContain("Loading classes to share: done.");
       
   223         output.shouldHaveExitValue(0);
       
   224 
       
   225         output = TestCommon.exec(appJar, "-Xbootclasspath/a:" + appJar, mainClass);
       
   226         checkExecOutput(output, "I am running on version 7");
       
   227 
       
   228         // 6. Sanity test case-insensitive "Multi-Release" attribute name
       
   229         output = TestCommon.dump(appJar2, appClasses);
       
   230         output.shouldContain("Loading classes to share: done.");
       
   231         output.shouldHaveExitValue(0);
       
   232 
       
   233         output = TestCommon.exec(appJar2, mainClass);
       
   234         checkExecOutput(output, "I am running on version " + MAJOR_VERSION_STRING);
       
   235     }
       
   236 }