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.deflate;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.nio.file.Files;
27  
28  import org.apache.commons.compress.AbstractTest;
29  import org.apache.commons.io.IOUtils;
30  import org.junit.jupiter.api.Test;
31  
32  class DeflateCompressorInputStreamTest {
33  
34      @Test
35      void testAvailableShouldReturnNonZero() throws IOException {
36          try (InputStream is = Files.newInputStream(AbstractTest.getPath("bla.tar.deflatez"));
37                  DeflateCompressorInputStream in = new DeflateCompressorInputStream(is)) {
38              assertTrue(in.available() > 0);
39          }
40      }
41  
42      @Test
43      void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
44          final byte[] buf = new byte[2];
45          try (InputStream is = Files.newInputStream(AbstractTest.getPath("bla.tar.deflatez"));
46                  DeflateCompressorInputStream in = new DeflateCompressorInputStream(is)) {
47              IOUtils.toByteArray(in);
48              assertEquals(-1, in.read(buf));
49              assertEquals(-1, in.read(buf));
50          }
51      }
52  
53      @Test
54      void testShouldBeAbleToSkipAByte() throws IOException {
55          try (InputStream is = Files.newInputStream(AbstractTest.getPath("bla.tar.deflatez"));
56                  DeflateCompressorInputStream in = new DeflateCompressorInputStream(is)) {
57              assertEquals(1, in.skip(1));
58          }
59      }
60  
61      @Test
62      void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
63          try (InputStream is = Files.newInputStream(AbstractTest.getPath("bla.tar.deflatez"));
64                  DeflateCompressorInputStream in = new DeflateCompressorInputStream(is)) {
65              IOUtils.toByteArray(in);
66              assertEquals(-1, in.read());
67              assertEquals(-1, in.read());
68          }
69      }
70  
71      @Test
72      void testSingleByteReadWorksAsExpected() throws IOException {
73          try (InputStream is = Files.newInputStream(AbstractTest.getPath("bla.tar.deflatez"));
74                  DeflateCompressorInputStream in = new DeflateCompressorInputStream(is)) {
75              // tar header starts with file name "test1.xml"
76              assertEquals('t', in.read());
77          }
78      }
79  
80  }