jdk/test/sun/misc/JarIndex/JarIndexMergeTest.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 13030 d4cdee9d5740
child 30820 0d4717a011d3
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13030
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     1
/*
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     4
 *
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     8
 *
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    13
 * accompanied this code).
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    14
 *
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    18
 *
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    21
 * questions.
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    22
 */
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    23
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    24
/*
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    25
 * @test
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    26
 * @bug 6901992
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    27
 * @compile -XDignore.symbol.file JarIndexMergeTest.java
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    28
 * @run main JarIndexMergeTest
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    29
 * @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    30
 * @author  Diego Belfer
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    31
 */
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    32
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    33
import java.io.File;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    34
import java.io.FileNotFoundException;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    35
import java.io.FileOutputStream;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    36
import java.io.IOException;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    37
import java.util.LinkedList;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    38
import java.util.jar.JarEntry;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    39
import java.util.jar.JarOutputStream;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    40
// implementation specific API
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    41
import sun.misc.JarIndex;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    42
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    43
public class JarIndexMergeTest {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    44
    static final String slash = File.separator;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    45
    static final String testClassesDir = System.getProperty("test.classes", ".");
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    46
    static final File tmpFolder = new File(testClassesDir);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    47
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    49
        File jar1 = buildJar1();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    50
        File jar2 = buildJar2();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    51
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    52
        JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() });
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    53
        JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() });
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    54
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    55
        jarIndex1.merge(jarIndex2, null);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    56
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    57
        assertFileResolved(jarIndex2, "com/test1/resource1.file",
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    58
                           jar1.getAbsolutePath());
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    59
        assertFileResolved(jarIndex2, "com/test2/resource2.file",
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    60
                           jar2.getAbsolutePath());
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    61
    }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    62
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    63
    static void assertFileResolved(JarIndex jarIndex2, String file,
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    64
                                   String jarName) {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    65
        @SuppressWarnings("unchecked")
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    66
        LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    67
        if (jarLists == null || jarLists.size() == 0 ||
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    68
            !jarName.equals(jarLists.get(0))) {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    69
            throw new RuntimeException(
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    70
                "Unexpected result: the merged index must resolve file: "
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    71
                + file);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    72
        }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    73
    }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    74
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    75
    private static File buildJar1() throws FileNotFoundException, IOException {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    76
        JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1-merge.jar");
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    77
        jar1Builder.addResourceFile("com/test1/resource1.file", "resource1");
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    78
        return jar1Builder.build();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    79
    }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    80
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    81
    private static File buildJar2() throws FileNotFoundException, IOException {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    82
        JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2-merge.jar");
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    83
        jar2Builder.addResourceFile("com/test2/resource2.file", "resource2");
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    84
        return jar2Builder.build();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    85
    }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    86
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    87
    /*
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    88
     * Helper class for building jar files
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    89
     */
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    90
    public static class JarBuilder {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    91
        private JarOutputStream os;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    92
        private File jarFile;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    93
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    94
        public JarBuilder(File tmpFolder, String jarName)
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    95
            throws FileNotFoundException, IOException
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    96
        {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    97
            this.jarFile = new File(tmpFolder, jarName);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    98
            this.os = new JarOutputStream(new FileOutputStream(jarFile));
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
    99
        }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   100
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   101
        public void addResourceFile(String pathFromRoot, String content)
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   102
            throws IOException
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   103
        {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   104
            JarEntry entry = new JarEntry(pathFromRoot);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   105
            os.putNextEntry(entry);
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   106
            os.write(content.getBytes("ASCII"));
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   107
            os.closeEntry();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   108
        }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   109
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   110
        public File build() throws IOException {
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   111
            os.close();
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   112
            return jarFile;
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   113
        }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   114
    }
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   115
}
d4cdee9d5740 6901992: InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
chegar
parents:
diff changeset
   116