jdk/test/tools/pack200/AttributeTests.java
author ksrini
Tue, 19 Feb 2013 16:49:28 -0800
changeset 16013 3569e84e7429
parent 15261 c5b882836677
child 16050 1eee624cddb3
permissions -rw-r--r--
8008262: pack200 should support MethodParameters - part 2 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
/*
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;
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    24
import java.nio.charset.Charset;
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    25
import java.nio.file.Files;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    26
import java.util.ArrayList;
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    27
import java.util.Arrays;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    28
import java.util.List;
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    29
import static java.nio.file.StandardOpenOption.*;
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    30
/*
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    31
 * @test
16013
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    32
 * @bug 6746111 8005252 8008262
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    33
 * @summary tests various classfile format and attribute handling by pack200
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    34
 * @compile -XDignore.symbol.file Utils.java AttributeTests.java
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    35
 * @run main AttributeTests
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    36
 * @author ksrini
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 class AttributeTests {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    39
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    40
    public static void main(String... args) throws Exception {
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    41
        test6746111();
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    42
        testMethodParameters();
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
    43
    }
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    44
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    45
    /*
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    46
     * this tests ensure that MethodParameters produces by javac is packed
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    47
     * correctly. Usually this is not the case as new attributes are available
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    48
     * in the sdk jars, since MethodParameters happens to be an optional
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    49
     * attribute, thus this test.
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    50
     */
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    51
    static void testMethodParameters() throws Exception {
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    52
        List<String> scratch = new ArrayList<>();
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    53
        final String fname = "MP";
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    54
        String javaFileName = fname + Utils.JAVA_FILE_EXT;
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    55
        String javaClassName = fname + Utils.CLASS_FILE_EXT;
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    56
        scratch.add("class " + fname + " {");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    57
        scratch.add("void foo2(int j, final int k){}");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    58
        scratch.add("}");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    59
        File cwd = new File(".");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    60
        File javaFile = new File(cwd, javaFileName);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    61
        Files.write(javaFile.toPath(), scratch, Charset.defaultCharset(),
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    62
                CREATE, TRUNCATE_EXISTING);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    63
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    64
        Utils.compiler(javaFile.getName(), "-parameters");
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    65
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    66
        // jar the file up
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    67
        File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    68
        Utils.jar("cvf", testjarFile.getName(), javaClassName);
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    69
16013
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    70
        // pack using native --repack
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    71
        File nativejarFile = new File(cwd, "out-n" + Utils.JAR_FILE_EXT);
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    72
        Utils.repack(testjarFile, nativejarFile, false,
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    73
                     "--unknown-attribute=error");
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    74
        Utils.doCompareVerify(testjarFile, nativejarFile);
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    75
16013
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    76
        // pack using java --repack
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    77
        File javajarFile = new File(cwd, "out-j" + Utils.JAR_FILE_EXT);
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    78
        Utils.repack(testjarFile, javajarFile, true,
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    79
                     "--unknown-attribute=error");
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
    80
        Utils.doCompareBitWise(nativejarFile, javajarFile);
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    81
    }
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12544
diff changeset
    82
    /*
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    83
     * this test checks to see if we get the expected strings for output
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    84
     */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    85
    static void test6746111() throws Exception {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    86
        String pack200Cmd = Utils.getPack200Cmd();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    87
        File badAttrJar = new File(".", "badattr.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    88
        Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    89
        File testJar = new File(".", "test.jar");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    90
        List<String> cmds = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    91
        cmds.add(pack200Cmd);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    92
        cmds.add("--repack");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    93
        cmds.add("-v");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    94
        cmds.add(testJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    95
        cmds.add(badAttrJar.getAbsolutePath());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    96
        List<String> output = Utils.runExec(cmds);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    97
        /*
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    98
         * compare the repacked jar bit-wise, as all the files
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
    99
         * should be transmitted "as-is".
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   100
         */
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   101
        Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   102
        String[] expectedStrings = {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   103
            "WARNING: Passing class file uncompressed due to unrecognized" +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   104
                    " attribute: Foo.class",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   105
            "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   106
                    "class attribute \"XourceFile\":  is unknown attribute " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   107
                    "in class Foo",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   108
            "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   109
                    "AnnotationDefault: attribute length cannot be zero, in Test.message()",
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   110
            "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   111
        };
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   112
        List<String> notfoundList = new ArrayList<String>();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   113
        notfoundList.addAll(Arrays.asList(expectedStrings));
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   114
        // make sure the expected messages are emitted
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   115
        for (String x : output) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   116
            findString(x, notfoundList, expectedStrings);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   117
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   118
        if (!notfoundList.isEmpty()) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   119
            System.out.println("Not found:");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   120
            for (String x : notfoundList) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   121
                System.out.println(x);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   122
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   123
            throw new Exception("Test fails: " + notfoundList.size() +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   124
                    " expected strings not found");
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   125
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   126
        testJar.delete();
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   127
        badAttrJar.delete();
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
    private static void findString(String outputStr, List<String> notfoundList,
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   131
            String[] expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   132
        for (String y : expectedStrings) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   133
            if (outputStr.contains(y)) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   134
                notfoundList.remove(y);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   135
                return;
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   136
            }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   137
        }
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   138
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
diff changeset
   139
}