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  * @library /test/lib
  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 
  74         List<ValueDescriptor> descriptors = event.getFields();
  75 
  76         System.out.println("Descriptors");
  77         for (ValueDescriptor descriptor : descriptors) {
  78             System.out.println(descriptor.getName());
  79             System.out.println(descriptor.getTypeName());
  80         }
  81         System.out.println("Descriptors end");
  82 
  83         Object recordedClass = event.getValue("clzField");
  84         Asserts.assertTrue(recordedClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass);
  85 
  86         Object recordedInt = event.getValue("intField");
  87         Asserts.assertTrue(recordedInt instanceof Integer);
  88 
  89         Object recordedString = event.getValue("stringField");
  90         System.out.println("recordedString class: " + recordedString.getClass());
  91         Asserts.assertTrue(recordedString instanceof String);
  92 
  93         Object myClass = event.getValue("myClass");
  94         Asserts.assertTrue(myClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass);
  95 
  96         RecordedClass myRecClass = (RecordedClass) myClass;
  97         Asserts.assertEquals(MyClass.class.getName(), myRecClass.getName(), "Got " + myRecClass.getName());
  98 
  99         Object recordedClassLoader = myRecClass.getValue("classLoader");
 100         Asserts.assertTrue(recordedClassLoader instanceof RecordedClassLoader, "Expected Recorded ClassLoader got " + recordedClassLoader);
 101 
 102         RecordedClassLoader myRecClassLoader = (RecordedClassLoader)recordedClassLoader;
 103         ClassLoader cl = MyClass.class.getClassLoader();
 104         Asserts.assertEquals(cl.getClass().getName(), myRecClassLoader.getType().getName(), "Expected Recorded ClassLoader type to equal loader type");
 105 
 106         Asserts.assertNotNull(myRecClass.getModifiers());
 107     }
 108 
 109 }