1 /* 2 * Copyright (c) 2005, 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 5085148 4143651 27 * @summary Test if Reader methods will check if the stream 28 * has been closed. 29 */ 30 31 import java.io.*; 32 33 public enum OpsAfterClose { 34 check(Reader r)35 READ { boolean check(Reader r) { 36 try { 37 r.read(); 38 } catch (IOException io) { 39 return true; 40 } 41 return false; 42 } }, 43 check(Reader r)44 READ_BUF { boolean check(Reader r) { 45 try { 46 char buf[] = new char[2]; 47 int len = 1; 48 r.read(buf, 0, len); 49 } catch (IOException io) { 50 return true; 51 } 52 return false; 53 } }, check(Reader r)54 READY { boolean check(Reader r) { 55 try { 56 r.ready(); 57 } catch (IOException io) { 58 return true; 59 } 60 return false; 61 } }, check(Reader r)62 MARK { boolean check(Reader r) { 63 try { 64 r.mark(1); 65 } catch (IOException io) { 66 return true; 67 } 68 return false; 69 } }, check(Reader r)70 SKIP { boolean check(Reader r) { 71 try { 72 r.skip(1); 73 } catch (IOException io) { 74 return true; 75 } 76 return false; 77 } }, check(Reader r)78 RESET { boolean check(Reader r) { 79 try { 80 r.reset(); 81 } catch (IOException io) { 82 return true; 83 } 84 return false; 85 } }, check(Reader r)86 CLOSE { boolean check(Reader r) { 87 try { 88 r.close(); 89 } catch (IOException io) { 90 return false; 91 } 92 return true; 93 } }; 94 check(Reader r)95 abstract boolean check(Reader r); 96 main(String args[])97 public static void main(String args[]) throws Exception { 98 99 boolean failed = false; 100 101 BufferedReader br = new BufferedReader( 102 new StringReader("abc def ghi")); 103 if (testReader(br)) { 104 failed = true; 105 } 106 107 CharArrayReader car = new CharArrayReader(new char[2]); 108 if (testReader(car)) { 109 failed = true; 110 } 111 112 PushbackReader pbr = new PushbackReader( 113 new CharArrayReader(new char[2])); 114 if (testReader(pbr)) { 115 failed = true; 116 } 117 if (testPushbackReader(pbr)) { 118 failed = true; 119 } 120 121 StringReader sr = new StringReader("abc def ghi"); 122 if (testReader(sr)) { 123 failed = true; 124 } 125 126 InputStreamReader isr = new InputStreamReader( 127 new ByteArrayInputStream("abc".getBytes())); 128 if (testReader(isr)) { 129 failed = true; 130 } 131 132 LineNumberReader lnr = new LineNumberReader( 133 new StringReader("abc def ghi")); 134 if (testReader(lnr)) { 135 failed = true; 136 } 137 138 File f = new File(System.getProperty("test.dir", "."), 139 "NewFile"); 140 f.createNewFile(); 141 f.deleteOnExit(); 142 143 FileReader fr = new FileReader(f); 144 if (testReader(fr)) { 145 failed = true; 146 } 147 148 PipedWriter pw = new PipedWriter(); 149 PipedReader pr = new PipedReader(pw); 150 if (testReader(pr)) { 151 failed = true; 152 } 153 if (failed) { 154 throw new Exception("Test failed for some of the operation{s}" + 155 " on some of the reader{s}, check the messages"); 156 } 157 } 158 testReader(Reader r)159 private static boolean testReader(Reader r) throws Exception { 160 r.close(); 161 boolean failed = false; 162 boolean result; 163 System.out.println("Testing reader:" + r); 164 for (OpsAfterClose op : OpsAfterClose.values()) { 165 result = op.check(r); 166 if (!result) { 167 failed = true; 168 } 169 System.out.println(op + ":" + result); 170 } 171 if (failed) { 172 System.out.println("Test failed for the failed operation{s}" + 173 " above for the Reader:" + r); 174 } 175 return failed; 176 } 177 testPushbackReader(PushbackReader pr)178 private static boolean testPushbackReader(PushbackReader pr) 179 throws Exception { 180 boolean failed = false; 181 try { 182 pr.unread(1); 183 System.out.println("Test failed for unread(int):" + pr); 184 failed = true; 185 } catch (IOException io) { 186 System.out.println("UNREAD(int):true"); 187 } 188 189 char buf[] = new char[2]; 190 try { 191 pr.unread(buf, 0, 2); 192 System.out.println("Test failed for unread(buf, offset, len):" + 193 pr); 194 failed = true; 195 } catch (IOException io) { 196 System.out.println("UNREAD(buf, offset, len):true"); 197 } 198 try { 199 pr.unread(buf); 200 System.out.println("Test failed for unread(char[] buf):" + pr); 201 failed = true; 202 } catch (IOException io) { 203 System.out.println("UNREAD(buf):true"); 204 } 205 return failed; 206 } 207 } 208