1 /* 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 24 /** 25 * @test 26 * @bug 6359397 27 * @summary Test if RandomAccessFile 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(RandomAccessFile 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(RandomAccessFile r) { 46 try { 47 byte buf[] = new byte[2]; 48 int len = 1; 49 r.read(buf, 0, len); 50 } catch (IOException io) { 51 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 52 return true; 53 } 54 return false; 55 } }, 56 GET_CHANNEL { boolean check(RandomAccessFile r) { 57 r.getChannel(); 58 return true; 59 } }, 60 GET_FD { boolean check(RandomAccessFile r) { 61 try { 62 r.getFD(); 63 return true; 64 } catch (IOException io) { 65 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 66 return false; 67 } 68 } }, 69 GET_FILE_PTR { boolean check(RandomAccessFile r) { 70 try { 71 r.getFilePointer(); 72 } catch (IOException io) { 73 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 74 return true; 75 } 76 return false; 77 } }, 78 GET_LENGTH { boolean check(RandomAccessFile r) { 79 try { 80 r.length(); 81 } catch (IOException io) { 82 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 83 return true; 84 } 85 return false; 86 } }, 87 SEEK { boolean check(RandomAccessFile r) { 88 try { 89 r.seek(1); 90 } catch (IOException io) { 91 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 92 return true; 93 } 94 return false; 95 } }, 96 SET_LENGTH { boolean check(RandomAccessFile r) { 97 try { 98 r.setLength(1); 99 } catch (IOException io) { 100 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 101 return true; 102 } 103 return false; 104 } }, 105 SKIP_BYTES { boolean check(RandomAccessFile r) { 106 try { 107 r.skipBytes(1); 108 } catch (IOException io) { 109 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 110 return true; 111 } 112 return false; 113 } }, 114 WRITE { boolean check(RandomAccessFile r) { 115 try { 116 r.write(1); 117 } catch (IOException io) { 118 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 119 return true; 120 } 121 return false; 122 } }, 123 WRITE_BUF { boolean check(RandomAccessFile r) { 124 try { 125 byte buf[] = new byte[2]; 126 int len = 1; 127 r.write(buf, 0, len); 128 } catch (IOException io) { 129 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 130 return true; 131 } 132 return false; 133 } }, 134 CLOSE { boolean check(RandomAccessFile r) { 135 try { 136 r.close(); 137 return true; // No Exception thrown on windows 138 } catch (IOException io) { 139 System.out.print("Excep Msg: "+ io.getMessage() + ", "); 140 return true; // Exception thrown on solaris and linux 141 } 142 } }; 143 144 abstract boolean check(RandomAccessFile r); 145 146 public static void main(String args[]) throws Exception { 147 148 boolean failed = false; 149 150 File f = new File(System.getProperty("test.dir", "."), 151 "raf.txt"); 152 f.createNewFile(); 153 f.deleteOnExit(); 154 155 RandomAccessFile raf = new RandomAccessFile(f, "rw"); 156 if (testRandomAccessFile(raf)) { 157 throw new Exception("Test failed for some of the operation{s}" + 158 " on RandomAccessFile, check the messages"); 159 } 160 } 161 162 private static boolean testRandomAccessFile(RandomAccessFile r) 163 throws Exception { 164 r.close(); 165 boolean failed = false; 166 boolean result; 167 System.out.println("Testing File:" + r); 168 for (OpsAfterClose op : OpsAfterClose.values()) { 169 result = op.check(r); 170 if (!result) { 171 failed = true; 172 } 173 System.out.println(op + ":" + result); 174 } 175 if (failed) { 176 System.out.println("Test failed for the failed operation{s}" + 177 " above for the RandomAccessFile:" + r); 178 } 179 return failed; 180 } 181 }