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