View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.compressors;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.commons.compress.AbstractTestCase;
34  import org.apache.commons.compress.archivers.ArchiveEntry;
35  import org.apache.commons.compress.archivers.ArchiveInputStream;
36  import org.apache.commons.compress.archivers.ArchiveOutputStream;
37  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
38  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
39  import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
40  import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
41  import org.apache.commons.compress.compressors.pack200.Pack200Strategy;
42  import org.apache.commons.compress.utils.IOUtils;
43  import org.junit.Test;
44  
45  public final class Pack200TestCase extends AbstractTestCase {
46  
47      @Test
48      public void testJarUnarchiveAllInMemory() throws Exception {
49          jarUnarchiveAll(false, Pack200Strategy.IN_MEMORY);
50      }
51  
52      @Test
53      public void testJarUnarchiveAllFileArgInMemory() throws Exception {
54          jarUnarchiveAll(true, Pack200Strategy.IN_MEMORY);
55      }
56  
57      @Test
58      public void testJarUnarchiveAllTempFile() throws Exception {
59          jarUnarchiveAll(false, Pack200Strategy.TEMP_FILE);
60      }
61  
62      @Test
63      public void testJarUnarchiveAllFileTempFile() throws Exception {
64          jarUnarchiveAll(true, Pack200Strategy.TEMP_FILE);
65      }
66  
67      private void jarUnarchiveAll(boolean useFile, Pack200Strategy mode)
68          throws Exception {
69          final File input = getFile("bla.pack");
70          final InputStream is = useFile
71              ? new Pack200CompressorInputStream(input, mode)
72              : new Pack200CompressorInputStream(new FileInputStream(input),
73                                                 mode);
74          try {
75              final ArchiveInputStream in = new ArchiveStreamFactory()
76                  .createArchiveInputStream("jar", is);
77  
78              ArchiveEntry entry = in.getNextEntry();
79              while (entry != null) {
80                  File archiveEntry = new File(dir, entry.getName());
81                  archiveEntry.getParentFile().mkdirs();
82                  if (entry.isDirectory()) {
83                      archiveEntry.mkdir();
84                      entry = in.getNextEntry();
85                      continue;
86                  }
87                  OutputStream out = new FileOutputStream(archiveEntry);
88                  IOUtils.copy(in, out);
89                  out.close();
90                  entry = in.getNextEntry();
91              }
92  
93              in.close();
94          } finally {
95              is.close();
96          }
97      }
98  
99      @Test
100     public void testJarArchiveCreationInMemory() throws Exception {
101         jarArchiveCreation(Pack200Strategy.IN_MEMORY);
102     }
103 
104     @Test
105     public void testJarArchiveCreationTempFile() throws Exception {
106         jarArchiveCreation(Pack200Strategy.TEMP_FILE);
107     }
108 
109     private void jarArchiveCreation(Pack200Strategy mode) throws Exception {
110         final File output = new File(dir, "bla.pack");
111 
112         final File file1 = getFile("test1.xml");
113         final File file2 = getFile("test2.xml");
114 
115         final OutputStream out = 
116             new Pack200CompressorOutputStream(new FileOutputStream(output),
117                                               mode);
118         try {
119             final ArchiveOutputStream os = new ArchiveStreamFactory()
120                 .createArchiveOutputStream("jar", out);
121 
122             os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
123             IOUtils.copy(new FileInputStream(file1), os);
124             os.closeArchiveEntry();
125 
126             os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
127             IOUtils.copy(new FileInputStream(file2), os);
128             os.closeArchiveEntry();
129 
130             os.close();
131         } finally {
132             out.close();
133         }
134 
135         final InputStream is = new Pack200CompressorInputStream(output);
136         try {
137             final ArchiveInputStream in = new ArchiveStreamFactory()
138                 .createArchiveInputStream("jar", is);
139             List<String> files = new ArrayList<String>();
140             files.add("testdata/test1.xml");
141             files.add("testdata/test2.xml");
142             checkArchiveContent(in, files);
143             in.close();
144         } finally {
145             is.close();
146         }
147     }
148 
149     @Test
150     public void testGoodSignature() throws Exception {
151         final InputStream is = new FileInputStream(getFile("bla.pack"));
152         try {
153             byte[] sig = new byte[4];
154             is.read(sig);
155             assertTrue(Pack200CompressorInputStream.matches(sig, 4));
156         } finally {
157             is.close();
158         }
159     }
160 
161     @Test
162     public void testBadSignature() throws Exception {
163         final InputStream is = new FileInputStream(getFile("bla.jar"));
164         try {
165             byte[] sig = new byte[4];
166             is.read(sig);
167             assertFalse(Pack200CompressorInputStream.matches(sig, 4));
168         } finally {
169             is.close();
170         }
171     }
172 
173     @Test
174     public void testShortSignature() throws Exception {
175         final InputStream is = new FileInputStream(getFile("bla.pack"));
176         try {
177             byte[] sig = new byte[2];
178             is.read(sig);
179             assertFalse(Pack200CompressorInputStream.matches(sig, 2));
180         } finally {
181             is.close();
182         }
183     }
184 
185     @Test
186     public void testInputStreamMethods() throws Exception {
187         Map<String, String> m = new HashMap<String, String>();
188         m.put("foo", "bar");
189         final InputStream is =
190             new Pack200CompressorInputStream(new FileInputStream(getFile("bla.jar")),
191                                              m);
192         try {
193             // packed file is a jar, which is a zip so it starts with
194             // a local file header
195             assertTrue(is.markSupported());
196             is.mark(5);
197             assertEquals(0x50, is.read());
198             byte[] rest = new byte[3];
199             assertEquals(3, is.read(rest));
200             assertEquals(0x4b, rest[0]);
201             assertEquals(3, rest[1]);
202             assertEquals(4, rest[2]);
203             assertEquals(1, is.skip(1));
204             is.reset();
205             assertEquals(0x50, is.read());
206             assertTrue(is.available() > 0);
207         } finally {
208             is.close();
209         }
210     }
211 
212     @Test
213     public void testOutputStreamMethods() throws Exception {
214         final File output = new File(dir, "bla.pack");
215         Map<String, String> m = new HashMap<String, String>();
216         m.put("foo", "bar");
217         final OutputStream out = new FileOutputStream(output);
218         try {
219             final OutputStream os = new Pack200CompressorOutputStream(out, m);
220             os.write(1);
221             os.write(new byte[] { 2, 3 });
222             os.close();
223         } finally {
224             out.close();
225         }
226     }
227 }