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.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.BufferedInputStream;
24  import java.io.File;
25  import java.io.InputStream;
26  import java.nio.file.Files;
27  import java.util.ArrayList;
28  
29  import org.apache.commons.compress.AbstractTest;
30  import org.apache.commons.compress.archivers.dump.DumpArchiveInputStream;
31  import org.junit.jupiter.api.Test;
32  
33  public final class DumpTest extends AbstractTest {
34  
35      private void archiveDetection(final File f) throws Exception {
36          try (InputStream is = Files.newInputStream(f.toPath());
37                  ArchiveInputStream<? extends ArchiveEntry> archiveInputStream = ArchiveStreamFactory.DEFAULT
38                          .createArchiveInputStream(new BufferedInputStream(is))) {
39              assertEquals(DumpArchiveInputStream.class, archiveInputStream.getClass());
40          }
41      }
42  
43      private void checkDumpArchive(final File f) throws Exception {
44          final ArrayList<String> expected = new ArrayList<>();
45          expected.add("");
46          expected.add("lost+found/");
47          expected.add("test1.xml");
48          expected.add("test2.xml");
49          try (InputStream is = Files.newInputStream(f.toPath());
50                  DumpArchiveInputStream inputStream = new DumpArchiveInputStream(is);) {
51              checkArchiveContent(inputStream, expected);
52          }
53      }
54  
55      @Test
56      public void testArchiveDetection() throws Exception {
57          archiveDetection(getFile("bla.dump"));
58      }
59  
60      @Test
61      public void testCheckArchive() throws Exception {
62          checkDumpArchive(getFile("bla.dump"));
63      }
64  
65      @Test
66      public void testCheckCompressedArchive() throws Exception {
67          checkDumpArchive(getFile("bla.z.dump"));
68      }
69  
70      @Test
71      public void testCompressedArchiveDetection() throws Exception {
72          archiveDetection(getFile("bla.z.dump"));
73      }
74  
75      @Test
76      public void testCompressedDumpUnarchiveAll() throws Exception {
77          unarchiveAll(getFile("bla.z.dump"));
78      }
79  
80      @Test
81      public void testDumpUnarchiveAll() throws Exception {
82          unarchiveAll(getFile("bla.dump"));
83      }
84  
85      private void unarchiveAll(final File input) throws Exception {
86          try (InputStream is = Files.newInputStream(input.toPath());
87                  ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("dump", is)) {
88              ArchiveEntry entry = in.getNextEntry();
89              while (entry != null) {
90                  final File archiveEntry = newTempFile(entry.getName());
91                  archiveEntry.getParentFile().mkdirs();
92                  if (entry.isDirectory()) {
93                      archiveEntry.mkdir();
94                      entry = in.getNextEntry();
95                      continue;
96                  }
97                  Files.copy(in, archiveEntry.toPath());
98                  entry = in.getNextEntry();
99              }
100         }
101     }
102 }