Path: blob/master/test/jdk/java/io/PrintWriter/OpsAfterClose.java
41152 views
/*1* Copyright (c) 2005, 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*/2223/**24* @test25* @bug 508514826* @summary Test if PrintWriter methods check if the stream27* has been closed.28*/2930import java.io.*;3132public enum OpsAfterClose {3334WRITE_BUF { boolean check(PrintWriter w) {35char buf[] = new char[2];36w.write(buf);37return w.checkError();38} },3940WRITE_BUF_OFF { boolean check(PrintWriter w) {41char buf[] = new char[2];42int len = 1;43w.write(buf, 0, len);44return w.checkError();45} },46WRITE_INT { boolean check(PrintWriter w) {47w.write(1);48return w.checkError();49} },50WRITE_STR { boolean check(PrintWriter w) {51String s = "abc";52w.write(s);53return w.checkError();54} },55WRITE_STR_OFF { boolean check(PrintWriter w) {56String s = "abc";57w.write(s, 0, s.length());58return w.checkError();59} };6061abstract boolean check(PrintWriter w);6263public static void main(String args[]) throws Exception {6465System.out.println("Testing PrintWriter");66boolean failed = false;67boolean result = false;68File f = new File(System.getProperty("test.dir", "."),69"print-writer.out");70f.deleteOnExit();7172for (OpsAfterClose op : OpsAfterClose.values()) {73PrintWriter pw = new PrintWriter(74new FileWriter(f));75pw.close();76result = op.check(pw);77if (!result) {78failed = true;79}80System.out.println(op + ":" + result);81}82if (failed) {83throw new Exception(84"Test failed for the failed operation{s} " +85"above for the PrintWriter");86}87}88}899091