jdk/test/tools/pack200/AttributeTests.java
changeset 6901 68f3d74dbda1
parent 6900 a3ca67586333
child 12544 5768f2e096de
equal deleted inserted replaced
6900:a3ca67586333 6901:68f3d74dbda1
    18  *
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
       
    24 import java.io.File;
    23 import java.io.File;
    25 import java.io.IOException;
    24 import java.io.IOException;
    26 import java.util.ArrayList;
    25 import java.util.ArrayList;
       
    26 import java.util.Arrays;
    27 import java.util.List;
    27 import java.util.List;
    28 
       
    29 /*
    28 /*
    30  * @test
    29  * @test
    31  * @bug 6982312
    30  * @bug 6982312
    32  * @summary tests various classfile format and attribute handling by pack200
    31  * @summary tests various classfile format and attribute handling by pack200
    33  * @compile -XDignore.symbol.file Utils.java AttributeTests.java
    32  * @compile -XDignore.symbol.file Utils.java AttributeTests.java
    34  * @run main AttributeTests
    33  * @run main AttributeTests
    35  * @author ksrini
    34  * @author ksrini
    36  */
    35  */
    37 
       
    38 
       
    39 public class AttributeTests {
    36 public class AttributeTests {
    40 
    37 
    41     public static void main(String... args) throws Exception {
    38     public static void main(String... args) throws Exception {
    42         test6982312();
    39         test6982312();
       
    40         test6746111();
    43     }
    41     }
    44     /*
    42     /*
    45      * This is an interim test, which ensures pack200 handles JSR-292 related
    43      * This is an interim test, which ensures pack200 handles JSR-292 related
    46      * classfile changes seamlessly, until all the classfile changes in jdk7
    44      * classfile changes seamlessly, until all the classfile changes in jdk7
    47      * and jdk8 are fully supported. At that time this test should be jettisoned,
    45      * and jdk8 are fully supported. At that time this test should be jettisoned,
    70          */
    68          */
    71         Utils.doCompareBitWise(dynJar.getAbsoluteFile(), testJar.getAbsoluteFile());
    69         Utils.doCompareBitWise(dynJar.getAbsoluteFile(), testJar.getAbsoluteFile());
    72         testJar.delete();
    70         testJar.delete();
    73         dynJar.delete();
    71         dynJar.delete();
    74     }
    72     }
       
    73 
       
    74     /*
       
    75      * this test checks to see if we get the expected strings for output
       
    76      */
       
    77     static void test6746111() throws Exception {
       
    78         String pack200Cmd = Utils.getPack200Cmd();
       
    79         File badAttrJar = new File(".", "badattr.jar");
       
    80         Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
       
    81         File testJar = new File(".", "test.jar");
       
    82         List<String> cmds = new ArrayList<String>();
       
    83         cmds.add(pack200Cmd);
       
    84         cmds.add("--repack");
       
    85         cmds.add("-v");
       
    86         cmds.add(testJar.getAbsolutePath());
       
    87         cmds.add(badAttrJar.getAbsolutePath());
       
    88         List<String> output = Utils.runExec(cmds);
       
    89         /*
       
    90          * compare the repacked jar bit-wise, as all the files
       
    91          * should be transmitted "as-is".
       
    92          */
       
    93         Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
       
    94         String[] expectedStrings = {
       
    95             "WARNING: Passing class file uncompressed due to unrecognized" +
       
    96                     " attribute: Foo.class",
       
    97             "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
       
    98                     "class attribute \"XourceFile\":  is unknown attribute " +
       
    99                     "in class Foo",
       
   100             "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
       
   101                     "AnnotationDefault: attribute length cannot be zero, in Test.message()",
       
   102             "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
       
   103         };
       
   104         List<String> notfoundList = new ArrayList<String>();
       
   105         notfoundList.addAll(Arrays.asList(expectedStrings));
       
   106         // make sure the expected messages are emitted
       
   107         for (String x : output) {
       
   108             findString(x, notfoundList, expectedStrings);
       
   109         }
       
   110         if (!notfoundList.isEmpty()) {
       
   111             System.out.println("Not found:");
       
   112             for (String x : notfoundList) {
       
   113                 System.out.println(x);
       
   114             }
       
   115             throw new Exception("Test fails: " + notfoundList.size() +
       
   116                     " expected strings not found");
       
   117         }
       
   118         testJar.delete();
       
   119         badAttrJar.delete();
       
   120     }
       
   121 
       
   122     private static void findString(String outputStr, List<String> notfoundList,
       
   123             String[] expectedStrings) {
       
   124         for (String y : expectedStrings) {
       
   125             if (outputStr.contains(y)) {
       
   126                 notfoundList.remove(y);
       
   127                 return;
       
   128             }
       
   129         }
       
   130     }
    75 }
   131 }