1 /* 2 * Copyright (c) 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.io; 27 28 import static jdk.test.lib.Asserts.assertEquals; 29 30 import java.io.File; 31 import java.io.RandomAccessFile; 32 import java.time.Duration; 33 import java.util.ArrayList; 34 import java.util.List; 35 36 import jdk.jfr.Recording; 37 import jdk.jfr.consumer.RecordedEvent; 38 import jdk.test.lib.jfr.Events; 39 40 /** 41 * @test 42 * @key jfr 43 * @requires vm.hasJFR 44 * @library /test/lib /test/jdk 45 * @run main/othervm jdk.jfr.event.io.TestRandomAccessFileEvents 46 */ 47 public class TestRandomAccessFileEvents { 48 49 public static void main(String[] args) throws Throwable { 50 File tmp = File.createTempFile("TestRandomAccessFileEvents", ".tmp", new File(".")); 51 tmp.deleteOnExit(); 52 Recording recording = new Recording(); 53 List<IOEvent> expectedEvents = new ArrayList<>(); 54 55 recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0)); 56 recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0)); 57 recording.start(); 58 59 RandomAccessFile ras = new RandomAccessFile(tmp, "rw"); 60 int writeInt = 47; 61 byte[] writeBuffer = {10,11,12,13}; 62 63 // Write an int and a buffer. 64 ras.write(writeInt); 65 expectedEvents.add(IOEvent.createFileWriteEvent(1, tmp)); 66 ras.write(writeBuffer); 67 expectedEvents.add(IOEvent.createFileWriteEvent(writeBuffer.length, tmp)); 68 69 ras.seek(0); 70 71 // Read int and buffer 72 int readInt = ras.read(); 73 assertEquals(readInt, writeInt, "wrong int read"); 74 expectedEvents.add(IOEvent.createFileReadEvent(1, tmp)); 75 byte[] readBuffer = new byte [writeBuffer.length]; 76 int size = ras.read(readBuffer); 77 verifyBufferEquals(readBuffer, writeBuffer); 78 expectedEvents.add(IOEvent.createFileReadEvent(readBuffer.length, tmp)); 79 80 // Read beyond EOF 81 readInt = ras.read(); 82 assertEquals(-1, readInt, "wrong read after EOF"); 83 expectedEvents.add(IOEvent.createFileReadEvent(-1, tmp)); 84 85 // Seek to beginning and verify we can read after EOF. 86 ras.seek(0); 87 readInt = ras.read(); 88 assertEquals(readInt, writeInt, "wrong int read after seek(0)"); 89 expectedEvents.add(IOEvent.createFileReadEvent(1, tmp)); 90 91 // seek beyond EOF and verify we get EOF when reading. 92 ras.seek(10); 93 readInt = ras.read(); 94 assertEquals(-1, readInt, "wrong read after seek beyond EOF"); 95 expectedEvents.add(IOEvent.createFileReadEvent(-1, tmp)); 96 97 // Read partial buffer. 98 int partialSize = writeBuffer.length - 2; 99 ras.seek(ras.length()-partialSize); 100 size = ras.read(readBuffer); 101 assertEquals(size, partialSize, "Wrong size partial buffer read"); 102 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 103 104 ras.close(); 105 recording.stop(); 106 List<RecordedEvent> events = Events.fromRecording(recording); 107 IOHelper.verifyEqualsInOrder(events, expectedEvents); 108 } 109 110 private static void verifyBufferEquals(byte[] a, byte[] b) { 111 assertEquals(a.length, b.length, "Wrong buffer size"); 112 for (int i = 0; i < a.length; ++i) { 113 assertEquals(a[i], b[i], "Wrong buffer content at pos " + i); 114 } 115 } 116 }