1 /*
2  * Copyright (c) 2007, 2013, 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 6359397
27  *  @summary Test if FileOutputStream methods will check if the stream
28  *          has been closed.
29  */
30 
31 import java.io.*;
32 
33 public enum OpsAfterClose {
34 
check(FileOutputStream r)35         WRITE { boolean check(FileOutputStream r) {
36                     try {
37                         r.write(1);
38                     } catch (IOException io) {
39                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
40                         return true;
41                     }
42                     return false;
43              } },
44 
check(FileOutputStream r)45         WRITE_BUF { boolean check(FileOutputStream r) {
46                     try {
47                         byte buf[] = new byte[2];
48                         r.write(buf);
49                     } catch (IOException io) {
50                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
51                         return true;
52                     }
53                     return false;
54             } },
check(FileOutputStream r)55         WRITE_BUF_OFF { boolean check(FileOutputStream r) {
56                     try {
57                         byte buf[] = new byte[2];
58                         int len = 1;
59                         r.write(buf, 0, len);
60                     } catch (IOException io) {
61                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
62                         return true;
63                     }
64                     return false;
65              } },
check(FileOutputStream r)66         GET_CHANNEL { boolean check(FileOutputStream r) {
67                     r.getChannel();
68                     return true;
69              } },
check(FileOutputStream r)70         GET_FD { boolean check(FileOutputStream r) {
71                     try {
72                         r.getFD();
73                         return true;
74                     } catch (IOException io) {
75                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
76                         return false;
77                     }
78              } },
check(FileOutputStream r)79         CLOSE { boolean check(FileOutputStream r) {
80                 try {
81                     r.close();
82                     return true; // No Exceptin thrown on Windows
83                 } catch (IOException io) {
84                     System.out.print("Excep Msg: "+ io.getMessage() + ", ");
85                     return true; // Exception thrown on solaris and linux
86                 }
87              } };
88 
check(FileOutputStream r)89     abstract boolean check(FileOutputStream r);
90 
main(String args[])91     public static void main(String args[]) throws Exception {
92 
93         boolean failed = false;
94 
95         File f = new File(System.getProperty("test.dir", "."),
96                           "f.txt");
97         f.createNewFile();
98         f.deleteOnExit();
99 
100         FileOutputStream fis = new FileOutputStream(f);
101         if (testFileOutputStream(fis)) {
102             throw new Exception("Test failed for some of the operation{s}" +
103                 " on FileOutputStream, check the messages");
104         }
105     }
106 
testFileOutputStream(FileOutputStream r)107     private static boolean testFileOutputStream(FileOutputStream r)
108             throws Exception {
109         r.close();
110         boolean failed = false;
111         boolean result;
112         System.out.println("Testing File:" + r);
113         for (OpsAfterClose op : OpsAfterClose.values()) {
114             result = op.check(r);
115             if (!result) {
116                 failed = true;
117             }
118            System.out.println(op + ":" + result);
119         }
120         if (failed) {
121             System.out.println("Test failed for the failed operation{s}" +
122                         " above for the FileOutputStream:" + r);
123         }
124         return failed;
125     }
126 }
127