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.jvm;
  27 
  28 import jdk.jfr.Event;
  29 import jdk.jfr.internal.JVM;
  30 
  31 import java.util.List;
  32 
  33 /**
  34  * @test TestGetAllEventClasses
  35  * @key jfr
  36  * 
  37  * @library /lib /
  38  * 
  39  *
  40  * @build jdk.jfr.jvm.HelloWorldEvent1
  41  * @build jdk.jfr.jvm.HelloWorldEvent2
  42  * @run main/othervm jdk.jfr.jvm.TestGetAllEventClasses
  43  */
  44 public class TestGetAllEventClasses {
  45 
  46     public static void main(String... args) throws ClassNotFoundException {
  47         JVM jvm = JVM.getJVM();
  48         // before creating  native
  49         assertEmptyEventList(jvm);
  50         jvm.createNativeJFR();
  51         // after creating native
  52         assertEmptyEventList(jvm);
  53 
  54         // Test event class load is triggered and only once
  55         Class<? extends Event> clazz = initialize("jdk.jfr.jvm.HelloWorldEvent1");
  56         // check that the event class is registered
  57         assertEventsIncluded(jvm, clazz);
  58         // second active use of the same event class should not add another class
  59         // to the list - it would already be loaded
  60         clazz = initialize(clazz);
  61         assertEventsIncluded(jvm, clazz);
  62 
  63         // second event class
  64         Class<? extends Event> clazz2 = initialize("jdk.jfr.jvm.HelloWorldEvent2");
  65         // the list of event classes should now have two classes registered
  66         assertEventsIncluded(jvm, clazz, clazz2);
  67 
  68         // verify that an abstract event class is not included
  69         Class<? extends Event> abstractClass = initialize(MyAbstractEvent.class); // to run <clinit>
  70         assertEventsExcluded(jvm, abstractClass);
  71 
  72         // verify that a class that is yet to run its <clinit> is not included in the list of event classes
  73         assertEventsExcluded(jvm, MyUnInitializedEvent.class);
  74 
  75         // ensure old classes are not lost
  76         assertEventsIncluded(jvm, clazz, clazz2);
  77 
  78         jvm.destroyNativeJFR();
  79     }
  80 
  81     private static Class<? extends Event> initialize(String name) throws ClassNotFoundException {
  82         // Class.forName() will force the class to run its <clinit> method
  83         return Class.forName(name).asSubclass(Event.class);
  84     }
  85 
  86     private static Class<? extends Event> initialize(Class<? extends Event> event) throws ClassNotFoundException {
  87         return initialize(event.getName());
  88     }
  89 
  90     private static void assertEmptyEventList(JVM jvm) {
  91         if (!jvm.getAllEventClasses().isEmpty()) {
  92             throw new AssertionError("should not have any event classes registered!");
  93         }
  94     }
  95 
  96     @SafeVarargs
  97     private static void assertEventsExcluded(JVM jvm, Class<? extends Event>... targetEvents) {
  98         assertEvents(jvm, false, targetEvents);
  99     }
 100 
 101     @SafeVarargs
 102     private static void assertEventsIncluded(JVM jvm, Class<? extends Event>... targetEvents) {
 103         assertEvents(jvm, true, targetEvents);
 104     }
 105 
 106     @SafeVarargs
 107     private static void assertEvents(JVM jvm, boolean inclusion, Class<? extends Event>... targetEvents) {
 108         final List<Class<? extends Event>> list = jvm.getAllEventClasses();
 109         for (Class<? extends Event> ev : targetEvents) {
 110            if (list.contains(ev)) {
 111                if (inclusion) {
 112                    continue;
 113                }
 114                throw new AssertionError(ev.getName() + " in list but should not be!");
 115            }
 116            if (!inclusion) {
 117                continue;
 118            }
 119            throw new AssertionError(ev.getName() + " in not in list but should be!");
 120        }
 121     }
 122 
 123     private static abstract class MyAbstractEvent extends Event {}
 124     private static class MyUnInitializedEvent extends Event {}
 125 }