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.bzip2;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.BufferedOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.io.Writer;
29  import java.nio.charset.StandardCharsets;
30  import java.nio.file.Files;
31  import java.nio.file.Path;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.junit.jupiter.api.io.TempDir;
35  import org.junit.jupiter.params.ParameterizedTest;
36  import org.junit.jupiter.params.provider.ValueSource;
37  
38  /**
39   * Tests COMPRESS-686.
40   */
41  class Compress686Test {
42  
43      @TempDir
44      private Path tempDir;
45  
46      private Path compressFile(final Path file, final boolean bufferOutput) throws IOException {
47          final Path newFile = tempDir.resolve(file.getFileName().toString() + ".bz2");
48          try (InputStream in = Files.newInputStream(file);
49                  BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(newOutputStream(newFile, bufferOutput))) {
50              IOUtils.copy(in, bzOut);
51          }
52          return newFile;
53      }
54  
55      private Path decompressBzip2File(final Path file) throws IOException {
56          final Path decompressedFile = file.resolveSibling(file.getFileName().toString() + ".decompressed");
57          try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(Files.newInputStream(file));
58                  OutputStream outputStream = Files.newOutputStream(decompressedFile)) {
59              IOUtils.copy(bzIn, outputStream);
60          }
61          return decompressedFile;
62      }
63  
64      private OutputStream newOutputStream(final Path newFile, final boolean bufferOutput) throws IOException {
65          final OutputStream outputStream = Files.newOutputStream(newFile);
66          return bufferOutput ? new BufferedOutputStream(outputStream) : outputStream;
67      }
68  
69      @ParameterizedTest
70      @ValueSource(booleans = { true, false })
71      void testRoundtrip(final boolean bufferCompressOutput) throws Exception {
72          final Path file = tempDir.resolve("test.txt");
73          final String contents = "a";
74          try (Writer w = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
75              IOUtils.write(contents, w);
76          }
77          final Path compressedFile = compressFile(file, bufferCompressOutput);
78          decompressBzip2File(compressedFile);
79          assertEquals(contents, IOUtils.toString(file.toUri(), StandardCharsets.UTF_8));
80      }
81  }