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.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   * Tests {@link LZMACompressorOutputStream}.
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          // @formatter:off
44          try (LZMACompressorOutputStream out = LZMACompressorOutputStream.builder()
45                  .setPath(outPath)
46                  .setLzma2Options(options)
47                  .get()) {
48              out.writeUtf8(data);
49          }
50          // @formatter:on
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          // This test does not use LZMA2Options
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  }