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.brotli;
21  
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.nio.file.Files;
31  
32  import org.apache.commons.compress.AbstractTest;
33  import org.apache.commons.compress.compressors.CompressorInputStream;
34  import org.apache.commons.compress.compressors.CompressorStreamFactory;
35  import org.apache.commons.io.IOUtils;
36  import org.junit.jupiter.api.Test;
37  
38  class BrotliCompressorInputStreamTest extends AbstractTest {
39  
40      @Test
41      void testAvailableShouldReturnZero() throws IOException {
42          try (InputStream is = newInputStream("brotli.testdata.compressed");
43                  BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
44              assertEquals(0, in.available());
45          }
46      }
47  
48      /**
49       * Tests bridge.
50       *
51       * @throws IOException if an I/O error occurs.
52       */
53      @Test
54      void testBrotliDecode() throws IOException {
55          try (InputStream inputStream = newInputStream("brotli.testdata.compressed");
56                  BrotliCompressorInputStream brotliInputStream = new BrotliCompressorInputStream(inputStream)) {
57              final byte[] expected = readAllBytes("brotli.testdata.uncompressed");
58              final ByteArrayOutputStream bos = new ByteArrayOutputStream();
59              IOUtils.copy(brotliInputStream, bos);
60              assertArrayEquals(expected, bos.toByteArray());
61          }
62      }
63  
64      @Test
65      void testBrotliUnarchive() throws Exception {
66          final File output = newTempFile("bla.tar");
67          try (InputStream is = newInputStream("bla.tar.br")) {
68              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("br", is)) {
69                  Files.copy(in, output.toPath());
70              }
71          }
72      }
73  
74      @Test
75      void testCachingIsEnabledByDefaultAndBrotliIsPresent() {
76          assertEquals(BrotliUtils.CachedAvailability.CACHED_AVAILABLE, BrotliUtils.getCachedBrotliAvailability());
77          assertTrue(BrotliUtils.isBrotliCompressionAvailable());
78      }
79  
80      @Test
81      void testCanTurnOffCaching() {
82          try {
83              BrotliUtils.setCacheBrotliAvailablity(false);
84              assertEquals(BrotliUtils.CachedAvailability.DONT_CACHE, BrotliUtils.getCachedBrotliAvailability());
85              assertTrue(BrotliUtils.isBrotliCompressionAvailable());
86          } finally {
87              BrotliUtils.setCacheBrotliAvailablity(true);
88          }
89      }
90  
91      @Test
92      void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
93          final byte[] buf = new byte[2];
94          try (InputStream is = newInputStream("brotli.testdata.compressed");
95                  BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
96              IOUtils.toByteArray(in);
97              assertEquals(-1, in.read(buf));
98              assertEquals(-1, in.read(buf));
99          }
100     }
101 
102     @Test
103     void testShouldBeAbleToSkipAByte() throws IOException {
104         try (InputStream is = newInputStream("brotli.testdata.compressed");
105                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
106             assertEquals(1, in.skip(1));
107         }
108     }
109 
110     @Test
111     void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
112         try (InputStream is = newInputStream("brotli.testdata.compressed");
113                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
114             IOUtils.toByteArray(in);
115             assertEquals(-1, in.read());
116             assertEquals(-1, in.read());
117         }
118     }
119 
120     @Test
121     void testSingleByteReadWorksAsExpected() throws IOException {
122         try (InputStream is = newInputStream("brotli.testdata.compressed");
123                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
124             // starts with file name "XXX"
125             assertEquals('X', in.read());
126         }
127     }
128 
129     @Test
130     void testTurningOnCachingReEvaluatesAvailability() {
131         try {
132             BrotliUtils.setCacheBrotliAvailablity(false);
133             assertEquals(BrotliUtils.CachedAvailability.DONT_CACHE, BrotliUtils.getCachedBrotliAvailability());
134             BrotliUtils.setCacheBrotliAvailablity(true);
135             assertEquals(BrotliUtils.CachedAvailability.CACHED_AVAILABLE, BrotliUtils.getCachedBrotliAvailability());
136         } finally {
137             BrotliUtils.setCacheBrotliAvailablity(true);
138         }
139     }
140 
141 }