1 /**
   2  *  @test
   3  *  @bug 6359397
   4  *  @summary Test if RandomAccessFile 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(RandomAccessFile 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(RandomAccessFile r) {
  23                     try {
  24                         byte buf[] = new byte[2];
  25                         int len = 1;
  26                         r.read(buf, 0, len);
  27                     } catch (IOException io) {
  28                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  29                         return true;
  30                     }
  31                     return false;
  32              } },
  33         GET_CHANNEL { boolean check(RandomAccessFile r) {
  34                     r.getChannel();
  35                     return true;
  36              } },
  37         GET_FD { boolean check(RandomAccessFile r) {
  38                     try {
  39                         r.getFD();
  40                         return true;
  41                     } catch (IOException io) {
  42                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  43                         return false;
  44                     }
  45              } },
  46         GET_FILE_PTR { boolean check(RandomAccessFile r) {
  47                     try {
  48                         r.getFilePointer();
  49                     } catch (IOException io) {
  50                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  51                         return true;
  52                     }
  53                     return false;
  54              } },
  55         GET_LENGTH { boolean check(RandomAccessFile r) {
  56                     try {
  57                         r.length();
  58                     } catch (IOException io) {
  59                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  60                         return true;
  61                     }
  62                     return false;
  63              } },
  64         SEEK { boolean check(RandomAccessFile r) {
  65                     try {
  66                         r.seek(1);
  67                     } catch (IOException io) {
  68                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  69                         return true;
  70                     }
  71                     return false;
  72              } },
  73         SET_LENGTH { boolean check(RandomAccessFile r) {
  74                     try {
  75                         r.setLength(1);
  76                     } catch (IOException io) {
  77                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  78                         return true;
  79                     }
  80                     return false;
  81              } },
  82         SKIP_BYTES { boolean check(RandomAccessFile r) {
  83                     try {
  84                         r.skipBytes(1);
  85                     } catch (IOException io) {
  86                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  87                         return true;
  88                     }
  89                     return false;
  90              } },
  91         WRITE { boolean check(RandomAccessFile r) {
  92                     try {
  93                         r.write(1);
  94                     } catch (IOException io) {
  95                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
  96                         return true;
  97                     }
  98                     return false;
  99              } },
 100         WRITE_BUF { boolean check(RandomAccessFile r) {
 101                     try {
 102                         byte buf[] = new byte[2];
 103                         int len = 1;
 104                         r.write(buf, 0, len);
 105                     } catch (IOException io) {
 106                         System.out.print("Excep Msg: "+ io.getMessage() + ", ");
 107                         return true;
 108                     }
 109                     return false;
 110              } },
 111         CLOSE { boolean check(RandomAccessFile r) {
 112                 try {
 113                     r.close();
 114                     return true; // No Exception thrown on windows
 115                 } catch (IOException io) {
 116                     System.out.print("Excep Msg: "+ io.getMessage() + ", ");
 117                     return true; // Exception thrown on solaris and linux
 118                 }
 119              } };
 120 
 121     abstract boolean check(RandomAccessFile r);
 122 
 123     public static void main(String args[]) throws Exception {
 124 
 125         boolean failed = false;
 126 
 127         File f = new File(System.getProperty("test.dir", "."),
 128                           "raf.txt");
 129         f.createNewFile();
 130         f.deleteOnExit();
 131 
 132         RandomAccessFile raf = new RandomAccessFile(f, "rw");
 133         if (testRandomAccessFile(raf)) {
 134             throw new Exception("Test failed for some of the operation{s}" +
 135                 " on RandomAccessFile, check the messages");
 136         }
 137     }
 138 
 139     private static boolean testRandomAccessFile(RandomAccessFile r)
 140             throws Exception {
 141         r.close();
 142         boolean failed = false;
 143         boolean result;
 144         System.out.println("Testing File:" + r);
 145         for (OpsAfterClose op : OpsAfterClose.values()) {
 146             result = op.check(r);
 147             if (!result) {
 148                 failed = true;
 149             }
 150            System.out.println(op + ":" + result);
 151         }
 152         if (failed) {
 153             System.out.println("Test failed for the failed operation{s}" +
 154                         " above for the RandomAccessFile:" + r);
 155         }
 156         return failed;
 157     }
 158 }