< prev index next >

test/jdk/nio/zipfs/MultiReleaseJarTest.java

Print this page
rev 16859 : 8176709: JarFileSystem::isMultiReleaseJar is incorrect
Reviewed-by: alanb, sherman, psandoz, mchung


   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 jdk.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")


 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) {


   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 8176709
  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 jdk.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 import java.util.concurrent.atomic.AtomicInteger;
  46 
  47 import org.testng.Assert;
  48 import org.testng.annotations.*;
  49 
  50 public class MultiReleaseJarTest {
  51     final private int MAJOR_VERSION = Runtime.version().major();
  52 
  53     final private String userdir = System.getProperty("user.dir",".");
  54     final private CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  55     final private Map<String,String> stringEnv = new HashMap<>();
  56     final private Map<String,Integer> integerEnv = new HashMap<>();
  57     final private Map<String,Version> versionEnv = new HashMap<>();
  58     final private String className = "version.Version";
  59     final private MethodType mt = MethodType.methodType(int.class);
  60 
  61     private String entryName;
  62     private URI uvuri;
  63     private URI mruri;
  64     private URI smruri;
  65 
  66     @BeforeClass
  67     public void initialize() throws Exception {

  68         creator.compileEntries();
  69         creator.buildUnversionedJar();
  70         creator.buildMultiReleaseJar();
  71         creator.buildShortMultiReleaseJar();
  72         String ssp = Paths.get(userdir, "unversioned.jar").toUri().toString();
  73         uvuri = new URI("jar", ssp , null);
  74         ssp = Paths.get(userdir, "multi-release.jar").toUri().toString();
  75         mruri = new URI("jar", ssp, null);
  76         ssp = Paths.get(userdir, "short-multi-release.jar").toUri().toString();
  77         smruri = new URI("jar", ssp, null);
  78         entryName = className.replace('.', '/') + ".class";
  79     }
  80 
  81     public void close() throws IOException {
  82         Files.delete(Paths.get(userdir, "unversioned.jar"));
  83         Files.delete(Paths.get(userdir, "multi-release.jar"));
  84         Files.delete(Paths.get(userdir, "short-multi-release.jar"));
  85     }
  86 
  87     @DataProvider(name="strings")


 169     @Test
 170     public void testShortJar() throws Throwable {
 171         integerEnv.put("multi-release", Integer.valueOf(10));
 172         runTest(smruri, integerEnv, 10);
 173         integerEnv.put("multi-release", Integer.valueOf(9));
 174         runTest(smruri, integerEnv, 8);
 175     }
 176 
 177     private void runTest(Map<String,?> env, int expected) throws Throwable {
 178         runTest(mruri, env, expected);
 179     }
 180 
 181     private void runTest(URI uri, Map<String,?> env, int expected) throws Throwable {
 182         try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
 183             Path version = fs.getPath(entryName);
 184             byte [] bytes = Files.readAllBytes(version);
 185             Class<?> vcls = (new ByteArrayClassLoader(fs)).defineClass(className, bytes);
 186             MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
 187             Assert.assertEquals((int)mh.invoke(vcls.newInstance()), expected);
 188         }
 189     }
 190 
 191     @Test
 192     public void testIsMultiReleaseJar() throws Exception {
 193         testCustomMultiReleaseValue("true", true);
 194         testCustomMultiReleaseValue("true\r\nOther: value", true);
 195         testCustomMultiReleaseValue("true\nOther: value", true);
 196         testCustomMultiReleaseValue("true\rOther: value", true);
 197 
 198         testCustomMultiReleaseValue("false", false);
 199         testCustomMultiReleaseValue(" true", false);
 200         testCustomMultiReleaseValue("true ", false);
 201         testCustomMultiReleaseValue("true\n ", false);
 202         testCustomMultiReleaseValue("true\r ", false);
 203         testCustomMultiReleaseValue("true\n true", false);
 204         testCustomMultiReleaseValue("true\r\n true", false);
 205     }
 206 
 207     private static final AtomicInteger JAR_COUNT = new AtomicInteger(0);
 208 
 209     private void testCustomMultiReleaseValue(String value, boolean expected)
 210             throws Exception {
 211         String fileName = "custom-mr" + JAR_COUNT.incrementAndGet() + ".jar";
 212         creator.buildCustomMultiReleaseJar(fileName, value, Map.of(),
 213                 /*addEntries*/true);
 214 
 215         Map<String,String> env = Map.of("multi-release", "runtime");
 216         Path filePath = Paths.get(userdir, fileName);
 217         String ssp = filePath.toUri().toString();
 218         URI customJar = new URI("jar", ssp , null);
 219         try (FileSystem fs = FileSystems.newFileSystem(customJar, env)) {
 220             if (expected) {
 221                 Assert.assertTrue(readAndCompare(fs, MAJOR_VERSION));
 222             } else {
 223                 Assert.assertTrue(readAndCompare(fs, 8));
 224             }
 225         }
 226         Files.delete(filePath);
 227     }
 228 
 229     private static class ByteArrayClassLoader extends ClassLoader {
 230         final private FileSystem fs;
 231 
 232         ByteArrayClassLoader(FileSystem fs) {
 233             super(null);
 234             this.fs = fs;
 235         }
 236 
 237         @Override
 238         public Class<?> loadClass(String name) throws ClassNotFoundException {
 239             try {
 240                 return super.loadClass(name);
 241             } catch (ClassNotFoundException x) {}
 242             Path cls = fs.getPath(name.replace('.', '/') + ".class");
 243             try {
 244                 byte[] bytes = Files.readAllBytes(cls);
 245                 return defineClass(name, bytes);
 246             } catch (IOException x) {
< prev index next >