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