View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link ClosedReader}.
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 }