1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }