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