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.ByteArrayOutputStream;
  25 import java.io.InputStream;
  26 import java.io.IOException;
  27 import org.testng.annotations.AfterGroups;
  28 import org.testng.annotations.BeforeGroups;
  29 import org.testng.annotations.Test;
  30 import static org.testng.Assert.*;
  31 
  32 /*
  33  * @test
  34  * @bug 4358774
  35  * @run testng NullInputStream
  36  * @summary Check for expected behavior of InputStream.nullInputStream().
  37  */
  38 public class NullInputStream {
  39     private static InputStream openStream;
  40     private static InputStream closedStream;
  41 
  42     @BeforeGroups(groups="open")
  43     public static void openStream() {
  44         openStream = InputStream.nullInputStream();
  45     }
  46 
  47     @BeforeGroups(groups="closed")
  48     public static void openAndCloseStream() {
  49         closedStream = InputStream.nullInputStream();
  50         try {
  51            closedStream.close();
  52         } catch (IOException e) {
  53             fail("Unexpected IOException");
  54         }
  55     }
  56 
  57     @AfterGroups(groups="open")
  58     public static void closeStream() {
  59         try {
  60             openStream.close();
  61         } catch (IOException e) {
  62             fail("Unexpected IOException");
  63         }
  64     }
  65 
  66     @Test(groups = "open")
  67     public static void testOpen() {
  68         assertNotNull(openStream, "InputStream.nullInputStream() returned null");
  69     }
  70 
  71     @Test(groups = "open")
  72     public static void testAvailable() {
  73         try {
  74             assertEquals(0, openStream.available(), "available() != 0");
  75         } catch (IOException ioe) {
  76             fail("Unexpected IOException");
  77         }
  78     }
  79 
  80     @Test(groups = "open")
  81     public static void testRead() {
  82         try {
  83             assertEquals(-1, openStream.read(), "read() != -1");
  84         } catch (IOException ioe) {
  85             fail("Unexpected IOException");
  86         }
  87     }
  88 
  89     @Test(groups = "open")
  90     public static void testReadBII() {
  91         try {
  92             assertEquals(-1, openStream.read(new byte[1], 0, 1),
  93                 "read(byte[],int,int) != -1");
  94         } catch (IOException ioe) {
  95             fail("Unexpected IOException");
  96         }
  97     }
  98 
  99     @Test(groups = "open")
 100     public static void testReadAllBytes() {
 101         try {
 102             assertEquals(0, openStream.readAllBytes().length,
 103                 "readAllBytes().length != 0");
 104         } catch (IOException ioe) {
 105             fail("Unexpected IOException");
 106         }
 107     }
 108 
 109     @Test(groups = "open")
 110     public static void testreadNBytes() {
 111         try {
 112             assertEquals(0, openStream.readNBytes(new byte[1], 0, 1),
 113                 "readNBytes(byte[],int,int) != 0");
 114         } catch (IOException ioe) {
 115             fail("Unexpected IOException");
 116         }
 117     }
 118 
 119     @Test(groups = "open")
 120     public static void testSkip() {
 121         try {
 122             assertEquals(0, openStream.skip(1), "skip() != 0");
 123         } catch (IOException ioe) {
 124             fail("Unexpected IOException");
 125         }
 126     }
 127 
 128     @Test(groups = "open")
 129     public static void testTransferTo() {
 130         try {
 131             assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)),
 132                 "transferTo() != 0");
 133         } catch (IOException ioe) {
 134             fail("Unexpected IOException");
 135         }
 136     }
 137 
 138     @Test(groups = "closed")
 139     public static void testAvailableClosed() {
 140         try {
 141             closedStream.available();
 142             fail("Expected IOException not thrown");
 143         } catch (IOException e) {
 144         }
 145     }
 146 
 147     @Test(groups = "closed")
 148     public static void testReadClosed() {
 149         try {
 150             closedStream.read();
 151             fail("Expected IOException not thrown");
 152         } catch (IOException e) {
 153         }
 154     }
 155 
 156     @Test(groups = "closed")
 157     public static void testReadBIIClosed() {
 158         try {
 159             closedStream.read(new byte[1], 0, 1);
 160             fail("Expected IOException not thrown");
 161         } catch (IOException e) {
 162         }
 163     }
 164 
 165     @Test(groups = "closed")
 166     public static void testReadAllBytesClosed() {
 167         try {
 168             closedStream.readAllBytes();
 169             fail("Expected IOException not thrown");
 170         } catch (IOException e) {
 171         }
 172     }
 173 
 174     @Test(groups = "closed")
 175     public static void testReadNBytesClosed() {
 176         try {
 177             closedStream.readNBytes(new byte[1], 0, 1);
 178             fail("Expected IOException not thrown");
 179         } catch (IOException e) {
 180         }
 181     }
 182 
 183     @Test(groups = "closed")
 184     public static void testSkipClosed() {
 185         try {
 186             closedStream.skip(1);
 187             fail("Expected IOException not thrown");
 188         } catch (IOException e) {
 189         }
 190     }
 191 
 192     @Test(groups = "closed")
 193     public static void testTransferToClosed() {
 194         try {
 195             closedStream.transferTo(new ByteArrayOutputStream(7));
 196             fail("Expected IOException not thrown");
 197         } catch (IOException e) {
 198         }
 199     }
 200 }