Path: blob/master/test/jdk/java/io/Writer/NullWriter.java
41149 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.AfterClass;24import org.testng.annotations.BeforeClass;25import org.testng.annotations.Test;2627import java.io.IOException;28import java.io.Writer;2930import static org.testng.Assert.assertNotNull;31import static org.testng.Assert.assertSame;3233/*34* @test35* @bug 819629836* @run testng NullWriter37* @summary Check for expected behavior of Writer.nullWriter().38*/39public class NullWriter {40private static Writer openWriter;41private static Writer closedWriter;4243@BeforeClass44public static void setup() throws IOException {45openWriter = Writer.nullWriter();46closedWriter = Writer.nullWriter();47closedWriter.close();48}4950@AfterClass51public static void closeStream() throws IOException {52openWriter.close();53}5455@Test56public static void testOpen() {57assertNotNull(openWriter, "Writer.nullWriter() returned null");58}5960@Test61public static void testAppendChar() throws IOException {62assertSame(openWriter, openWriter.append('x'));63}6465@Test66public static void testAppendCharSequence() throws IOException {67CharSequence cs = "abc";68assertSame(openWriter, openWriter.append(cs));69}7071@Test72public static void testAppendCharSequenceNull() throws IOException {73assertSame(openWriter, openWriter.append(null));74}7576@Test77public static void testAppendCharSequenceII() throws IOException {78CharSequence cs = "abc";79assertSame(openWriter, openWriter.append(cs, 0, 1));80}8182@Test83public static void testAppendCharSequenceIINull() throws IOException {84assertSame(openWriter, openWriter.append(null, 2, 1));85}8687@Test88public static void testFlush() throws IOException {89openWriter.flush();90}9192@Test93public static void testWrite() throws IOException {94openWriter.write(62832);95}9697@Test98public static void testWriteString() throws IOException {99openWriter.write("");100}101102@Test103public static void testWriteStringII() throws IOException {104openWriter.write("", 0, 0);105}106107@Test108public static void testWriteBII() throws IOException, Exception {109openWriter.write(new char[]{(char) 6}, 0, 1);110}111112@Test(expectedExceptions = IOException.class)113public static void testAppendCharClosed() throws IOException {114closedWriter.append('x');115}116117@Test(expectedExceptions = IOException.class)118public static void testAppendCharSequenceClosed() throws IOException {119CharSequence cs = "abc";120closedWriter.append(cs);121}122123@Test(expectedExceptions = IOException.class)124public static void testAppendCharSequenceNullClosed() throws IOException {125closedWriter.append(null);126}127128@Test(expectedExceptions = IOException.class)129public static void testAppendCharSequenceIIClosed() throws IOException {130CharSequence cs = "abc";131closedWriter.append(cs, 0, 1);132}133134@Test(expectedExceptions = IOException.class)135public static void testAppendCharSequenceIINullClosed() throws IOException {136closedWriter.append(null, 2, 1);137}138139@Test(expectedExceptions = IOException.class)140public static void testFlushClosed() throws IOException {141closedWriter.flush();142}143144@Test(expectedExceptions = IOException.class)145public static void testWriteClosed() throws IOException {146closedWriter.write(62832);147}148149@Test(expectedExceptions = IOException.class)150public static void testWriteStringClosed() throws IOException {151closedWriter.write("");152}153154@Test(expectedExceptions = IOException.class)155public static void testWriteStringIIClosed() throws IOException {156closedWriter.write("", 0, 0);157}158159@Test(expectedExceptions = IOException.class)160public static void testWriteBIIClosed() throws IOException {161closedWriter.write(new char[]{(char) 6}, 0, 1);162}163}164165166