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  * @library /test/lib
  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         try (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             Events.hasEvents(events);
  67             RecordedEvent event = events.get(0);
  68             testHasField(event);
  69             testGetField(event, myEvent);
  70         }
  71     }
  72 
  73     private static void testGetField(RecordedEvent event, MyEvent myEvent) {
  74         String stringField = event.getValue("stringField");
  75         Asserts.assertEquals(stringField, myEvent.stringField);
  76 
  77         int intField = event.getValue("intField");
  78         Asserts.assertEquals(intField, myEvent.intField);
  79 
  80         long longField = event.getValue("longField");
  81         Asserts.assertEquals(longField, myEvent.longField);
  82 
  83         short shortField = event.getValue("shortField");
  84         Asserts.assertEquals(shortField, myEvent.shortField);
  85 
  86         double doubleField = event.getValue("doubleField");
  87         Asserts.assertEquals(doubleField, myEvent.doubleField);
  88 
  89         float floatField = event.getValue("floatField");
  90         Asserts.assertEquals(floatField, myEvent.floatField);
  91 
  92         boolean booleanField = event.getValue("booleanField");
  93         Asserts.assertEquals(booleanField, myEvent.booleanField);
  94 
  95         RecordedThread threadField = event.getValue("eventThread");
  96         Asserts.assertEquals(threadField.getJavaName(), myEvent.threadField.getName());
  97         String threadGroupName = event.getValue("eventThread.group.name");
  98         Asserts.assertEquals(threadField.getThreadGroup().getName(), threadGroupName);
  99 
 100         RecordedClass  classField = event.getValue("classField");
 101         Asserts.assertEquals(classField.getName(), myEvent.classField.getName());
 102         String className = event.getValue("classField.name");
 103         Asserts.assertEquals(classField.getName(), className.replace("/", "."));
 104 
 105 
 106         try {
 107             event.getValue("doesnotexist");
 108         } catch (IllegalArgumentException iae) {
 109             // as expected
 110         }
 111 
 112         try {
 113             event.getValue("classField.doesnotexist");
 114         } catch (IllegalArgumentException iae) {
 115             // as expected
 116         }
 117 
 118         try {
 119             event.getValue(null);
 120         } catch (NullPointerException npe) {
 121             // as expected
 122         }
 123     }
 124 
 125     private static void testHasField(RecordedEvent event) {
 126         System.out.println(event);
 127         Asserts.assertTrue(event.hasField("stringField"));
 128         Asserts.assertTrue(event.hasField("intField"));
 129         Asserts.assertTrue(event.hasField("longField"));
 130         Asserts.assertTrue(event.hasField("shortField"));
 131         Asserts.assertTrue(event.hasField("doubleField"));
 132         Asserts.assertTrue(event.hasField("floatField"));
 133         Asserts.assertTrue(event.hasField("threadField"));
 134         Asserts.assertTrue(event.hasField("classField"));
 135         Asserts.assertTrue(event.hasField("classField.name"));
 136         Asserts.assertTrue(event.hasField("eventThread"));
 137         Asserts.assertTrue(event.hasField("eventThread.group.name"));
 138         Asserts.assertTrue(event.hasField("startTime"));
 139         Asserts.assertTrue(event.hasField("stackTrace"));
 140         Asserts.assertTrue(event.hasField("duration"));
 141         Asserts.assertFalse(event.hasField("doesNotExist"));
 142         Asserts.assertFalse(event.hasField("classField.doesNotExist"));
 143         Asserts.assertFalse(event.hasField(""));
 144         try {
 145             event.hasField(null);
 146         } catch (NullPointerException npe) {
 147             // as expected
 148         }
 149     }
 150 }