blob: a9bfcd6c88c6b0983851f3b8fbe377b5bf829585 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4143651 5085148
27 *
28 * @summary Test if Writer methods will check if the stream
29 * has been closed.
30 */
31
32import java.io.*;
33
34public class WriteAfterClose {
35
36 static boolean failed = false;
37
38 static void testWrite(Writer wtr) throws Exception {
39
40 // These tests could be tighted to only check for a particular
41 // kind of exception, but in 1.2beta4 StringWriter and
42 // other writers throw RuntimeExceptions
43 char[] cbuf = new char[2];
44 wtr.close();
45 System.out.println("Class " + wtr.getClass().getName());
46
47 try {
48 wtr.write('a');
49 System.out.println("FAILED: Allows char write on a closed stream");
50 failed = true;
51 } catch (Exception e) {
52 }
53
54 try {
55 wtr.write(cbuf, 0, 1);
56 System.out.println("FAILED: Allows buffer write on a closed stream");
57 failed = true;
58 } catch (Exception e) {
59 }
60
61 try {
62 wtr.write(cbuf, 0, 0);
63 System.out.println("FAILED: Allows empty write on a closed stream");
64 failed = true;
65 } catch (Exception e) {
66 }
67
68 try {
69 wtr.write("a");
70 System.out.println("FAILED: Allows string write on a closed stream");
71 failed = true;
72 } catch (Exception e) {
73 }
74
75 try {
76 wtr.write("a", 0, 1);
77 System.out.println("FALIED: Allows string buf write on a closed stream");
78 failed = true;
79 } catch (Exception e) {
80 }
81
82 try {
83 wtr.flush();
84 System.out.println("FAILED: Allows flushing writer on a closed stream");
85 failed = true;
86 } catch (Exception e) {
87 }
88
89 wtr.close();
90 }
91
92 public static void main(String argv[]) throws Exception {
93 StringWriter sw = new StringWriter();
94 testWrite(new BufferedWriter(sw));
95
96 ByteArrayOutputStream bos = new ByteArrayOutputStream();
97 OutputStreamWriter osw = new OutputStreamWriter(bos);
98 testWrite(osw);
99
100 File f = new File(System.getProperty("test.dir", "."),
101 "NewFile");
102 f.createNewFile();
103 f.deleteOnExit();
104 FileWriter fr = new FileWriter(f);
105 testWrite(fr);
106
107 if (failed) {
108 throw new Exception("The test failed because one of the"
109 + " writer operation{s} failed. Check the messages");
110 }
111 }
112}