Path: blob/master/test/jdk/java/io/OutputStreamWriter/WriteAfterClose.java
41149 views
/*1* Copyright (c) 1998, 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 4143651 508514826*27* @summary Test if Writer methods will check if the stream28* has been closed.29*/3031import java.io.*;3233public class WriteAfterClose {3435static boolean failed = false;3637static void testWrite(Writer wtr) throws Exception {3839// These tests could be tighted to only check for a particular40// kind of exception, but in 1.2beta4 StringWriter and41// other writers throw RuntimeExceptions42char[] cbuf = new char[2];43wtr.close();44System.out.println("Class " + wtr.getClass().getName());4546try {47wtr.write('a');48System.out.println("FAILED: Allows char write on a closed stream");49failed = true;50} catch (Exception e) {51}5253try {54wtr.write(cbuf, 0, 1);55System.out.println("FAILED: Allows buffer write on a closed stream");56failed = true;57} catch (Exception e) {58}5960try {61wtr.write(cbuf, 0, 0);62System.out.println("FAILED: Allows empty write on a closed stream");63failed = true;64} catch (Exception e) {65}6667try {68wtr.write("a");69System.out.println("FAILED: Allows string write on a closed stream");70failed = true;71} catch (Exception e) {72}7374try {75wtr.write("a", 0, 1);76System.out.println("FALIED: Allows string buf write on a closed stream");77failed = true;78} catch (Exception e) {79}8081try {82wtr.flush();83System.out.println("FAILED: Allows flushing writer on a closed stream");84failed = true;85} catch (Exception e) {86}8788wtr.close();89}9091public static void main(String argv[]) throws Exception {92StringWriter sw = new StringWriter();93testWrite(new BufferedWriter(sw));9495ByteArrayOutputStream bos = new ByteArrayOutputStream();96OutputStreamWriter osw = new OutputStreamWriter(bos);97testWrite(osw);9899File f = new File(System.getProperty("test.dir", "."),100"NewFile");101f.createNewFile();102f.deleteOnExit();103FileWriter fr = new FileWriter(f);104testWrite(fr);105106if (failed) {107throw new Exception("The test failed because one of the"108+ " writer operation{s} failed. Check the messages");109}110}111}112113114