Path: blob/master/test/jdk/java/io/Reader/OpsAfterClose.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 5085148 414365126* @summary Test if Reader methods will check if the stream27* has been closed.28*/2930import java.io.*;3132public enum OpsAfterClose {3334READ { boolean check(Reader r) {35try {36r.read();37} catch (IOException io) {38return true;39}40return false;41} },4243READ_BUF { boolean check(Reader r) {44try {45char buf[] = new char[2];46int len = 1;47r.read(buf, 0, len);48} catch (IOException io) {49return true;50}51return false;52} },53READY { boolean check(Reader r) {54try {55r.ready();56} catch (IOException io) {57return true;58}59return false;60} },61MARK { boolean check(Reader r) {62try {63r.mark(1);64} catch (IOException io) {65return true;66}67return false;68} },69SKIP { boolean check(Reader r) {70try {71r.skip(1);72} catch (IOException io) {73return true;74}75return false;76} },77RESET { boolean check(Reader r) {78try {79r.reset();80} catch (IOException io) {81return true;82}83return false;84} },85CLOSE { boolean check(Reader r) {86try {87r.close();88} catch (IOException io) {89return false;90}91return true;92} };9394abstract boolean check(Reader r);9596public static void main(String args[]) throws Exception {9798boolean failed = false;99100BufferedReader br = new BufferedReader(101new StringReader("abc def ghi"));102if (testReader(br)) {103failed = true;104}105106CharArrayReader car = new CharArrayReader(new char[2]);107if (testReader(car)) {108failed = true;109}110111PushbackReader pbr = new PushbackReader(112new CharArrayReader(new char[2]));113if (testReader(pbr)) {114failed = true;115}116if (testPushbackReader(pbr)) {117failed = true;118}119120StringReader sr = new StringReader("abc def ghi");121if (testReader(sr)) {122failed = true;123}124125InputStreamReader isr = new InputStreamReader(126new ByteArrayInputStream("abc".getBytes()));127if (testReader(isr)) {128failed = true;129}130131LineNumberReader lnr = new LineNumberReader(132new StringReader("abc def ghi"));133if (testReader(lnr)) {134failed = true;135}136137File f = new File(System.getProperty("test.dir", "."),138"NewFile");139f.createNewFile();140f.deleteOnExit();141142FileReader fr = new FileReader(f);143if (testReader(fr)) {144failed = true;145}146147PipedWriter pw = new PipedWriter();148PipedReader pr = new PipedReader(pw);149if (testReader(pr)) {150failed = true;151}152if (failed) {153throw new Exception("Test failed for some of the operation{s}" +154" on some of the reader{s}, check the messages");155}156}157158private static boolean testReader(Reader r) throws Exception {159r.close();160boolean failed = false;161boolean result;162System.out.println("Testing reader:" + r);163for (OpsAfterClose op : OpsAfterClose.values()) {164result = op.check(r);165if (!result) {166failed = true;167}168System.out.println(op + ":" + result);169}170if (failed) {171System.out.println("Test failed for the failed operation{s}" +172" above for the Reader:" + r);173}174return failed;175}176177private static boolean testPushbackReader(PushbackReader pr)178throws Exception {179boolean failed = false;180try {181pr.unread(1);182System.out.println("Test failed for unread(int):" + pr);183failed = true;184} catch (IOException io) {185System.out.println("UNREAD(int):true");186}187188char buf[] = new char[2];189try {190pr.unread(buf, 0, 2);191System.out.println("Test failed for unread(buf, offset, len):" +192pr);193failed = true;194} catch (IOException io) {195System.out.println("UNREAD(buf, offset, len):true");196}197try {198pr.unread(buf);199System.out.println("Test failed for unread(char[] buf):" + pr);200failed = true;201} catch (IOException io) {202System.out.println("UNREAD(buf):true");203}204return failed;205}206}207208209