J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 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 4181483 |
| 27 | * @summary Test if InputStream methods will check if the stream |
| 28 | * has been closed. |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | |
| 33 | public enum OpsAfterClose { |
| 34 | |
| 35 | READ { boolean check(InputStream is) { |
| 36 | try { |
| 37 | int read = is.read(); |
| 38 | System.out.println("read returns: " + read); |
| 39 | } catch (IOException io) { |
| 40 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 41 | return true; |
| 42 | } |
| 43 | return false; |
| 44 | } }, |
| 45 | |
| 46 | READ_BUF { boolean check(InputStream is) { |
| 47 | try { |
| 48 | byte buf[] = new byte[2]; |
| 49 | int read = is.read(buf); |
| 50 | System.out.println("read(buf) returns: " + read); |
| 51 | } catch (IOException io) { |
| 52 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } }, |
| 57 | READ_BUF_OFF { boolean check(InputStream is) { |
| 58 | try { |
| 59 | byte buf[] = new byte[2]; |
| 60 | int len = 1; |
| 61 | int read = is.read(buf, 0, len); |
| 62 | System.out.println("read(buf, 0, len) returns: " + read); |
| 63 | } catch (IOException io) { |
| 64 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } }, |
| 69 | AVAILABLE { boolean check(InputStream is) { |
| 70 | try { |
| 71 | int avail = is.available(); |
| 72 | System.out.println("available() returns: " + avail); |
| 73 | return false; |
| 74 | } catch (IOException io) { |
| 75 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 76 | return true; |
| 77 | } |
| 78 | } }, |
| 79 | SKIP { boolean check(InputStream is) { |
| 80 | try { |
| 81 | long skipped = is.skip(1); |
| 82 | System.out.println("skip() returns: " + skipped); |
| 83 | } catch (IOException io) { |
| 84 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | } }, |
| 89 | MARK { boolean check(InputStream is) { |
| 90 | is.mark(20); |
| 91 | return true; |
| 92 | } }, |
| 93 | RESET { boolean check(InputStream is) { |
| 94 | try { |
| 95 | is.reset(); |
| 96 | } catch (IOException io) { |
| 97 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } }, |
| 102 | MARK_SUPPORTED { boolean check(InputStream is) { |
| 103 | is.markSupported(); |
| 104 | return true; |
| 105 | } }, |
| 106 | CLOSE { boolean check(InputStream is) { |
| 107 | try { |
| 108 | is.close(); |
| 109 | return true; // No Exception thrown on windows for FileInputStream |
| 110 | } catch (IOException io) { |
| 111 | System.out.print("Excep Msg: "+ io.getMessage() + ", "); |
| 112 | return true; // Exception thrown on solaris and linux for FileInputStream |
| 113 | } |
| 114 | } }; |
| 115 | |
| 116 | abstract boolean check(InputStream is); |
| 117 | |
| 118 | public static void main(String args[]) throws Exception { |
| 119 | |
| 120 | boolean failed = false; |
| 121 | |
| 122 | File f = new File(System.getProperty("test.dir", "."), |
| 123 | "f.txt"); |
| 124 | f.createNewFile(); |
| 125 | f.deleteOnExit(); |
| 126 | |
| 127 | FileInputStream fis = new FileInputStream(f); |
| 128 | if (testInputStream(fis)) { |
| 129 | failed = true; |
| 130 | } |
| 131 | if (testFileInputStream(fis)) { |
| 132 | failed = true; |
| 133 | } |
| 134 | |
| 135 | BufferedInputStream bs = new BufferedInputStream( |
| 136 | new FileInputStream(f)); |
| 137 | if (testInputStream(bs)) { |
| 138 | failed = true; |
| 139 | } |
| 140 | |
| 141 | DataInputStream dis = new DataInputStream( |
| 142 | new FileInputStream(f)); |
| 143 | if (testInputStream(dis)) { |
| 144 | failed = true; |
| 145 | } |
| 146 | |
| 147 | PushbackInputStream pbis = new PushbackInputStream( |
| 148 | new ByteArrayInputStream(new byte[20])); |
| 149 | if (testInputStream(pbis)) { |
| 150 | failed = true; |
| 151 | } |
| 152 | |
| 153 | if (testPushbackInputStream(pbis)) { |
| 154 | failed = true; |
| 155 | } |
| 156 | |
| 157 | PipedInputStream pis = new PipedInputStream(new PipedOutputStream()); |
| 158 | if (testInputStream(pis)) { |
| 159 | failed = true; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * The SequenceInputStream and ObjectInputStream does not throw IOException |
| 164 | |
| 165 | SequenceInputStream sqis = new SequenceInputStream( |
| 166 | new FileInputStream(f), |
| 167 | new PipedInputStream(new PipedOutputStream()) |
| 168 | ); |
| 169 | if (testInputStream(sqis)) { |
| 170 | failed = true; |
| 171 | } |
| 172 | |
| 173 | String serStr = "abc"; |
| 174 | ObjectOutputStream oos = new ObjectOutputStream( |
| 175 | new FileOutputStream(f)); |
| 176 | oos.writeObject(serStr); |
| 177 | oos.close(); |
| 178 | |
| 179 | ObjectInputStream ois = new ObjectInputStream( |
| 180 | new FileInputStream(f)); |
| 181 | if (testInputStream(ois)) { |
| 182 | failed = true; |
| 183 | } |
| 184 | |
| 185 | */ |
| 186 | |
| 187 | if (failed) { |
| 188 | throw new Exception( |
| 189 | "Some Op for some Stream failed, check the failed status above"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private static boolean testInputStream(InputStream is) |
| 194 | throws Exception { |
| 195 | is.close(); |
| 196 | boolean failed = false; |
| 197 | boolean result; |
| 198 | System.out.println("Testing :" + is); |
| 199 | for (OpsAfterClose op : OpsAfterClose.values()) { |
| 200 | |
| 201 | if (op.equals(AVAILABLE) && (is instanceof PipedInputStream)) { |
| 202 | // skip the test as available() returns 0 |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | result = op.check(is); |
| 207 | if (!result) { |
| 208 | failed = true; |
| 209 | } |
| 210 | System.out.println(op + ":" + result); |
| 211 | } |
| 212 | if (failed) { |
| 213 | System.out.println("Test failed for the failed operation{s}" + |
| 214 | " above for :" + is); |
| 215 | } |
| 216 | return failed; |
| 217 | } |
| 218 | |
| 219 | private static boolean testPushbackInputStream(PushbackInputStream pis) |
| 220 | throws Exception { |
| 221 | boolean failed = false; |
| 222 | try { |
| 223 | pis.unread(1); |
| 224 | System.out.println("Test failed for unread(int):" + pis); |
| 225 | failed = true; |
| 226 | } catch (IOException io) { |
| 227 | System.out.println("UNREAD(int):true"); |
| 228 | } |
| 229 | |
| 230 | byte buf[] = new byte[2]; |
| 231 | try { |
| 232 | pis.unread(buf, 0, 2); |
| 233 | System.out.println("Test failed for unread(buf, offset, len):" + |
| 234 | pis); |
| 235 | failed = true; |
| 236 | } catch (IOException io) { |
| 237 | System.out.println("UNREAD(buf, offset, len):true"); |
| 238 | } |
| 239 | try { |
| 240 | pis.unread(buf); |
| 241 | System.out.println("Test failed for unread(char[] buf):" + pis); |
| 242 | failed = true; |
| 243 | } catch (IOException io) { |
| 244 | System.out.println("UNREAD(buf):true"); |
| 245 | } |
| 246 | return failed; |
| 247 | } |
| 248 | |
| 249 | private static boolean testFileInputStream(FileInputStream fis) |
| 250 | throws Exception { |
| 251 | boolean failed = false; |
| 252 | try { |
| 253 | fis.getFD(); |
| 254 | System.out.println("GetFD: true"); |
| 255 | } catch (IOException io) { |
| 256 | System.out.println("GetFD: false"); |
| 257 | failed = true; |
| 258 | } |
| 259 | fis.getChannel(); |
| 260 | System.out.println("GetChannel: true"); |
| 261 | return failed; |
| 262 | } |
| 263 | } |