1 /* |
|
2 * Copyright (c) 2015, 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 8132734 |
|
27 * @summary Test the extended API and the aliasing additions in JarFile that |
|
28 * support multi-release jar files |
|
29 * @library /lib/testlibrary/java/util/jar |
|
30 * @build Compiler JarBuilder CreateMultiReleaseTestJars |
|
31 * @run testng MultiReleaseJarAPI |
|
32 */ |
|
33 |
|
34 import java.io.File; |
|
35 import java.io.IOException; |
|
36 import java.io.InputStream; |
|
37 import java.nio.file.Files; |
|
38 import java.util.Arrays; |
|
39 import java.util.jar.JarFile; |
|
40 import java.util.zip.ZipEntry; |
|
41 import java.util.zip.ZipFile; |
|
42 import jdk.Version; |
|
43 |
|
44 import static java.util.jar.JarFile.Release; |
|
45 |
|
46 import org.testng.Assert; |
|
47 import org.testng.annotations.AfterClass; |
|
48 import org.testng.annotations.BeforeClass; |
|
49 import org.testng.annotations.Test; |
|
50 |
|
51 |
|
52 public class MultiReleaseJarAPI { |
|
53 |
|
54 static final int MAJOR_VERSION = Version.current().major(); |
|
55 |
|
56 String userdir = System.getProperty("user.dir","."); |
|
57 CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars(); |
|
58 File unversioned = new File(userdir, "unversioned.jar"); |
|
59 File multirelease = new File(userdir, "multi-release.jar"); |
|
60 File signedmultirelease = new File(userdir, "signed-multi-release.jar"); |
|
61 Release[] values = JarFile.Release.values(); |
|
62 |
|
63 |
|
64 @BeforeClass |
|
65 public void initialize() throws Exception { |
|
66 creator.compileEntries(); |
|
67 creator.buildUnversionedJar(); |
|
68 creator.buildMultiReleaseJar(); |
|
69 creator.buildSignedMultiReleaseJar(); |
|
70 } |
|
71 |
|
72 @AfterClass |
|
73 public void close() throws IOException { |
|
74 Files.delete(unversioned.toPath()); |
|
75 Files.delete(multirelease.toPath()); |
|
76 Files.delete(signedmultirelease.toPath()); |
|
77 } |
|
78 |
|
79 @Test |
|
80 public void isMultiReleaseJar() throws Exception { |
|
81 try (JarFile jf = new JarFile(unversioned)) { |
|
82 Assert.assertFalse(jf.isMultiRelease()); |
|
83 } |
|
84 |
|
85 try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Release.RUNTIME)) { |
|
86 Assert.assertFalse(jf.isMultiRelease()); |
|
87 } |
|
88 |
|
89 try (JarFile jf = new JarFile(multirelease)) { |
|
90 Assert.assertFalse(jf.isMultiRelease()); |
|
91 } |
|
92 |
|
93 try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.RUNTIME)) { |
|
94 Assert.assertTrue(jf.isMultiRelease()); |
|
95 } |
|
96 |
|
97 testCustomMultiReleaseValue("true", true); |
|
98 testCustomMultiReleaseValue("true\r\nOther: value", true); |
|
99 testCustomMultiReleaseValue("true\nOther: value", true); |
|
100 testCustomMultiReleaseValue("true\rOther: value", true); |
|
101 |
|
102 testCustomMultiReleaseValue("false", false); |
|
103 testCustomMultiReleaseValue(" true", false); |
|
104 testCustomMultiReleaseValue("true ", false); |
|
105 testCustomMultiReleaseValue("true\n ", false); |
|
106 testCustomMultiReleaseValue("true\r ", false); |
|
107 testCustomMultiReleaseValue("true\n true", false); |
|
108 testCustomMultiReleaseValue("true\r\n true", false); |
|
109 } |
|
110 |
|
111 private void testCustomMultiReleaseValue(String value, boolean expected) throws Exception { |
|
112 creator.buildCustomMultiReleaseJar("custom-mr.jar", value); |
|
113 File custom = new File(userdir, "custom-mr.jar"); |
|
114 try (JarFile jf = new JarFile(custom, true, ZipFile.OPEN_READ, Release.RUNTIME)) { |
|
115 Assert.assertEquals(jf.isMultiRelease(), expected); |
|
116 } |
|
117 Files.delete(custom.toPath()); |
|
118 } |
|
119 |
|
120 @Test |
|
121 public void testVersioning() throws Exception { |
|
122 // multi-release jar |
|
123 JarFile jar = new JarFile(multirelease); |
|
124 Assert.assertEquals(Release.BASE, jar.getVersion()); |
|
125 jar.close(); |
|
126 |
|
127 for (Release value : values) { |
|
128 System.err.println("test versioning for Release " + value); |
|
129 try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) { |
|
130 Assert.assertEquals(value, jf.getVersion()); |
|
131 } |
|
132 } |
|
133 |
|
134 // regular, unversioned, jar |
|
135 for (Release value : values) { |
|
136 try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) { |
|
137 Assert.assertEquals(Release.BASE, jf.getVersion()); |
|
138 } |
|
139 } |
|
140 |
|
141 // assure that we have a Release object corresponding to the actual runtime version |
|
142 String version = "VERSION_" + MAJOR_VERSION; |
|
143 boolean runtimeVersionExists = false; |
|
144 for (Release value : values) { |
|
145 if (version.equals(value.name())) runtimeVersionExists = true; |
|
146 } |
|
147 Assert.assertTrue(runtimeVersionExists); |
|
148 } |
|
149 |
|
150 @Test |
|
151 public void testAliasing() throws Exception { |
|
152 for (Release value : values) { |
|
153 System.err.println("test aliasing for Release " + value); |
|
154 String name = value.name(); |
|
155 String prefix; |
|
156 if (name.equals("BASE")) { |
|
157 prefix = ""; |
|
158 } else if (name.equals("RUNTIME")) { |
|
159 prefix = "META-INF/versions/" + MAJOR_VERSION + "/"; |
|
160 } else { |
|
161 prefix = "META-INF/versions/" + name.substring(8) + "/"; |
|
162 } |
|
163 // test both multi-release jars |
|
164 readAndCompare(multirelease, value, "README", prefix + "README"); |
|
165 readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class"); |
|
166 // and signed multi-release jars |
|
167 readAndCompare(signedmultirelease, value, "README", prefix + "README"); |
|
168 readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class"); |
|
169 } |
|
170 } |
|
171 |
|
172 private void readAndCompare(File jar, Release version, String name, String realName) throws Exception { |
|
173 byte[] baseBytes; |
|
174 byte[] versionedBytes; |
|
175 try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, Release.BASE)) { |
|
176 ZipEntry ze = jf.getEntry(realName); |
|
177 try (InputStream is = jf.getInputStream(ze)) { |
|
178 baseBytes = is.readAllBytes(); |
|
179 } |
|
180 } |
|
181 assert baseBytes.length > 0; |
|
182 |
|
183 try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) { |
|
184 ZipEntry ze = jf.getEntry(name); |
|
185 try (InputStream is = jf.getInputStream(ze)) { |
|
186 versionedBytes = is.readAllBytes(); |
|
187 } |
|
188 } |
|
189 assert versionedBytes.length > 0; |
|
190 |
|
191 Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes)); |
|
192 } |
|
193 |
|
194 @Test |
|
195 public void testNames() throws Exception { |
|
196 String rname = "version/Version.class"; |
|
197 String vname = "META-INF/versions/9/version/Version.class"; |
|
198 ZipEntry ze1; |
|
199 ZipEntry ze2; |
|
200 try (JarFile jf = new JarFile(multirelease)) { |
|
201 ze1 = jf.getEntry(vname); |
|
202 } |
|
203 Assert.assertEquals(ze1.getName(), vname); |
|
204 try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) { |
|
205 ze2 = jf.getEntry(rname); |
|
206 } |
|
207 Assert.assertEquals(ze2.getName(), rname); |
|
208 Assert.assertNotEquals(ze1.getName(), ze2.getName()); |
|
209 } |
|
210 } |
|