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.Reader; 24 25 import java.io.Reader; 26 import java.io.IOException; 27 import java.io.StringWriter; 28 import java.nio.CharBuffer; 29 import java.nio.ReadOnlyBufferException; 30 31 import org.testng.annotations.AfterGroups; 32 import org.testng.annotations.BeforeGroups; 33 import org.testng.annotations.Test; 34 35 import static org.testng.Assert.*; 36 37 /* 38 * @test 39 * @bug 8196298 8204930 40 * @run testng NullReader 41 * @summary Check for expected behavior of Reader.nullReader(). 42 */ 43 public class NullReader { 44 private static Reader openReader; 45 private static Reader closedReader; 46 47 @BeforeGroups(groups = "open") openStream()48 public static void openStream() { 49 openReader = Reader.nullReader(); 50 } 51 52 @BeforeGroups(groups = "closed") openAndCloseStream()53 public static void openAndCloseStream() throws IOException { 54 closedReader = Reader.nullReader(); 55 closedReader.close(); 56 } 57 58 @AfterGroups(groups = "open") closeStream()59 public static void closeStream() throws IOException { 60 openReader.close(); 61 } 62 63 @Test(groups = "open") testOpen()64 public static void testOpen() { 65 assertNotNull(openReader, "Reader.nullReader() returned null"); 66 } 67 68 @Test(groups = "open") testRead()69 public static void testRead() throws IOException { 70 assertEquals(-1, openReader.read(), "read() != -1"); 71 } 72 73 @Test(groups = "open") testReadBII()74 public static void testReadBII() throws IOException { 75 assertEquals(-1, openReader.read(new char[1], 0, 1), 76 "read(char[],int,int) != -1"); 77 } 78 79 @Test(groups = "open") testReadBIILenZero()80 public static void testReadBIILenZero() throws IOException { 81 assertEquals(0, openReader.read(new char[1], 0, 0), 82 "read(char[],int,int) != 0"); 83 } 84 85 @Test(groups = "open") testReadCharBuffer()86 public static void testReadCharBuffer() throws IOException { 87 CharBuffer charBuffer = CharBuffer.allocate(1); 88 assertEquals(-1, openReader.read(charBuffer), 89 "read(CharBuffer) != -1"); 90 } 91 92 @Test(groups = "open") testReadCharBufferZeroRemaining()93 public static void testReadCharBufferZeroRemaining() throws IOException { 94 CharBuffer charBuffer = CharBuffer.allocate(0); 95 assertEquals(0, openReader.read(charBuffer), 96 "read(CharBuffer) != 0"); 97 } 98 99 @Test(groups = "open") testReady()100 public static void testReady() throws IOException { 101 assertFalse(openReader.ready()); 102 } 103 104 @Test(groups = "open") testSkip()105 public static void testSkip() throws IOException { 106 assertEquals(0, openReader.skip(1), "skip() != 0"); 107 } 108 109 @Test(groups = "open") testTransferTo()110 public static void testTransferTo() throws IOException { 111 assertEquals(0, openReader.transferTo(new StringWriter(7)), 112 "transferTo() != 0"); 113 } 114 115 @Test(groups = "closed", expectedExceptions = IOException.class) testReadClosed()116 public static void testReadClosed() throws IOException { 117 closedReader.read(); 118 } 119 120 @Test(groups = "closed", expectedExceptions = IOException.class) testReadBIIClosed()121 public static void testReadBIIClosed() throws IOException { 122 closedReader.read(new char[1], 0, 1); 123 } 124 125 @Test(groups = "closed", expectedExceptions = IOException.class) testReadCharBufferClosed()126 public static void testReadCharBufferClosed() throws IOException { 127 CharBuffer charBuffer = CharBuffer.allocate(0); 128 closedReader.read(charBuffer); 129 } 130 131 @Test(groups = "closed", expectedExceptions = IOException.class) testReadCharBufferZeroRemainingClosed()132 public static void testReadCharBufferZeroRemainingClosed() throws IOException { 133 CharBuffer charBuffer = CharBuffer.allocate(0); 134 closedReader.read(charBuffer); 135 } 136 137 @Test(groups = "closed", expectedExceptions = IOException.class) testReadyClosed()138 public static void testReadyClosed() throws IOException { 139 closedReader.ready(); 140 } 141 142 @Test(groups = "closed", expectedExceptions = IOException.class) testSkipClosed()143 public static void testSkipClosed() throws IOException { 144 closedReader.skip(1); 145 } 146 147 @Test(groups = "closed", expectedExceptions = IOException.class) testTransferToClosed()148 public static void testTransferToClosed() throws IOException { 149 closedReader.transferTo(new StringWriter(7)); 150 } 151 }