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 // Android-added: Test package.
24 package test.java.io.Writer;
25 
26 import java.io.IOException;
27 import java.io.Writer;
28 
29 import org.testng.annotations.AfterGroups;
30 import org.testng.annotations.BeforeGroups;
31 import org.testng.annotations.Test;
32 
33 import static org.testng.Assert.*;
34 
35 /*
36  * @test
37  * @bug 8196298
38  * @run testng NullWriter
39  * @summary Check for expected behavior of Writer.nullWriter().
40  */
41 public class NullWriter {
42     private static Writer openWriter;
43     private static Writer closedWriter;
44 
45     @BeforeGroups(groups = "open")
openStream()46     public static void openStream() {
47         openWriter = Writer.nullWriter();
48     }
49 
50     @BeforeGroups(groups = "closed")
openAndCloseStream()51     public static void openAndCloseStream() throws IOException {
52         closedWriter = Writer.nullWriter();
53         closedWriter.close();
54     }
55 
56     @AfterGroups(groups = "open")
closeStream()57     public static void closeStream() throws IOException {
58         openWriter.close();
59     }
60 
61     @Test(groups = "open")
testOpen()62     public static void testOpen() {
63         assertNotNull(openWriter, "Writer.nullWriter() returned null");
64     }
65 
66     @Test(groups = "open")
testAppendChar()67     public static void testAppendChar() throws IOException {
68         assertSame(openWriter, openWriter.append('x'));
69     }
70 
71     @Test(groups = "open")
testAppendCharSequence()72     public static void testAppendCharSequence() throws IOException {
73         CharSequence cs = "abc";
74         assertSame(openWriter, openWriter.append(cs));
75     }
76 
77     @Test(groups = "open")
testAppendCharSequenceNull()78     public static void testAppendCharSequenceNull() throws IOException {
79         assertSame(openWriter, openWriter.append(null));
80     }
81 
82     @Test(groups = "open")
testAppendCharSequenceII()83     public static void testAppendCharSequenceII() throws IOException {
84         CharSequence cs = "abc";
85         assertSame(openWriter, openWriter.append(cs, 0, 1));
86     }
87 
88     @Test(groups = "open")
testAppendCharSequenceIINull()89     public static void testAppendCharSequenceIINull() throws IOException {
90         assertSame(openWriter, openWriter.append(null, 2, 1));
91     }
92 
93     @Test(groups = "open")
testFlush()94     public static void testFlush() throws IOException {
95         openWriter.flush();
96     }
97 
98     @Test(groups = "open")
testWrite()99     public static void testWrite() throws IOException {
100         openWriter.write(62832);
101     }
102 
103     @Test(groups = "open")
testWriteString()104     public static void testWriteString() throws IOException {
105         openWriter.write("");
106     }
107 
108     @Test(groups = "open")
testWriteStringII()109     public static void testWriteStringII() throws IOException {
110         openWriter.write("", 0, 0);
111     }
112 
113     @Test(groups = "open")
testWriteBII()114     public static void testWriteBII() throws IOException, Exception {
115         openWriter.write(new char[]{(char) 6}, 0, 1);
116     }
117 
118     @Test(groups = "closed", expectedExceptions = IOException.class)
testAppendCharClosed()119     public static void testAppendCharClosed() throws IOException {
120         closedWriter.append('x');
121     }
122 
123     @Test(groups = "closed", expectedExceptions = IOException.class)
testAppendCharSequenceClosed()124     public static void testAppendCharSequenceClosed() throws IOException {
125         CharSequence cs = "abc";
126         closedWriter.append(cs);
127     }
128 
129     @Test(groups = "closed", expectedExceptions = IOException.class)
testAppendCharSequenceNullClosed()130     public static void testAppendCharSequenceNullClosed() throws IOException {
131         closedWriter.append(null);
132     }
133 
134     @Test(groups = "closed", expectedExceptions = IOException.class)
testAppendCharSequenceIIClosed()135     public static void testAppendCharSequenceIIClosed() throws IOException {
136         CharSequence cs = "abc";
137         closedWriter.append(cs, 0, 1);
138     }
139 
140     @Test(groups = "closed", expectedExceptions = IOException.class)
testAppendCharSequenceIINullClosed()141     public static void testAppendCharSequenceIINullClosed() throws IOException {
142         closedWriter.append(null, 2, 1);
143     }
144 
145     @Test(groups = "closed", expectedExceptions = IOException.class)
testFlushClosed()146     public static void testFlushClosed() throws IOException {
147         closedWriter.flush();
148     }
149 
150     @Test(groups = "closed", expectedExceptions = IOException.class)
testWriteClosed()151     public static void testWriteClosed() throws IOException {
152         closedWriter.write(62832);
153     }
154 
155     @Test(groups = "closed", expectedExceptions = IOException.class)
testWriteStringClosed()156     public static void testWriteStringClosed() throws IOException {
157         closedWriter.write("");
158     }
159 
160     @Test(groups = "closed", expectedExceptions = IOException.class)
testWriteStringIIClosed()161     public static void testWriteStringIIClosed() throws IOException {
162         closedWriter.write("", 0, 0);
163     }
164 
165     @Test(groups = "closed", expectedExceptions = IOException.class)
testWriteBIIClosed()166     public static void testWriteBIIClosed() throws IOException {
167         closedWriter.write(new char[]{(char) 6}, 0, 1);
168     }
169 }
170