1 /*
   2  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  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
  21  * questions.
  22  */
  23 import java.io.File;
  24 import java.io.IOException;
  25 import java.util.ArrayList;
  26 import java.util.Arrays;
  27 import java.util.List;
  28 /*
  29  * @test
  30  * @bug 6746111
  31  * @summary tests various classfile format and attribute handling by pack200
  32  * @compile -XDignore.symbol.file Utils.java AttributeTests.java
  33  * @run main AttributeTests
  34  * @author ksrini
  35  */
  36 public class AttributeTests {
  37 
  38     public static void main(String... args) throws Exception {
  39         test6746111();
  40     }
  41 
  42     /*
  43      * this test checks to see if we get the expected strings for output
  44      */
  45     static void test6746111() throws Exception {
  46         String pack200Cmd = Utils.getPack200Cmd();
  47         File badAttrJar = new File(".", "badattr.jar");
  48         Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
  49         File testJar = new File(".", "test.jar");
  50         List<String> cmds = new ArrayList<String>();
  51         cmds.add(pack200Cmd);
  52         cmds.add("--repack");
  53         cmds.add("-v");
  54         cmds.add(testJar.getAbsolutePath());
  55         cmds.add(badAttrJar.getAbsolutePath());
  56         List<String> output = Utils.runExec(cmds);
  57         /*
  58          * compare the repacked jar bit-wise, as all the files
  59          * should be transmitted "as-is".
  60          */
  61         Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
  62         String[] expectedStrings = {
  63             "WARNING: Passing class file uncompressed due to unrecognized" +
  64                     " attribute: Foo.class",
  65             "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
  66                     "class attribute \"XourceFile\":  is unknown attribute " +
  67                     "in class Foo",
  68             "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
  69                     "AnnotationDefault: attribute length cannot be zero, in Test.message()",
  70             "WARNING: Passing class file uncompressed due to unknown class format: Test.class"
  71         };
  72         List<String> notfoundList = new ArrayList<String>();
  73         notfoundList.addAll(Arrays.asList(expectedStrings));
  74         // make sure the expected messages are emitted
  75         for (String x : output) {
  76             findString(x, notfoundList, expectedStrings);
  77         }
  78         if (!notfoundList.isEmpty()) {
  79             System.out.println("Not found:");
  80             for (String x : notfoundList) {
  81                 System.out.println(x);
  82             }
  83             throw new Exception("Test fails: " + notfoundList.size() +
  84                     " expected strings not found");
  85         }
  86         testJar.delete();
  87         badAttrJar.delete();
  88     }
  89 
  90     private static void findString(String outputStr, List<String> notfoundList,
  91             String[] expectedStrings) {
  92         for (String y : expectedStrings) {
  93             if (outputStr.contains(y)) {
  94                 notfoundList.remove(y);
  95                 return;
  96             }
  97         }
  98     }
  99 }