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.apache.commons.io.IOUtils.EOF;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22
23 import java.io.InputStream;
24
25 import org.junit.jupiter.api.Test;
26
27
28
29
30 class ClosedInputStreamTest {
31
32 private void assertEof(final ClosedInputStream cis) {
33 assertEquals(EOF, cis.read(), "read()");
34 }
35
36 @Test
37 void testAvailableAfterClose() throws Exception {
38 assertEquals(0, ClosedInputStream.INSTANCE.available());
39 assertEquals(0, ClosedInputStream.INSTANCE.available());
40 final InputStream shadow;
41 try (InputStream in = new ClosedInputStream()) {
42 assertEquals(0, in.available());
43 shadow = in;
44 }
45 assertEquals(0, shadow.available());
46 }
47
48 @Test
49 void testAvailableAfterOpen() throws Exception {
50 assertEquals(0, ClosedInputStream.INSTANCE.available());
51 assertEquals(0, ClosedInputStream.INSTANCE.available());
52 try (ClosedInputStream cis = new ClosedInputStream()) {
53 assertEquals(0, cis.available());
54 }
55 }
56
57 @SuppressWarnings("resource")
58 @Test
59 void testNonNull() throws Exception {
60 assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(null));
61 assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(ClosedInputStream.INSTANCE));
62 assertSame(System.in, ClosedInputStream.ifNull(System.in));
63 }
64
65 @Test
66 void testRead() throws Exception {
67 try (ClosedInputStream cis = new ClosedInputStream()) {
68 assertEof(cis);
69 }
70 }
71
72 @Test
73 void testReadAfterCose() throws Exception {
74 assertEquals(0, ClosedInputStream.INSTANCE.available());
75 assertEquals(0, ClosedInputStream.INSTANCE.available());
76 final InputStream shadow;
77 try (InputStream in = new ClosedInputStream()) {
78 assertEquals(0, in.available());
79 shadow = in;
80 }
81 assertEquals(EOF, shadow.read());
82 }
83
84 @Test
85 void testReadArray() throws Exception {
86 try (ClosedInputStream cis = new ClosedInputStream()) {
87 assertEquals(EOF, cis.read(new byte[4096]));
88 assertEquals(EOF, cis.read(new byte[1]));
89 assertEquals(EOF, cis.read(new byte[0]));
90 }
91 }
92
93 @Test
94 void testReadArrayIndex() throws Exception {
95 try (ClosedInputStream cis = new ClosedInputStream()) {
96 assertEquals(EOF, cis.read(new byte[4096], 0, 1));
97 assertEquals(EOF, cis.read(new byte[1], 0, 1));
98 assertEquals(EOF, cis.read(new byte[0], 0, 0));
99 }
100 }
101
102 @Test
103 void testSingleton() throws Exception {
104 try (@SuppressWarnings("deprecation")
105 ClosedInputStream cis = ClosedInputStream.CLOSED_INPUT_STREAM) {
106 assertEof(cis);
107 }
108 try (ClosedInputStream cis = ClosedInputStream.INSTANCE) {
109 assertEof(cis);
110 }
111 }
112
113 }