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