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 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 }