1 /*
2  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.java.io.InputStream;
24 
25 import org.testng.annotations.AfterClass;
26 import org.testng.annotations.BeforeClass;
27 import org.testng.annotations.Test;
28 
29 import java.io.ByteArrayOutputStream;
30 import java.io.EOFException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 
34 import static org.testng.Assert.*;
35 
36 /*
37  * @test
38  * @bug 4358774 6516099 8139206
39  * @run testng NullInputStream
40  * @summary Check for expected behavior of InputStream.nullInputStream().
41  */
42 public class NullInputStream {
43     private static InputStream openStream;
44     private static InputStream closedStream;
45 
46     @BeforeClass
setup()47     public static void setup() {
48         openStream = InputStream.nullInputStream();
49         closedStream = InputStream.nullInputStream();
50         try {
51            closedStream.close();
52         } catch (IOException e) {
53             fail("Unexpected IOException");
54         }
55     }
56 
57     @AfterClass
closeStream()58     public static void closeStream() {
59         try {
60             openStream.close();
61         } catch (IOException e) {
62             fail("Unexpected IOException");
63         }
64     }
65 
66     @Test
testOpen()67     public static void testOpen() {
68         assertNotNull(openStream, "InputStream.nullInputStream() returned null");
69     }
70 
71     @Test
testAvailable()72     public static void testAvailable() {
73         try {
74             assertEquals(0, openStream.available(), "available() != 0");
75         } catch (IOException ioe) {
76             fail("Unexpected IOException");
77         }
78     }
79 
80     @Test
testRead()81     public static void testRead() {
82         try {
83             assertEquals(-1, openStream.read(), "read() != -1");
84         } catch (IOException ioe) {
85             fail("Unexpected IOException");
86         }
87     }
88 
89     @Test
testReadBII()90     public static void testReadBII() {
91         try {
92             assertEquals(-1, openStream.read(new byte[1], 0, 1),
93                 "read(byte[],int,int) != -1");
94         } catch (IOException ioe) {
95             fail("Unexpected IOException");
96         }
97     }
98 
99     @Test
testReadAllBytes()100     public static void testReadAllBytes() {
101         try {
102             assertEquals(0, openStream.readAllBytes().length,
103                 "readAllBytes().length != 0");
104         } catch (IOException ioe) {
105             fail("Unexpected IOException");
106         }
107     }
108 
109     @Test
testReadNBytes()110     public static void testReadNBytes() {
111         try {
112             assertEquals(0, openStream.readNBytes(new byte[1], 0, 1),
113                 "readNBytes(byte[],int,int) != 0");
114         } catch (IOException ioe) {
115             fail("Unexpected IOException");
116         }
117     }
118 
119     @Test
testReadNBytesWithLength()120     public static void testReadNBytesWithLength() {
121         try {
122             assertEquals(0, openStream.readNBytes(-1).length,
123                 "readNBytes(-1) != 0");
124             fail("Expected IllegalArgumentException not thrown");
125         } catch (IllegalArgumentException iae) {
126         } catch (IOException ioe) {
127             fail("Unexpected IOException");
128         }
129         try {
130             assertEquals(0, openStream.readNBytes(0).length,
131                 "readNBytes(0, false) != 0");
132             assertEquals(0, openStream.readNBytes(1).length,
133                 "readNBytes(1, false) != 0");
134         } catch (IOException ioe) {
135             fail("Unexpected IOException");
136         }
137     }
138 
139     @Test
testSkip()140     public static void testSkip() {
141         try {
142             assertEquals(0, openStream.skip(1), "skip() != 0");
143         } catch (IOException ioe) {
144             fail("Unexpected IOException");
145         }
146     }
147 
148     @Test
testSkipNBytes()149     public static void testSkipNBytes() {
150         try {
151             openStream.skipNBytes(-1);
152             openStream.skipNBytes(0);
153         } catch (IOException ioe) {
154             fail("Unexpected IOException");
155         }
156     }
157 
158     @Test(expectedExceptions = EOFException.class)
testSkipNBytesEOF()159     public static void testSkipNBytesEOF() throws IOException {
160         openStream.skipNBytes(1);
161     }
162 
163     @Test
testTransferTo()164     public static void testTransferTo() {
165         try {
166             assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)),
167                 "transferTo() != 0");
168         } catch (IOException ioe) {
169             fail("Unexpected IOException");
170         }
171     }
172 
173     @Test
testAvailableClosed()174     public static void testAvailableClosed() {
175         try {
176             closedStream.available();
177             fail("Expected IOException not thrown");
178         } catch (IOException e) {
179         }
180     }
181 
182     @Test
testReadClosed()183     public static void testReadClosed() {
184         try {
185             closedStream.read();
186             fail("Expected IOException not thrown");
187         } catch (IOException e) {
188         }
189     }
190 
191     @Test
testReadBIIClosed()192     public static void testReadBIIClosed() {
193         try {
194             closedStream.read(new byte[1], 0, 1);
195             fail("Expected IOException not thrown");
196         } catch (IOException e) {
197         }
198     }
199 
200     @Test
testReadAllBytesClosed()201     public static void testReadAllBytesClosed() {
202         try {
203             closedStream.readAllBytes();
204             fail("Expected IOException not thrown");
205         } catch (IOException e) {
206         }
207     }
208 
209     @Test
testReadNBytesClosed()210     public static void testReadNBytesClosed() {
211         try {
212             closedStream.readNBytes(new byte[1], 0, 1);
213             fail("Expected IOException not thrown");
214         } catch (IOException e) {
215         }
216     }
217 
218     @Test
testReadNBytesWithLengthClosed()219     public static void testReadNBytesWithLengthClosed() {
220         try {
221             closedStream.readNBytes(1);
222             fail("Expected IOException not thrown");
223         } catch (IOException e) {
224         }
225     }
226 
227     @Test
testSkipClosed()228     public static void testSkipClosed() {
229         try {
230             closedStream.skip(1);
231             fail("Expected IOException not thrown");
232         } catch (IOException e) {
233         }
234     }
235 
236     @Test
testSkipNBytesClosed()237     public static void testSkipNBytesClosed() {
238         try {
239             closedStream.skipNBytes(1);
240             fail("Expected IOException not thrown");
241         } catch (IOException e) {
242         }
243     }
244 
245     @Test
testTransferToClosed()246     public static void testTransferToClosed() {
247         try {
248             closedStream.transferTo(new ByteArrayOutputStream(7));
249             fail("Expected IOException not thrown");
250         } catch (IOException e) {
251         }
252     }
253 }
254