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.InputStream; 26 import java.io.IOException; 27 import org.testng.annotations.AfterGroups; 28 import org.testng.annotations.BeforeGroups; 29 import org.testng.annotations.Test; 30 import static org.testng.Assert.*; 31 32 /* 33 * @test 34 * @bug 4358774 8139206 35 * @run testng NullInputStream 36 * @summary Check for expected behavior of InputStream.nullInputStream(). 37 */ 38 public class NullInputStream { 39 private static InputStream openStream; 40 private static InputStream closedStream; 41 42 @BeforeGroups(groups="open") 43 public static void openStream() { 44 openStream = InputStream.nullInputStream(); 45 } 46 47 @BeforeGroups(groups="closed") 48 public static void openAndCloseStream() { 49 closedStream = InputStream.nullInputStream(); 50 try { 51 closedStream.close(); 52 } catch (IOException e) { 53 fail("Unexpected IOException"); 54 } 55 } 56 57 @AfterGroups(groups="open") 58 public static void closeStream() { 59 try { 60 openStream.close(); 61 } catch (IOException e) { 62 fail("Unexpected IOException"); 63 } 64 } 65 66 @Test(groups = "open") 67 public static void testOpen() { 68 assertNotNull(openStream, "InputStream.nullInputStream() returned null"); 69 } 70 71 @Test(groups = "open") 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(groups = "open") 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(groups = "open") 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(groups = "open") 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(groups = "open") 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(groups = "open") 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(groups = "open") 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(groups = "open") 149 public static void testTransferTo() { 150 try { 151 assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)), 152 "transferTo() != 0"); 153 } catch (IOException ioe) { 154 fail("Unexpected IOException"); 155 } 156 } 157 158 @Test(groups = "closed") 159 public static void testAvailableClosed() { 160 try { 161 closedStream.available(); 162 fail("Expected IOException not thrown"); 163 } catch (IOException e) { 164 } 165 } 166 167 @Test(groups = "closed") 168 public static void testReadClosed() { 169 try { 170 closedStream.read(); 171 fail("Expected IOException not thrown"); 172 } catch (IOException e) { 173 } 174 } 175 176 @Test(groups = "closed") 177 public static void testReadBIIClosed() { 178 try { 179 closedStream.read(new byte[1], 0, 1); 180 fail("Expected IOException not thrown"); 181 } catch (IOException e) { 182 } 183 } 184 185 @Test(groups = "closed") 186 public static void testReadAllBytesClosed() { 187 try { 188 closedStream.readAllBytes(); 189 fail("Expected IOException not thrown"); 190 } catch (IOException e) { 191 } 192 } 193 194 @Test(groups = "closed") 195 public static void testReadNBytesClosed() { 196 try { 197 closedStream.readNBytes(new byte[1], 0, 1); 198 fail("Expected IOException not thrown"); 199 } catch (IOException e) { 200 } 201 } 202 203 @Test(groups = "closed") 204 public static void testReadNBytesWithLengthClosed() { 205 try { 206 closedStream.readNBytes(1); 207 fail("Expected IOException not thrown"); 208 } catch (IOException e) { 209 } 210 } 211 212 @Test(groups = "closed") 213 public static void testSkipClosed() { 214 try { 215 closedStream.skip(1); 216 fail("Expected IOException not thrown"); 217 } catch (IOException e) { 218 } 219 } 220 221 @Test(groups = "closed") 222 public static void testTransferToClosed() { 223 try { 224 closedStream.transferTo(new ByteArrayOutputStream(7)); 225 fail("Expected IOException not thrown"); 226 } catch (IOException e) { 227 } 228 } 229 }