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  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 }