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.xz;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28
29 import org.apache.commons.io.IOUtils;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.io.TempDir;
32 import org.junitpioneer.jupiter.cartesian.CartesianTest;
33 import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
34 import org.tukaani.xz.LZMA2Options;
35 import org.tukaani.xz.XZOutputStream;
36
37
38
39
40
41
42
43 class XZCompressorRoundtripTest {
44
45 @TempDir
46 static Path tempDir;
47
48 private void roundtrip(final Path outPath, final LZMA2Options options, final boolean decompressConcatenated, final int memoryLimitKiB) throws IOException {
49 final String data = "Hello World!";
50
51 try (XZCompressorOutputStream out = XZCompressorOutputStream.builder()
52 .setPath(outPath)
53 .setLzma2Options(options)
54 .get()) {
55 out.writeUtf8(data);
56 }
57 try (XZCompressorInputStream out = XZCompressorInputStream.builder()
58 .setPath(outPath)
59 .setDecompressConcatenated(decompressConcatenated)
60 .setMemoryLimitKiB(memoryLimitKiB)
61 .get()) {
62 assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
63 }
64
65 try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath))) {
66 assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
67 }
68
69 try (XZCompressorOutputStream out = new XZCompressorOutputStream(new XZOutputStream(Files.newOutputStream(outPath), options))) {
70 out.writeUtf8(data);
71 }
72 }
73
74 @CartesianTest
75 void testBuilderOptions(@Values(ints = { LZMA2Options.PRESET_MAX, LZMA2Options.PRESET_MIN, LZMA2Options.PRESET_DEFAULT }) final int preset,
76 @Values(booleans = { false, true }) final boolean decompressConcatenated, @Values(ints = { -1, 100_000 }) final int memoryLimitKiB)
77 throws IOException {
78 roundtrip(tempDir.resolve("out.xz"), new LZMA2Options(preset), false, -1);
79 }
80
81 @Test
82 void testBuilderOptionsAll() throws IOException {
83 final int dictSize = LZMA2Options.DICT_SIZE_MIN;
84 final int lc = LZMA2Options.LC_LP_MAX - 4;
85 final int lp = LZMA2Options.LC_LP_MAX - 4;
86 final int pb = LZMA2Options.PB_MAX;
87 final int mode = LZMA2Options.MODE_NORMAL;
88 final int niceLen = LZMA2Options.NICE_LEN_MIN;
89 final int mf = LZMA2Options.MF_BT4;
90 final int depthLimit = 50;
91 roundtrip(tempDir.resolve("out.xz"), new LZMA2Options(dictSize, lc, lp, pb, mode, niceLen, mf, depthLimit), false, -1);
92 }
93
94 @CartesianTest
95 void testBuilderOptionsDefgault(@Values(booleans = { false, true }) final boolean decompressConcatenated,
96 @Values(ints = { -1, 100_000 }) final int memoryLimitKiB) throws IOException {
97 roundtrip(tempDir.resolve("out.xz"), new LZMA2Options(), decompressConcatenated, memoryLimitKiB);
98 }
99
100 @Test
101 void testBuilderPath() throws IOException {
102
103 final String data = "Hello World!";
104 final Path outPath = tempDir.resolve("out.xz");
105 try (XZCompressorOutputStream out = XZCompressorOutputStream.builder().setPath(outPath).get()) {
106 out.writeUtf8(data);
107 }
108
109 try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath), false)) {
110 assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
111 }
112
113 try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath), false, -1)) {
114 assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
115 }
116 }
117 }