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   * http://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  package org.apache.commons.compress.compressors.lz4;
20  
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.nio.file.Files;
29  import java.util.Random;
30  import java.util.stream.Stream;
31  
32  import org.apache.commons.compress.AbstractTest;
33  import org.apache.commons.io.IOUtils;
34  import org.junit.jupiter.api.Test;
35  import org.junit.jupiter.params.ParameterizedTest;
36  import org.junit.jupiter.params.provider.Arguments;
37  import org.junit.jupiter.params.provider.MethodSource;
38  
39  public final class FramedLZ4CompressorRoundtripTest extends AbstractTest {
40  
41      public static Stream<Arguments> factory() {
42          return Stream.of(Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64)),
43                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K256)),
44                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.M1)),
45                  Arguments.of(FramedLZ4CompressorOutputStream.Parameters.DEFAULT),
46                  // default without content checksum
47                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.M4, false, false, false)),
48                  // default with block checksum
49                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.M4, true, true, false)),
50                  // small blocksize (so we get enough blocks) and enabled block dependency, otherwise defaults
51                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64, true, false, true)),
52                  // default, tuned for speed
53                  Arguments.of(new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.M4, true, false, false,
54                          BlockLZ4CompressorOutputStream.createParameterBuilder().tunedForSpeed().build())));
55      }
56  
57      @ParameterizedTest
58      @MethodSource("factory")
59      public void biggerFileRoundtrip(final FramedLZ4CompressorOutputStream.Parameters params) throws IOException {
60          roundTripTest("COMPRESS-256.7z", params);
61      }
62  
63      // should yield decent compression
64      @ParameterizedTest
65      @MethodSource("factory")
66      public void blaTarRoundtrip(final FramedLZ4CompressorOutputStream.Parameters params) throws IOException {
67          roundTripTest("bla.tar", params);
68      }
69  
70      // yields no compression at all
71      @ParameterizedTest
72      @MethodSource("factory")
73      public void gzippedLoremIpsumRoundtrip(final FramedLZ4CompressorOutputStream.Parameters params) throws IOException {
74          roundTripTest("lorem-ipsum.txt.gz", params);
75      }
76  
77      private void roundTripTest(final String testFile, final FramedLZ4CompressorOutputStream.Parameters params) throws IOException {
78          final File input = getFile(testFile);
79          byte[] expected;
80          try (InputStream is = Files.newInputStream(input.toPath())) {
81              expected = IOUtils.toByteArray(is);
82          }
83          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
84          try (FramedLZ4CompressorOutputStream los = new FramedLZ4CompressorOutputStream(bos, params)) {
85              IOUtils.copy(new ByteArrayInputStream(expected), los);
86          }
87          try (FramedLZ4CompressorInputStream sis = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
88              final byte[] actual = IOUtils.toByteArray(sis);
89              assertArrayEquals(expected, actual);
90          }
91      }
92  
93      @Test
94      public void test64KMultipleBlocks() throws IOException {
95          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
96          final byte[] expected = new byte[98304];
97          new Random(0).nextBytes(expected);
98          try (FramedLZ4CompressorOutputStream compressor = new FramedLZ4CompressorOutputStream(buffer,
99                  new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64, true, false, false))) {
100             compressor.write(expected);
101         }
102         try (FramedLZ4CompressorInputStream sis = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(buffer.toByteArray()))) {
103             assertArrayEquals(expected, IOUtils.toByteArray(sis));
104         }
105     }
106 }