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.xz;
20  
21  import static org.apache.commons.compress.AbstractTest.getFile;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.file.Files;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.junit.jupiter.api.Test;
33  
34  public class XZCompressorInputStreamTest {
35      private void multiByteReadConsistentlyReturnsMinusOneAtEof(final boolean decompressConcatenated) throws IOException {
36          final File input = getFile("bla.tar.xz");
37          final byte[] buf = new byte[2];
38          try (InputStream is = Files.newInputStream(input.toPath());
39                  XZCompressorInputStream in = new XZCompressorInputStream(is, decompressConcatenated);) {
40              IOUtils.toByteArray(in);
41              assertEquals(-1, in.read(buf));
42              assertEquals(-1, in.read(buf));
43          }
44      }
45  
46      private void singleByteReadConsistentlyReturnsMinusOneAtEof(final boolean decompressConcatenated) throws IOException {
47          final File input = getFile("bla.tar.xz");
48          try (InputStream is = Files.newInputStream(input.toPath());
49                  XZCompressorInputStream in = new XZCompressorInputStream(is, decompressConcatenated)) {
50              IOUtils.toByteArray(in);
51              assertEquals(-1, in.read());
52              assertEquals(-1, in.read());
53          }
54      }
55  
56      @Test
57      public void testMultiByteReadConsistentlyReturnsMinusOneAtEofDecompressConcatenated() throws IOException {
58          multiByteReadConsistentlyReturnsMinusOneAtEof(true);
59      }
60  
61      @Test
62      public void testMultiByteReadConsistentlyReturnsMinusOneAtEofNoDecompressConcatenated() throws IOException {
63          multiByteReadConsistentlyReturnsMinusOneAtEof(false);
64      }
65  
66      @Test
67      public void testRedundantTestOfAlmostDeprecatedMatchesMethod() {
68          final byte[] data = { (byte) 0xFD, '7', 'z', 'X', 'Z', '\0' };
69          assertFalse(XZCompressorInputStream.matches(data, 5));
70          assertTrue(XZCompressorInputStream.matches(data, 6));
71          assertTrue(XZCompressorInputStream.matches(data, 7));
72          data[5] = '0';
73          assertFalse(XZCompressorInputStream.matches(data, 6));
74      }
75  
76      @Test
77      public void testSingleByteReadConsistentlyReturnsMinusOneAtEofDecompressConcatenated() throws IOException {
78          singleByteReadConsistentlyReturnsMinusOneAtEof(true);
79      }
80  
81      @Test
82      public void testSingleByteReadConsistentlyReturnsMinusOneAtEofNoDecompressConcatenated() throws IOException {
83          singleByteReadConsistentlyReturnsMinusOneAtEof(false);
84      }
85  
86  }