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