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