1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.io.File;
  24 import java.io.FileInputStream;
  25 import java.io.FileNotFoundException;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;
  28 import java.io.RandomAccessFile;
  29 
  30 /*
  31  * @test
  32  * @run shell ioTraceTest.sh IoTraceFileReadWrite
  33  */
  34 public class IoTraceFileReadWrite extends IoTraceBase {
  35 
  36     private void testWrite(File f) throws IOException, FileNotFoundException,
  37             Exception {
  38         try (FileOutputStream fos = new FileOutputStream(f)) {
  39             fos.write(11);
  40         }
  41         expectFile(1, 0, f);
  42     }
  43 
  44 
  45     private void testRead(File f) throws IOException, FileNotFoundException,
  46             Exception {
  47         try (FileInputStream fos = new FileInputStream(f)) {
  48             fos.read();
  49         }
  50         expectFile(0, 1, f);
  51     }
  52 
  53     private void testRandomAccessWrite(File f) throws Exception {
  54         try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
  55             raf.write(11);
  56         }
  57         expectFile(1, 0, f);
  58     }
  59 
  60     private void testRandomAccessRead(File f) throws Exception {
  61         try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
  62             raf.read();
  63         }
  64         expectFile(0, 1, f);
  65     }
  66 
  67     public void test() throws Exception {
  68         IoTraceAgent.setListener(this);
  69         File f = File.createTempFile("IoTraceFileReadWrite", ".bin");
  70         try {
  71             clear();
  72             testWrite(f);
  73             clear();
  74             testRead(f);
  75             clear();
  76             testRandomAccessWrite(f);
  77             clear();
  78             testRandomAccessRead(f);
  79         } finally {
  80             f.delete();
  81         }
  82     }
  83 
  84     public static void main(String... args) throws Exception {
  85         IoTraceFileReadWrite t = new IoTraceFileReadWrite();
  86         t.test();
  87     }
  88 }