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.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.nio.file.Files;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.compress.AbstractTest;
31  import org.apache.commons.compress.compressors.lz77support.Parameters;
32  import org.apache.commons.io.IOUtils;
33  import org.junit.jupiter.params.ParameterizedTest;
34  import org.junit.jupiter.params.provider.Arguments;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  public final class BlockLZ4CompressorRoundtripTest extends AbstractTest {
38  
39      public static Stream<Arguments> factory() {
40          return Stream.of(Arguments.of("default", BlockLZ4CompressorOutputStream.createParameterBuilder().build()),
41                  Arguments.of("tuned for speed", BlockLZ4CompressorOutputStream.createParameterBuilder().tunedForSpeed().build()),
42                  Arguments.of("tuned for compression ratio", BlockLZ4CompressorOutputStream.createParameterBuilder().tunedForCompressionRatio().build()));
43      }
44  
45      // yields no compression at all
46      @ParameterizedTest
47      @MethodSource("factory")
48      public void biggerFileRoundtrip(final String config, final Parameters params) throws IOException {
49          roundTripTest("COMPRESS-256.7z", config, params);
50      }
51  
52      // should yield decent compression
53      @ParameterizedTest
54      @MethodSource("factory")
55      public void blaTarRoundtrip(final String config, final Parameters params) throws IOException {
56          roundTripTest("bla.tar", config, params);
57      }
58  
59      // yields no compression at all
60      @ParameterizedTest
61      @MethodSource("factory")
62      public void gzippedLoremIpsumRoundtrip(final String config, final Parameters params) throws IOException {
63          roundTripTest("lorem-ipsum.txt.gz", config, params);
64      }
65  
66      private void roundTripTest(final String testFile, final String config, final Parameters params) throws IOException {
67          final File input = getFile(testFile);
68          final File outputSz = newTempFile(input.getName() + ".block.lz4");
69          try (OutputStream os = Files.newOutputStream(outputSz.toPath());
70                  BlockLZ4CompressorOutputStream los = new BlockLZ4CompressorOutputStream(os, params)) {
71              Files.copy(input.toPath(), los);
72          }
73          try (InputStream is = Files.newInputStream(input.toPath());
74                  BlockLZ4CompressorInputStream sis = new BlockLZ4CompressorInputStream(Files.newInputStream(outputSz.toPath()))) {
75              final byte[] expected = IOUtils.toByteArray(is);
76              final byte[] actual = IOUtils.toByteArray(sis);
77              assertArrayEquals(expected, actual);
78          }
79      }
80  
81  }