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.jar.JarEntry;
  56 import java.util.jar.JarFile;
  57 import jdk.Version;
  58 
  59 import org.testng.Assert;
  60 import org.testng.annotations.AfterClass;
  61 import org.testng.annotations.BeforeClass;
  62 import org.testng.annotations.Test;
  63 
  64 public class MultiReleaseJarProperties {
  65 
  66     static final int MAJOR_VERSION = Version.current().major();
  67 
  68     final static int ROOTVERSION = 8; // magic number from knowledge of internals
  69     final static String userdir = System.getProperty("user.dir", ".");
  70     final static File multirelease = new File(userdir, "multi-release.jar");
  71     protected int rtVersion;
  72     boolean force;
  73     protected ClassLoader cldr;
  74     protected Class<?> rootClass;
  75 
  76     @BeforeClass
  77     public void initialize() throws Exception {
  78         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  79         creator.compileEntries();
  80         creator.buildMultiReleaseJar();
  81 
  82         rtVersion = Integer.getInteger("jdk.util.jar.version", MAJOR_VERSION);
  83         String mrprop = System.getProperty("jdk.util.jar.enableMultiRelease", "");
  84         if (mrprop.equals("false")) {
  85             rtVersion = ROOTVERSION;
  86         } else if (rtVersion < ROOTVERSION) {
  87             rtVersion = ROOTVERSION;
  88         } else if (rtVersion > MAJOR_VERSION) {
  89             rtVersion = MAJOR_VERSION;
  90         }
  91         force = mrprop.equals("force");
  92 
  93         initializeClassLoader();
  94     }
  95 
  96     protected void initializeClassLoader() throws Exception {
  97         URL[] urls = new URL[]{multirelease.toURI().toURL()};
  98         cldr = new URLClassLoader(urls);
  99         // load any class, Main is convenient and in the root entries
 100         rootClass = cldr.loadClass("version.Main");
 101     }
 102 
 103     @AfterClass
 104     public void close() throws IOException {
 105         ((URLClassLoader)cldr).close();
 106         Files.delete(multirelease.toPath());
 107     }
 108 
 109     /*
 110      * jdk.util.jar.enableMultiRelease=force is a no-op for URLClassLoader
 111      */
 112     @Test
 113     public void testURLClassLoader() throws Throwable {
 114         Class<?> vcls = cldr.loadClass("version.Version");
 115         invokeMethod(vcls, rtVersion);
 116     }
 117 
 118     protected void invokeMethod(Class<?> vcls, int expected) throws Throwable {
 119         MethodType mt = MethodType.methodType(int.class);
 120         MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
 121         Assert.assertEquals(expected, (int) mh.invoke(vcls.newInstance()));
 122     }
 123 
 124     /*
 125      * jdk.util.jar.enableMultiRelease=force should affect a custom class loader
 126      */
 127     @Test
 128     public void testClassLoader() throws Throwable {
 129         try (JarFile jf = new JarFile(multirelease)) {  // do not set runtime versioning
 130             ClassLoader cldr = new CustomClassLoader(jf);
 131             Class<?> vcls = cldr.loadClass("version.Version");
 132             if (rtVersion == 9) {
 133                 try {
 134                     cldr.loadClass("version.PackagePrivate");
 135                 } catch (ClassNotFoundException x) {
 136                     if (force) throw x;
 137                 }
 138             }
 139             invokeMethod(vcls, force ? rtVersion : ROOTVERSION);
 140         }
 141     }
 142 
 143     private static class CustomClassLoader extends ClassLoader {
 144         private final JarFile jf;
 145 
 146         CustomClassLoader(JarFile jf) throws Exception {
 147             super(null);
 148             this.jf = jf;
 149         }
 150 
 151         protected Class<?> findClass(String name) throws ClassNotFoundException {
 152             try {
 153                 byte[] b;
 154                 String entryName = name.replace(".", "/") + ".class";
 155                 JarEntry je = jf.getJarEntry(entryName);
 156                 if (je != null) {
 157                     try (InputStream is = jf.getInputStream(je)) {
 158                         b = new byte[(int) je.getSize()];
 159                         is.read(b);
 160                     }
 161                     return defineClass(name, b, 0, b.length);
 162                 }
 163                 throw new ClassNotFoundException(name);
 164             } catch (IOException x) {
 165                 throw new ClassNotFoundException(x.getMessage());
 166             }
 167         }
 168     }
 169 
 170     @Test
 171     public void testGetResourceAsStream() throws Exception {
 172         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 173         // use fileRootClass as a base for getting resources
 174         getResourceAsStream(rootClass, resource);
 175     }
 176 
 177     protected void getResourceAsStream(Class<?> rootClass, String resource) throws Exception {
 178         try (InputStream is = rootClass.getResourceAsStream(resource)) {
 179             byte[] bytes = is.readAllBytes();
 180             resource = new String(bytes);
 181         }
 182         String match = "return " + rtVersion + ";";
 183         Assert.assertTrue(resource.contains(match));
 184     }
 185 
 186     @Test
 187     public void testGetResource() throws Exception {
 188         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 189         // use rootClass as a base for getting resources
 190         getResource(rootClass, resource);
 191     }
 192 
 193     protected void getResource(Class<?> rootClass, String resource) throws Exception {
 194         URL url = rootClass.getResource(resource);
 195         try (InputStream is = url.openStream()) {
 196             byte[] bytes = is.readAllBytes();
 197             resource = new String(bytes);
 198         }
 199         String match = "return " + rtVersion + ";";
 200         Assert.assertTrue(resource.contains(match));
 201     }
 202 }