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