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