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 8144355 27 * @summary Test aliasing additions to ZipFileSystem for multi-release jar files 28 * @library /lib/testlibrary/java/util/jar 29 * @build Compiler JarBuilder CreateMultiReleaseTestJars 30 * @run testng MultiReleaseJarTest 31 */ 32 33 import java.io.IOException; 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.invoke.MethodType; 37 import java.net.URI; 38 import java.nio.file.*; 39 import java.util.HashMap; 40 import java.util.Map; 41 import jdk.Version; 42 43 import org.testng.Assert; 44 import org.testng.annotations.*; 45 46 public class MultiReleaseJarTest { 47 final private int MAJOR_VERSION = Version.current().major(); 48 49 final private String userdir = System.getProperty("user.dir","."); 50 final private Map<String,String> stringEnv = new HashMap<>(); 51 final private Map<String,Integer> integerEnv = new HashMap<>(); 52 final private Map<String,Version> versionEnv = new HashMap<>(); 53 final private String className = "version.Version"; 54 final private MethodType mt = MethodType.methodType(int.class); 55 56 private String entryName; 57 private URI uvuri; 58 private URI mruri; 59 private URI smruri; 60 61 @BeforeClass 62 public void initialize() throws Exception { 63 CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars(); 64 creator.compileEntries(); 65 creator.buildUnversionedJar(); 66 creator.buildMultiReleaseJar(); 67 creator.buildShortMultiReleaseJar(); 68 String ssp = Paths.get(userdir, "unversioned.jar").toUri().toString(); 69 uvuri = new URI("jar", ssp , null); 70 ssp = Paths.get(userdir, "multi-release.jar").toUri().toString(); 71 mruri = new URI("jar", ssp, null); 72 ssp = Paths.get(userdir, "short-multi-release.jar").toUri().toString(); 73 smruri = new URI("jar", ssp, null); 74 entryName = className.replace('.', '/') + ".class"; 75 } 76 77 public void close() throws IOException { 78 Files.delete(Paths.get(userdir, "unversioned.jar")); 79 Files.delete(Paths.get(userdir, "multi-release.jar")); 80 Files.delete(Paths.get(userdir, "short-multi-release.jar")); 81 } 82 83 @DataProvider(name="strings") 84 public Object[][] createStrings() { 85 return new Object[][]{ 86 {"runtime", MAJOR_VERSION}, 87 {"-20", 8}, 88 {"0", 8}, 89 {"8", 8}, 90 {"9", 9}, 91 {"10", 10}, 92 {"11", 10}, 93 {"50", 10} 94 }; 95 } 96 97 @DataProvider(name="integers") 98 public Object[][] createIntegers() { 99 return new Object[][] { 100 {new Integer(-5), 8}, 101 {new Integer(0), 8}, 102 {new Integer(8), 8}, 103 {new Integer(9), 9}, 104 {new Integer(10), 10}, 105 {new Integer(11), 10}, 106 {new Integer(100), 10} 107 }; 108 } 109 110 @DataProvider(name="versions") 111 public Object[][] createVersions() { 112 return new Object[][] { 113 {Version.parse("8"), 8}, 114 {Version.parse("9"), 9}, 115 {Version.parse("10"), 10}, 116 {Version.parse("11"), 10}, 117 {Version.parse("100"), 10} 118 }; 119 } 120 121 // Not the best test but all I can do since ZipFileSystem and JarFileSystem 122 // are not public, so I can't use (fs instanceof ...) 123 @Test 124 public void testNewFileSystem() throws Exception { 125 Map<String,String> env = new HashMap<>(); 126 // no configuration, treat multi-release jar as unversioned 127 try (FileSystem fs = FileSystems.newFileSystem(mruri, env)) { 128 Assert.assertTrue(readAndCompare(fs, 8)); 129 } 130 env.put("multi-release", "runtime"); 131 // a configuration and jar file is multi-release 132 try (FileSystem fs = FileSystems.newFileSystem(mruri, env)) { 133 Assert.assertTrue(readAndCompare(fs, MAJOR_VERSION)); 134 } 135 // a configuration but jar file is unversioned 136 try (FileSystem fs = FileSystems.newFileSystem(uvuri, env)) { 137 Assert.assertTrue(readAndCompare(fs, 8)); 138 } 139 } 140 141 private boolean readAndCompare(FileSystem fs, int expected) throws IOException { 142 Path path = fs.getPath("version/Version.java"); 143 String src = new String(Files.readAllBytes(path)); 144 return src.contains("return " + expected); 145 } 146 147 @Test(dataProvider="strings") 148 public void testStrings(String value, int expected) throws Throwable { 149 stringEnv.put("multi-release", value); 150 runTest(stringEnv, expected); 151 } 152 153 @Test(dataProvider="integers") 154 public void testIntegers(Integer value, int expected) throws Throwable { 155 integerEnv.put("multi-release", value); 156 runTest(integerEnv, expected); 157 } 158 159 @Test(dataProvider="versions") 160 public void testVersions(Version value, int expected) throws Throwable { 161 versionEnv.put("multi-release", value); 162 runTest(versionEnv, expected); 163 } 164 165 @Test 166 public void testShortJar() throws Throwable { 167 integerEnv.put("multi-release", Integer.valueOf(10)); 168 runTest(smruri, integerEnv, 10); 169 integerEnv.put("multi-release", Integer.valueOf(9)); 170 runTest(smruri, integerEnv, 8); 171 } 172 173 private void runTest(Map<String,?> env, int expected) throws Throwable { 174 runTest(mruri, env, expected); 175 } 176 177 private void runTest(URI uri, Map<String,?> env, int expected) throws Throwable { 178 try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { 179 Path version = fs.getPath(entryName); 180 byte [] bytes = Files.readAllBytes(version); 181 Class<?> vcls = (new ByteArrayClassLoader(fs)).defineClass(className, bytes); 182 MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt); 183 Assert.assertEquals((int)mh.invoke(vcls.newInstance()), expected); 184 } 185 } 186 187 private static class ByteArrayClassLoader extends ClassLoader { 188 final private FileSystem fs; 189 190 ByteArrayClassLoader(FileSystem fs) { 191 super(null); 192 this.fs = fs; 193 } 194 195 @Override 196 public Class<?> loadClass(String name) throws ClassNotFoundException { 197 try { 198 return super.loadClass(name); 199 } catch (ClassNotFoundException x) {} 200 Path cls = fs.getPath(name.replace('.', '/') + ".class"); 201 try { 202 byte[] bytes = Files.readAllBytes(cls); 203 return defineClass(name, bytes); 204 } catch (IOException x) { 205 throw new ClassNotFoundException(x.getMessage()); 206 } 207 } 208 209 public Class<?> defineClass(String name, byte[] bytes) throws ClassNotFoundException { 210 if (bytes == null) throw new ClassNotFoundException("No bytes for " + name); 211 return defineClass(name, bytes, 0, bytes.length); 212 } 213 } 214 }