author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 52334 | a181612f0715 |
permissions | -rw-r--r-- |
50113 | 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 |
||
51214 | 33 |
/** |
50113 | 34 |
* @test TestGetAllEventClasses |
35 |
* @key jfr |
|
51214 | 36 |
* @requires vm.hasJFR |
50113 | 37 |
* @library /test/lib /test/jdk |
38 |
* @modules jdk.jfr/jdk.jfr.internal |
|
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 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
51214
diff
changeset
|
107 |
@SuppressWarnings("rawtypes") |
50113 | 108 |
private static void assertEvents(JVM jvm, boolean inclusion, Class<? extends Event>... targetEvents) { |
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
51214
diff
changeset
|
109 |
final List list = jvm.getAllEventClasses(); |
50113 | 110 |
for (Class<? extends Event> ev : targetEvents) { |
111 |
if (list.contains(ev)) { |
|
112 |
if (inclusion) { |
|
113 |
continue; |
|
114 |
} |
|
115 |
throw new AssertionError(ev.getName() + " in list but should not be!"); |
|
116 |
} |
|
117 |
if (!inclusion) { |
|
118 |
continue; |
|
119 |
} |
|
120 |
throw new AssertionError(ev.getName() + " in not in list but should be!"); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
private static abstract class MyAbstractEvent extends Event {} |
|
125 |
private static class MyUnInitializedEvent extends Event {} |
|
126 |
} |