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.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.test.lib.Asserts;
  38 import jdk.test.lib.jfr.Events;
  39 
  40 /**
  41  * @test
  42  * @summary Verifies the methods of the RecordedEvent
  43  * @key jfr
  44  *
  45  * @library /lib /
  46  * @run main/othervm jdk.jfr.api.consumer.TestRecordedEvent
  47  */
  48 public class TestRecordedEvent {
  49 
  50     static class MyClass {
  51     }
  52 
  53     static class TestEvent extends Event {
  54 
  55         @Description("MyField")
  56         Class<?> clzField = String.class;
  57         int intField;
  58         String stringField = "myString";
  59         Class<?> myClass = MyClass.class;
  60     }
  61 
  62     public static void main(String[] args) throws Throwable {
  63         Recording r = new Recording();
  64         r.start();
  65         TestEvent t = new TestEvent();
  66         t.commit();
  67         r.stop();
  68         List<RecordedEvent> events = Events.fromRecording(r);
  69         Events.hasEvents(events);
  70 
  71         Asserts.assertEquals(events.size(), 1);
  72 
  73         RecordedEvent event = events.get(0);
  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 }