1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
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
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 }