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