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.lzma;
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.Path;
27
28 import org.apache.commons.io.IOUtils;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.io.TempDir;
31 import org.tukaani.xz.LZMA2Options;
32
33
34
35
36 class LZMACompressorOutputStreamTest {
37
38 @TempDir
39 static Path tempDir;
40
41 private void roundtrip(final Path outPath, final LZMA2Options options) throws IOException {
42 final String data = "Hello World!";
43
44 try (LZMACompressorOutputStream out = LZMACompressorOutputStream.builder()
45 .setPath(outPath)
46 .setLzma2Options(options)
47 .get()) {
48 out.writeUtf8(data);
49 }
50
51 try (LZMACompressorInputStream out = LZMACompressorInputStream.builder().setPath(outPath).setMemoryLimitKiB(-1).get()) {
52 assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
53 }
54 }
55
56 @Test
57 void testBuilderOptionsAll() throws IOException {
58 final int dictSize = LZMA2Options.DICT_SIZE_MIN;
59 final int lc = LZMA2Options.LC_LP_MAX - 4;
60 final int lp = LZMA2Options.LC_LP_MAX - 4;
61 final int pb = LZMA2Options.PB_MAX;
62 final int mode = LZMA2Options.MODE_NORMAL;
63 final int niceLen = LZMA2Options.NICE_LEN_MIN;
64 final int mf = LZMA2Options.MF_BT4;
65 final int depthLimit = 50;
66 roundtrip(tempDir.resolve("out.lzma"), new LZMA2Options(dictSize, lc, lp, pb, mode, niceLen, mf, depthLimit));
67 }
68
69 @Test
70 void testBuilderOptionsDefault() throws IOException {
71 roundtrip(tempDir.resolve("out.lzma"), new LZMA2Options());
72 }
73
74 @Test
75 void testBuilderOptionsPreset() throws IOException {
76 roundtrip(tempDir.resolve("out.lzma"), new LZMA2Options(LZMA2Options.PRESET_MAX));
77 }
78
79 @Test
80 void testBuilderPath() throws IOException {
81
82 final Path outPath = tempDir.resolve("out.lzma");
83 try (LZMACompressorOutputStream out = LZMACompressorOutputStream.builder().setPath(outPath).get()) {
84 out.writeUtf8("Hello World!");
85 }
86 }
87 }