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 package test.java.io.InputStream; 24 25 import java.io.ByteArrayOutputStream; 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 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") openStream()44 public static void openStream() { 45 openStream = InputStream.nullInputStream(); 46 } 47 48 @BeforeGroups(groups="closed") openAndCloseStream()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") closeStream()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") testOpen()68 public static void testOpen() { 69 assertNotNull(openStream, "InputStream.nullInputStream() returned null"); 70 } 71 72 @Test(groups = "open") testAvailable()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") testRead()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") testReadBII()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") testReadAllBytes()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") testReadNBytes()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") testReadNBytesWithLength()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") testSkip()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") testTransferTo()150 public static void testTransferTo() { 151 try { 152 assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)), 153 "transferTo() != 0"); 154 } catch (IOException ioe) { 155 fail("Unexpected IOException"); 156 } 157 } 158 159 @Test(groups = "closed") testAvailableClosed()160 public static void testAvailableClosed() { 161 try { 162 closedStream.available(); 163 fail("Expected IOException not thrown"); 164 } catch (IOException e) { 165 } 166 } 167 168 @Test(groups = "closed") testReadClosed()169 public static void testReadClosed() { 170 try { 171 closedStream.read(); 172 fail("Expected IOException not thrown"); 173 } catch (IOException e) { 174 } 175 } 176 177 @Test(groups = "closed") testReadBIIClosed()178 public static void testReadBIIClosed() { 179 try { 180 closedStream.read(new byte[1], 0, 1); 181 fail("Expected IOException not thrown"); 182 } catch (IOException e) { 183 } 184 } 185 186 @Test(groups = "closed") testReadAllBytesClosed()187 public static void testReadAllBytesClosed() { 188 try { 189 closedStream.readAllBytes(); 190 fail("Expected IOException not thrown"); 191 } catch (IOException e) { 192 } 193 } 194 195 @Test(groups = "closed") testReadNBytesClosed()196 public static void testReadNBytesClosed() { 197 try { 198 closedStream.readNBytes(new byte[1], 0, 1); 199 fail("Expected IOException not thrown"); 200 } catch (IOException e) { 201 } 202 } 203 204 @Test(groups = "closed") testReadNBytesWithLengthClosed()205 public static void testReadNBytesWithLengthClosed() { 206 try { 207 closedStream.readNBytes(1); 208 fail("Expected IOException not thrown"); 209 } catch (IOException e) { 210 } 211 } 212 213 @Test(groups = "closed") testSkipClosed()214 public static void testSkipClosed() { 215 try { 216 closedStream.skip(1); 217 fail("Expected IOException not thrown"); 218 } catch (IOException e) { 219 } 220 } 221 222 @Test(groups = "closed") testTransferToClosed()223 public static void testTransferToClosed() { 224 try { 225 closedStream.transferTo(new ByteArrayOutputStream(7)); 226 fail("Expected IOException not thrown"); 227 } catch (IOException e) { 228 } 229 } 230 }