1 /* 2 * Copyright (c) 2018, 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 24 import java.io.ByteArrayOutputStream; 25 import java.io.EOFException; 26 import java.io.InputStream; 27 import java.io.IOException; 28 import org.testng.annotations.AfterGroups; 29 import org.testng.annotations.BeforeGroups; 30 import org.testng.annotations.Test; 31 import static org.testng.Assert.*; 32 33 /* 34 * @test 35 * @bug 4358774 6516099 8139206 36 * @run testng NullInputStream 37 * @summary Check for expected behavior of InputStream.nullInputStream(). 38 */ 39 public class NullInputStream { 40 private static InputStream openStream; 41 private static InputStream closedStream; 42 43 @BeforeGroups(groups="open") 44 public static void openStream() { 45 openStream = InputStream.nullInputStream(); 46 } 47 48 @BeforeGroups(groups="closed") 49 public static void openAndCloseStream() { 50 closedStream = InputStream.nullInputStream(); 51 try { 52 closedStream.close(); 53 } catch (IOException e) { 54 fail("Unexpected IOException"); 55 } 56 } 57 58 @AfterGroups(groups="open") 59 public static void closeStream() { 60 try { 61 openStream.close(); 62 } catch (IOException e) { 63 fail("Unexpected IOException"); 64 } 65 } 66 67 @Test(groups = "open") 68 public static void testOpen() { 69 assertNotNull(openStream, "InputStream.nullInputStream() returned null"); 70 } 71 72 @Test(groups = "open") 73 public static void testAvailable() { 74 try { 75 assertEquals(0, openStream.available(), "available() != 0"); 76 } catch (IOException ioe) { 77 fail("Unexpected IOException"); 78 } 79 } 80 81 @Test(groups = "open") 82 public static void testRead() { 83 try { 84 assertEquals(-1, openStream.read(), "read() != -1"); 85 } catch (IOException ioe) { 86 fail("Unexpected IOException"); 87 } 88 } 89 90 @Test(groups = "open") 91 public static void testReadBII() { 92 try { 93 assertEquals(-1, openStream.read(new byte[1], 0, 1), 94 "read(byte[],int,int) != -1"); 95 } catch (IOException ioe) { 96 fail("Unexpected IOException"); 97 } 98 } 99 100 @Test(groups = "open") 101 public static void testReadAllBytes() { 102 try { 103 assertEquals(0, openStream.readAllBytes().length, 104 "readAllBytes().length != 0"); 105 } catch (IOException ioe) { 106 fail("Unexpected IOException"); 107 } 108 } 109 110 @Test(groups = "open") 111 public static void testReadNBytes() { 112 try { 113 assertEquals(0, openStream.readNBytes(new byte[1], 0, 1), 114 "readNBytes(byte[],int,int) != 0"); 115 } catch (IOException ioe) { 116 fail("Unexpected IOException"); 117 } 118 } 119 120 @Test(groups = "open") 121 public static void testReadNBytesWithLength() { 122 try { 123 assertEquals(0, openStream.readNBytes(-1).length, 124 "readNBytes(-1) != 0"); 125 fail("Expected IllegalArgumentException not thrown"); 126 } catch (IllegalArgumentException iae) { 127 } catch (IOException ioe) { 128 fail("Unexpected IOException"); 129 } 130 try { 131 assertEquals(0, openStream.readNBytes(0).length, 132 "readNBytes(0, false) != 0"); 133 assertEquals(0, openStream.readNBytes(1).length, 134 "readNBytes(1, false) != 0"); 135 } catch (IOException ioe) { 136 fail("Unexpected IOException"); 137 } 138 } 139 140 @Test(groups = "open") 141 public static void testSkip() { 142 try { 143 assertEquals(0, openStream.skip(1), "skip() != 0"); 144 } catch (IOException ioe) { 145 fail("Unexpected IOException"); 146 } 147 } 148 149 @Test(groups = "open") 150 public static void testSkipNBytes() { 151 try { 152 openStream.skipNBytes(-1); 153 openStream.skipNBytes(0); 154 } catch (IOException ioe) { 155 fail("Unexpected IOException"); 156 } 157 } 158 159 @Test(groups = "open", expectedExceptions = EOFException.class) 160 public static void testSkipNBytesEOF() throws IOException { 161 openStream.skipNBytes(1); 162 } 163 164 @Test(groups = "open") 165 public static void testTransferTo() { 166 try { 167 assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)), 168 "transferTo() != 0"); 169 } catch (IOException ioe) { 170 fail("Unexpected IOException"); 171 } 172 } 173 174 @Test(groups = "closed") 175 public static void testAvailableClosed() { 176 try { 177 closedStream.available(); 178 fail("Expected IOException not thrown"); 179 } catch (IOException e) { 180 } 181 } 182 183 @Test(groups = "closed") 184 public static void testReadClosed() { 185 try { 186 closedStream.read(); 187 fail("Expected IOException not thrown"); 188 } catch (IOException e) { 189 } 190 } 191 192 @Test(groups = "closed") 193 public static void testReadBIIClosed() { 194 try { 195 closedStream.read(new byte[1], 0, 1); 196 fail("Expected IOException not thrown"); 197 } catch (IOException e) { 198 } 199 } 200 201 @Test(groups = "closed") 202 public static void testReadAllBytesClosed() { 203 try { 204 closedStream.readAllBytes(); 205 fail("Expected IOException not thrown"); 206 } catch (IOException e) { 207 } 208 } 209 210 @Test(groups = "closed") 211 public static void testReadNBytesClosed() { 212 try { 213 closedStream.readNBytes(new byte[1], 0, 1); 214 fail("Expected IOException not thrown"); 215 } catch (IOException e) { 216 } 217 } 218 219 @Test(groups = "closed") 220 public static void testReadNBytesWithLengthClosed() { 221 try { 222 closedStream.readNBytes(1); 223 fail("Expected IOException not thrown"); 224 } catch (IOException e) { 225 } 226 } 227 228 @Test(groups = "closed") 229 public static void testSkipClosed() { 230 try { 231 closedStream.skip(1); 232 fail("Expected IOException not thrown"); 233 } catch (IOException e) { 234 } 235 } 236 237 @Test(groups = "closed") 238 public static void testSkipNBytesClosed() { 239 try { 240 closedStream.skipNBytes(1); 241 fail("Expected IOException not thrown"); 242 } catch (IOException e) { 243 } 244 } 245 246 @Test(groups = "closed") 247 public static void testTransferToClosed() { 248 try { 249 closedStream.transferTo(new ByteArrayOutputStream(7)); 250 fail("Expected IOException not thrown"); 251 } catch (IOException e) { 252 } 253 } 254 }