1 /**
   2  *  @test
   3  *  @bug 6359397
   4  *  @summary Test if FileInputStream methods will check if the stream
   5  *          has been closed.
   6  */
   7 
   8 import java.io.*;
   9 
  10 public enum OpsAfterClose {
  11 
  12         READ { boolean check(FileInputStream r) {
  13                     try {
  14                         r.read();
  15                     } catch (IOException io) {
  16                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  17                         return true;
  18                     }
  19                     return false;
  20              } },
  21 
  22         READ_BUF { boolean check(FileInputStream r) {
  23                     try {
  24                         byte buf[] = new byte[2];
  25                         r.read(buf);
  26                     } catch (IOException io) {
  27                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  28                         return true;
  29                     }
  30                     return false;
  31             } },
  32         READ_BUF_OFF { boolean check(FileInputStream r) {
  33                     try {
  34                         byte buf[] = new byte[2];
  35                         int len = 1;
  36                         r.read(buf, 0, len);
  37                     } catch (IOException io) {
  38                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  39                         return true;
  40                     }
  41                     return false;
  42              } },
  43         GET_CHANNEL { boolean check(FileInputStream r) {
  44                     r.getChannel();
  45                     return true;
  46              } },
  47         GET_FD { boolean check(FileInputStream r) {
  48                     try {
  49                         r.getFD();
  50                         return true;
  51                     } catch (IOException io) {
  52                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  53                         return false;
  54                     }
  55              } },
  56         SKIP { boolean check(FileInputStream r) {
  57                     try {
  58                         r.skip(1);
  59                     } catch (IOException io) {
  60                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  61                         return true;
  62                     }
  63                     return false;
  64              } },
  65         CLOSE { boolean check(FileInputStream r) {
  66                 try {
  67                     r.close();
  68                     return true; // No Exception thrown on windows
  69                 } catch (IOException io) {
  70                     System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  71                     return true; // Exception thrown on solaris and linux
  72                 }
  73              } };
  74 
  75     abstract boolean check(FileInputStream r);
  76 
  77     public static void main(String args[]) throws Exception {
  78 
  79         boolean failed = false;
  80 
  81         File f = new File(System.getProperty("test.dir", "."),
  82                           "f.txt");
  83         f.createNewFile();
  84         f.deleteOnExit();
  85 
  86         FileInputStream fis = new FileInputStream(f);
  87         if (testFileInputStream(fis)) {
  88             throw new Exception("Test failed for some of the operation{s}" +
  89                 " on FileInputStream, check the messages");
  90         }
  91     }
  92 
  93     private static boolean testFileInputStream(FileInputStream r)
  94             throws Exception {
  95         r.close();
  96         boolean failed = false;
  97         boolean result;
  98         System.out.println("Testing File:" + r);
  99         for (OpsAfterClose op : OpsAfterClose.values()) {
 100             result = op.check(r);
 101             if (!result) {
 102                 failed = true;
 103             }
 104            System.out.println(op + ":" + result);
 105         }
 106         if (failed) {
 107             System.out.println("Test failed for the failed operation{s}" +
 108                         " above for the FileInputStream:" + r);
 109         }
 110         return failed;
 111     }
 112 }