blob: 8ea086626d0cc7bdfc26899938120be31975fba1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/**
2 * @test
3 * @bug 6359397
4 * @summary Test if FileOutputStream methods will check if the stream
5 * has been closed.
6 */
7
8import java.io.*;
9
10public enum OpsAfterClose {
11
12 WRITE { boolean check(FileOutputStream r) {
13 try {
14 r.write(1);
15 } catch (IOException io) {
16 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
17 return true;
18 }
19 return false;
20 } },
21
22 WRITE_BUF { boolean check(FileOutputStream r) {
23 try {
24 byte buf[] = new byte[2];
25 r.write(buf);
26 } catch (IOException io) {
27 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
28 return true;
29 }
30 return false;
31 } },
32 WRITE_BUF_OFF { boolean check(FileOutputStream r) {
33 try {
34 byte buf[] = new byte[2];
35 int len = 1;
36 r.write(buf, 0, len);
37 } catch (IOException io) {
38 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
39 return true;
40 }
41 return false;
42 } },
43 GET_CHANNEL { boolean check(FileOutputStream r) {
44 r.getChannel();
45 return true;
46 } },
47 GET_FD { boolean check(FileOutputStream r) {
48 try {
49 r.getFD();
50 return true;
51 } catch (IOException io) {
52 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
53 return false;
54 }
55 } },
56 CLOSE { boolean check(FileOutputStream r) {
57 try {
58 r.close();
59 return true; // No Exceptin thrown on Windows
60 } catch (IOException io) {
61 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
62 return true; // Exception thrown on solaris and linux
63 }
64 } };
65
66 abstract boolean check(FileOutputStream r);
67
68 public static void main(String args[]) throws Exception {
69
70 boolean failed = false;
71
72 File f = new File(System.getProperty("test.dir", "."),
73 "f.txt");
74 f.createNewFile();
75 f.deleteOnExit();
76
77 FileOutputStream fis = new FileOutputStream(f);
78 if (testFileOutputStream(fis)) {
79 throw new Exception("Test failed for some of the operation{s}" +
80 " on FileOutputStream, check the messages");
81 }
82 }
83
84 private static boolean testFileOutputStream(FileOutputStream r)
85 throws Exception {
86 r.close();
87 boolean failed = false;
88 boolean result;
89 System.out.println("Testing File:" + r);
90 for (OpsAfterClose op : OpsAfterClose.values()) {
91 result = op.check(r);
92 if (!result) {
93 failed = true;
94 }
95 System.out.println(op + ":" + result);
96 }
97 if (failed) {
98 System.out.println("Test failed for the failed operation{s}" +
99 " above for the FileOutputStream:" + r);
100 }
101 return failed;
102 }
103}