1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.utils;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import org.junit.jupiter.api.Test;
29
30 class SkipShieldingInputStreamTest {
31
32 @Test
33 void testSkipDelegatesToRead() throws IOException {
34 try (InputStream i = new SkipShieldingInputStream(new InputStream() {
35 @Override
36 public int read() {
37 return -1;
38 }
39
40 @Override
41 public int read(byte[] b, int off, int len) {
42 return len;
43 }
44
45 @Override
46 public long skip(long n) {
47 fail("skip invoked");
48 return -1;
49 }
50 })) {
51 assertEquals(100, i.skip(100));
52 }
53 }
54
55 @Test
56 void testSkipHasAnUpperBoundOnRead() throws IOException {
57 try (InputStream i = new SkipShieldingInputStream(new InputStream() {
58 @Override
59 public int read() {
60 return -1;
61 }
62
63 @Override
64 public int read(byte[] b, int off, int len) {
65 return len;
66 }
67
68 @Override
69 public long skip(long n) {
70 fail("skip invoked");
71 return -1;
72 }
73 })) {
74 assertTrue(Integer.MAX_VALUE > i.skip(Long.MAX_VALUE));
75 }
76 }
77
78 @Test
79 void testSkipSwallowsNegativeArguments() throws IOException {
80 try (InputStream i = new SkipShieldingInputStream(new InputStream() {
81 @Override
82 public int read() {
83 return -1;
84 }
85
86 @Override
87 public int read(byte[] b, int off, int len) {
88 fail("read invoked");
89 return len;
90 }
91
92 @Override
93 public long skip(long n) {
94 fail("skip invoked");
95 return -1;
96 }
97 })) {
98 assertEquals(0, i.skip(Long.MIN_VALUE));
99 }
100 }
101
102 }