jdk/test/tools/pack200/AttributeTests.java
author ksrini
Thu, 14 Oct 2010 14:55:03 -0700
changeset 6901 68f3d74dbda1
parent 6900 a3ca67586333
child 12544 5768f2e096de
permissions -rw-r--r--
6746111: Improve pack200 error message Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     1
/*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     4
 *
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     8
 *
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    13
 * accompanied this code).
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    14
 *
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    18
 *
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    21
 * questions.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    22
 */
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    23
import java.io.File;
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    24
import java.io.IOException;
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    25
import java.util.ArrayList;
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    26
import java.util.Arrays;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    27
import java.util.List;
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    28
/*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    29
 * @test
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    30
 * @bug 6982312
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    31
 * @summary tests various classfile format and attribute handling by pack200
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    32
 * @compile -XDignore.symbol.file Utils.java AttributeTests.java
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    33
 * @run main AttributeTests
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    34
 * @author ksrini
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    35
 */
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    36
public class AttributeTests {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    37
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    38
    public static void main(String... args) throws Exception {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    39
        test6982312();
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    40
        test6746111();
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    41
    }
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    42
    /*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    43
     * This is an interim test, which ensures pack200 handles JSR-292 related
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    44
     * classfile changes seamlessly, until all the classfile changes in jdk7
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    45
     * and jdk8 are fully supported. At that time this test should be jettisoned,
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    46
     * along with the associated jar file.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    47
     *
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    48
     * The jar file  contains sources and classes noting the classes were
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    49
     * derived by using the javac from the lambda project,
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    50
     * see http://openjdk.java.net/projects/lambda/.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    51
     * Therefore the classes contained in the jar cannot be compiled, using
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    52
     * the standard jdk7's javac compiler.
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    53
     */
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    54
    static void test6982312() throws IOException {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    55
        String pack200Cmd = Utils.getPack200Cmd();
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    56
        File dynJar = new File(".", "dyn.jar");
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    57
        Utils.copyFile(new File(Utils.TEST_SRC_DIR, "dyn.jar"), dynJar);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    58
        File testJar = new File(".", "test.jar");
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    59
        List<String> cmds = new ArrayList<String>();
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    60
        cmds.add(pack200Cmd);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    61
        cmds.add("--repack");
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    62
        cmds.add(testJar.getAbsolutePath());
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    63
        cmds.add(dynJar.getAbsolutePath());
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    64
        Utils.runExec(cmds);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    65
        /*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    66
         * compare the repacked jar bit-wise, as all the files
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    67
         * should be transmitted "as-is".
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    68
         */
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    69
        Utils.doCompareBitWise(dynJar.getAbsoluteFile(), testJar.getAbsoluteFile());
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    70
        testJar.delete();
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    71
        dynJar.delete();
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    72
    }
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    73
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    74
    /*
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    75
     * this test checks to see if we get the expected strings for output
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    76
     */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    77
    static void test6746111() throws Exception {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    78
        String pack200Cmd = Utils.getPack200Cmd();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    79
        File badAttrJar = new File(".", "badattr.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    80
        Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    81
        File testJar = new File(".", "test.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    82
        List<String> cmds = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    83
        cmds.add(pack200Cmd);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    84
        cmds.add("--repack");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    85
        cmds.add("-v");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    86
        cmds.add(testJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    87
        cmds.add(badAttrJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    88
        List<String> output = Utils.runExec(cmds);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    89
        /*
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    90
         * compare the repacked jar bit-wise, as all the files
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    91
         * should be transmitted "as-is".
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    92
         */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    93
        Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    94
        String[] expectedStrings = {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    95
            "WARNING: Passing class file uncompressed due to unrecognized" +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    96
                    " attribute: Foo.class",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    97
            "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    98
                    "class attribute \"XourceFile\":  is unknown attribute " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    99
                    "in class Foo",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   100
            "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   101
                    "AnnotationDefault: attribute length cannot be zero, in Test.message()",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   102
            "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   103
        };
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   104
        List<String> notfoundList = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   105
        notfoundList.addAll(Arrays.asList(expectedStrings));
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   106
        // make sure the expected messages are emitted
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   107
        for (String x : output) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   108
            findString(x, notfoundList, expectedStrings);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   109
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   110
        if (!notfoundList.isEmpty()) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   111
            System.out.println("Not found:");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   112
            for (String x : notfoundList) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   113
                System.out.println(x);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   114
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   115
            throw new Exception("Test fails: " + notfoundList.size() +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   116
                    " expected strings not found");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   117
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   118
        testJar.delete();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   119
        badAttrJar.delete();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   120
    }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   121
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   122
    private static void findString(String outputStr, List<String> notfoundList,
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   123
            String[] expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   124
        for (String y : expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   125
            if (outputStr.contains(y)) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   126
                notfoundList.remove(y);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   127
                return;
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   128
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   129
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   130
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
   131
}