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 extended API and the aliasing additions in JarFile that
  28  *          support multi-release jar files
  29  * @library /lib/testlibrary/java/util/jar
  30  * @build Compiler JarBuilder CreateMultiReleaseTestJars
  31  * @run testng MultiReleaseJarAPI
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.nio.file.Files;
  38 import java.util.Arrays;
  39 import java.util.jar.JarFile;
  40 import java.util.zip.ZipEntry;
  41 import java.util.zip.ZipFile;
  42 import jdk.Version;
  43 
  44 import static java.util.jar.JarFile.Release;
  45 
  46 import org.testng.Assert;
  47 import org.testng.annotations.AfterClass;
  48 import org.testng.annotations.BeforeClass;
  49 import org.testng.annotations.Test;
  50 
  51 
  52 public class MultiReleaseJarAPI {
  53 
  54     static final int MAJOR_VERSION = Version.current().major();
  55 
  56     String userdir = System.getProperty("user.dir",".");
  57     File unversioned = new File(userdir, "unversioned.jar");
  58     File multirelease = new File(userdir, "multi-release.jar");
  59     File signedmultirelease = new File(userdir, "signed-multi-release.jar");
  60     Release[] values = JarFile.Release.values();
  61 
  62 
  63     @BeforeClass
  64     public void initialize() throws Exception {
  65         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
  66         creator.compileEntries();
  67         creator.buildUnversionedJar();
  68         creator.buildMultiReleaseJar();
  69         creator.buildSignedMultiReleaseJar();
  70     }
  71 
  72     @AfterClass
  73     public void close() throws IOException {
  74         Files.delete(unversioned.toPath());
  75         Files.delete(multirelease.toPath());
  76         Files.delete(signedmultirelease.toPath());
  77     }
  78 
  79     @Test
  80     public void isMultiReleaseJar() throws Exception {
  81         try (JarFile jf = new JarFile(unversioned)) {
  82             Assert.assertFalse(jf.isMultiRelease());
  83         }
  84 
  85         try (JarFile jf = new JarFile(multirelease)) {
  86             Assert.assertFalse(jf.isMultiRelease());
  87         }
  88 
  89         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
  90             Assert.assertTrue(jf.isMultiRelease());
  91         }
  92     }
  93 
  94     @Test
  95     public void testVersioning() throws Exception {
  96         // multi-release jar
  97         JarFile jar = new JarFile(multirelease);
  98         Assert.assertEquals(Release.BASE, jar.getVersion());
  99         jar.close();
 100 
 101         for (Release value : values) {
 102             System.err.println("test versioning for Release " + value);
 103             try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) {
 104                 Assert.assertEquals(value, jf.getVersion());
 105             }
 106         }
 107 
 108         // regular, unversioned, jar
 109         for (Release value : values) {
 110             try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) {
 111                 Assert.assertEquals(Release.BASE, jf.getVersion());
 112             }
 113         }
 114 
 115         // assure that we have a Release object corresponding to the actual runtime version
 116         String version = "VERSION_" + MAJOR_VERSION;
 117         boolean runtimeVersionExists = false;
 118         for (Release value : values) {
 119             if (version.equals(value.name())) runtimeVersionExists = true;
 120         }
 121         Assert.assertTrue(runtimeVersionExists);
 122     }
 123 
 124     @Test
 125     public void testAliasing() throws Exception {
 126         for (Release value : values) {
 127             System.err.println("test aliasing for Release " + value);
 128             String name = value.name();
 129             String prefix;
 130             if (name.equals("BASE")) {
 131                 prefix = "";
 132             } else if (name.equals("RUNTIME")) {
 133                 prefix = "META-INF/versions/" + MAJOR_VERSION + "/";
 134             } else {
 135                 prefix = "META-INF/versions/" + name.substring(8) + "/";
 136             }
 137             // test both multi-release jars
 138             readAndCompare(multirelease, value, "README", prefix + "README");
 139             readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class");
 140             // and signed multi-release jars
 141             readAndCompare(signedmultirelease, value, "README", prefix + "README");
 142             readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class");
 143         }
 144     }
 145 
 146     private void readAndCompare(File jar, Release version, String name, String realName) throws Exception {
 147         byte[] baseBytes;
 148         byte[] versionedBytes;
 149         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, Release.BASE)) {
 150             ZipEntry ze = jf.getEntry(realName);
 151             try (InputStream is = jf.getInputStream(ze)) {
 152                 baseBytes = is.readAllBytes();
 153             }
 154         }
 155         assert baseBytes.length > 0;
 156 
 157         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) {
 158             ZipEntry ze = jf.getEntry(name);
 159             try (InputStream is = jf.getInputStream(ze)) {
 160                 versionedBytes = is.readAllBytes();
 161             }
 162         }
 163         assert versionedBytes.length > 0;
 164 
 165         Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes));
 166     }
 167 
 168     @Test
 169     public void testNames() throws Exception {
 170         String rname = "version/Version.class";
 171         String vname = "META-INF/versions/9/version/Version.class";
 172         ZipEntry ze1;
 173         ZipEntry ze2;
 174         try (JarFile jf = new JarFile(multirelease)) {
 175             ze1 = jf.getEntry(vname);
 176         }
 177         Assert.assertEquals(ze1.getName(), vname);
 178         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
 179             ze2 = jf.getEntry(rname);
 180         }
 181         Assert.assertEquals(ze2.getName(), rname);
 182         Assert.assertNotEquals(ze1.getName(), ze2.getName());
 183     }
 184 }