diff --git a/test/jdk/java/io/Writer/Of.java b/test/jdk/java/io/Writer/Of.java new file mode 100644 index 0000000000000..11a46f2a5f553 --- /dev/null +++ b/test/jdk/java/io/Writer/Of.java @@ -0,0 +1,139 @@ +/* + * 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(Writer writer, Supplier spy) {}; + + /* + * 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 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(sw, sw::toString), + new Config(Writer.of(sbuf), sbuf::toString), + new Config(Writer.of(sbld), sbld::toString), + new Config(Writer.of(dcb), () -> { dcb.flip(); return dcb.toString(); }), + new Config(Writer.of(wcb), () -> { wcb.flip(); return wcb.toString(); }), + new Config(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, + "read(char[],int,int) != 0"); + + } + + @Test(dataProvider = "writers") + public void testAppendCharSequence(Config config) throws IOException { + config.writer.append((CharSequence) CONTENT); + } + + @Test(dataProvider = "writers") + public void testAppendSubCharSequence(Config config) throws IOException { + config.writer.append((CharSequence) CONTENT, 1, CONTENT.length() - 1); + } + + @Test(dataProvider = "writers") + public void testWriteCharArray(Config config) throws IOException { + config.writer.write(CONTENT.toCharArray()); + } + + @Test(dataProvider = "writers") + public void testWriteSubCharArray(Config config) throws IOException { + config.writer.write(CONTENT.toCharArray(), 1, CONTENT.length() - 2); + } + + @Test(dataProvider = "writers") + public void testWriteChar(Config config) throws IOException { + for (int i = 0; i < CONTENT.length(); i++) + config.writer.write(CONTENT.charAt(i)); + } + + @Test(dataProvider = "writers") + public void testWriteString(Config config) throws IOException { + config.writer.write(CONTENT); + } + + @Test(dataProvider = "writers") + public void testWriteSubString(Config config) throws IOException { + config.writer.write(CONTENT, 1, CONTENT.length() - 2); + } +}