Path: blob/master/test/jdk/java/io/InputStream/NullInputStream.java
41149 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.AfterClass;24import org.testng.annotations.BeforeClass;25import org.testng.annotations.Test;2627import java.io.ByteArrayOutputStream;28import java.io.EOFException;29import java.io.IOException;30import java.io.InputStream;3132import static org.testng.Assert.*;3334/*35* @test36* @bug 4358774 6516099 813920637* @run testng NullInputStream38* @summary Check for expected behavior of InputStream.nullInputStream().39*/40public class NullInputStream {41private static InputStream openStream;42private static InputStream closedStream;4344@BeforeClass45public static void setup() {46openStream = InputStream.nullInputStream();47closedStream = InputStream.nullInputStream();48try {49closedStream.close();50} catch (IOException e) {51fail("Unexpected IOException");52}53}5455@AfterClass56public static void closeStream() {57try {58openStream.close();59} catch (IOException e) {60fail("Unexpected IOException");61}62}6364@Test65public static void testOpen() {66assertNotNull(openStream, "InputStream.nullInputStream() returned null");67}6869@Test70public static void testAvailable() {71try {72assertEquals(0, openStream.available(), "available() != 0");73} catch (IOException ioe) {74fail("Unexpected IOException");75}76}7778@Test79public static void testRead() {80try {81assertEquals(-1, openStream.read(), "read() != -1");82} catch (IOException ioe) {83fail("Unexpected IOException");84}85}8687@Test88public static void testReadBII() {89try {90assertEquals(-1, openStream.read(new byte[1], 0, 1),91"read(byte[],int,int) != -1");92} catch (IOException ioe) {93fail("Unexpected IOException");94}95}9697@Test98public static void testReadAllBytes() {99try {100assertEquals(0, openStream.readAllBytes().length,101"readAllBytes().length != 0");102} catch (IOException ioe) {103fail("Unexpected IOException");104}105}106107@Test108public static void testReadNBytes() {109try {110assertEquals(0, openStream.readNBytes(new byte[1], 0, 1),111"readNBytes(byte[],int,int) != 0");112} catch (IOException ioe) {113fail("Unexpected IOException");114}115}116117@Test118public static void testReadNBytesWithLength() {119try {120assertEquals(0, openStream.readNBytes(-1).length,121"readNBytes(-1) != 0");122fail("Expected IllegalArgumentException not thrown");123} catch (IllegalArgumentException iae) {124} catch (IOException ioe) {125fail("Unexpected IOException");126}127try {128assertEquals(0, openStream.readNBytes(0).length,129"readNBytes(0, false) != 0");130assertEquals(0, openStream.readNBytes(1).length,131"readNBytes(1, false) != 0");132} catch (IOException ioe) {133fail("Unexpected IOException");134}135}136137@Test138public static void testSkip() {139try {140assertEquals(0, openStream.skip(1), "skip() != 0");141} catch (IOException ioe) {142fail("Unexpected IOException");143}144}145146@Test147public static void testSkipNBytes() {148try {149openStream.skipNBytes(-1);150openStream.skipNBytes(0);151} catch (IOException ioe) {152fail("Unexpected IOException");153}154}155156@Test(expectedExceptions = EOFException.class)157public static void testSkipNBytesEOF() throws IOException {158openStream.skipNBytes(1);159}160161@Test162public static void testTransferTo() {163try {164assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)),165"transferTo() != 0");166} catch (IOException ioe) {167fail("Unexpected IOException");168}169}170171@Test172public static void testAvailableClosed() {173try {174closedStream.available();175fail("Expected IOException not thrown");176} catch (IOException e) {177}178}179180@Test181public static void testReadClosed() {182try {183closedStream.read();184fail("Expected IOException not thrown");185} catch (IOException e) {186}187}188189@Test190public static void testReadBIIClosed() {191try {192closedStream.read(new byte[1], 0, 1);193fail("Expected IOException not thrown");194} catch (IOException e) {195}196}197198@Test199public static void testReadAllBytesClosed() {200try {201closedStream.readAllBytes();202fail("Expected IOException not thrown");203} catch (IOException e) {204}205}206207@Test208public static void testReadNBytesClosed() {209try {210closedStream.readNBytes(new byte[1], 0, 1);211fail("Expected IOException not thrown");212} catch (IOException e) {213}214}215216@Test217public static void testReadNBytesWithLengthClosed() {218try {219closedStream.readNBytes(1);220fail("Expected IOException not thrown");221} catch (IOException e) {222}223}224225@Test226public static void testSkipClosed() {227try {228closedStream.skip(1);229fail("Expected IOException not thrown");230} catch (IOException e) {231}232}233234@Test235public static void testSkipNBytesClosed() {236try {237closedStream.skipNBytes(1);238fail("Expected IOException not thrown");239} catch (IOException e) {240}241}242243@Test244public static void testTransferToClosed() {245try {246closedStream.transferTo(new ByteArrayOutputStream(7));247fail("Expected IOException not thrown");248} catch (IOException e) {249}250}251}252253254