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