View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests for class {@link XZCompressorOutputStream} and {@link XZCompressorInputStream}.
39   *
40   * @see XZCompressorOutputStream
41   * @see XZCompressorInputStream
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          // @formatter:off
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          // @formatter:on
65          try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath))) {
66              assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
67          }
68          // deprecated
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         // This test does not use LZMA2Options
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         // deprecated
109         try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath), false)) {
110             assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
111         }
112         // deprecated
113         try (XZCompressorInputStream out = new XZCompressorInputStream(Files.newInputStream(outPath), false, -1)) {
114             assertEquals(data, IOUtils.toString(out, StandardCharsets.UTF_8));
115         }
116     }
117 }