jdk/test/tools/pack200/AttributeTests.java
author darcy
Wed, 29 Apr 2015 10:25:53 -0700
changeset 30046 cf2c86e1819e
parent 18594 b6a3c9f71ac8
permissions -rw-r--r--
8078334: Mark regression tests using randomness Reviewed-by: xuelei, alanb
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
/*
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
6900
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.util.ArrayList;
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    25
import java.util.Arrays;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    26
import java.util.List;
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    27
/*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    28
 * @test
16013
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    29
 * @bug 6746111 8005252 8008262
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    30
 * @summary tests various classfile format and attribute handling by pack200
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    31
 * @compile -XDignore.symbol.file Utils.java AttributeTests.java
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    32
 * @run main AttributeTests
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    33
 * @author ksrini
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    34
 */
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    35
public class AttributeTests {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    36
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    38
        test6746111();
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    39
        testMethodParameters();
18594
b6a3c9f71ac8 8017463: [TEST_BUG] 2 tests from tools/pack200/ remain about 1 GB of data in work directory after execution
ksrini
parents: 17490
diff changeset
    40
        Utils.cleanup();
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    41
    }
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    42
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    43
    /*
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    44
     * this tests ensure that MethodParameters produces by javac is packed
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    45
     * correctly. Usually this is not the case as new attributes are available
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    46
     * in the sdk jars, since MethodParameters happens to be an optional
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    47
     * attribute, thus this test.
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    48
     */
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    49
    static void testMethodParameters() throws Exception {
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    50
        List<String> scratch = new ArrayList<>();
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    51
        final String fname = "MP";
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    52
        String javaFileName = fname + Utils.JAVA_FILE_EXT;
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    53
        String javaClassName = fname + Utils.CLASS_FILE_EXT;
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    54
        scratch.add("class " + fname + " {");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    55
        scratch.add("void foo2(int j, final int k){}");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    56
        scratch.add("}");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    57
        File cwd = new File(".");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    58
        File javaFile = new File(cwd, javaFileName);
17490
46864558d068 8001163: [pack200] should support attributes introduced by JSR-308
ksrini
parents: 16050
diff changeset
    59
        Utils.createFile(javaFile, scratch);
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    60
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    61
        Utils.compiler(javaFile.getName(), "-parameters");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    62
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    63
        // jar the file up
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    64
        File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    65
        Utils.jar("cvf", testjarFile.getName(), javaClassName);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    66
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
    67
        Utils.testWithRepack(testjarFile, "--unknown-attribute=error");
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    68
    }
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    69
    /*
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    70
     * this test checks to see if we get the expected strings for output
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    71
     */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    72
    static void test6746111() throws Exception {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    73
        String pack200Cmd = Utils.getPack200Cmd();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    74
        File badAttrJar = new File(".", "badattr.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    75
        Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    76
        File testJar = new File(".", "test.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    77
        List<String> cmds = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    78
        cmds.add(pack200Cmd);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    79
        cmds.add("--repack");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    80
        cmds.add("-v");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    81
        cmds.add(testJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    82
        cmds.add(badAttrJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    83
        List<String> output = Utils.runExec(cmds);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    84
        /*
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    85
         * compare the repacked jar bit-wise, as all the files
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    86
         * should be transmitted "as-is".
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    87
         */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    88
        Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    89
        String[] expectedStrings = {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    90
            "WARNING: Passing class file uncompressed due to unrecognized" +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    91
                    " attribute: Foo.class",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    92
            "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    93
                    "class attribute \"XourceFile\":  is unknown attribute " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    94
                    "in class Foo",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    95
            "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    96
                    "AnnotationDefault: attribute length cannot be zero, in Test.message()",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    97
            "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    98
        };
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    99
        List<String> notfoundList = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   100
        notfoundList.addAll(Arrays.asList(expectedStrings));
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   101
        // make sure the expected messages are emitted
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   102
        for (String x : output) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   103
            findString(x, notfoundList, expectedStrings);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   104
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   105
        if (!notfoundList.isEmpty()) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   106
            System.out.println("Not found:");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   107
            for (String x : notfoundList) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   108
                System.out.println(x);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   109
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   110
            throw new Exception("Test fails: " + notfoundList.size() +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   111
                    " expected strings not found");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   112
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   113
        testJar.delete();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   114
        badAttrJar.delete();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   115
    }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   116
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   117
    private static void findString(String outputStr, List<String> notfoundList,
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   118
            String[] expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   119
        for (String y : expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   120
            if (outputStr.contains(y)) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   121
                notfoundList.remove(y);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   122
                return;
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   123
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   124
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   125
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
   126
}