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.api.event;
|
|
27 |
|
|
28 |
import jdk.jfr.Event;
|
|
29 |
import jdk.jfr.EventType;
|
|
30 |
import jdk.jfr.Recording;
|
|
31 |
import jdk.jfr.ValueDescriptor;
|
|
32 |
import jdk.jfr.consumer.RecordedEvent;
|
|
33 |
import jdk.test.lib.Asserts;
|
|
34 |
import jdk.test.lib.jfr.Events;
|
|
35 |
|
|
36 |
/*
|
|
37 |
* @test
|
|
38 |
* @summary Test with event class inheritance
|
|
39 |
* @key jfr
|
|
40 |
* @library /test/lib
|
|
41 |
* @run main/othervm jdk.jfr.api.event.TestExtends
|
|
42 |
*/
|
|
43 |
|
|
44 |
public class TestExtends {
|
|
45 |
|
|
46 |
private static final int DEFAULT_FIELD_COUNT = 4;
|
|
47 |
|
|
48 |
@SuppressWarnings("unused")
|
|
49 |
private static class GrandpaEvent extends Event {
|
|
50 |
public int gPublicField = 4;
|
|
51 |
protected int gProtectedField = 3;
|
|
52 |
private int gPrivateField = 2;
|
|
53 |
int gDefaultField = 1;
|
|
54 |
private int hiddenField = 4711;
|
|
55 |
}
|
|
56 |
|
|
57 |
@SuppressWarnings("unused")
|
|
58 |
private static class ParentEvent extends GrandpaEvent {
|
|
59 |
public int pPublicField = 40;
|
|
60 |
protected int pProtectedField = 30;
|
|
61 |
private int pPrivateField = 20;
|
|
62 |
int pDefaultField = 10;
|
|
63 |
private boolean hiddenField = true;
|
|
64 |
}
|
|
65 |
|
|
66 |
@SuppressWarnings("unused")
|
|
67 |
private static class MeEvent extends ParentEvent {
|
|
68 |
public int mPublicField = 400;
|
|
69 |
protected int mProtectedField = 300;
|
|
70 |
private int mPrivateField = 200;
|
|
71 |
int mDefaultField = 100;
|
|
72 |
private String hiddenField = "Hidden";
|
|
73 |
}
|
|
74 |
|
|
75 |
public static void main(String[] args) throws Exception {
|
|
76 |
Recording r = new Recording();
|
|
77 |
r.enable(GrandpaEvent.class).withoutStackTrace();
|
|
78 |
r.enable(ParentEvent.class).withStackTrace();
|
|
79 |
r.enable(MeEvent.class).withoutStackTrace();
|
|
80 |
r.start();
|
|
81 |
|
|
82 |
GrandpaEvent g = new GrandpaEvent();
|
|
83 |
g.commit();
|
|
84 |
|
|
85 |
ParentEvent p = new ParentEvent();
|
|
86 |
p.commit();
|
|
87 |
|
|
88 |
MeEvent m = new MeEvent();
|
|
89 |
m.commit();
|
|
90 |
|
|
91 |
r.stop();
|
|
92 |
for (RecordedEvent re : Events.fromRecording(r)) {
|
|
93 |
System.out.println(re);
|
|
94 |
}
|
|
95 |
// Grandpa
|
|
96 |
EventType grandpaType = EventType.getEventType(GrandpaEvent.class);
|
|
97 |
verifyField(grandpaType, "gPublicField");
|
|
98 |
verifyField(grandpaType, "gProtectedField");
|
|
99 |
verifyField(grandpaType, "gPrivateField");
|
|
100 |
verifyField(grandpaType, "gDefaultField");
|
|
101 |
verifyField(grandpaType, "hiddenField");
|
|
102 |
verifyFieldCount(grandpaType, 5);
|
|
103 |
|
|
104 |
// Parent
|
|
105 |
EventType parentType = EventType.getEventType(ParentEvent.class);
|
|
106 |
verifyField(parentType, "gPublicField");
|
|
107 |
verifyField(parentType, "gProtectedField");
|
|
108 |
verifyField(parentType, "gDefaultField");
|
|
109 |
verifyField(parentType, "pPublicField");
|
|
110 |
verifyField(parentType, "pProtectedField");
|
|
111 |
verifyField(parentType, "pPrivateField");
|
|
112 |
verifyField(parentType, "pDefaultField");
|
|
113 |
verifyField(parentType, "hiddenField");
|
|
114 |
verifyFieldCount(parentType, 8);
|
|
115 |
|
|
116 |
// Me
|
|
117 |
EventType meType = EventType.getEventType(MeEvent.class);
|
|
118 |
verifyField(meType, "gPublicField");
|
|
119 |
verifyField(meType, "gProtectedField");
|
|
120 |
verifyField(meType, "gDefaultField");
|
|
121 |
verifyField(meType, "pPublicField");
|
|
122 |
verifyField(meType, "pProtectedField");
|
|
123 |
verifyField(meType, "pDefaultField");
|
|
124 |
verifyField(meType, "mPublicField");
|
|
125 |
verifyField(meType, "mProtectedField");
|
|
126 |
verifyField(meType, "mPrivateField");
|
|
127 |
verifyField(meType, "mDefaultField");
|
|
128 |
verifyField(meType, "hiddenField");
|
|
129 |
verifyFieldCount(meType, 11);
|
|
130 |
|
|
131 |
for (RecordedEvent re : Events.fromRecording(r)) {
|
|
132 |
System.out.println(re);
|
|
133 |
}
|
|
134 |
|
|
135 |
RecordedEvent grandpa = findEvent(r, GrandpaEvent.class.getName());
|
|
136 |
Asserts.assertEquals(grandpa.getValue("gPublicField"), 4);
|
|
137 |
Asserts.assertEquals(grandpa.getValue("gProtectedField"), 3);
|
|
138 |
Asserts.assertEquals(grandpa.getValue("gPrivateField"), 2);
|
|
139 |
Asserts.assertEquals(grandpa.getValue("gDefaultField"), 1);
|
|
140 |
Asserts.assertEquals(grandpa.getValue("hiddenField"), 4711);
|
|
141 |
|
|
142 |
RecordedEvent parent = findEvent(r, ParentEvent.class.getName());
|
|
143 |
Asserts.assertEquals(parent.getValue("gPublicField"), 4);
|
|
144 |
Asserts.assertEquals(parent.getValue("gProtectedField"), 3);
|
|
145 |
Asserts.assertEquals(parent.getValue("gDefaultField"), 1);
|
|
146 |
Asserts.assertEquals(parent.getValue("pPublicField"), 40);
|
|
147 |
Asserts.assertEquals(parent.getValue("pProtectedField"), 30);
|
|
148 |
Asserts.assertEquals(parent.getValue("pPrivateField"), 20);
|
|
149 |
Asserts.assertEquals(parent.getValue("pDefaultField"), 10);
|
|
150 |
Asserts.assertEquals(parent.getValue("hiddenField"), true);
|
|
151 |
|
|
152 |
RecordedEvent me = findEvent(r, MeEvent.class.getName());
|
|
153 |
Asserts.assertEquals(me.getValue("gPublicField"), 4);
|
|
154 |
Asserts.assertEquals(me.getValue("gProtectedField"), 3);
|
|
155 |
Asserts.assertEquals(me.getValue("gDefaultField"), 1);
|
|
156 |
Asserts.assertEquals(me.getValue("pPublicField"), 40);
|
|
157 |
Asserts.assertEquals(me.getValue("pProtectedField"), 30);
|
|
158 |
Asserts.assertEquals(me.getValue("pDefaultField"), 10);
|
|
159 |
Asserts.assertEquals(me.getValue("mPublicField"), 400);
|
|
160 |
Asserts.assertEquals(me.getValue("mProtectedField"), 300);
|
|
161 |
Asserts.assertEquals(me.getValue("mPrivateField"), 200);
|
|
162 |
Asserts.assertEquals(me.getValue("mDefaultField"), 100);
|
|
163 |
Asserts.assertEquals(me.getValue("hiddenField"), "Hidden");
|
|
164 |
}
|
|
165 |
|
|
166 |
private static RecordedEvent findEvent(Recording r, String name) throws Exception {
|
|
167 |
for (RecordedEvent re : Events.fromRecording(r)) {
|
|
168 |
if (re.getEventType().getName().equals(name)) {
|
|
169 |
return re;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
throw new Exception("Event type hierarchy exist, but missing event " + name + " from recording");
|
|
173 |
}
|
|
174 |
|
|
175 |
private static void verifyFieldCount(EventType t, int count) throws Exception {
|
|
176 |
if (t.getFields().size() != count + DEFAULT_FIELD_COUNT) {
|
|
177 |
throw new Exception("Incorrect number of fields " + count);
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
private static void verifyField(EventType t, String name) throws Exception {
|
|
182 |
ValueDescriptor d = t.getField(name);
|
|
183 |
if (d == null) {
|
|
184 |
throw new Exception("Missing field " + name + " in event " + t.getName());
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|