001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package tests;
028:
029: import java.io.*;
030: import java.util.*;
031: import java.util.zip.*;
032: import util.*;
033:
034: /**
035: * This class is used to generate JAR file entries for unit test cases.
036: *
037: * We add JAR entries of very long names into tests.jar
038: */
039: public class CreateTestJarEntries {
040: static void usage() {
041: System.out.println("Usage: java -jar buildtool.jar "
042: + "testjarentries tests.jar");
043: }
044:
045: public static void main(String args[]) throws Throwable {
046: if (args.length != 1) {
047: usage();
048: System.exit(1);
049: }
050: File tests_jar = new File(args[0]);
051: InputStream in = readEntireFileInBuffer(tests_jar);
052: FileOutputStream out = new FileOutputStream(tests_jar);
053: ZipOutputStream zout = new ZipOutputStream(out);
054:
055: // Add Test JAR entries in front
056: addTestEntry(zout, 1000, "Hello1.class");
057: addTestEntry(zout, 1000, "Hello1.txt");
058:
059: // Copy all entries from tests.jar
060: copyEntries(zout, in);
061:
062: // Add Test JAR entries at the back
063: addTestEntry(zout, 1000, "Hello2.class");
064: addTestEntry(zout, 1000, "Hello2.txt");
065:
066: // Add test entry for the test case
067: // vm.share.runtime.ClassPathAccess.read_completely1
068: addReadCompletely1Entry(zout);
069:
070: // Add test entries for the test case
071: // vm.share.runtime.ClassPathAccess.incremental_decompress
072: addIncrementalDecomressEntries(zout);
073:
074: // Add entry for ResourceInput unit test case.
075: // See src/tests/javaapi/com/sun/cldc/io/resource_input.java
076: addResourceInputEntry(zout);
077:
078: zout.close();
079: }
080:
081: static InputStream readEntireFileInBuffer(File file)
082: throws Throwable {
083: ByteArrayOutputStream baos = new ByteArrayOutputStream();
084: FileInputStream in = new FileInputStream(file);
085: int n;
086: byte buff[] = new byte[1024];
087:
088: while ((n = in.read(buff)) > 0) {
089: baos.write(buff, 0, n);
090: }
091: in.close();
092:
093: byte data[] = baos.toByteArray();
094: baos.close();
095: return new ByteArrayInputStream(data);
096: }
097:
098: /**
099: * Copy the content of of the given zip file source
100: * to the ZIP file output stream
101: */
102: static void copyEntries(ZipOutputStream zout, InputStream in)
103: throws Throwable {
104: ZipInputStream zin = new ZipInputStream(in);
105: for (;;) {
106: ZipEntry entry = zin.getNextEntry();
107: if (entry == null) {
108: break;
109: }
110:
111: // Read the entry from input
112: int size = (int) entry.getSize();
113: byte data[] = new byte[size];
114: zin.read(data, 0, size);
115: zin.closeEntry();
116:
117: // Write entry to output
118: zout.putNextEntry(entry);
119: zout.write(data, 0, data.length);
120: zout.closeEntry();
121: }
122: zin.close();
123: }
124:
125: static String getLongPath(int numLevels, String sep) {
126: String path = "long" + sep;
127: for (int i = 0; i < numLevels; i++) {
128: path += "long" + sep;
129: }
130: return path;
131: }
132:
133: /**
134: * Create a JAR entry with a very long name. The JAR entry contains
135: * with 100 bytes of zeros.
136: */
137: static void addTestEntry(ZipOutputStream zout, int numLevels,
138: String name) throws Throwable {
139: System.out
140: .println("adding in very long path: \"" + name + "\"");
141: String longName = getLongPath(numLevels, "/") + name;
142: ZipEntry entry = new ZipEntry(longName);
143:
144: byte data[] = new byte[100];
145:
146: zout.putNextEntry(entry);
147: zout.write(data, 0, data.length);
148: zout.closeEntry();
149: }
150:
151: static void addResourceInputEntry(ZipOutputStream zout)
152: throws Throwable {
153: ZipEntry entry = new ZipEntry(
154: "javaapi/com/sun/cldc/io/sampledata.txt");
155: byte data[] = new byte[100];
156: zout.putNextEntry(entry);
157: zout.write(data, 0, data.length);
158: zout.closeEntry();
159: }
160:
161: /**
162: * Create a JAR entry with non-compressible bytes (random data)
163: * intermixed with compressible bytes (simple sequence of numbers)
164: * as a stress test case for the JAR decoder, especially
165: * the BTYPE_NO_COMPRESSION case.
166: */
167: static void addReadCompletely1Entry(ZipOutputStream zout)
168: throws Throwable {
169: String name = "vm/share/runtime/ClassPathAccess/read_completely1.dat";
170: System.out.println("adding \"" + name + "\"");
171: ZipEntry entry = new ZipEntry(name);
172: ByteArrayOutputStream baos = new ByteArrayOutputStream();
173:
174: for (int i = 0; i < 10; i++) {
175: // Simple sequence of bytes, compressible
176: for (int j = 0; j < 256; j++) {
177: baos.write(j);
178: }
179:
180: /*
181: * Random sequence, non-compressible. Will be stored as
182: * BTYPE_NO_COMPRESSION. See Inflate.cpp and search for
183: * BTYPE_NO_COMPRESSION
184: */
185: int count = 10240;
186: for (int k = 0; k < count; k++) {
187: baos.write((local_rand() >> 24) & 0xff);
188: }
189: }
190:
191: byte data[] = baos.toByteArray();
192: zout.putNextEntry(entry);
193: zout.write(data, 0, data.length);
194: zout.closeEntry();
195: }
196:
197: /**
198: * Create large JAR entries (>1Mb) which cannot be decompressed at once.
199: * This tests Incremental Decompressing feature.
200: */
201: static void addIncrementalDecomressEntries(ZipOutputStream zout)
202: throws Throwable {
203: int count = 2048 * 1024; // 2 Mb
204:
205: System.out.println("adding poorly-compressed large file");
206: ByteArrayOutputStream baos = new ByteArrayOutputStream();
207: for (int i = 0; i < count; i++) {
208: baos.write((local_rand() >> 24) & 0xff);
209: }
210: writeToZip(zout, baos, "incremental_decompress1.dat");
211:
212: System.out.println("adding well-compressed large file");
213: baos.reset();
214: int written = 0;
215: int groupLength = 1;
216: while (written < count) { // written may exceed count - that's ok
217: int b = (groupLength % 10) + '0';
218: for (int i = 0; i < groupLength; i++) {
219: baos.write(b);
220: }
221: written += groupLength;
222: groupLength++;
223: }
224: writeToZip(zout, baos, "incremental_decompress2.dat");
225: }
226:
227: static void writeToZip(ZipOutputStream zout,
228: ByteArrayOutputStream baos, String name) throws Throwable {
229: ZipEntry entry = new ZipEntry("javaapi/com/sun/cldc/io/" + name);
230: zout.putNextEntry(entry);
231: baos.writeTo(zout);
232: zout.closeEntry();
233: }
234:
235: static int state = 0x23451921;
236: static int multiplier = 0xDEECE66D;
237: static int addend = 0xB;
238:
239: static int local_rand() {
240: state = state * multiplier + addend;
241: return state;
242: }
243:
244: }
|