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.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.io.BufferedInputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.nio.channels.FileChannel;
34 import java.nio.channels.SeekableByteChannel;
35 import java.nio.file.Files;
36 import java.nio.file.StandardOpenOption;
37 import java.util.stream.Stream;
38
39 import org.apache.commons.compress.AbstractTest;
40 import org.apache.commons.compress.archivers.ArchiveEntry;
41 import org.apache.commons.compress.archivers.ArchiveException;
42 import org.apache.commons.compress.archivers.ArchiveInputStream;
43 import org.apache.commons.compress.archivers.ArchiveOutputStream;
44 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
45 import org.apache.commons.io.IOUtils;
46 import org.junit.jupiter.params.ParameterizedTest;
47 import org.junit.jupiter.params.provider.Arguments;
48 import org.junit.jupiter.params.provider.MethodSource;
49
50 public class ParameterizedArchiverTest extends AbstractTest {
51
52
53
54
55 public static Stream<Arguments> data() {
56 return Stream.of(Arguments.of("tar"), Arguments.of("cpio"), Arguments.of("zip"));
57 }
58
59 private File target;
60
61 @ParameterizedTest
62 @MethodSource("data")
63 public void archiveStreamVersion(final String format) throws Exception {
64
65 setUp(format);
66 try (OutputStream os = Files.newOutputStream(target.toPath());
67 ArchiveOutputStream<?> aos = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(format, os)) {
68 new Archiver().create(aos, getTempDirFile());
69 }
70 verifyContent(format);
71 }
72
73 private void assertDir(final String expectedName, final ArchiveEntry entry) {
74 assertNotNull(entry, () -> expectedName + " does not exists");
75 assertEquals(expectedName + "/", entry.getName());
76 assertTrue(entry.isDirectory(), expectedName + " is not a directory");
77 }
78
79 private void assertHelloWorld(final String expectedName, final String suffix, final ArchiveEntry entry, final InputStream is) throws IOException {
80 assertNotNull(entry, () -> expectedName + " does not exists");
81 assertEquals(expectedName, entry.getName());
82 assertFalse(entry.isDirectory(), expectedName + " is a directory");
83 final byte[] expected = ("Hello, world " + suffix).getBytes(UTF_8);
84 final byte[] actual = IOUtils.toByteArray(is);
85 assertArrayEquals(expected, actual);
86 }
87
88 @ParameterizedTest
89 @MethodSource("data")
90 public void channelVersion(final String format) throws Exception {
91
92 setUp(format);
93 try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE,
94 StandardOpenOption.TRUNCATE_EXISTING)) {
95 new Archiver().create(format, c, getTempDirFile());
96 }
97 verifyContent(format);
98 }
99
100 @ParameterizedTest
101 @MethodSource("data")
102 public void fileVersion(final String format) throws Exception {
103
104 setUp(format);
105 new Archiver().create(format, target, getTempDirFile());
106 verifyContent(format);
107 }
108
109 @ParameterizedTest
110 @MethodSource("data")
111 public void outputStreamVersion(final String format) throws Exception {
112
113 setUp(format);
114 try (OutputStream os = Files.newOutputStream(target.toPath())) {
115 new Archiver().create(format, os, getTempDirFile());
116 }
117 verifyContent(format);
118 }
119
120 public void setUp(final String format) throws Exception {
121 final File c = newTempFile("a/b/c");
122 c.mkdirs();
123 try (OutputStream os = Files.newOutputStream(newTempFile("a/b/d.txt").toPath())) {
124 os.write("Hello, world 1".getBytes(UTF_8));
125 }
126 try (OutputStream os = Files.newOutputStream(newTempFile("a/b/c/e.txt").toPath())) {
127 os.write("Hello, world 2".getBytes(UTF_8));
128 }
129 target = new File(tempResultDir, "test." + format);
130 }
131
132 private void verifyContent(final String format) throws IOException, ArchiveException {
133 try (InputStream is = Files.newInputStream(target.toPath());
134 BufferedInputStream bis = new BufferedInputStream(is);
135 ArchiveInputStream<?> ais = ArchiveStreamFactory.DEFAULT.createArchiveInputStream(format, bis)) {
136 assertDir("a", ais.getNextEntry());
137 assertDir("a/b", ais.getNextEntry());
138 final ArchiveEntry n = ais.getNextEntry();
139 assertNotNull(n);
140
141 if (n.getName().endsWith("/")) {
142 assertDir("a/b/c", n);
143 assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
144 assertHelloWorld("a/b/d.txt", "1", ais.getNextEntry(), ais);
145 } else {
146 assertHelloWorld("a/b/d.txt", "1", n, ais);
147 assertDir("a/b/c", ais.getNextEntry());
148 assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
149 }
150 }
151 }
152 }