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