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 8204930
  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 testReady() throws IOException {
 100         assertFalse(openReader.ready());
 101     }
 102 
 103     @Test(groups = "open")
 104     public static void testSkip() throws IOException {
 105         assertEquals(0, openReader.skip(1), "skip() != 0");
 106     }
 107 
 108     @Test(groups = "open")
 109     public static void testTransferTo() throws IOException {
 110         assertEquals(0, openReader.transferTo(new StringWriter(7)),
 111                 "transferTo() != 0");
 112     }
 113 
 114     @Test(groups = "closed", expectedExceptions = IOException.class)
 115     public static void testReadClosed() throws IOException {
 116         closedReader.read();
 117     }
 118 
 119     @Test(groups = "closed", expectedExceptions = IOException.class)
 120     public static void testReadBIIClosed() throws IOException {
 121         closedReader.read(new char[1], 0, 1);
 122     }
 123 
 124     @Test(groups = "closed", expectedExceptions = IOException.class)
 125     public static void testReadCharBufferClosed() throws IOException {
 126         CharBuffer charBuffer = CharBuffer.allocate(0);
 127         closedReader.read(charBuffer);
 128     }
 129 
 130     @Test(groups = "closed", expectedExceptions = IOException.class)
 131     public static void testReadCharBufferZeroRemainingClosed() throws IOException {
 132         CharBuffer charBuffer = CharBuffer.allocate(0);
 133         closedReader.read(charBuffer);
 134     }
 135 
 136     @Test(groups = "closed", expectedExceptions = IOException.class)
 137     public static void testReadyClosed() throws IOException {
 138         closedReader.ready();
 139     }
 140 
 141     @Test(groups = "closed", expectedExceptions = IOException.class)
 142     public static void testSkipClosed() throws IOException {
 143         closedReader.skip(1);
 144     }
 145 
 146     @Test(groups = "closed", expectedExceptions = IOException.class)
 147     public static void testTransferToClosed() throws IOException {
 148         closedReader.transferTo(new StringWriter(7));
 149     }
 150 }