1 /* 2 * Copyright (c) 2018, 2019, 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.api.consumer; 27 28 import java.util.List; 29 30 import jdk.jfr.Description; 31 import jdk.jfr.Event; 32 import jdk.jfr.Recording; 33 import jdk.jfr.ValueDescriptor; 34 import jdk.jfr.consumer.RecordedClass; 35 import jdk.jfr.consumer.RecordedClassLoader; 36 import jdk.jfr.consumer.RecordedEvent; 37 import jdk.testlibrary.Asserts; 38 import jdk.testlibrary.jfr.Events; 39 40 /* 41 * @test 42 * @summary Verifies the methods of the RecordedEvent 43 * @key jfr 44 * @library /lib/testlibrary 45 * @run main/othervm jdk.jfr.api.consumer.TestRecordedEvent 46 */ 47 public class TestRecordedEvent { 48 49 static class MyClass { 50 } 51 52 static class TestEvent extends Event { 53 54 @Description("MyField") 55 Class<?> clzField = String.class; 56 int intField; 57 String stringField = "myString"; 58 Class<?> myClass = MyClass.class; 59 } 60 61 public static void main(String[] args) throws Throwable { 62 Recording r = new Recording(); 63 r.start(); 64 TestEvent t = new TestEvent(); 65 t.commit(); 66 r.stop(); 67 List<RecordedEvent> events = Events.fromRecording(r); 68 Events.hasEvents(events); 69 70 Asserts.assertEquals(events.size(), 1); 71 72 RecordedEvent event = events.get(0); 73 System.out.println(event); 74 75 List<ValueDescriptor> descriptors = event.getFields(); 76 77 System.out.println("Descriptors"); 78 for (ValueDescriptor descriptor : descriptors) { 79 System.out.println(descriptor.getName()); 80 System.out.println(descriptor.getTypeName()); 81 } 82 System.out.println("Descriptors end"); 83 84 Object recordedClass = event.getValue("clzField"); 85 Asserts.assertTrue(recordedClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass); 86 87 Object recordedInt = event.getValue("intField"); 88 Asserts.assertTrue(recordedInt instanceof Integer); 89 90 Object recordedString = event.getValue("stringField"); 91 System.out.println("recordedString class: " + recordedString.getClass()); 92 Asserts.assertTrue(recordedString instanceof String); 93 94 Object myClass = event.getValue("myClass"); 95 Asserts.assertTrue(myClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass); 96 97 RecordedClass myRecClass = (RecordedClass) myClass; 98 Asserts.assertEquals(MyClass.class.getName(), myRecClass.getName(), "Got " + myRecClass.getName()); 99 100 Object recordedClassLoader = myRecClass.getValue("classLoader"); 101 Asserts.assertTrue(recordedClassLoader instanceof RecordedClassLoader, "Expected Recorded ClassLoader got " + recordedClassLoader); 102 103 RecordedClassLoader myRecClassLoader = (RecordedClassLoader)recordedClassLoader; 104 ClassLoader cl = MyClass.class.getClassLoader(); 105 Asserts.assertEquals(cl.getClass().getName(), myRecClassLoader.getType().getName(), "Expected Recorded ClassLoader type to equal loader type"); 106 107 Asserts.assertNotNull(myRecClass.getModifiers()); 108 } 109 110 }