blob: 943d584ff94431232c15f9661cd88ad9bc59c98d [file] [log] [blame]
serb9adafbe2013-11-12 20:24:25 +04001/*
2 * Copyright (c) 2007, 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
duke6e45e102007-12-01 00:00:00 +000024/**
25 * @test
26 * @bug 6359397
27 * @summary Test if FileInputStream methods will check if the stream
28 * has been closed.
29 */
30
31import java.io.*;
32
33public enum OpsAfterClose {
34
35 READ { boolean check(FileInputStream r) {
36 try {
37 r.read();
38 } catch (IOException io) {
39 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
40 return true;
41 }
42 return false;
43 } },
44
45 READ_BUF { boolean check(FileInputStream r) {
46 try {
47 byte buf[] = new byte[2];
48 r.read(buf);
49 } catch (IOException io) {
50 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
51 return true;
52 }
53 return false;
54 } },
55 READ_BUF_OFF { boolean check(FileInputStream r) {
56 try {
57 byte buf[] = new byte[2];
58 int len = 1;
59 r.read(buf, 0, len);
60 } catch (IOException io) {
61 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
62 return true;
63 }
64 return false;
65 } },
66 GET_CHANNEL { boolean check(FileInputStream r) {
67 r.getChannel();
68 return true;
69 } },
70 GET_FD { boolean check(FileInputStream 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 } },
79 SKIP { boolean check(FileInputStream r) {
80 try {
81 r.skip(1);
82 } catch (IOException io) {
83 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
84 return true;
85 }
86 return false;
87 } },
88 CLOSE { boolean check(FileInputStream r) {
89 try {
90 r.close();
91 return true; // No Exception thrown on windows
92 } catch (IOException io) {
93 System.out.print("Excep Msg: "+ io.getMessage() + ", ");
94 return true; // Exception thrown on solaris and linux
95 }
96 } };
97
98 abstract boolean check(FileInputStream r);
99
100 public static void main(String args[]) throws Exception {
101
102 boolean failed = false;
103
104 File f = new File(System.getProperty("test.dir", "."),
105 "f.txt");
106 f.createNewFile();
107 f.deleteOnExit();
108
109 FileInputStream fis = new FileInputStream(f);
110 if (testFileInputStream(fis)) {
111 throw new Exception("Test failed for some of the operation{s}" +
112 " on FileInputStream, check the messages");
113 }
114 }
115
116 private static boolean testFileInputStream(FileInputStream r)
117 throws Exception {
118 r.close();
119 boolean failed = false;
120 boolean result;
121 System.out.println("Testing File:" + r);
122 for (OpsAfterClose op : OpsAfterClose.values()) {
123 result = op.check(r);
124 if (!result) {
125 failed = true;
126 }
127 System.out.println(op + ":" + result);
128 }
129 if (failed) {
130 System.out.println("Test failed for the failed operation{s}" +
131 " above for the FileInputStream:" + r);
132 }
133 return failed;
134 }
135}