6746111: Improve pack200 error message
authorksrini
Thu, 14 Oct 2010 14:55:03 -0700
changeset 6901 68f3d74dbda1
parent 6900 a3ca67586333
child 6903 9a677f58dc85
6746111: Improve pack200 error message Reviewed-by: jrose
jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java
jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java
jdk/test/tools/pack200/AttributeTests.java
jdk/test/tools/pack200/badattr.jar
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java	Thu Oct 14 14:41:08 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/Attribute.java	Thu Oct 14 14:55:03 2010 -0700
@@ -654,8 +654,8 @@
         String layout;
         public FormatException(String message,
                                int ctype, String name, String layout) {
-            super(ATTR_CONTEXT_NAME[ctype]+"."+name
-                  +(message == null? "": (": "+message)));
+            super(ATTR_CONTEXT_NAME[ctype]+ " attribute \"" + name + "\"" +
+                  (message == null? "" : (": " + message)));
             this.ctype = ctype;
             this.name = name;
             this.layout = layout;
--- a/jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java	Thu Oct 14 14:41:08 2010 -0700
+++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java	Thu Oct 14 14:55:03 2010 -0700
@@ -30,6 +30,7 @@
 import com.sun.java.util.jar.pack.Package.Class;
 import com.sun.java.util.jar.pack.Package.InnerClass;
 import com.sun.java.util.jar.pack.ConstantPool.*;
+import com.sun.tools.classfile.AttributeException;
 
 /**
  * Reader for a class file that is being incorporated into a package.
@@ -405,7 +406,7 @@
                     skip(length, "unknown "+name+" attribute in "+h);
                     continue;
                 } else {
-                    String message = "unknown in "+h;
+                    String message = " is unknown attribute in class " + h;
                     throw new Attribute.FormatException(message, ctype, name,
                                                         unknownAttrCommand);
                 }
@@ -434,6 +435,10 @@
                 in.readFully(bytes);
                 a = a.addContent(bytes);
             }
+            if (a.size() == 0 && !a.layout().isEmpty()) {
+                throw new ClassFormatException(name +
+                        ": attribute length cannot be zero, in " + h);
+            }
             h.addAttribute(a);
             if (verbose > 2)
                 Utils.log.fine("read "+a);
--- a/jdk/test/tools/pack200/AttributeTests.java	Thu Oct 14 14:41:08 2010 -0700
+++ b/jdk/test/tools/pack200/AttributeTests.java	Thu Oct 14 14:55:03 2010 -0700
@@ -20,12 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
-
 /*
  * @test
  * @bug 6982312
@@ -34,12 +33,11 @@
  * @run main AttributeTests
  * @author ksrini
  */
-
-
 public class AttributeTests {
 
     public static void main(String... args) throws Exception {
         test6982312();
+        test6746111();
     }
     /*
      * This is an interim test, which ensures pack200 handles JSR-292 related
@@ -72,4 +70,62 @@
         testJar.delete();
         dynJar.delete();
     }
+
+    /*
+     * this test checks to see if we get the expected strings for output
+     */
+    static void test6746111() throws Exception {
+        String pack200Cmd = Utils.getPack200Cmd();
+        File badAttrJar = new File(".", "badattr.jar");
+        Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
+        File testJar = new File(".", "test.jar");
+        List<String> cmds = new ArrayList<String>();
+        cmds.add(pack200Cmd);
+        cmds.add("--repack");
+        cmds.add("-v");
+        cmds.add(testJar.getAbsolutePath());
+        cmds.add(badAttrJar.getAbsolutePath());
+        List<String> output = Utils.runExec(cmds);
+        /*
+         * compare the repacked jar bit-wise, as all the files
+         * should be transmitted "as-is".
+         */
+        Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
+        String[] expectedStrings = {
+            "WARNING: Passing class file uncompressed due to unrecognized" +
+                    " attribute: Foo.class",
+            "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
+                    "class attribute \"XourceFile\":  is unknown attribute " +
+                    "in class Foo",
+            "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
+                    "AnnotationDefault: attribute length cannot be zero, in Test.message()",
+            "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
+        };
+        List<String> notfoundList = new ArrayList<String>();
+        notfoundList.addAll(Arrays.asList(expectedStrings));
+        // make sure the expected messages are emitted
+        for (String x : output) {
+            findString(x, notfoundList, expectedStrings);
+        }
+        if (!notfoundList.isEmpty()) {
+            System.out.println("Not found:");
+            for (String x : notfoundList) {
+                System.out.println(x);
+            }
+            throw new Exception("Test fails: " + notfoundList.size() +
+                    " expected strings not found");
+        }
+        testJar.delete();
+        badAttrJar.delete();
+    }
+
+    private static void findString(String outputStr, List<String> notfoundList,
+            String[] expectedStrings) {
+        for (String y : expectedStrings) {
+            if (outputStr.contains(y)) {
+                notfoundList.remove(y);
+                return;
+            }
+        }
+    }
 }
Binary file jdk/test/tools/pack200/badattr.jar has changed