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.assertThrowsExactly;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import org.apache.commons.io.IOUtils;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class CloseShieldInputStreamTest {
36
37 private byte[] data;
38
39 private InputStream byteArrayInputStream;
40
41 private InputStream shielded;
42
43 private boolean closed;
44
45 @BeforeEach
46 public void setUp() {
47 data = new byte[] { 'x', 'y', 'z' };
48 byteArrayInputStream = new ByteArrayInputStream(data) {
49 @Override
50 public void close() {
51 closed = true;
52 }
53 };
54 closed = false;
55 }
56
57 @Test
58 void testAvailableAfterClose() throws Exception {
59 final InputStream shadow;
60 try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
61 assertEquals(3, in.available());
62 shadow = in;
63 }
64 assertEquals(0, shadow.available());
65 }
66
67 @Test
68 void testAvailableAfterOpen() throws Exception {
69 try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
70 assertEquals(3, in.available());
71 }
72 }
73
74 @Test
75 void testClose() throws IOException {
76 shielded = CloseShieldInputStream.wrap(byteArrayInputStream);
77 shielded.close();
78 assertFalse(closed, "closed");
79 assertEquals(-1, shielded.read(), "read()");
80 assertEquals(data[0], byteArrayInputStream.read(), "read()");
81 }
82
83 @Test
84 void testOnClose() throws Exception {
85 try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
86 assertFalse(closed);
87 closed = true;
88 return ClosedInputStream.INSTANCE;
89 }).get()) {
90 assertEquals(3, in.available());
91 }
92 assertTrue(closed);
93 }
94
95 @Test
96 void testOnCloseException() throws Exception {
97 final String message = "test";
98 assertEquals("test", assertThrowsExactly(IOException.class, () -> {
99 try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
100 assertFalse(closed);
101 throw new IOException(message);
102 }).get()) {
103 assertEquals("test", assertThrowsExactly(IOException.class, in::close).getMessage());
104 }
105 }).getMessage());
106 assertFalse(closed);
107 }
108
109 @Test
110 void testReadAfterCose() throws Exception {
111 final InputStream shadow;
112 try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
113 assertEquals(3, in.available());
114 shadow = in;
115 }
116 assertEquals(IOUtils.EOF, shadow.read());
117 }
118
119 @Test
120 void testSystemInOnSystemInNo() throws IOException {
121 shielded = CloseShieldInputStream.systemIn(byteArrayInputStream);
122 shielded.close();
123 assertTrue(closed, "closed");
124 assertEquals(data[0], shielded.read(), "read()");
125 assertEquals(data[1], byteArrayInputStream.read(), "read()");
126 }
127
128 @Test
129 void testSystemInOnSystemInYes() throws IOException {
130 shielded = CloseShieldInputStream.systemIn(System.in);
131 shielded.close();
132 assertFalse(closed, "closed");
133 assertEquals(-1, shielded.read(), "read()");
134 assertEquals(data[0], byteArrayInputStream.read(), "read()");
135 }
136
137 }