Path: blob/master/test/jdk/java/io/Reader/NullReader.java
41152 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 java.io.Reader;24import java.io.IOException;25import java.io.StringWriter;26import java.nio.CharBuffer;27import java.nio.ReadOnlyBufferException;2829import org.testng.annotations.*;3031import static org.testng.Assert.*;3233/*34* @test35* @bug 8196298 820493036* @run testng NullReader37* @summary Check for expected behavior of Reader.nullReader().38*/39public class NullReader {40private static Reader openReader;41private static Reader closedReader;4243@BeforeClass44public static void setup() throws IOException {45openReader = Reader.nullReader();46closedReader = Reader.nullReader();47closedReader.close();48}4950@AfterClass51public static void closeStream() throws IOException {52openReader.close();53}5455@Test56public static void testOpen() {57assertNotNull(openReader, "Reader.nullReader() returned null");58}5960@Test61public static void testRead() throws IOException {62assertEquals(-1, openReader.read(), "read() != -1");63}6465@Test66public static void testReadBII() throws IOException {67assertEquals(-1, openReader.read(new char[1], 0, 1),68"read(char[],int,int) != -1");69}7071@Test72public static void testReadBIILenZero() throws IOException {73assertEquals(0, openReader.read(new char[1], 0, 0),74"read(char[],int,int) != 0");75}7677@Test78public static void testReadCharBuffer() throws IOException {79CharBuffer charBuffer = CharBuffer.allocate(1);80assertEquals(-1, openReader.read(charBuffer),81"read(CharBuffer) != -1");82}8384@Test85public static void testReadCharBufferZeroRemaining() throws IOException {86CharBuffer charBuffer = CharBuffer.allocate(0);87assertEquals(0, openReader.read(charBuffer),88"read(CharBuffer) != 0");89}9091@Test92public static void testReady() throws IOException {93assertFalse(openReader.ready());94}9596@Test97public static void testSkip() throws IOException {98assertEquals(0, openReader.skip(1), "skip() != 0");99}100101@Test102public static void testTransferTo() throws IOException {103assertEquals(0, openReader.transferTo(new StringWriter(7)),104"transferTo() != 0");105}106107@Test(expectedExceptions = IOException.class)108public static void testReadClosed() throws IOException {109closedReader.read();110}111112@Test(expectedExceptions = IOException.class)113public static void testReadBIIClosed() throws IOException {114closedReader.read(new char[1], 0, 1);115}116117@Test(expectedExceptions = IOException.class)118public static void testReadCharBufferClosed() throws IOException {119CharBuffer charBuffer = CharBuffer.allocate(0);120closedReader.read(charBuffer);121}122123@Test(expectedExceptions = IOException.class)124public static void testReadCharBufferZeroRemainingClosed() throws IOException {125CharBuffer charBuffer = CharBuffer.allocate(0);126closedReader.read(charBuffer);127}128129@Test(expectedExceptions = IOException.class)130public static void testReadyClosed() throws IOException {131closedReader.ready();132}133134@Test(expectedExceptions = IOException.class)135public static void testSkipClosed() throws IOException {136closedReader.skip(1);137}138139@Test(expectedExceptions = IOException.class)140public static void testTransferToClosed() throws IOException {141closedReader.transferTo(new StringWriter(7));142}143}144145146