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.assertTrue(jf.isMultiRelease());
  87         }
  88     }
  89 
  90     @Test
  91     public void testVersioning() throws Exception {
  92         // multi-release jar
  93         JarFile jar = new JarFile(multirelease);
  94         Assert.assertEquals(Release.BASE, jar.getVersion());
  95         jar.close();
  96 
  97         for (Release value : values) {
  98             System.err.println("test versioning for Release " + value);
  99             try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) {
 100                 Assert.assertEquals(value, jf.getVersion());
 101             }
 102         }
 103 
 104         // regular, unversioned, jar
 105         for (Release value : values) {
 106             try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) {
 107                 Assert.assertEquals(Release.BASE, jf.getVersion());
 108             }
 109         }
 110 
 111         // assure that we have a Release object corresponding to the actual runtime version
 112         String version = "VERSION_" + MAJOR_VERSION;
 113         boolean runtimeVersionExists = false;
 114         for (Release value : values) {
 115             if (version.equals(value.name())) runtimeVersionExists = true;
 116         }
 117         Assert.assertTrue(runtimeVersionExists);
 118     }
 119 
 120     @Test
 121     public void testAliasing() throws Exception {
 122         for (Release value : values) {
 123             System.err.println("test aliasing for Release " + value);
 124             String name = value.name();
 125             String prefix;
 126             if (name.equals("BASE")) {
 127                 prefix = "";
 128             } else if (name.equals("RUNTIME")) {
 129                 prefix = "META-INF/versions/" + MAJOR_VERSION + "/";
 130             } else {
 131                 prefix = "META-INF/versions/" + name.substring(8) + "/";
 132             }
 133             // test both multi-release jars
 134             readAndCompare(multirelease, value, "README", prefix + "README");
 135             readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class");
 136             // and signed multi-release jars
 137             readAndCompare(signedmultirelease, value, "README", prefix + "README");
 138             readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class");
 139         }
 140     }
 141 
 142     private void readAndCompare(File jar, Release version, String name, String realName) throws Exception {
 143         byte[] baseBytes;
 144         byte[] versionedBytes;
 145         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, Release.BASE)) {
 146             ZipEntry ze = jf.getEntry(realName);
 147             try (InputStream is = jf.getInputStream(ze)) {
 148                 baseBytes = is.readAllBytes();
 149             }
 150         }
 151         assert baseBytes.length > 0;
 152 
 153         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) {
 154             ZipEntry ze = jf.getEntry(name);
 155             try (InputStream is = jf.getInputStream(ze)) {
 156                 versionedBytes = is.readAllBytes();
 157             }
 158         }
 159         assert versionedBytes.length > 0;
 160 
 161         Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes));
 162     }
 163 
 164     @Test
 165     public void testNames() throws Exception {
 166         String rname = "version/Version.class";
 167         String vname = "META-INF/versions/9/version/Version.class";
 168         ZipEntry ze1;
 169         ZipEntry ze2;
 170         try (JarFile jf = new JarFile(multirelease)) {
 171             ze1 = jf.getEntry(vname);
 172         }
 173         Assert.assertEquals(ze1.getName(), vname);
 174         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
 175             ze2 = jf.getEntry(rname);
 176         }
 177         Assert.assertEquals(ze2.getName(), rname);
 178         Assert.assertNotEquals(ze1.getName(), ze2.getName());
 179     }
 180 }