1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43 this.markcount++;
44 this.readLimit = readLimit;
45
46
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
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
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
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
111 try (MarkShieldInputStream msis = new MarkShieldInputStream(
112 new NullInputStream(64, true, false))) {
113 assertThrows(UnsupportedOperationException.class, msis::reset);
114 }
115 }
116 }