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;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.nio.charset.StandardCharsets;
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.junit.jupiter.params.ParameterizedTest;
30  import org.junit.jupiter.params.provider.Arguments;
31  import org.junit.jupiter.params.provider.MethodSource;
32  
33  public class CompressorStreamFactoryRoundtripTest {
34  
35      public static Stream<Arguments> data() {
36          // @formatter:off
37          return Stream.of(
38                  Arguments.of(CompressorStreamFactory.BZIP2),
39                  Arguments.of(CompressorStreamFactory.DEFLATE),
40                  Arguments.of(CompressorStreamFactory.GZIP),
41                  // CompressorStreamFactory.LZMA, // Not implemented yet
42                  // CompressorStreamFactory.PACK200, // Bug
43                  // CompressorStreamFactory.SNAPPY_FRAMED, // Not implemented yet
44                  // CompressorStreamFactory.SNAPPY_RAW, // Not implemented yet
45                  Arguments.of(CompressorStreamFactory.XZ)
46                  // CompressorStreamFactory.Z, // Not implemented yet
47          );
48          // @formatter:on
49      }
50  
51      @ParameterizedTest
52      @MethodSource("data")
53      public void testCompressorStreamFactoryRoundtrip(final String compressorName) throws Exception {
54          final CompressorStreamProvider factory = new CompressorStreamFactory();
55          final ByteArrayOutputStream compressedOs = new ByteArrayOutputStream();
56          final String fixture = "The quick brown fox jumps over the lazy dog";
57          try (CompressorOutputStream compressorOutputStream = factory.createCompressorOutputStream(compressorName, compressedOs)) {
58              compressorOutputStream.write(fixture.getBytes(StandardCharsets.UTF_8.name()));
59              compressorOutputStream.flush();
60          }
61          final ByteArrayInputStream is = new ByteArrayInputStream(compressedOs.toByteArray());
62          try (CompressorInputStream compressorInputStream = factory.createCompressorInputStream(compressorName, is, false);
63                  ByteArrayOutputStream decompressedOs = new ByteArrayOutputStream()) {
64              IOUtils.copy(compressorInputStream, decompressedOs);
65              compressorInputStream.close();
66              decompressedOs.flush();
67              decompressedOs.close();
68              assertEquals(fixture, decompressedOs.toString(StandardCharsets.UTF_8.name()));
69          }
70      }
71  
72  }