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