jdk/test/java/util/jar/JarFile/MultiReleaseJarIterators.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 MultiReleaseJarIterators
       
    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.Enumeration;
       
    39 import java.util.HashMap;
       
    40 import java.util.Map;
       
    41 import java.util.jar.JarEntry;
       
    42 import java.util.jar.JarFile;
       
    43 import java.util.stream.Collectors;
       
    44 import java.util.zip.ZipFile;
       
    45 
       
    46 import static java.util.jar.JarFile.Release;
       
    47 import static sun.misc.Version.jdkMajorVersion;  // fixme JEP 223 Version
       
    48 
       
    49 import org.testng.Assert;
       
    50 import org.testng.annotations.AfterClass;
       
    51 import org.testng.annotations.BeforeClass;
       
    52 import org.testng.annotations.Test;
       
    53 
       
    54 
       
    55 public class MultiReleaseJarIterators {
       
    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     Map<String,JarEntry> uvEntries = new HashMap<>();
       
    60     Map<String,JarEntry> mrEntries = new HashMap<>();
       
    61     Map<String,JarEntry> baseEntries = new HashMap<>();
       
    62     Map<String,JarEntry> v9Entries = new HashMap<>();
       
    63     Map<String, JarEntry> v10Entries = new HashMap<>();
       
    64 
       
    65     @BeforeClass
       
    66     public void initialize() throws Exception {
       
    67         CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
       
    68         creator.compileEntries();
       
    69         creator.buildUnversionedJar();
       
    70         creator.buildMultiReleaseJar();
       
    71 
       
    72         try (JarFile jf = new JarFile(multirelease)) {
       
    73             for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
       
    74                 JarEntry je = e.nextElement();
       
    75                 String name = je.getName();
       
    76                 mrEntries.put(name, je);
       
    77                 if (name.startsWith("META-INF/versions/")) {
       
    78                     if (name.startsWith("META-INF/versions/9/")) {
       
    79                         v9Entries.put(name.substring(20), je);
       
    80                     } else if (name.startsWith("META-INF/versions/10/")) {
       
    81                         v10Entries.put(name.substring(21), je);
       
    82                     }
       
    83                 } else {
       
    84                     baseEntries.put(name, je);
       
    85                 }
       
    86             }
       
    87         }
       
    88         Assert.assertEquals(mrEntries.size(), 14);
       
    89         Assert.assertEquals(baseEntries.size(), 6);
       
    90         Assert.assertEquals(v9Entries.size(), 5);
       
    91         Assert.assertEquals(v10Entries.size(), 3);
       
    92 
       
    93         try (JarFile jf = new JarFile(unversioned)) {
       
    94             jf.entries().asIterator().forEachRemaining(je -> uvEntries.put(je.getName(), je));
       
    95         }
       
    96         Assert.assertEquals(uvEntries.size(), 6);
       
    97     }
       
    98 
       
    99     @AfterClass
       
   100     public void close() throws IOException {
       
   101         Files.delete(unversioned.toPath());
       
   102         Files.delete(multirelease.toPath());
       
   103     }
       
   104 
       
   105     @Test
       
   106     public void testMultiReleaseJar() throws IOException {
       
   107         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ)) {
       
   108             testEnumeration(jf, mrEntries);
       
   109             testStream(jf, mrEntries);
       
   110         }
       
   111 
       
   112         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.BASE)) {
       
   113             testEnumeration(jf, baseEntries);
       
   114             testStream(jf, baseEntries);
       
   115         }
       
   116 
       
   117         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
       
   118             testEnumeration(jf, v9Entries);
       
   119             testStream(jf, v9Entries);
       
   120         }
       
   121 
       
   122         try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
       
   123             Map<String,JarEntry> expectedEntries;
       
   124             switch (jdkMajorVersion()) {
       
   125                 case 9:
       
   126                     expectedEntries = v9Entries;
       
   127                     break;
       
   128                 case 10:  // won't get here until JDK 10
       
   129                     expectedEntries = v10Entries;
       
   130                     break;
       
   131                 default:
       
   132                     expectedEntries = baseEntries;
       
   133                     break;
       
   134             }
       
   135 
       
   136             testEnumeration(jf, expectedEntries);
       
   137             testStream(jf, expectedEntries);
       
   138         }
       
   139     }
       
   140 
       
   141     @Test
       
   142     public void testUnversionedJar() throws IOException {
       
   143         try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ)) {
       
   144             testEnumeration(jf, uvEntries);
       
   145             testStream(jf, uvEntries);
       
   146         }
       
   147 
       
   148         try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Release.BASE)) {
       
   149             testEnumeration(jf, uvEntries);
       
   150             testStream(jf, uvEntries);
       
   151         }
       
   152 
       
   153         try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Release.VERSION_9)) {
       
   154             testEnumeration(jf, uvEntries);
       
   155             testStream(jf, uvEntries);
       
   156         }
       
   157 
       
   158         try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Release.RUNTIME)) {
       
   159             testEnumeration(jf, uvEntries);
       
   160             testStream(jf, uvEntries);
       
   161         }
       
   162     }
       
   163 
       
   164     private void testEnumeration(JarFile jf, Map<String,JarEntry> expectedEntries) {
       
   165         Map<String, JarEntry> actualEntries = new HashMap<>();
       
   166         for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
       
   167             JarEntry je = e.nextElement();
       
   168             actualEntries.put(je.getName(), je);
       
   169         }
       
   170 
       
   171         testEntries(jf, actualEntries, expectedEntries);
       
   172     }
       
   173 
       
   174 
       
   175     private void testStream(JarFile jf, Map<String,JarEntry> expectedEntries) {
       
   176         Map<String,JarEntry> actualEntries = jf.stream().collect(Collectors.toMap(je -> je.getName(), je -> je));
       
   177 
       
   178         testEntries(jf, actualEntries, expectedEntries);
       
   179     }
       
   180 
       
   181     private void testEntries(JarFile jf, Map<String,JarEntry> actualEntries, Map<String,JarEntry> expectedEntries) {
       
   182         /* For multi-release jar files constructed with a Release object,
       
   183          * actualEntries contain versionedEntries that are considered part of the
       
   184          * public API.  They have a 1-1 correspondence with baseEntries,
       
   185          * so entries that are not part of the public API won't be present,
       
   186          * i.e. those entries with a name that starts with version/PackagePrivate
       
   187          * in this particular jar file (multi-release.jar)
       
   188          */
       
   189 
       
   190         Map<String,JarEntry> entries;
       
   191         if (expectedEntries == mrEntries) {
       
   192             Assert.assertEquals(actualEntries.size(), mrEntries.size());
       
   193             entries = mrEntries;
       
   194         } else if (expectedEntries == uvEntries) {
       
   195             Assert.assertEquals(actualEntries.size(), uvEntries.size());
       
   196             entries = uvEntries;
       
   197         } else {
       
   198             Assert.assertEquals(actualEntries.size(), baseEntries.size());  // this is correct
       
   199             entries = baseEntries;
       
   200         }
       
   201 
       
   202         entries.keySet().forEach(name -> {
       
   203             JarEntry ee = expectedEntries.get(name);
       
   204             if (ee == null) ee = entries.get(name);
       
   205             JarEntry ae = actualEntries.get(name);
       
   206             try {
       
   207                 compare(jf, ae, ee);
       
   208             } catch (IOException x) {
       
   209                 throw new RuntimeException(x);
       
   210             }
       
   211         });
       
   212     }
       
   213 
       
   214     private void compare(JarFile jf, JarEntry actual, JarEntry expected) throws IOException {
       
   215         byte[] abytes;
       
   216         byte[] ebytes;
       
   217 
       
   218         try (InputStream is = jf.getInputStream(actual)) {
       
   219             abytes = is.readAllBytes();
       
   220         }
       
   221 
       
   222         try (InputStream is = jf.getInputStream(expected)) {
       
   223             ebytes = is.readAllBytes();
       
   224         }
       
   225 
       
   226         Assert.assertEquals(abytes, ebytes);
       
   227     }
       
   228 }