2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
import java.util.*;
|
|
26 |
import java.io.*;
|
|
27 |
import java.util.jar.*;
|
|
28 |
import java.util.zip.*;
|
|
29 |
|
|
30 |
/*
|
|
31 |
* Pack200Test.java
|
|
32 |
*
|
|
33 |
* @author ksrini
|
|
34 |
*/
|
|
35 |
|
|
36 |
/**
|
|
37 |
* These tests are very rudimentary smoke tests to ensure that the packing
|
|
38 |
* unpacking process works on a select set of JARs.
|
|
39 |
*/
|
|
40 |
public class Pack200Test {
|
|
41 |
|
|
42 |
private static ArrayList <File> jarList = new ArrayList();
|
|
43 |
private static final String PACKEXT = ".pack";
|
|
44 |
|
|
45 |
/** Creates a new instance of Pack200Test */
|
|
46 |
private Pack200Test() {}
|
|
47 |
|
|
48 |
private static void doPackUnpack() {
|
|
49 |
for (File in : jarList) {
|
|
50 |
Pack200.Packer packer = Pack200.newPacker();
|
|
51 |
Map p = packer.properties();
|
|
52 |
// Take the time optimization vs. space
|
|
53 |
p.put(packer.EFFORT, "1"); // CAUTION: do not use 0.
|
|
54 |
// Make the memory consumption as effective as possible
|
|
55 |
p.put(packer.SEGMENT_LIMIT,"10000");
|
|
56 |
// throw an error if an attribute is unrecognized
|
|
57 |
p.put(packer.UNKNOWN_ATTRIBUTE, packer.ERROR);
|
|
58 |
// ignore all JAR deflation requests to save time
|
|
59 |
p.put(packer.DEFLATE_HINT, packer.FALSE);
|
|
60 |
// save the file ordering of the original JAR
|
|
61 |
p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
|
|
62 |
|
|
63 |
try {
|
|
64 |
JarFile jarFile = new JarFile(in);
|
|
65 |
|
|
66 |
// Write out to a jtreg scratch area
|
|
67 |
FileOutputStream fos = new FileOutputStream(in.getName() + PACKEXT);
|
|
68 |
|
|
69 |
System.out.print("Packing [" + in.toString() + "]...");
|
|
70 |
// Call the packer
|
|
71 |
packer.pack(jarFile, fos);
|
|
72 |
jarFile.close();
|
|
73 |
fos.close();
|
|
74 |
|
|
75 |
System.out.print("Unpacking...");
|
|
76 |
File f = new File(in.getName() + PACKEXT);
|
|
77 |
|
|
78 |
// Write out to current directory, jtreg will setup a scratch area
|
|
79 |
JarOutputStream jostream = new JarOutputStream(new FileOutputStream(in.getName()));
|
|
80 |
|
|
81 |
// Unpack the files
|
|
82 |
Pack200.Unpacker unpacker = Pack200.newUnpacker();
|
|
83 |
// Call the unpacker
|
|
84 |
unpacker.unpack(f, jostream);
|
|
85 |
// Must explicitly close the output.
|
|
86 |
jostream.close();
|
|
87 |
System.out.print("Testing...");
|
|
88 |
// Ok we have unpacked the file, lets test it.
|
|
89 |
doTest(in);
|
|
90 |
System.out.println("Done.");
|
|
91 |
} catch (Exception e) {
|
|
92 |
System.out.println("ERROR: " + e.getMessage());
|
|
93 |
System.exit(1);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
private static ArrayList <String> getZipFileEntryNames(ZipFile z) {
|
|
99 |
ArrayList <String> out = new ArrayList();
|
|
100 |
for (ZipEntry ze : Collections.list(z.entries())) {
|
|
101 |
out.add(ze.getName());
|
|
102 |
}
|
|
103 |
return out;
|
|
104 |
}
|
|
105 |
|
|
106 |
private static void doTest(File in) throws Exception {
|
|
107 |
// make sure all the files in the original jar exists in the other
|
|
108 |
ArrayList <String> refList = getZipFileEntryNames(new ZipFile(in));
|
|
109 |
ArrayList <String> cmpList = getZipFileEntryNames(new ZipFile(in.getName()));
|
|
110 |
|
|
111 |
System.out.print(refList.size() + "/" + cmpList.size() + " entries...");
|
|
112 |
|
|
113 |
if (refList.size() != cmpList.size()) {
|
|
114 |
throw new Exception("Missing: files ?, entries don't match");
|
|
115 |
}
|
|
116 |
|
|
117 |
for (String ename: refList) {
|
|
118 |
if (!cmpList.contains(ename)) {
|
|
119 |
throw new Exception("Does not contain : " + ename);
|
|
120 |
}
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
private static void doSanity(String[] args) {
|
|
125 |
for (String s: args) {
|
|
126 |
File f = new File(s);
|
|
127 |
if (f.exists()) {
|
|
128 |
jarList.add(f);
|
|
129 |
} else {
|
|
130 |
System.out.println("Warning: The JAR file " + f.toString() + " does not exist,");
|
|
131 |
System.out.println(" this test requires a JDK image, this file will be skipped.");
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* @param args the command line arguments
|
|
138 |
*/
|
|
139 |
public static void main(String[] args) {
|
|
140 |
if (args.length < 1) {
|
|
141 |
System.out.println("Usage: jar1 jar2 jar3 .....");
|
|
142 |
System.exit(1);
|
|
143 |
}
|
|
144 |
doSanity(args);
|
|
145 |
doPackUnpack();
|
|
146 |
}
|
|
147 |
}
|