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 System properties for JarFile that support multi-release jar files
  28  * @library /lib/testlibrary/java/util/jar
  29  * @build Compiler JarBuilder CreateMultiReleaseTestJars
  30  * @run testng MultiReleaseJarProperties
  31  * @run testng/othervm -Djdk.util.jar.version=0 MultiReleaseJarProperties
  32  * @run testng/othervm -Djdk.util.jar.version=8 MultiReleaseJarProperties
  33  * @run testng/othervm -Djdk.util.jar.version=9 MultiReleaseJarProperties
  34  * @run testng/othervm -Djdk.util.jar.version=10 MultiReleaseJarProperties
  35  * @run testng/othervm -Djdk.util.jar.version=100 MultiReleaseJarProperties
  36  * @run testng/othervm -Djdk.util.jar.version=8 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarProperties
  37  * @run testng/othervm -Djdk.util.jar.version=9 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarProperties
  38  * @run testng/othervm -Djdk.util.jar.version=10 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarProperties
  39  * @run testng/othervm -Djdk.util.jar.version=8 -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarProperties
  40  * @run testng/othervm -Djdk.util.jar.version=9 -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarProperties
  41  * @run testng/othervm -Djdk.util.jar.version=10 -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarProperties
  42  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarProperties
  43  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarProperties
  44  */
  45 
  46 import java.io.File;
  47 import java.io.IOException;
  48 import java.io.InputStream;
  49 import java.lang.invoke.MethodHandle;
  50 import java.lang.invoke.MethodHandles;
  51 import java.lang.invoke.MethodType;
  52 import java.net.URL;
  53 import java.net.URLClassLoader;
  54 import java.nio.file.Files;
  55 import java.util.Arrays;
  56 import java.util.jar.JarEntry;
  57 import java.util.jar.JarFile;
  58 
  59 import static sun.misc.Version.jdkMinorVersion;
  60 
  61 import org.testng.Assert;
  62 import org.testng.annotations.AfterClass;
  63 import org.testng.annotations.BeforeClass;
  64 import org.testng.annotations.Test;
  65 
  66 public class MultiReleaseJarProperties {
  67     final static int ROOTVERSION = 8;   // magic number, from inspecting the jar file
  68     final static String userdir = System.getProperty("user.dir", ".");
  69     final static File multirelease = new File(userdir, "multi-release.jar");
  70     int rtVersion;
  71     boolean force;
  72     String mrprop;
  73     ClassLoader cldr;
  74     ByteArrayClassLoader bacldr;
  75     Class<?> rootClass;
  76 
  77     @BeforeClass
  78     public void initialize() throws Exception {
  79         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  80         creator.compileEntries();
  81         creator.buildMultiReleaseJar();
  82 
  83         rtVersion = Integer.getInteger("jdk.util.jar.version", jdkMinorVersion());
  84         mrprop = System.getProperty("jdk.util.jar.enableMultiRelease", "");
  85         if (mrprop.equals("false")) {
  86             rtVersion = ROOTVERSION;
  87         } else if (rtVersion < ROOTVERSION) {
  88             rtVersion = ROOTVERSION;
  89         } else if (rtVersion > jdkMinorVersion()) {
  90             rtVersion = jdkMinorVersion();
  91         }
  92         force = mrprop.equals("force");
  93         bacldr = new ByteArrayClassLoader();
  94         URL[] urls = new URL[]{multirelease.toURI().toURL()};
  95         cldr = new URLClassLoader(urls);
  96         // load any old class from the jar file
  97         rootClass = cldr.loadClass("version.Main");
  98     }
  99 
 100     @AfterClass
 101     public void close() throws IOException {
 102         ((URLClassLoader)cldr).close();
 103         Files.delete(multirelease.toPath());
 104     }
 105 
 106     /*
 107      * jdk.util.jar.enableMultiRelease=force is a no-op for URLClassLoader
 108      */
 109     @Test
 110     public void testURLClassLoader() throws Throwable {
 111         Class<?> vcls = cldr.loadClass("version.Version");
 112         invokeMethod(vcls, rtVersion);
 113     }
 114 
 115     /*
 116      * jdk.util.jar.enableMultiRelease=force should affect a custom class loader
 117      */
 118     @Test
 119     public void testClassLoader() throws Throwable {
 120         try (JarFile jf = new JarFile(multirelease)) {  // do not set runtime versioning
 121             ClassLoader cldr = new CustomClassLoader(jf);
 122             Class<?> vcls = cldr.loadClass("version.Version");
 123             if (rtVersion == 9) {
 124                 try {
 125                     cldr.loadClass("version.PackagePrivate");
 126                 } catch (ClassNotFoundException x) {
 127                     if (force) throw x;
 128                 }
 129             }
 130             invokeMethod(vcls, force ? rtVersion : ROOTVERSION);
 131         }
 132     }
 133 
 134     @Test
 135     public void testGetResourceAsStream() throws Exception {
 136         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 137         // use rootClass as a base for getting resources
 138         try (InputStream is = rootClass.getResourceAsStream(resource)) {
 139             byte[] bytes = is.readAllBytes();
 140             resource = new String(bytes);
 141         }
 142         String match = "return " + rtVersion + ";";
 143         Assert.assertTrue(resource.contains(match));
 144     }
 145 
 146     @Test
 147     public void testGetResource() throws Exception {
 148         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 149         // use rootClass as a base for getting resources
 150         URL url = rootClass.getResource(resource);
 151         try (InputStream is = url.openStream()) {
 152             byte[] bytes = is.readAllBytes();
 153             resource = new String(bytes);
 154         }
 155         String match = "return " + rtVersion + ";";
 156         Assert.assertTrue(resource.contains(match));
 157     }
 158 
 159     @Test
 160     public void testGetRuntimeVersioned() throws Throwable {
 161         String name1 = "version.Version";
 162         String name2 = "version.PackagePrivate";
 163         try (JarFile jf = new JarFile(multirelease)) {
 164             byte[] bytes = null;
 165 
 166             if (rtVersion == 9) {
 167                 JarEntry je = jf.getRuntimeVersionedEntry(name2.replace('.', '/') + ".class");
 168                 try (InputStream is = jf.getInputStream(je)) {
 169                     bytes = is.readAllBytes();
 170                 }
 171                 assert bytes != null;
 172                 bacldr.defineClass(name2, bytes);
 173             }
 174 
 175             JarEntry je = jf.getRuntimeVersionedEntry(name1.replace('.', '/') + ".class");
 176             try (InputStream is = jf.getInputStream(je)) {
 177                 bytes = is.readAllBytes();
 178             }
 179             assert bytes != null;
 180             Class<?> vcls = bacldr.defineClass(name1, bytes);
 181 
 182             invokeMethod(vcls, rtVersion);
 183         }
 184     }
 185 
 186     @Test
 187     public void testRuntimeVersioning() throws Exception {
 188         String name = "version/Version.class";
 189         JarEntry je1;
 190         JarEntry je2;
 191         byte[] b1 = null;
 192         byte[] b2 = null;
 193 
 194         try (JarFile jf = new JarFile(multirelease)) {
 195             je1 = jf.getRuntimeVersionedEntry(name);
 196             try (InputStream is = jf.getInputStream(je1)) {
 197                 b1 = is.readAllBytes();
 198             }
 199         }
 200         assert b1 != null;
 201 
 202         try (JarFile jf = new JarFile(multirelease).setRuntimeVersioned()) {
 203             je2 = jf.getJarEntry(name);
 204             try (InputStream is = jf.getInputStream(je2)) {
 205                 b2 = is.readAllBytes();
 206             }
 207         }
 208         assert b2 != null;
 209 
 210         Assert.assertEquals(je1.getName(), je2.getName());
 211         Assert.assertEquals(je1.getSize(), je2.getSize());
 212         Assert.assertTrue(Arrays.equals(b1, b2));
 213     }
 214 
 215     private void invokeMethod(Class<?> vcls, int expected) throws Throwable {
 216         MethodType mt = MethodType.methodType(int.class);
 217         MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
 218         Assert.assertEquals(expected, (int) mh.invoke(vcls.newInstance()));
 219     }
 220 
 221     private static class ByteArrayClassLoader extends ClassLoader {
 222         ByteArrayClassLoader() {
 223             super(null);
 224         }
 225 
 226         public Class<?> defineClass(String name, byte[] bytes) {
 227             return defineClass(name, bytes, 0, bytes.length);
 228         }
 229     }
 230 
 231     private static class CustomClassLoader extends ClassLoader {
 232         private final JarFile jf;
 233 
 234         CustomClassLoader(JarFile jf) throws Exception {
 235             super(null);
 236             this.jf = jf;
 237         }
 238 
 239         protected Class<?> findClass(String name) throws ClassNotFoundException {
 240             try {
 241                 byte[] b;
 242                 String entryName = name.replace(".", "/") + ".class";
 243                 JarEntry je = jf.getJarEntry(entryName);
 244                 if (je != null) {
 245                     try (InputStream is = jf.getInputStream(je)) {
 246                         b = new byte[(int) je.getSize()];
 247                         is.read(b);
 248                     }
 249                     return defineClass(name, b, 0, b.length);
 250                 }
 251                 throw new ClassNotFoundException(name);
 252             } catch (IOException x) {
 253                 throw new ClassNotFoundException(x.getMessage());
 254             }
 255         }
 256     }
 257 }