Path: blob/master/test/jdk/java/io/FileInputStream/OpsAfterClose.java
41149 views
/*1* Copyright (c) 2007, 2013, 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 635939726* @summary Test if FileInputStream methods will check if the stream27* has been closed.28*/2930import java.io.*;3132public enum OpsAfterClose {3334READ { boolean check(FileInputStream r) {35try {36r.read();37} catch (IOException io) {38System.out.print("Excep Msg: "+ io.getMessage() + ", ");39return true;40}41return false;42} },4344READ_BUF { boolean check(FileInputStream r) {45try {46byte buf[] = new byte[2];47r.read(buf);48} catch (IOException io) {49System.out.print("Excep Msg: "+ io.getMessage() + ", ");50return true;51}52return false;53} },54READ_BUF_OFF { boolean check(FileInputStream r) {55try {56byte buf[] = new byte[2];57int len = 1;58r.read(buf, 0, len);59} catch (IOException io) {60System.out.print("Excep Msg: "+ io.getMessage() + ", ");61return true;62}63return false;64} },65GET_CHANNEL { boolean check(FileInputStream r) {66r.getChannel();67return true;68} },69GET_FD { boolean check(FileInputStream r) {70try {71r.getFD();72return true;73} catch (IOException io) {74System.out.print("Excep Msg: "+ io.getMessage() + ", ");75return false;76}77} },78SKIP { boolean check(FileInputStream r) {79try {80r.skip(1);81} catch (IOException io) {82System.out.print("Excep Msg: "+ io.getMessage() + ", ");83return true;84}85return false;86} },87CLOSE { boolean check(FileInputStream r) {88try {89r.close();90return true; // No Exception thrown on windows91} catch (IOException io) {92System.out.print("Excep Msg: "+ io.getMessage() + ", ");93return true; // Exception thrown on solaris and linux94}95} };9697abstract boolean check(FileInputStream r);9899public static void main(String args[]) throws Exception {100101boolean failed = false;102103File f = new File(System.getProperty("test.dir", "."),104"f.txt");105f.createNewFile();106f.deleteOnExit();107108FileInputStream fis = new FileInputStream(f);109if (testFileInputStream(fis)) {110throw new Exception("Test failed for some of the operation{s}" +111" on FileInputStream, check the messages");112}113}114115private static boolean testFileInputStream(FileInputStream r)116throws Exception {117r.close();118boolean failed = false;119boolean result;120System.out.println("Testing File:" + r);121for (OpsAfterClose op : OpsAfterClose.values()) {122result = op.check(r);123if (!result) {124failed = true;125}126System.out.println(op + ":" + result);127}128if (failed) {129System.out.println("Test failed for the failed operation{s}" +130" above for the FileInputStream:" + r);131}132return failed;133}134}135136137