KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > util > zip > ManySmallEntriesTest


1 /*
2  * ManySmallEntriesTest.java
3  * JUnit based test
4  *
5  * Created on 11. September 2005, 13:43
6  */

7 /*
8  * Copyright 2005-2006 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package de.schlichtherle.util.zip;
24
25 import de.schlichtherle.util.Arrays;
26
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 import junit.framework.*;
36
37 /**
38  * Tests compression of data.
39  *
40  * @author Christian Schlichtherle
41  */

42 public class ManySmallEntriesTest extends TestCase {
43
44     private static final Logger JavaDoc logger
45             = Logger.getLogger(ManySmallEntriesTest.class.getName());
46
47     private static final byte[] data = "Hello World!".getBytes();
48
49     public static Test suite() throws Exception JavaDoc {
50         TestSuite suite = new TestSuite(ManySmallEntriesTest.class);
51         
52         return suite;
53     }
54
55     private File JavaDoc zip;
56
57     public ManySmallEntriesTest(String JavaDoc testName) {
58         super(testName);
59     }
60
61     protected void setUp() throws IOException JavaDoc {
62         zip = File.createTempFile("zip", null);
63     }
64
65     protected void tearDown() {
66         assertTrue(zip.delete());
67     }
68
69     public void testManySmallEntries() throws IOException JavaDoc {
70         logger.fine("testManySmallEntries");
71         
72         final int n = 70000;
73         logger.finer("Compressing " + n + " ZIP file entries to: " + zip.getPath());
74         logger.finer("Note that the max. number of entries supported by the ZIP File Format Spec. is 65535!");
75
76         final HashSet JavaDoc set = new HashSet JavaDoc();
77
78         final ZipOutputStream zipOut
79                 = new ZipOutputStream(new FileOutputStream JavaDoc(zip));
80         for (int i = 100000; i < 100000 + n; i++) {
81             String JavaDoc name = i + ".txt";
82             zipOut.putNextEntry(new ZipEntry(name));
83             zipOut.write(data);
84             assertTrue(set.add(name));
85         }
86         zipOut.close();
87         logger.finer("Compressed " + n + " ZIP file entries into " + zip.length() / 1024 + " KB ZIP file length.");
88
89         final ZipFile zipIn = new ZipFile(zip);
90         try {
91             final byte[] buf = new byte[data.length];
92             for (Enumeration JavaDoc e = zipIn.entries(); e.hasMoreElements(); ) {
93                 final ZipEntry entry = (ZipEntry) e.nextElement();
94
95                 assertEquals(data.length, entry.getSize());
96
97                 InputStream JavaDoc in = zipIn.getInputStream(entry);
98                 int off = 0;
99                 int read;
100                 do {
101                     read = in.read(buf);
102                     if (read < 0)
103                         break;
104                     assertTrue(read > 0);
105                     assertTrue(Arrays.equals(data, off, buf, 0, read));
106                     off += read;
107                 } while (true);
108                 assertEquals(-1, read);
109                 assertEquals(off, data.length);
110                 assertEquals(0, in.read(new byte[0]));
111                 in.close();
112
113                 assertTrue(set.remove(entry.getName()));
114             }
115         } finally {
116             zipIn.close();
117         }
118
119         assertTrue(set.isEmpty());
120         logger.finer("Successfully decompressed the data in all entries.");
121     }
122 }
123
Popular Tags