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                  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          final InputStream is = new FileInputStream(f);
94          try {
95              assertEquals(DumpArchiveInputStream.class,
96                           new ArchiveStreamFactory()
97                           .createArchiveInputStream(new BufferedInputStream(is))
98                           .getClass());
99          } finally {
100             is.close();
101         }
102     }
103 
104     @Test
105     public void testCheckArchive() throws Exception {
106         checkDumpArchive(getFile("bla.dump"));
107     }
108 
109     @Test
110     public void testCheckCompressedArchive() throws Exception {
111         checkDumpArchive(getFile("bla.z.dump"));
112     }
113 
114     private void checkDumpArchive(final File f) throws Exception {
115         ArrayList<String> expected = new ArrayList<String>();
116         expected.add("");
117         expected.add("lost+found/");
118         expected.add("test1.xml");
119         expected.add("test2.xml");
120         final InputStream is = new FileInputStream(f);
121         try {
122             checkArchiveContent(new DumpArchiveInputStream(is),
123                                 expected);
124         } finally {
125             is.close();
126         }
127     }
128 }