jdk/test/tools/pack200/TestNormal.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 27176 7009a264e019
child 38421 f75f729fdc5c
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     1
/*
27176
7009a264e019 8060432: tools/pack200/TestNormal.java fails on Windows with java.io.FileNotFoundException after JDK-8058854
ksrini
parents: 27035
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     4
 *
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     8
 *
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    13
 * accompanied this code).
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    14
 *
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    18
 *
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    21
 * questions.
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    22
 */
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    23
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    24
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    25
/*
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    26
 * @test
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    27
 * @run main/timeout=600 TestNormal
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    28
 * @bug 8020802
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    29
 * @summary Need an ability to create jar files that are invariant to the pack200 packing/unpacking
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    30
 * @author Alexander Zuev
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    31
 */
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    32
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    33
import java.io.*;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    34
import java.util.Collections;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    35
import java.util.Properties;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    36
import java.util.jar.JarEntry;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    37
import java.util.jar.JarFile;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    38
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    39
public class TestNormal {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    40
    private static String FS = File.separator;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    41
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    42
    public static void main(String args[]) throws Exception {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    43
        Properties p = System.getProperties();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    44
        String java_home = p.getProperty("test.jdk");
27176
7009a264e019 8060432: tools/pack200/TestNormal.java fails on Windows with java.io.FileNotFoundException after JDK-8058854
ksrini
parents: 27035
diff changeset
    45
        String testdir = Utils.TEST_CLS_DIR.getAbsolutePath();
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    46
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    47
        try {
27176
7009a264e019 8060432: tools/pack200/TestNormal.java fails on Windows with java.io.FileNotFoundException after JDK-8058854
ksrini
parents: 27035
diff changeset
    48
            execJavaCommand(java_home, "jar cnf normalized.jar -C " + testdir + " .");
7009a264e019 8060432: tools/pack200/TestNormal.java fails on Windows with java.io.FileNotFoundException after JDK-8058854
ksrini
parents: 27035
diff changeset
    49
            execJavaCommand(java_home, "jar cf original.jar -C " + testdir + " .");
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    50
            execJavaCommand(java_home, "pack200 -r repacked.jar original.jar");
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    51
            compareJars(new JarFile("normalized.jar"), new JarFile("repacked.jar"));
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    52
        } finally {
27176
7009a264e019 8060432: tools/pack200/TestNormal.java fails on Windows with java.io.FileNotFoundException after JDK-8058854
ksrini
parents: 27035
diff changeset
    53
            String[] cleanupList = {"normalized.jar", "original.jar", "repacked.jar"};
21348
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    54
            for (String s : cleanupList) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    55
                delete(new File(s));
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    56
            }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    57
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    58
    }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    59
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    60
    public static void execJavaCommand(String java_home, String cmd) throws Exception {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    61
        Process proc = Runtime.getRuntime().exec(java_home + FS + "bin" + FS + cmd);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    62
        String s;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    63
        BufferedReader stdInput =
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    64
                new BufferedReader(new InputStreamReader(proc.getInputStream()));
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    65
        BufferedReader stdError =
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    66
                new BufferedReader(new InputStreamReader(proc.getErrorStream()));
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    67
        while ((s = stdInput.readLine()) != null) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    68
            System.out.println(s);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    69
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    70
        while ((s = stdError.readLine()) != null) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    71
            System.err.println(s);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    72
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    73
    }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    74
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    75
    public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    76
        try {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    77
            if (jf1.size() != jf2.size()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    78
                throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    79
                        + " have different number of entries");
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    80
            }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    81
            for (JarEntry elem1 : Collections.list(jf1.entries())) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    82
                JarEntry elem2 = jf2.getJarEntry(elem1.getName());
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    83
                if (elem2 == null) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    84
                    throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    85
                }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    86
                if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    87
                    throw new Exception("The crc of " + elem1.getName() + " is different.");
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    88
                }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    89
            }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    90
        } finally {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    91
            jf1.close();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    92
            jf2.close();
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    93
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    94
    }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    95
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    96
    static void delete(File f) throws IOException {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    97
        if (!f.exists()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    98
            return;
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
    99
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   100
        if (f.isDirectory()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   101
            for (File c : f.listFiles()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   102
                delete(c);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   103
            }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   104
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   105
        if (!f.delete()) {
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   106
            throw new FileNotFoundException("Failed to delete file: " + f);
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   107
        }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   108
    }
e30c5696b4c5 8020802: Need an ability to create jar files that are invariant to the pack200 packing/unpacking
kizune
parents:
diff changeset
   109
}