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