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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link CloseShieldInputStream}.
34   */
35  class CloseShieldInputStreamTest {
36  
37      private byte[] data;
38  
39      private InputStream byteArrayInputStream;
40  
41      private InputStream shielded;
42  
43      private boolean closed;
44  
45      @BeforeEach
46      public void setUp() {
47          data = new byte[] { 'x', 'y', 'z' };
48          byteArrayInputStream = new ByteArrayInputStream(data) {
49              @Override
50              public void close() {
51                  closed = true;
52              }
53          };
54          closed = false;
55      }
56  
57      @Test
58      void testAvailableAfterClose() throws Exception {
59          final InputStream shadow;
60          try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
61              assertEquals(3, in.available());
62              shadow = in;
63          }
64          assertEquals(0, shadow.available());
65      }
66  
67      @Test
68      void testAvailableAfterOpen() throws Exception {
69          try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
70              assertEquals(3, in.available());
71          }
72      }
73  
74      @Test
75      void testClose() throws IOException {
76          shielded = CloseShieldInputStream.wrap(byteArrayInputStream);
77          shielded.close();
78          assertFalse(closed, "closed");
79          assertEquals(-1, shielded.read(), "read()");
80          assertEquals(data[0], byteArrayInputStream.read(), "read()");
81      }
82  
83      @Test
84      void testOnClose() throws Exception {
85          try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
86              assertFalse(closed);
87              closed = true;
88              return ClosedInputStream.INSTANCE;
89          }).get()) {
90              assertEquals(3, in.available());
91          }
92          assertTrue(closed);
93      }
94  
95      @Test
96      void testOnCloseException() throws Exception {
97          final String message = "test";
98          assertEquals("test", assertThrowsExactly(IOException.class, () -> {
99              try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
100                 assertFalse(closed);
101                 throw new IOException(message);
102             }).get()) {
103                 assertEquals("test", assertThrowsExactly(IOException.class, in::close).getMessage());
104             }
105         }).getMessage());
106         assertFalse(closed);
107     }
108 
109     @Test
110     void testReadAfterCose() throws Exception {
111         final InputStream shadow;
112         try (InputStream in = CloseShieldInputStream.wrap(byteArrayInputStream)) {
113             assertEquals(3, in.available());
114             shadow = in;
115         }
116         assertEquals(IOUtils.EOF, shadow.read());
117     }
118 
119     @Test
120     void testSystemInOnSystemInNo() throws IOException {
121         shielded = CloseShieldInputStream.systemIn(byteArrayInputStream);
122         shielded.close();
123         assertTrue(closed, "closed");
124         assertEquals(data[0], shielded.read(), "read()");
125         assertEquals(data[1], byteArrayInputStream.read(), "read()");
126     }
127 
128     @Test
129     void testSystemInOnSystemInYes() throws IOException {
130         shielded = CloseShieldInputStream.systemIn(System.in);
131         shielded.close();
132         assertFalse(closed, "closed");
133         assertEquals(-1, shielded.read(), "read()");
134         assertEquals(data[0], byteArrayInputStream.read(), "read()");
135     }
136 
137 }