jdk/test/sun/misc/JarIndex/JarIndexMergeTest.java
changeset 13030 d4cdee9d5740
child 30820 0d4717a011d3
equal deleted inserted replaced
13029:dfd934f86768 13030:d4cdee9d5740
       
     1 /*
       
     2  * Copyright (c) 2012, 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 6901992
       
    27  * @compile -XDignore.symbol.file JarIndexMergeTest.java
       
    28  * @run main JarIndexMergeTest
       
    29  * @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
       
    30  * @author  Diego Belfer
       
    31  */
       
    32 
       
    33 import java.io.File;
       
    34 import java.io.FileNotFoundException;
       
    35 import java.io.FileOutputStream;
       
    36 import java.io.IOException;
       
    37 import java.util.LinkedList;
       
    38 import java.util.jar.JarEntry;
       
    39 import java.util.jar.JarOutputStream;
       
    40 // implementation specific API
       
    41 import sun.misc.JarIndex;
       
    42 
       
    43 public class JarIndexMergeTest {
       
    44     static final String slash = File.separator;
       
    45     static final String testClassesDir = System.getProperty("test.classes", ".");
       
    46     static final File tmpFolder = new File(testClassesDir);
       
    47 
       
    48     public static void main(String[] args) throws Exception {
       
    49         File jar1 = buildJar1();
       
    50         File jar2 = buildJar2();
       
    51 
       
    52         JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() });
       
    53         JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() });
       
    54 
       
    55         jarIndex1.merge(jarIndex2, null);
       
    56 
       
    57         assertFileResolved(jarIndex2, "com/test1/resource1.file",
       
    58                            jar1.getAbsolutePath());
       
    59         assertFileResolved(jarIndex2, "com/test2/resource2.file",
       
    60                            jar2.getAbsolutePath());
       
    61     }
       
    62 
       
    63     static void assertFileResolved(JarIndex jarIndex2, String file,
       
    64                                    String jarName) {
       
    65         @SuppressWarnings("unchecked")
       
    66         LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file);
       
    67         if (jarLists == null || jarLists.size() == 0 ||
       
    68             !jarName.equals(jarLists.get(0))) {
       
    69             throw new RuntimeException(
       
    70                 "Unexpected result: the merged index must resolve file: "
       
    71                 + file);
       
    72         }
       
    73     }
       
    74 
       
    75     private static File buildJar1() throws FileNotFoundException, IOException {
       
    76         JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1-merge.jar");
       
    77         jar1Builder.addResourceFile("com/test1/resource1.file", "resource1");
       
    78         return jar1Builder.build();
       
    79     }
       
    80 
       
    81     private static File buildJar2() throws FileNotFoundException, IOException {
       
    82         JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2-merge.jar");
       
    83         jar2Builder.addResourceFile("com/test2/resource2.file", "resource2");
       
    84         return jar2Builder.build();
       
    85     }
       
    86 
       
    87     /*
       
    88      * Helper class for building jar files
       
    89      */
       
    90     public static class JarBuilder {
       
    91         private JarOutputStream os;
       
    92         private File jarFile;
       
    93 
       
    94         public JarBuilder(File tmpFolder, String jarName)
       
    95             throws FileNotFoundException, IOException
       
    96         {
       
    97             this.jarFile = new File(tmpFolder, jarName);
       
    98             this.os = new JarOutputStream(new FileOutputStream(jarFile));
       
    99         }
       
   100 
       
   101         public void addResourceFile(String pathFromRoot, String content)
       
   102             throws IOException
       
   103         {
       
   104             JarEntry entry = new JarEntry(pathFromRoot);
       
   105             os.putNextEntry(entry);
       
   106             os.write(content.getBytes("ASCII"));
       
   107             os.closeEntry();
       
   108         }
       
   109 
       
   110         public File build() throws IOException {
       
   111             os.close();
       
   112             return jarFile;
       
   113         }
       
   114     }
       
   115 }
       
   116