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.archivers;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.BufferedInputStream;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.util.ArrayList;
30  
31  import org.apache.commons.compress.AbstractTestCase;
32  import org.apache.commons.compress.archivers.dump.DumpArchiveInputStream;
33  import org.apache.commons.compress.utils.IOUtils;
34  import org.junit.Test;
35  
36  public final class DumpTestCase extends AbstractTestCase {
37  
38      @Test
39      public void testDumpUnarchiveAll() throws Exception {
40          unarchiveAll(getFile("bla.dump"));
41      }
42  
43      @Test
44      public void testCompressedDumpUnarchiveAll() throws Exception {
45          unarchiveAll(getFile("bla.z.dump"));
46      }
47  
48      private void unarchiveAll(final File input) throws Exception {
49          final InputStream is = new FileInputStream(input);
50          ArchiveInputStream in = null;
51          OutputStream out = null;
52          try {
53              in = new ArchiveStreamFactory()
54                  .createArchiveInputStream("dump", is);
55  
56              ArchiveEntry entry = in.getNextEntry();
57              while (entry != null) {
58                  final File archiveEntry = new File(dir, entry.getName());
59                  archiveEntry.getParentFile().mkdirs();
60                  if (entry.isDirectory()) {
61                      archiveEntry.mkdir();
62                      entry = in.getNextEntry();
63                      continue;
64                  }
65                  out = new FileOutputStream(archiveEntry);
66                  IOUtils.copy(in, out);
67                  out.close();
68                  out = null;
69                  entry = in.getNextEntry();
70              }
71          } finally {
72              if (out != null) {
73                  out.close();
74              }
75              if (in != null) {
76                  in.close();
77              }
78              is.close();
79          }
80      }
81  
82      @Test
83      public void testArchiveDetection() throws Exception {
84          archiveDetection(getFile("bla.dump"));
85      }
86  
87      @Test
88      public void testCompressedArchiveDetection() throws Exception {
89          archiveDetection(getFile("bla.z.dump"));
90      }
91  
92      private void archiveDetection(final File f) throws Exception {
93          try (InputStream is = new FileInputStream(f)) {
94              assertEquals(DumpArchiveInputStream.class,
95                      new ArchiveStreamFactory()
96                              .createArchiveInputStream(new BufferedInputStream(is))
97                              .getClass());
98          }
99      }
100 
101     @Test
102     public void testCheckArchive() throws Exception {
103         checkDumpArchive(getFile("bla.dump"));
104     }
105 
106     @Test
107     public void testCheckCompressedArchive() throws Exception {
108         checkDumpArchive(getFile("bla.z.dump"));
109     }
110 
111     private void checkDumpArchive(final File f) throws Exception {
112         final ArrayList<String> expected = new ArrayList<>();
113         expected.add("");
114         expected.add("lost+found/");
115         expected.add("test1.xml");
116         expected.add("test2.xml");
117         try (InputStream is = new FileInputStream(f)) {
118             checkArchiveContent(new DumpArchiveInputStream(is),
119                     expected);
120         }
121     }
122 }