1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.compressors.bzip2;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.BufferedOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.Writer;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32
33 import org.apache.commons.io.IOUtils;
34 import org.junit.jupiter.api.io.TempDir;
35 import org.junit.jupiter.params.ParameterizedTest;
36 import org.junit.jupiter.params.provider.ValueSource;
37
38
39
40
41 class Compress686Test {
42
43 @TempDir
44 private Path tempDir;
45
46 private Path compressFile(final Path file, final boolean bufferOutput) throws IOException {
47 final Path newFile = tempDir.resolve(file.getFileName().toString() + ".bz2");
48 try (InputStream in = Files.newInputStream(file);
49 BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(newOutputStream(newFile, bufferOutput))) {
50 IOUtils.copy(in, bzOut);
51 }
52 return newFile;
53 }
54
55 private Path decompressBzip2File(final Path file) throws IOException {
56 final Path decompressedFile = file.resolveSibling(file.getFileName().toString() + ".decompressed");
57 try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(Files.newInputStream(file));
58 OutputStream outputStream = Files.newOutputStream(decompressedFile)) {
59 IOUtils.copy(bzIn, outputStream);
60 }
61 return decompressedFile;
62 }
63
64 private OutputStream newOutputStream(final Path newFile, final boolean bufferOutput) throws IOException {
65 final OutputStream outputStream = Files.newOutputStream(newFile);
66 return bufferOutput ? new BufferedOutputStream(outputStream) : outputStream;
67 }
68
69 @ParameterizedTest
70 @ValueSource(booleans = { true, false })
71 void testRoundtrip(final boolean bufferCompressOutput) throws Exception {
72 final Path file = tempDir.resolve("test.txt");
73 final String contents = "a";
74 try (Writer w = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
75 IOUtils.write(contents, w);
76 }
77 final Path compressedFile = compressFile(file, bufferCompressOutput);
78 decompressBzip2File(compressedFile);
79 assertEquals(contents, IOUtils.toString(file.toUri(), StandardCharsets.UTF_8));
80 }
81 }