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.assertThrows;
22
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.nio.CharBuffer;
26
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 class ClosedReaderTest {
33
34 private void assertEof(final Reader reader) throws IOException {
35 assertEquals(EOF, reader.read(), "read()");
36 }
37
38 @Test
39 void testRead() throws IOException {
40 try (Reader reader = new ClosedReader()) {
41 assertEof(reader);
42 }
43 }
44
45 @Test
46 void testReadArray() throws Exception {
47 try (Reader reader = new ClosedReader()) {
48 assertEquals(EOF, reader.read(new char[4096]));
49 assertEquals(EOF, reader.read(new char[1]));
50 assertEquals(0, reader.read(new char[0]));
51 assertThrows(NullPointerException.class, () -> reader.read((char[]) null));
52 }
53 }
54
55 @Test
56 void testReadArrayIndex() throws Exception {
57 try (Reader reader = new ClosedReader()) {
58 final char[] cbuf = new char[4096];
59 assertEquals(EOF, reader.read(cbuf, 0, 2048));
60 assertEquals(EOF, reader.read(cbuf, 2048, 2048));
61 assertEquals(0, reader.read(cbuf, 4096, 0));
62 assertThrows(IndexOutOfBoundsException.class, () -> reader.read(cbuf, -1, 1));
63 assertThrows(IndexOutOfBoundsException.class, () -> reader.read(cbuf, 0, 4097));
64 assertThrows(IndexOutOfBoundsException.class, () -> reader.read(cbuf, 1, -1));
65
66 assertEquals(EOF, reader.read(new char[1]));
67 assertEquals(0, reader.read(new char[0]));
68 assertThrows(NullPointerException.class, () -> reader.read(null, 0, 0));
69 }
70 }
71
72 @Test
73 void testReadCharBuffer() throws Exception {
74 try (Reader reader = new ClosedReader()) {
75 final CharBuffer charBuffer = CharBuffer.wrap(new char[4096]);
76 assertEquals(EOF, reader.read(charBuffer));
77 charBuffer.position(4096);
78 assertEquals(0, reader.read(charBuffer));
79
80 assertEquals(EOF, reader.read(CharBuffer.wrap(new char[1])));
81 assertEquals(0, reader.read(CharBuffer.wrap(new char[0])));
82 assertThrows(NullPointerException.class, () -> reader.read((CharBuffer) null));
83 }
84 }
85
86 @Test
87 void testSingleton() throws Exception {
88 try (@SuppressWarnings("deprecation")
89 Reader reader = ClosedReader.CLOSED_READER) {
90 assertEof(reader);
91 }
92 try (Reader reader = ClosedReader.INSTANCE) {
93 assertEof(reader);
94 }
95 }
96
97 @Test
98 void testSkip() throws Exception {
99 try (Reader reader = new ClosedReader()) {
100 assertEquals(0, reader.skip(4096));
101 assertEquals(0, reader.skip(1));
102 assertEquals(0, reader.skip(0));
103 }
104 }
105
106 }