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    *      http://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  
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   * Tests {@link ClosedReader}.
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  }