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