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  package org.apache.commons.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.junit.jupiter.api.Test;
28  
29  public class MarkShieldInputStreamTest {
30  
31      private static final class MarkTestableInputStream extends ProxyInputStream {
32          int markcount;
33          int readLimit;
34  
35          public MarkTestableInputStream(final InputStream in) {
36              super(in);
37          }
38  
39          @SuppressWarnings("sync-override")
40          @Override
41          public void mark(final int readLimit) {
42              // record that `mark` was called
43              this.markcount++;
44              this.readLimit = readLimit;
45  
46              // invoke on super
47              super.mark(readLimit);
48          }
49      }
50  
51      @Test
52      public void testMarkIsNoOpWhenUnderlyingDoesNotSupport() throws IOException {
53          try (MarkTestableInputStream in = new MarkTestableInputStream(new NullInputStream(64, false, false));
54               final MarkShieldInputStream msis = new MarkShieldInputStream(in)) {
55  
56              msis.mark(1024);
57  
58              assertEquals(0, in.markcount);
59              assertEquals(0, in.readLimit);
60          }
61      }
62  
63      @Test
64      public void testMarkIsNoOpWhenUnderlyingSupports() throws IOException {
65          try (MarkTestableInputStream in = new MarkTestableInputStream(new NullInputStream(64, true, false));
66               final MarkShieldInputStream msis = new MarkShieldInputStream(in)) {
67  
68              msis.mark(1024);
69  
70              assertEquals(0, in.markcount);
71              assertEquals(0, in.readLimit);
72          }
73      }
74  
75      @Test
76      public void testMarkSupportedIsFalseWhenUnderlyingFalse() throws IOException {
77          // test wrapping an underlying stream which does NOT support marking
78          try (InputStream is = new NullInputStream(64, false, false)) {
79              assertFalse(is.markSupported());
80  
81              try (MarkShieldInputStream msis = new MarkShieldInputStream(is)) {
82                  assertFalse(msis.markSupported());
83              }
84          }
85      }
86  
87      @Test
88      public void testMarkSupportedIsFalseWhenUnderlyingTrue() throws IOException {
89          // test wrapping an underlying stream which supports marking
90          try (InputStream is = new NullInputStream(64, true, false)) {
91              assertTrue(is.markSupported());
92  
93              try (MarkShieldInputStream msis = new MarkShieldInputStream(is)) {
94                  assertFalse(msis.markSupported());
95              }
96          }
97      }
98  
99      @Test
100     public void testResetThrowsExceptionWhenUnderlyingDoesNotSupport() throws IOException {
101         // test wrapping an underlying stream which does NOT support marking
102         try (MarkShieldInputStream msis = new MarkShieldInputStream(
103                 new NullInputStream(64, false, false))) {
104             assertThrows(UnsupportedOperationException.class, msis::reset);
105         }
106     }
107 
108     @Test
109     public void testResetThrowsExceptionWhenUnderlyingSupports() throws IOException {
110         // test wrapping an underlying stream which supports marking
111         try (MarkShieldInputStream msis = new MarkShieldInputStream(
112                 new NullInputStream(64, true, false))) {
113             assertThrows(UnsupportedOperationException.class, msis::reset);
114         }
115     }
116 }