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