forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft: Test for Writer.of(Appendable)
- Loading branch information
Showing
1 changed file
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import java.io.Writer; | ||
import java.io.StringWriter; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.ReadOnlyBufferException; | ||
import java.util.function.Supplier; | ||
|
||
import org.testng.annotations.*; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
/* | ||
* @test | ||
* @bug 1234567 | ||
* @summary Check for expected behavior of Writer.of(). | ||
* @run testng Of | ||
*/ | ||
public class Of { | ||
private static final String CONTENT = "Some Writer Test"; | ||
|
||
private static record Config(String id, Writer writer, Supplier<String> spy) { | ||
@Override | ||
public String toString() { | ||
return id; // allows to identify config when test case fails | ||
} | ||
}; | ||
|
||
/* | ||
* Writers to be tested. | ||
*/ | ||
@DataProvider | ||
public static Config[] writers() { | ||
var sw = new StringWriter(); | ||
var sbuf = new StringBuffer(); | ||
var sbld = new StringBuilder(); | ||
var dcb = ByteBuffer.allocateDirect(CONTENT.length() * 2).asCharBuffer(); | ||
var wcb = CharBuffer.wrap(new char[CONTENT.length()]); | ||
var w = new Writer() { | ||
String s = ""; | ||
|
||
@Override | ||
public Writer append(char c) throws IOException { | ||
s += c; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Writer append(CharSequence csq) throws IOException { | ||
s += String.valueOf(csq); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Writer append(CharSequence csq, int start, int end) | ||
throws IOException { | ||
s += String.valueOf(csq).subSequence(start, end); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return s; | ||
} | ||
|
||
@Override | ||
public void write(char[] cbuf, int off, int len) throws IOException { | ||
s += new String(cbuf, off, len); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
} | ||
}; | ||
var a = new Appendable() { | ||
String s = ""; | ||
|
||
@Override | ||
public Appendable append(char c) throws IOException { | ||
s += c; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Appendable append(CharSequence csq) throws IOException { | ||
s += String.valueOf(csq); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Appendable append(CharSequence csq, int start, int end) | ||
throws IOException { | ||
s += String.valueOf(csq).subSequence(start, end); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return s; | ||
} | ||
}; | ||
return new Config[] { | ||
new Config("StringWriter", sw, sw::toString), | ||
new Config("StringBuffer", Writer.of(sbuf), sbuf::toString), | ||
new Config("StringBuilder", Writer.of(sbld), sbld::toString), | ||
new Config("Direct CharBuffer", Writer.of(dcb), | ||
() -> { dcb.flip(); return dcb.toString(); }), | ||
new Config("Wrapped CharBuffer", Writer.of(wcb), | ||
() -> { wcb.flip(); return wcb.toString(); }), | ||
new Config("Custom Writer", w, w::toString), | ||
new Config("Custom Appendable", Writer.of(a), a::toString) | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testAppendChar(Config config) throws IOException { | ||
for (int i = 0; i < CONTENT.length(); i++) | ||
config.writer.append(CONTENT.charAt(i)); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testAppendCharSequence(Config config) throws IOException { | ||
config.writer.append((CharSequence) CONTENT); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testAppendSubCharSequence(Config config) throws IOException { | ||
config.writer.append((CharSequence) CONTENT, 0, CONTENT.length()); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testWriteCharArray(Config config) throws IOException { | ||
config.writer.write(CONTENT.toCharArray()); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testWriteSubCharArray(Config config) throws IOException { | ||
config.writer.write(CONTENT.toCharArray(), 0, CONTENT.length()); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testWriteChar(Config config) throws IOException { | ||
for (int i = 0; i < CONTENT.length(); i++) | ||
config.writer.write(CONTENT.charAt(i)); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testWriteString(Config config) throws IOException { | ||
config.writer.write(CONTENT); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
|
||
@Test(dataProvider = "writers") | ||
public void testWriteSubString(Config config) throws IOException { | ||
config.writer.write(CONTENT, 0, CONTENT.length()); | ||
assertEquals(config.spy.get(), CONTENT); | ||
} | ||
} |