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.examples;
20
21 import static java.nio.charset.StandardCharsets.UTF_8;
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.BufferedInputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.nio.channels.FileChannel;
31 import java.nio.channels.SeekableByteChannel;
32 import java.nio.file.Files;
33 import java.nio.file.StandardOpenOption;
34 import java.util.stream.Stream;
35
36 import org.apache.commons.compress.AbstractTest;
37 import org.apache.commons.compress.archivers.ArchiveEntry;
38 import org.apache.commons.compress.archivers.ArchiveInputStream;
39 import org.apache.commons.compress.archivers.ArchiveOutputStream;
40 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
41 import org.junit.jupiter.params.ParameterizedTest;
42 import org.junit.jupiter.params.provider.Arguments;
43 import org.junit.jupiter.params.provider.MethodSource;
44 import org.junit.runners.Parameterized.Parameters;
45
46 public class ParameterizedExpanderTest extends AbstractTest {
47
48
49 @Parameters(name = "format={0}")
50 public static Stream<Arguments> data() {
51 return Stream.of(Arguments.of("tar"), Arguments.of("cpio"), Arguments.of("zip"));
52 }
53
54 private File archive;
55
56 @ParameterizedTest
57 @MethodSource("data")
58 public void archiveInputStreamVersion(final String format) throws Exception {
59
60 setUp(format);
61 try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()));
62 ArchiveInputStream<?> ais = ArchiveStreamFactory.DEFAULT.createArchiveInputStream(format, i)) {
63 new Expander().expand(ais, tempResultDir);
64 }
65 verifyTargetDir();
66 }
67
68 private void assertHelloWorld(final String fileName, final String suffix) throws IOException {
69 assertTrue(new File(tempResultDir, fileName).isFile(), fileName + " does not exist");
70 final byte[] expected = ("Hello, world " + suffix).getBytes(UTF_8);
71 final byte[] actual = Files.readAllBytes(tempResultDir.toPath().resolve(fileName));
72 assertArrayEquals(expected, actual);
73 }
74
75 @ParameterizedTest
76 @MethodSource("data")
77 public void channelVersion(final String format) throws Exception {
78
79 setUp(format);
80 try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) {
81 new Expander().expand(format, c, tempResultDir);
82 }
83 verifyTargetDir();
84 }
85
86 @ParameterizedTest
87 @MethodSource("data")
88 public void fileVersion(final String format) throws Exception {
89
90 setUp(format);
91 new Expander().expand(format, archive, tempResultDir);
92 verifyTargetDir();
93 }
94
95 @ParameterizedTest
96 @MethodSource("data")
97 public void fileVersionWithAutoDetection(final String format) throws Exception {
98
99 setUp(format);
100 new Expander().expand(archive, tempResultDir);
101 verifyTargetDir();
102 }
103
104 @ParameterizedTest
105 @MethodSource("data")
106 public void inputStreamVersion(final String format) throws Exception {
107
108 setUp(format);
109 try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
110 new Expander().expand(format, i, tempResultDir);
111 }
112 verifyTargetDir();
113 }
114
115 @ParameterizedTest
116 @MethodSource("data")
117 public void inputStreamVersionWithAutoDetection(final String format) throws Exception {
118
119 setUp(format);
120 try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) {
121 new Expander().expand(i, tempResultDir);
122 }
123 verifyTargetDir();
124 }
125
126 public void setUp(final String format) throws Exception {
127 archive = newTempFile("test." + format);
128 final File dummy = newTempFile("x");
129 try (OutputStream o = Files.newOutputStream(dummy.toPath())) {
130 o.write(new byte[14]);
131 }
132 try (ArchiveOutputStream<ArchiveEntry> aos = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(format, Files.newOutputStream(archive.toPath()))) {
133 aos.putArchiveEntry(aos.createArchiveEntry(getTempDirFile().toPath(), "a"));
134 aos.closeArchiveEntry();
135 aos.putArchiveEntry(aos.createArchiveEntry(getTempDirFile(), "a/b"));
136 aos.closeArchiveEntry();
137 aos.putArchiveEntry(aos.createArchiveEntry(getTempDirFile(), "a/b/c"));
138 aos.closeArchiveEntry();
139 aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/d.txt"));
140 aos.write("Hello, world 1".getBytes(UTF_8));
141 aos.closeArchiveEntry();
142 aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/c/e.txt"));
143 aos.write("Hello, world 2".getBytes(UTF_8));
144 aos.closeArchiveEntry();
145 aos.finish();
146 }
147 }
148
149 private void verifyTargetDir() throws IOException {
150 assertTrue(new File(tempResultDir, "a").isDirectory(), "a has not been created");
151 assertTrue(new File(tempResultDir, "a/b").isDirectory(), "a/b has not been created");
152 assertTrue(new File(tempResultDir, "a/b/c").isDirectory(), "a/b/c has not been created");
153 assertHelloWorld("a/b/d.txt", "1");
154 assertHelloWorld("a/b/c/e.txt", "2");
155 }
156
157 }