OpsAfterClose.java revision 0
0N/A/**
2273N/A * @test
0N/A * @bug 6359397
0N/A * @summary Test if FileInputStream methods will check if the stream
0N/A * has been closed.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/A
0N/Apublic enum OpsAfterClose {
0N/A
0N/A READ { boolean check(FileInputStream r) {
0N/A try {
0N/A r.read();
0N/A } catch (IOException io) {
0N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
0N/A return true;
0N/A }
1472N/A return false;
1472N/A } },
1472N/A
0N/A READ_BUF { boolean check(FileInputStream r) {
0N/A try {
0N/A byte buf[] = new byte[2];
1879N/A r.read(buf);
1879N/A } catch (IOException io) {
1879N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
1879N/A return true;
1879N/A }
1879N/A return false;
0N/A } },
0N/A READ_BUF_OFF { boolean check(FileInputStream r) {
0N/A try {
0N/A byte buf[] = new byte[2];
0N/A int len = 1;
0N/A r.read(buf, 0, len);
0N/A } catch (IOException io) {
0N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
0N/A return true;
0N/A }
0N/A return false;
0N/A } },
0N/A GET_CHANNEL { boolean check(FileInputStream r) {
0N/A r.getChannel();
10N/A return true;
10N/A } },
2679N/A GET_FD { boolean check(FileInputStream r) {
10N/A try {
10N/A r.getFD();
10N/A return true;
10N/A } catch (IOException io) {
0N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
0N/A return false;
0N/A }
0N/A } },
0N/A SKIP { boolean check(FileInputStream r) {
0N/A try {
0N/A r.skip(1);
0N/A } catch (IOException io) {
0N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
0N/A return true;
0N/A }
2080N/A return false;
0N/A } },
0N/A CLOSE { boolean check(FileInputStream r) {
0N/A try {
0N/A r.close();
0N/A return true; // No Exception thrown on windows
0N/A } catch (IOException io) {
0N/A System.out.print("Excep Msg: "+ io.getMessage() + ", ");
0N/A return true; // Exception thrown on solaris and linux
0N/A }
0N/A } };
3177N/A
0N/A abstract boolean check(FileInputStream r);
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A boolean failed = false;
0N/A
0N/A File f = new File(System.getProperty("test.dir", "."),
2679N/A "f.txt");
2679N/A f.createNewFile();
2679N/A f.deleteOnExit();
2679N/A
2679N/A FileInputStream fis = new FileInputStream(f);
2679N/A if (testFileInputStream(fis)) {
1601N/A throw new Exception("Test failed for some of the operation{s}" +
1601N/A " on FileInputStream, check the messages");
1601N/A }
0N/A }
0N/A
0N/A private static boolean testFileInputStream(FileInputStream r)
0N/A throws Exception {
0N/A r.close();
10N/A boolean failed = false;
10N/A boolean result;
10N/A System.out.println("Testing File:" + r);
10N/A for (OpsAfterClose op : OpsAfterClose.values()) {
10N/A result = op.check(r);
10N/A if (!result) {
10N/A failed = true;
10N/A }
10N/A System.out.println(op + ":" + result);
0N/A }
1879N/A if (failed) {
1879N/A System.out.println("Test failed for the failed operation{s}" +
" above for the FileInputStream:" + r);
}
return failed;
}
}