1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.compressors;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.commons.compress.AbstractTestCase;
32 import org.apache.commons.compress.archivers.ArchiveEntry;
33 import org.apache.commons.compress.archivers.ArchiveInputStream;
34 import org.apache.commons.compress.archivers.ArchiveOutputStream;
35 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
36 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
37 import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
38 import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
39 import org.apache.commons.compress.compressors.pack200.Pack200Strategy;
40 import org.apache.commons.compress.utils.IOUtils;
41
42 public final class Pack200TestCase extends AbstractTestCase {
43
44 public void testJarUnarchiveAllInMemory() throws Exception {
45 jarUnarchiveAll(false, Pack200Strategy.IN_MEMORY);
46 }
47
48 public void testJarUnarchiveAllFileArgInMemory() throws Exception {
49 jarUnarchiveAll(true, Pack200Strategy.IN_MEMORY);
50 }
51
52 public void testJarUnarchiveAllTempFile() throws Exception {
53 jarUnarchiveAll(false, Pack200Strategy.TEMP_FILE);
54 }
55
56 public void testJarUnarchiveAllFileTempFile() throws Exception {
57 jarUnarchiveAll(true, Pack200Strategy.TEMP_FILE);
58 }
59
60 private void jarUnarchiveAll(boolean useFile, Pack200Strategy mode)
61 throws Exception {
62 final File input = getFile("bla.pack");
63 final InputStream is = useFile
64 ? new Pack200CompressorInputStream(input, mode)
65 : new Pack200CompressorInputStream(new FileInputStream(input),
66 mode);
67 try {
68 final ArchiveInputStream in = new ArchiveStreamFactory()
69 .createArchiveInputStream("jar", is);
70
71 ArchiveEntry entry = in.getNextEntry();
72 while (entry != null) {
73 File archiveEntry = new File(dir, entry.getName());
74 archiveEntry.getParentFile().mkdirs();
75 if (entry.isDirectory()) {
76 archiveEntry.mkdir();
77 entry = in.getNextEntry();
78 continue;
79 }
80 OutputStream out = new FileOutputStream(archiveEntry);
81 IOUtils.copy(in, out);
82 out.close();
83 entry = in.getNextEntry();
84 }
85
86 in.close();
87 } finally {
88 is.close();
89 }
90 }
91
92 public void testJarArchiveCreationInMemory() throws Exception {
93 jarArchiveCreation(Pack200Strategy.IN_MEMORY);
94 }
95
96 public void testJarArchiveCreationTempFile() throws Exception {
97 jarArchiveCreation(Pack200Strategy.TEMP_FILE);
98 }
99
100 private void jarArchiveCreation(Pack200Strategy mode) throws Exception {
101 final File output = new File(dir, "bla.pack");
102
103 final File file1 = getFile("test1.xml");
104 final File file2 = getFile("test2.xml");
105
106 final OutputStream out =
107 new Pack200CompressorOutputStream(new FileOutputStream(output),
108 mode);
109 try {
110 final ArchiveOutputStream os = new ArchiveStreamFactory()
111 .createArchiveOutputStream("jar", out);
112
113 os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
114 IOUtils.copy(new FileInputStream(file1), os);
115 os.closeArchiveEntry();
116
117 os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
118 IOUtils.copy(new FileInputStream(file2), os);
119 os.closeArchiveEntry();
120
121 os.close();
122 } finally {
123 out.close();
124 }
125
126 final InputStream is = new Pack200CompressorInputStream(output);
127 try {
128 final ArchiveInputStream in = new ArchiveStreamFactory()
129 .createArchiveInputStream("jar", is);
130 List<String> files = new ArrayList<String>();
131 files.add("testdata/test1.xml");
132 files.add("testdata/test2.xml");
133 checkArchiveContent(in, files);
134 in.close();
135 } finally {
136 is.close();
137 }
138 }
139
140 public void testGoodSignature() throws Exception {
141 final InputStream is = new FileInputStream(getFile("bla.pack"));
142 try {
143 byte[] sig = new byte[4];
144 is.read(sig);
145 assertTrue(Pack200CompressorInputStream.matches(sig, 4));
146 } finally {
147 is.close();
148 }
149 }
150
151 public void testBadSignature() throws Exception {
152 final InputStream is = new FileInputStream(getFile("bla.jar"));
153 try {
154 byte[] sig = new byte[4];
155 is.read(sig);
156 assertFalse(Pack200CompressorInputStream.matches(sig, 4));
157 } finally {
158 is.close();
159 }
160 }
161
162 public void testShortSignature() throws Exception {
163 final InputStream is = new FileInputStream(getFile("bla.pack"));
164 try {
165 byte[] sig = new byte[2];
166 is.read(sig);
167 assertFalse(Pack200CompressorInputStream.matches(sig, 2));
168 } finally {
169 is.close();
170 }
171 }
172
173 public void testInputStreamMethods() throws Exception {
174 Map<String, String> m = new HashMap<String, String>();
175 m.put("foo", "bar");
176 final InputStream is =
177 new Pack200CompressorInputStream(new FileInputStream(getFile("bla.jar")),
178 m);
179 try {
180
181
182 assertTrue(is.markSupported());
183 is.mark(5);
184 assertEquals(0x50, is.read());
185 byte[] rest = new byte[3];
186 assertEquals(3, is.read(rest));
187 assertEquals(0x4b, rest[0]);
188 assertEquals(3, rest[1]);
189 assertEquals(4, rest[2]);
190 assertEquals(1, is.skip(1));
191 is.reset();
192 assertEquals(0x50, is.read());
193 assertTrue(is.available() > 0);
194 } finally {
195 is.close();
196 }
197 }
198
199 public void testOutputStreamMethods() throws Exception {
200 final File output = new File(dir, "bla.pack");
201 Map<String, String> m = new HashMap<String, String>();
202 m.put("foo", "bar");
203 final OutputStream out = new FileOutputStream(output);
204 try {
205 final OutputStream os = new Pack200CompressorOutputStream(out, m);
206 os.write(1);
207 os.write(new byte[] { 2, 3 });
208 os.close();
209 } finally {
210 out.close();
211 }
212 }
213 }