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(final boolean useFile, final Pack200Strategy mode)
68          throws Exception {
69          final File input = getFile("bla.pack");
70          try (InputStream is = useFile
71                  ? new Pack200CompressorInputStream(input, mode)
72                  : new Pack200CompressorInputStream(new FileInputStream(input),
73                  mode)) {
74              final ArchiveInputStream in = new ArchiveStreamFactory()
75                      .createArchiveInputStream("jar", is);
76  
77              ArchiveEntry entry = in.getNextEntry();
78              while (entry != null) {
79                  final File archiveEntry = new File(dir, entry.getName());
80                  archiveEntry.getParentFile().mkdirs();
81                  if (entry.isDirectory()) {
82                      archiveEntry.mkdir();
83                      entry = in.getNextEntry();
84                      continue;
85                  }
86                  final OutputStream out = new FileOutputStream(archiveEntry);
87                  IOUtils.copy(in, out);
88                  out.close();
89                  entry = in.getNextEntry();
90              }
91  
92              in.close();
93          }
94      }
95  
96      @Test
97      public void testJarArchiveCreationInMemory() throws Exception {
98          jarArchiveCreation(Pack200Strategy.IN_MEMORY);
99      }
100 
101     @Test
102     public void testJarArchiveCreationTempFile() throws Exception {
103         jarArchiveCreation(Pack200Strategy.TEMP_FILE);
104     }
105 
106     private void jarArchiveCreation(final Pack200Strategy mode) throws Exception {
107         final File output = new File(dir, "bla.pack");
108 
109         final File file1 = getFile("test1.xml");
110         final File file2 = getFile("test2.xml");
111 
112         try (OutputStream out = new Pack200CompressorOutputStream(new FileOutputStream(output),
113                 mode)) {
114             final ArchiveOutputStream os = new ArchiveStreamFactory()
115                     .createArchiveOutputStream("jar", out);
116 
117             os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
118             IOUtils.copy(new FileInputStream(file1), os);
119             os.closeArchiveEntry();
120 
121             os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
122             IOUtils.copy(new FileInputStream(file2), os);
123             os.closeArchiveEntry();
124 
125             os.close();
126         }
127 
128         try (InputStream is = new Pack200CompressorInputStream(output)) {
129             final ArchiveInputStream in = new ArchiveStreamFactory()
130                     .createArchiveInputStream("jar", is);
131             final List<String> files = new ArrayList<>();
132             files.add("testdata/test1.xml");
133             files.add("testdata/test2.xml");
134             checkArchiveContent(in, files);
135             in.close();
136         }
137     }
138 
139     @Test
140     public void testGoodSignature() throws Exception {
141         try (InputStream is = new FileInputStream(getFile("bla.pack"))) {
142             final byte[] sig = new byte[4];
143             is.read(sig);
144             assertTrue(Pack200CompressorInputStream.matches(sig, 4));
145         }
146     }
147 
148     @Test
149     public void testBadSignature() throws Exception {
150         try (InputStream is = new FileInputStream(getFile("bla.jar"))) {
151             final byte[] sig = new byte[4];
152             is.read(sig);
153             assertFalse(Pack200CompressorInputStream.matches(sig, 4));
154         }
155     }
156 
157     @Test
158     public void testShortSignature() throws Exception {
159         try (InputStream is = new FileInputStream(getFile("bla.pack"))) {
160             final byte[] sig = new byte[2];
161             is.read(sig);
162             assertFalse(Pack200CompressorInputStream.matches(sig, 2));
163         }
164     }
165 
166     @Test
167     public void testInputStreamMethods() throws Exception {
168         final Map<String, String> m = new HashMap<>();
169         m.put("foo", "bar");
170         try (InputStream is = new Pack200CompressorInputStream(new FileInputStream(getFile("bla.jar")),
171                 m)) {
172             // packed file is a jar, which is a zip so it starts with
173             // a local file header
174             assertTrue(is.markSupported());
175             is.mark(5);
176             assertEquals(0x50, is.read());
177             final byte[] rest = new byte[3];
178             assertEquals(3, is.read(rest));
179             assertEquals(0x4b, rest[0]);
180             assertEquals(3, rest[1]);
181             assertEquals(4, rest[2]);
182             assertEquals(1, is.skip(1));
183             is.reset();
184             assertEquals(0x50, is.read());
185             assertTrue(is.available() > 0);
186         }
187     }
188 
189     @Test
190     public void testOutputStreamMethods() throws Exception {
191         final File output = new File(dir, "bla.pack");
192         final Map<String, String> m = new HashMap<>();
193         m.put("foo", "bar");
194         try (OutputStream out = new FileOutputStream(output)) {
195             final OutputStream os = new Pack200CompressorOutputStream(out, m);
196             os.write(1);
197             os.write(new byte[] { 2, 3 });
198             os.close();
199         }
200     }
201 }