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.Event; 31 import jdk.jfr.Recording; 32 import jdk.jfr.consumer.RecordedClass; 33 import jdk.jfr.consumer.RecordedEvent; 34 import jdk.jfr.consumer.RecordedThread; 35 import jdk.testlibrary.Asserts; 36 import jdk.testlibrary.jfr.Events; 37 38 /* 39 * @test 40 * @key jfr 41 * @library /lib/testlibrary 42 * @run main/othervm jdk.jfr.api.consumer.TestFieldAccess 43 */ 44 public class TestFieldAccess { 45 46 private static class MyEvent extends Event { 47 String stringField = "Hello"; 48 int intField = 4711; 49 long longField = 4712L; 50 short shortField = (short)67; 51 double doubleField = Double.NaN; 52 float floatField = Float.MIN_VALUE; 53 boolean booleanField = false; 54 Thread threadField = Thread.currentThread(); 55 Class<?> classField = MyEvent.class; 56 } 57 58 public static void main(String[] args) throws Throwable { 59 Recording r = new Recording(); 60 r.enable(MyEvent.class); 61 r.start(); 62 MyEvent myEvent = new MyEvent(); 63 myEvent.commit(); 64 r.stop(); 65 List<RecordedEvent> events = Events.fromRecording(r); 66 if (events.isEmpty()) { 67 throw new AssertionError("Expected at least one event"); 68 } 69 Events.hasEvents(events); 70 RecordedEvent event = events.get(0); 71 testHasField(event); 72 testGetField(event, myEvent); 73 } 74 75 private static void testGetField(RecordedEvent event, MyEvent myEvent) { 76 String stringField = event.getValue("stringField"); 77 Asserts.assertEquals(stringField, myEvent.stringField); 78 79 int intField = event.getValue("intField"); 80 Asserts.assertEquals(intField, myEvent.intField); 81 82 long longField = event.getValue("longField"); 83 Asserts.assertEquals(longField, myEvent.longField); 84 85 short shortField = event.getValue("shortField"); 86 Asserts.assertEquals(shortField, myEvent.shortField); 87 88 double doubleField = event.getValue("doubleField"); 89 Asserts.assertEquals(doubleField, myEvent.doubleField); 90 91 float floatField = event.getValue("floatField"); 92 Asserts.assertEquals(floatField, myEvent.floatField); 93 94 boolean booleanField = event.getValue("booleanField"); 95 Asserts.assertEquals(booleanField, myEvent.booleanField); 96 97 RecordedThread threadField = event.getValue("eventThread"); 98 Asserts.assertEquals(threadField.getJavaName(), myEvent.threadField.getName()); 99 String threadGroupName = event.getValue("eventThread.group.name"); 100 Asserts.assertEquals(threadField.getThreadGroup().getName(), threadGroupName); 101 102 RecordedClass classField = event.getValue("classField"); 103 Asserts.assertEquals(classField.getName(), myEvent.classField.getName()); 104 String className = event.getValue("classField.name"); 105 Asserts.assertEquals(classField.getName(), className.replace("/", ".")); 106 107 108 try { 109 event.getValue("doesnotexist"); 110 } catch (IllegalArgumentException iae) { 111 // as expected 112 } 113 114 try { 115 event.getValue("classField.doesnotexist"); 116 } catch (IllegalArgumentException iae) { 117 // as expected 118 } 119 120 try { 121 event.getValue(null); 122 } catch (NullPointerException npe) { 123 // as expected 124 } 125 } 126 127 private static void testHasField(RecordedEvent event) { 128 System.out.println(event); 129 Asserts.assertTrue(event.hasField("stringField")); 130 Asserts.assertTrue(event.hasField("intField")); 131 Asserts.assertTrue(event.hasField("longField")); 132 Asserts.assertTrue(event.hasField("shortField")); 133 Asserts.assertTrue(event.hasField("doubleField")); 134 Asserts.assertTrue(event.hasField("floatField")); 135 Asserts.assertTrue(event.hasField("threadField")); 136 Asserts.assertTrue(event.hasField("classField")); 137 Asserts.assertTrue(event.hasField("classField.name")); 138 Asserts.assertTrue(event.hasField("eventThread")); 139 Asserts.assertTrue(event.hasField("eventThread.group.name")); 140 Asserts.assertTrue(event.hasField("startTime")); 141 Asserts.assertTrue(event.hasField("stackTrace")); 142 Asserts.assertTrue(event.hasField("duration")); 143 Asserts.assertFalse(event.hasField("doesNotExist")); 144 Asserts.assertFalse(event.hasField("classField.doesNotExist")); 145 Asserts.assertFalse(event.hasField("")); 146 try { 147 event.hasField(null); 148 } catch (NullPointerException npe) { 149 // as expected 150 } 151 } 152 }