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