1 /* 2 * Copyright (c) 2005, 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 /** 25 * @test 26 * @bug 5085148 27 * @summary Test if PrintWriter methods check if the stream 28 * has been closed. 29 */ 30 31 import java.io.*; 32 33 public enum OpsAfterClose { 34 check(PrintWriter w)35 WRITE_BUF { boolean check(PrintWriter w) { 36 char buf[] = new char[2]; 37 w.write(buf); 38 return w.checkError(); 39 } }, 40 check(PrintWriter w)41 WRITE_BUF_OFF { boolean check(PrintWriter w) { 42 char buf[] = new char[2]; 43 int len = 1; 44 w.write(buf, 0, len); 45 return w.checkError(); 46 } }, check(PrintWriter w)47 WRITE_INT { boolean check(PrintWriter w) { 48 w.write(1); 49 return w.checkError(); 50 } }, check(PrintWriter w)51 WRITE_STR { boolean check(PrintWriter w) { 52 String s = "abc"; 53 w.write(s); 54 return w.checkError(); 55 } }, check(PrintWriter w)56 WRITE_STR_OFF { boolean check(PrintWriter w) { 57 String s = "abc"; 58 w.write(s, 0, s.length()); 59 return w.checkError(); 60 } }; 61 check(PrintWriter w)62 abstract boolean check(PrintWriter w); 63 main(String args[])64 public static void main(String args[]) throws Exception { 65 66 System.out.println("Testing PrintWriter"); 67 boolean failed = false; 68 boolean result = false; 69 File f = new File(System.getProperty("test.dir", "."), 70 "print-writer.out"); 71 f.deleteOnExit(); 72 73 for (OpsAfterClose op : OpsAfterClose.values()) { 74 PrintWriter pw = new PrintWriter( 75 new FileWriter(f)); 76 pw.close(); 77 result = op.check(pw); 78 if (!result) { 79 failed = true; 80 } 81 System.out.println(op + ":" + result); 82 } 83 if (failed) { 84 throw new Exception( 85 "Test failed for the failed operation{s} " + 86 "above for the PrintWriter"); 87 } 88 } 89 } 90