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.IOException;
  25 import java.io.OutputStream;
  26 import org.testng.annotations.AfterGroups;
  27 import org.testng.annotations.BeforeGroups;
  28 import org.testng.annotations.Test;
  29 import static org.testng.Assert.*;
  30 
  31 /*
  32  * @test
  33  * @bug 4358774
  34  * @run testng NullOutputStream
  35  * @summary Check for expected behavior of OutputStream.nullOutputStream().
  36  */
  37 public class NullOutputStream {
  38     private static OutputStream openStream;
  39     private static OutputStream closedStream;
  40 
  41     @BeforeGroups(groups="open")
  42     public static void openStream() {
  43         openStream = OutputStream.nullOutputStream();
  44     }
  45 
  46     @BeforeGroups(groups="closed")
  47     public static void openAndCloseStream() {
  48         closedStream = OutputStream.nullOutputStream();
  49         try {
  50            closedStream.close();
  51         } catch (IOException e) {
  52             fail("Unexpected IOException");
  53         }
  54     }
  55 
  56     @AfterGroups(groups="open")
  57     public static void closeStream() {
  58         try {
  59             openStream.close();
  60         } catch (IOException e) {
  61             fail("Unexpected IOException");
  62         }
  63     }
  64 
  65     @Test(groups="open")
  66     public static void testOpen() {
  67         assertNotNull(openStream,
  68             "OutputStream.nullOutputStream() returned null");
  69     }
  70 
  71     @Test(groups="open")
  72     public static void testWrite() {
  73         try {
  74             openStream.write(62832);
  75         } catch (IOException e) {
  76             fail("Unexpected IOException");
  77         }
  78     }
  79 
  80     @Test(groups="open")
  81     public static void testWriteBII() {
  82         try {
  83             openStream.write(new byte[] {(byte)6}, 0, 1);
  84         } catch (Exception e) {
  85             fail("Unexpected IOException");
  86         }
  87     }
  88 
  89     @Test(groups="closed")
  90     public static void testWriteClosed() {
  91         try {
  92             closedStream.write(62832);
  93             fail("Expected IOException not thrown");
  94         } catch (IOException e) {
  95         }
  96     }
  97 
  98     @Test(groups="closed")
  99     public static void testWriteBIIClosed() {
 100         try {
 101             closedStream.write(new byte[] {(byte)6}, 0, 1);
 102             fail("Expected IOException not thrown");
 103         } catch (IOException e) {
 104         }
 105     }
 106 }