author | iklam |
Mon, 04 Dec 2017 08:59:47 -0800 | |
changeset 48469 | 7312ae4465d6 |
parent 48403 | 6d4e1efac80a |
child 48741 | d682d3da7923 |
permissions | -rw-r--r-- |
48138 | 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. |
|
48469
7312ae4465d6
8193672: [test] Enhance vm.cds property to check for all conditions required to run CDS tests
iklam
parents:
48403
diff
changeset
|
29 |
* @requires vm.cds |
48138 | 30 |
* @library /test/lib |
31 |
* @modules java.base/jdk.internal.misc |
|
32 |
* jdk.jartool/sun.tools.jar |
|
33 |
* @run main/othervm MultiReleaseJars |
|
34 |
*/ |
|
35 |
||
36 |
import java.io.File; |
|
37 |
import java.io.FileOutputStream; |
|
38 |
import java.io.PrintStream; |
|
39 |
import java.io.IOException; |
|
40 |
import jdk.test.lib.process.OutputAnalyzer; |
|
41 |
||
42 |
public class MultiReleaseJars { |
|
43 |
||
44 |
static final int MAJOR_VERSION = Runtime.version().major(); |
|
45 |
static final String MAJOR_VERSION_STRING = String.valueOf(Runtime.version().major()); |
|
46 |
||
47 |
static String[] getMain() { |
|
48 |
String[] sts = { |
|
49 |
"package version;", |
|
50 |
"public class Main {", |
|
51 |
" public static void main(String[] args) {", |
|
52 |
" Version version = new Version();", |
|
53 |
" System.out.println(\"I am running on version \" + version.getVersion());", |
|
54 |
" }", |
|
55 |
"}" |
|
56 |
}; |
|
57 |
return sts; |
|
58 |
} |
|
59 |
||
60 |
static String[] getVersion(int version) { |
|
61 |
String[] sts = { |
|
62 |
"package version;", |
|
63 |
"public class Version {", |
|
64 |
" public int getVersion(){ return " + version + "; }", |
|
65 |
"}" |
|
66 |
}; |
|
67 |
return sts; |
|
68 |
} |
|
69 |
||
70 |
static void writeFile(File file, String... contents) throws Exception { |
|
71 |
if (contents == null) { |
|
72 |
throw new java.lang.RuntimeException("No input for writing to file" + file); |
|
73 |
} |
|
48403
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
74 |
try ( |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
75 |
FileOutputStream fos = new FileOutputStream(file); |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
76 |
PrintStream ps = new PrintStream(fos) |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
77 |
) { |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
78 |
for (String str : contents) { |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
79 |
ps.println(str); |
6d4e1efac80a
8165603: runtime/appcds/UseAppCDS.java: failed to clean up files after test when running with agentvm
iklam
parents:
48138
diff
changeset
|
80 |
} |
48138 | 81 |
} |
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 |
} |