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