View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.compress.compressors.brotli;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.nio.file.Files;
29  
30  import org.apache.commons.compress.AbstractTest;
31  import org.apache.commons.compress.compressors.CompressorInputStream;
32  import org.apache.commons.compress.compressors.CompressorStreamFactory;
33  import org.apache.commons.io.IOUtils;
34  import org.junit.jupiter.api.Test;
35  
36  public class BrotliCompressorInputStreamTest extends AbstractTest {
37  
38      @Test
39      public void testAvailableShouldReturnZero() throws IOException {
40          try (InputStream is = newInputStream("brotli.testdata.compressed");
41                  BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
42              assertEquals(0, in.available());
43          }
44      }
45  
46      /**
47       * Tests bridge.
48       *
49       * @throws IOException
50       */
51      @Test
52      public void testBrotliDecode() throws IOException {
53          try (InputStream inputStream = newInputStream("brotli.testdata.compressed");
54                  BrotliCompressorInputStream brotliInputStream = new BrotliCompressorInputStream(inputStream)) {
55              final byte[] expected = readAllBytes("brotli.testdata.uncompressed");
56              final ByteArrayOutputStream bos = new ByteArrayOutputStream();
57              int readByte = -1;
58              while ((readByte = brotliInputStream.read()) != -1) {
59                  bos.write(readByte);
60              }
61              assertArrayEquals(expected, bos.toByteArray());
62          }
63      }
64  
65      @Test
66      public void testBrotliUnarchive() throws Exception {
67          final File output = newTempFile("bla.tar");
68          try (InputStream is = newInputStream("bla.tar.br")) {
69              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("br", is)) {
70                  Files.copy(in, output.toPath());
71              }
72          }
73      }
74  
75      @Test
76      public void testCachingIsEnabledByDefaultAndBrotliIsPresent() {
77          assertEquals(BrotliUtils.CachedAvailability.CACHED_AVAILABLE, BrotliUtils.getCachedBrotliAvailability());
78          assertTrue(BrotliUtils.isBrotliCompressionAvailable());
79      }
80  
81      @Test
82      public void testCanTurnOffCaching() {
83          try {
84              BrotliUtils.setCacheBrotliAvailablity(false);
85              assertEquals(BrotliUtils.CachedAvailability.DONT_CACHE, BrotliUtils.getCachedBrotliAvailability());
86              assertTrue(BrotliUtils.isBrotliCompressionAvailable());
87          } finally {
88              BrotliUtils.setCacheBrotliAvailablity(true);
89          }
90      }
91  
92      @Test
93      public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
94          final byte[] buf = new byte[2];
95          try (InputStream is = newInputStream("brotli.testdata.compressed");
96                  BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
97              IOUtils.toByteArray(in);
98              assertEquals(-1, in.read(buf));
99              assertEquals(-1, in.read(buf));
100         }
101     }
102 
103     @Test
104     public void testShouldBeAbleToSkipAByte() throws IOException {
105         try (InputStream is = newInputStream("brotli.testdata.compressed");
106                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
107             assertEquals(1, in.skip(1));
108         }
109     }
110 
111     @Test
112     public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
113         try (InputStream is = newInputStream("brotli.testdata.compressed");
114                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
115             IOUtils.toByteArray(in);
116             assertEquals(-1, in.read());
117             assertEquals(-1, in.read());
118         }
119     }
120 
121     @Test
122     public void testSingleByteReadWorksAsExpected() throws IOException {
123         try (InputStream is = newInputStream("brotli.testdata.compressed");
124                 BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
125             // starts with file name "XXX"
126             assertEquals('X', in.read());
127         }
128     }
129 
130     @Test
131     public void testTurningOnCachingReEvaluatesAvailability() {
132         try {
133             BrotliUtils.setCacheBrotliAvailablity(false);
134             assertEquals(BrotliUtils.CachedAvailability.DONT_CACHE, BrotliUtils.getCachedBrotliAvailability());
135             BrotliUtils.setCacheBrotliAvailablity(true);
136             assertEquals(BrotliUtils.CachedAvailability.CACHED_AVAILABLE, BrotliUtils.getCachedBrotliAvailability());
137         } finally {
138             BrotliUtils.setCacheBrotliAvailablity(true);
139         }
140     }
141 
142 }