jdk/test/java/util/jar/JarFile/MultiReleaseJarAPI.java
changeset 36129 332b49163fc9
child 36219 437e72684a42
equal deleted inserted replaced
36128:e17994ab030a 36129:332b49163fc9
       
     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 
       
    43 import static java.util.jar.JarFile.Release;
       
    44 import static sun.misc.Version.jdkMajorVersion;  // fixme JEP 223 Version
       
    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     String userdir = System.getProperty("user.dir",".");
       
    54     File unversioned = new File(userdir, "unversioned.jar");
       
    55     File multirelease = new File(userdir, "multi-release.jar");
       
    56     File signedmultirelease = new File(userdir, "signed-multi-release.jar");
       
    57     Release[] values = JarFile.Release.values();
       
    58 
       
    59 
       
    60     @BeforeClass
       
    61     public void initialize() throws Exception {
       
    62         CreateMultiReleaseTestJars creator =  new CreateMultiReleaseTestJars();
       
    63         creator.compileEntries();
       
    64         creator.buildUnversionedJar();
       
    65         creator.buildMultiReleaseJar();
       
    66         creator.buildSignedMultiReleaseJar();
       
    67     }
       
    68 
       
    69     @AfterClass
       
    70     public void close() throws IOException {
       
    71         Files.delete(unversioned.toPath());
       
    72         Files.delete(multirelease.toPath());
       
    73         Files.delete(signedmultirelease.toPath());
       
    74     }
       
    75 
       
    76     @Test
       
    77     public void isMultiReleaseJar() throws Exception {
       
    78         try (JarFile jf = new JarFile(unversioned)) {
       
    79             Assert.assertFalse(jf.isMultiRelease());
       
    80         }
       
    81 
       
    82         try (JarFile jf = new JarFile(multirelease)) {
       
    83             Assert.assertTrue(jf.isMultiRelease());
       
    84         }
       
    85     }
       
    86 
       
    87     @Test
       
    88     public void testVersioning() throws Exception {
       
    89         // multi-release jar
       
    90         JarFile jar = new JarFile(multirelease);
       
    91         Assert.assertEquals(Release.BASE, jar.getVersion());
       
    92         jar.close();
       
    93 
       
    94         for (Release value : values) {
       
    95             System.err.println("test versioning for Release " + value);
       
    96             try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) {
       
    97                 Assert.assertEquals(value, jf.getVersion());
       
    98             }
       
    99         }
       
   100 
       
   101         // regular, unversioned, jar
       
   102         for (Release value : values) {
       
   103             try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) {
       
   104                 Assert.assertEquals(Release.BASE, jf.getVersion());
       
   105             }
       
   106         }
       
   107 
       
   108         // assure that we have a Release object corresponding to the actual runtime version
       
   109         String version = "VERSION_" + jdkMajorVersion();
       
   110         boolean runtimeVersionExists = false;
       
   111         for (Release value : values) {
       
   112             if (version.equals(value.name())) runtimeVersionExists = true;
       
   113         }
       
   114         Assert.assertTrue(runtimeVersionExists);
       
   115     }
       
   116 
       
   117     @Test
       
   118     public void testAliasing() throws Exception {
       
   119         for (Release value : values) {
       
   120             System.err.println("test aliasing for Release " + value);
       
   121             String name = value.name();
       
   122             String prefix;
       
   123             if (name.equals("BASE")) {
       
   124                 prefix = "";
       
   125             } else if (name.equals("RUNTIME")) {
       
   126                 prefix = "META-INF/versions/" + jdkMajorVersion() + "/";
       
   127             } else {
       
   128                 prefix = "META-INF/versions/" + name.substring(8) + "/";
       
   129             }
       
   130             // test both multi-release jars
       
   131             readAndCompare(multirelease, value, "README", prefix + "README");
       
   132             readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class");
       
   133             // and signed multi-release jars
       
   134             readAndCompare(signedmultirelease, value, "README", prefix + "README");
       
   135             readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class");
       
   136         }
       
   137     }
       
   138 
       
   139     private void readAndCompare(File jar, Release version, String name, String realName) throws Exception {
       
   140         byte[] baseBytes;
       
   141         byte[] versionedBytes;
       
   142         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, Release.BASE)) {
       
   143             ZipEntry ze = jf.getEntry(realName);
       
   144             try (InputStream is = jf.getInputStream(ze)) {
       
   145                 baseBytes = is.readAllBytes();
       
   146             }
       
   147         }
       
   148         assert baseBytes.length > 0;
       
   149 
       
   150         try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) {
       
   151             ZipEntry ze = jf.getEntry(name);
       
   152             try (InputStream is = jf.getInputStream(ze)) {
       
   153                 versionedBytes = is.readAllBytes();
       
   154             }
       
   155         }
       
   156         assert versionedBytes.length > 0;
       
   157 
       
   158         Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes));
       
   159     }
       
   160 
       
   161     @Test
       
   162     public void testNames() throws Exception {
       
   163         String rname = "version/Version.class";
       
   164         String vname = "META-INF/versions/9/version/Version.class";
       
   165         ZipEntry ze1;
       
   166         ZipEntry ze2;
       
   167         try (JarFile jf = new JarFile(multirelease)) {
       
   168             ze1 = jf.getEntry(vname);
       
   169         }
       
   170         Assert.assertEquals(ze1.getName(), vname);
       
   171         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
       
   172             ze2 = jf.getEntry(rname);
       
   173         }
       
   174         Assert.assertEquals(ze2.getName(), rname);
       
   175         Assert.assertNotEquals(ze1.getName(), ze2.getName());
       
   176     }
       
   177 }