Skip to content

Commit 175e096

Browse files
committed
Draft: Test for Writer.of(Appendable)
1 parent 0bf767b commit 175e096

File tree

1 file changed

+303
-0
lines changed

1 file changed

+303
-0
lines changed

test/jdk/java/io/Writer/Of.java

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
/*
2+
* Copyright (c) 2024, 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.Writer;
25+
import java.io.StringWriter;
26+
import java.io.IOException;
27+
import java.io.StringWriter;
28+
import java.nio.ByteBuffer;
29+
import java.nio.CharBuffer;
30+
import java.nio.ReadOnlyBufferException;
31+
import java.util.function.Supplier;
32+
33+
import org.testng.annotations.*;
34+
35+
import static org.testng.Assert.*;
36+
37+
/*
38+
* @test
39+
* @bug 1234567
40+
* @summary Check for expected behavior of Writer.of().
41+
* @run testng Of
42+
*/
43+
public class Of {
44+
private static final String CONTENT = "Some Writer Test";
45+
46+
private static record Config(String id, Writer writer, Supplier<String> spy) {
47+
@Override
48+
public String toString() {
49+
return id; // allows to identify config when test case fails
50+
}
51+
};
52+
53+
/*
54+
* Writers to be tested.
55+
*/
56+
@DataProvider
57+
public static Config[] writers() {
58+
var sw = new StringWriter();
59+
var sbuf = new StringBuffer();
60+
var sbld = new StringBuilder();
61+
var dcb = ByteBuffer.allocateDirect(CONTENT.length() * 2).asCharBuffer();
62+
var wcb = CharBuffer.wrap(new char[CONTENT.length()]);
63+
var w = new Writer() {
64+
private String s = "";
65+
private boolean isClosed;
66+
67+
private void ensureOpen() throws IOException {
68+
if (isClosed)
69+
throw new IOException("Stream closed");
70+
}
71+
72+
@Override
73+
public Writer append(char c) throws IOException {
74+
ensureOpen();
75+
s += c;
76+
return this;
77+
}
78+
79+
@Override
80+
public Writer append(CharSequence csq) throws IOException {
81+
ensureOpen();
82+
s += String.valueOf(csq);
83+
return this;
84+
}
85+
86+
@Override
87+
public Writer append(CharSequence csq, int start, int end)
88+
throws IOException {
89+
ensureOpen();
90+
s += String.valueOf(csq).subSequence(start, end);
91+
return this;
92+
}
93+
94+
@Override
95+
public void write(char[] cbuf, int off, int len) throws IOException {
96+
ensureOpen();
97+
s += new String(cbuf, off, len);
98+
}
99+
100+
@Override
101+
public void flush() throws IOException {
102+
ensureOpen();
103+
}
104+
105+
@Override
106+
public void close() throws IOException {
107+
isClosed = true;
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return s;
113+
}
114+
};
115+
var a = new Appendable() {
116+
private String s = "";
117+
118+
@Override
119+
public Appendable append(char c) throws IOException {
120+
s += c;
121+
return this;
122+
}
123+
124+
@Override
125+
public Appendable append(CharSequence csq) throws IOException {
126+
s += String.valueOf(csq);
127+
return this;
128+
}
129+
130+
@Override
131+
public Appendable append(CharSequence csq, int start, int end)
132+
throws IOException {
133+
s += String.valueOf(csq).subSequence(start, end);
134+
return this;
135+
}
136+
137+
@Override
138+
public String toString() {
139+
return s;
140+
}
141+
};
142+
return new Config[] {
143+
new Config("StringWriter", sw, sw::toString),
144+
new Config("StringBuffer", Writer.of(sbuf), sbuf::toString),
145+
new Config("StringBuilder", Writer.of(sbld), sbld::toString),
146+
new Config("Direct CharBuffer", Writer.of(dcb),
147+
() -> { dcb.flip(); return dcb.toString(); }),
148+
new Config("Wrapped CharBuffer", Writer.of(wcb),
149+
() -> { wcb.flip(); return wcb.toString(); }),
150+
new Config("Custom Writer", w, w::toString),
151+
new Config("Custom Appendable", Writer.of(a), a::toString)
152+
};
153+
}
154+
155+
@Test(dataProvider = "writers")
156+
public void testAppendChar(Config config) throws IOException {
157+
for (int i = 0; i < CONTENT.length(); i++)
158+
config.writer.append(CONTENT.charAt(i));
159+
assertEquals(config.spy.get(), CONTENT);
160+
}
161+
162+
@Test(dataProvider = "writers")
163+
public void testAppendCharSequence(Config config) throws IOException {
164+
config.writer.append((CharSequence) CONTENT);
165+
assertEquals(config.spy.get(), CONTENT);
166+
}
167+
168+
@Test(dataProvider = "writers")
169+
public void testAppendSubCharSequence(Config config) throws IOException {
170+
config.writer.append((CharSequence) CONTENT, 0, CONTENT.length());
171+
assertEquals(config.spy.get(), CONTENT);
172+
}
173+
174+
@Test(dataProvider = "writers")
175+
public void testWriteCharArray(Config config) throws IOException {
176+
config.writer.write(CONTENT.toCharArray());
177+
assertEquals(config.spy.get(), CONTENT);
178+
}
179+
180+
@Test(dataProvider = "writers")
181+
public void testWriteSubCharArray(Config config) throws IOException {
182+
config.writer.write(CONTENT.toCharArray(), 0, CONTENT.length());
183+
assertEquals(config.spy.get(), CONTENT);
184+
}
185+
186+
@Test(dataProvider = "writers")
187+
public void testWriteChar(Config config) throws IOException {
188+
for (int i = 0; i < CONTENT.length(); i++)
189+
config.writer.write(CONTENT.charAt(i));
190+
assertEquals(config.spy.get(), CONTENT);
191+
}
192+
193+
@Test(dataProvider = "writers")
194+
public void testWriteString(Config config) throws IOException {
195+
config.writer.write(CONTENT);
196+
assertEquals(config.spy.get(), CONTENT);
197+
}
198+
199+
@Test(dataProvider = "writers")
200+
public void testWriteSubString(Config config) throws IOException {
201+
config.writer.write(CONTENT, 0, CONTENT.length());
202+
assertEquals(config.spy.get(), CONTENT);
203+
}
204+
205+
@Test(dataProvider = "writers")
206+
public void testAppendCharClosed(Config config) throws IOException {
207+
config.writer.close();
208+
209+
// StringWriter intentionally never throws exceptions
210+
if (config.writer instanceof StringWriter)
211+
config.writer.append('x');
212+
else
213+
assertThrows(IOException.class, () -> config.writer.append('x'));
214+
}
215+
216+
@Test(dataProvider = "writers")
217+
public void testAppendCharSequenceClosed(Config config) throws IOException {
218+
config.writer.close();
219+
220+
// StringWriter intentionally never throws exceptions
221+
if (config.writer instanceof StringWriter)
222+
config.writer.append((CharSequence) CONTENT);
223+
else
224+
assertThrows(IOException.class, () ->
225+
config.writer.append((CharSequence) CONTENT));
226+
}
227+
228+
@Test(dataProvider = "writers")
229+
public void testAppendSubCharSequenceClosed(Config config) throws IOException {
230+
config.writer.close();
231+
232+
// StringWriter intentionally never throws exceptions
233+
if (config.writer instanceof StringWriter)
234+
config.writer.append((CharSequence) CONTENT, 0, CONTENT.length());
235+
else
236+
assertThrows(IOException.class, () ->
237+
config.writer.append((CharSequence) CONTENT, 0, CONTENT.length()));
238+
}
239+
240+
@Test(dataProvider = "writers")
241+
public void testWriteCharArrayClosed(Config config) throws IOException {
242+
config.writer.close();
243+
244+
// StringWriter intentionally never throws exceptions
245+
if (config.writer instanceof StringWriter)
246+
config.writer.write(CONTENT.toCharArray());
247+
else
248+
assertThrows(IOException.class, () ->
249+
config.writer.write(CONTENT.toCharArray()));
250+
}
251+
252+
@Test(dataProvider = "writers")
253+
public void testWriteSubCharArrayClosed(Config config) throws IOException {
254+
config.writer.close();
255+
256+
// StringWriter intentionally never throws exceptions
257+
if (config.writer instanceof StringWriter)
258+
config.writer.write(CONTENT.toCharArray(), 0, CONTENT.length());
259+
else
260+
assertThrows(IOException.class, () ->
261+
config.writer.write(CONTENT.toCharArray(), 0, CONTENT.length()));
262+
}
263+
264+
@Test(dataProvider = "writers")
265+
public void testWriteCharClosed(Config config) throws IOException {
266+
config.writer.close();
267+
268+
// StringWriter intentionally never throws exceptions
269+
if (config.writer instanceof StringWriter)
270+
config.writer.write('x');
271+
else
272+
assertThrows(IOException.class, () -> config.writer.write('x'));
273+
}
274+
275+
@Test(dataProvider = "writers")
276+
public void testWriteStringClosed(Config config) throws IOException {
277+
config.writer.close();
278+
279+
// StringWriter intentionally never throws exceptions
280+
if (config.writer instanceof StringWriter)
281+
config.writer.write(CONTENT);
282+
else
283+
assertThrows(IOException.class, () -> config.writer.write(CONTENT));
284+
}
285+
286+
@Test(dataProvider = "writers")
287+
public void testWriteSubStringClosed(Config config) throws IOException {
288+
config.writer.close();
289+
290+
// StringWriter intentionally never throws exceptions
291+
if (config.writer instanceof StringWriter)
292+
config.writer.write(CONTENT, 0, CONTENT.length());
293+
else
294+
assertThrows(IOException.class, () ->
295+
config.writer.write(CONTENT, 0, CONTENT.length()));
296+
}
297+
298+
@Test(dataProvider = "writers")
299+
public void testClosedClosed(Config config) throws IOException {
300+
config.writer.close();
301+
config.writer.close();
302+
}
303+
}

0 commit comments

Comments
 (0)