author | egahlin |
Tue, 22 May 2018 23:27:54 +0200 | |
changeset 50225 | 2942ae532175 |
parent 50113 | caf115bb98ad |
child 50251 | dc01f2104e46 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2014, 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 |
package jdk.jfr.jvm; |
|
26 |
||
27 |
||
28 |
import java.io.FileReader; |
|
29 |
import java.io.IOException; |
|
30 |
import java.nio.file.Files; |
|
31 |
import java.nio.file.Path; |
|
32 |
import java.nio.file.Paths; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.concurrent.Callable; |
|
35 |
||
36 |
import jdk.jfr.AnnotationElement; |
|
37 |
import jdk.jfr.Configuration; |
|
38 |
import jdk.jfr.Description; |
|
39 |
import jdk.jfr.Event; |
|
40 |
import jdk.jfr.EventFactory; |
|
41 |
import jdk.jfr.EventSettings; |
|
42 |
import jdk.jfr.EventType; |
|
43 |
import jdk.jfr.FlightRecorder; |
|
44 |
import jdk.jfr.FlightRecorderListener; |
|
45 |
import jdk.jfr.FlightRecorderPermission; |
|
46 |
import jdk.jfr.Label; |
|
47 |
import jdk.jfr.Recording; |
|
48 |
import jdk.jfr.RecordingState; |
|
49 |
import jdk.jfr.SettingControl; |
|
50 |
import jdk.jfr.ValueDescriptor; |
|
51 |
import jdk.jfr.consumer.RecordedClass; |
|
52 |
import jdk.jfr.consumer.RecordedEvent; |
|
53 |
import jdk.jfr.consumer.RecordedFrame; |
|
54 |
import jdk.jfr.consumer.RecordedMethod; |
|
55 |
import jdk.jfr.consumer.RecordedObject; |
|
56 |
import jdk.jfr.consumer.RecordedStackTrace; |
|
57 |
import jdk.jfr.consumer.RecordedThread; |
|
58 |
import jdk.jfr.consumer.RecordedThreadGroup; |
|
59 |
import jdk.jfr.consumer.RecordingFile; |
|
60 |
import jdk.management.jfr.ConfigurationInfo; |
|
61 |
import jdk.management.jfr.EventTypeInfo; |
|
62 |
import jdk.management.jfr.FlightRecorderMXBean; |
|
63 |
import jdk.management.jfr.RecordingInfo; |
|
64 |
import jdk.management.jfr.SettingDescriptorInfo; |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
65 |
import jdk.test.lib.Utils; |
50113 | 66 |
|
67 |
/* |
|
68 |
* @test TestUnsupportedVM |
|
69 |
* @key jfr |
|
70 |
* |
|
71 |
* @modules jdk.jfr |
|
72 |
* jdk.management.jfr |
|
73 |
* |
|
74 |
* @run main/othervm -Dprepare-recording=true jdk.jfr.jvm.TestUnsupportedVM |
|
75 |
* @run main/othervm -Djfr.unsupported.vm=true jdk.jfr.jvm.TestUnsupportedVM |
|
76 |
*/ |
|
77 |
public class TestUnsupportedVM { |
|
78 |
||
79 |
private static Path RECORDING_FILE = Paths.get("recording.jfr"); |
|
80 |
private static Class<?> [] APIClasses = { |
|
81 |
AnnotationElement.class, |
|
82 |
Configuration.class, |
|
83 |
ConfigurationInfo.class, |
|
84 |
Event.class, |
|
85 |
EventFactory.class, |
|
86 |
EventSettings.class, |
|
87 |
EventType.class, |
|
88 |
EventTypeInfo.class, |
|
89 |
FlightRecorder.class, |
|
90 |
FlightRecorderPermission.class, |
|
91 |
FlightRecorderListener.class, |
|
92 |
FlightRecorderMXBean.class, |
|
93 |
RecordedClass.class, |
|
94 |
RecordedEvent.class, |
|
95 |
RecordedFrame.class, |
|
96 |
RecordedMethod.class, |
|
97 |
RecordedObject.class, |
|
98 |
RecordedStackTrace.class, |
|
99 |
RecordedThread.class, |
|
100 |
RecordedThreadGroup.class, |
|
101 |
Recording.class, |
|
102 |
RecordingFile.class, |
|
103 |
RecordingInfo.class, |
|
104 |
RecordingState.class, |
|
105 |
SettingControl.class, |
|
106 |
SettingDescriptorInfo.class, |
|
107 |
ValueDescriptor.class |
|
108 |
}; |
|
109 |
// * @run main/othervm -Dprepare-recording=true jdk.jfr.jvm.TestUnsupportedVM |
|
110 |
@Label("My Event") |
|
111 |
@Description("My fine event") |
|
112 |
static class MyEvent extends Event { |
|
113 |
int myValue; |
|
114 |
} |
|
115 |
||
116 |
public static void main(String... args) throws Exception { |
|
117 |
if (Boolean.getBoolean("prepare-recording")) { |
|
118 |
Recording r = new Recording(Configuration.getConfiguration("default")); |
|
119 |
r.start(); |
|
120 |
r.stop(); |
|
121 |
r.dump(RECORDING_FILE); |
|
122 |
r.close(); |
|
123 |
return; |
|
124 |
} |
|
125 |
||
126 |
System.out.println("jdk.jfr.unsupportedvm=" + System.getProperty("jdk.jfr.unsupportedvm")); |
|
127 |
// Class FlightRecorder |
|
128 |
if (FlightRecorder.isAvailable()) { |
|
129 |
throw new AssertionError("JFR should not be available on an unsupported VM"); |
|
130 |
} |
|
131 |
||
132 |
if (FlightRecorder.isInitialized()) { |
|
133 |
throw new AssertionError("JFR should not be initialized on an unsupported VM"); |
|
134 |
} |
|
135 |
||
136 |
assertIllegalStateException(() -> FlightRecorder.getFlightRecorder()); |
|
137 |
assertSwallow(() -> FlightRecorder.addListener(new FlightRecorderListener() {})); |
|
138 |
assertSwallow(() -> FlightRecorder.removeListener(new FlightRecorderListener() {})); |
|
139 |
assertSwallow(() -> FlightRecorder.register(MyEvent.class)); |
|
140 |
assertSwallow(() -> FlightRecorder.unregister(MyEvent.class)); |
|
141 |
assertSwallow(() -> FlightRecorder.addPeriodicEvent(MyEvent.class, new Runnable() { public void run() {} })); |
|
142 |
assertSwallow(() -> FlightRecorder.removePeriodicEvent(new Runnable() { public void run() {} })); |
|
143 |
||
144 |
// Class Configuration |
|
145 |
if (!Configuration.getConfigurations().isEmpty()) { |
|
146 |
throw new AssertionError("Configuration files should not exist on an unsupported VM"); |
|
147 |
} |
|
50225
2942ae532175
8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents:
50113
diff
changeset
|
148 |
Path jfcFile = Utils.createTempFile("empty", ".jfr"); |
50113 | 149 |
assertIOException(() -> Configuration.getConfiguration("default")); |
150 |
assertIOException(() -> Configuration.create(jfcFile)); |
|
151 |
assertIOException(() -> Configuration.create(new FileReader(jfcFile.toFile()))); |
|
152 |
||
153 |
// Class EventType |
|
154 |
assertInternalError(() -> EventType.getEventType(MyEvent.class)); |
|
155 |
||
156 |
// Class EventFactory |
|
157 |
assertInternalError(() -> EventFactory.create(new ArrayList<>(), new ArrayList<>())); |
|
158 |
||
159 |
// Create a static event |
|
160 |
MyEvent myEvent = new MyEvent(); |
|
161 |
myEvent.begin(); |
|
162 |
myEvent.end(); |
|
163 |
myEvent.shouldCommit(); |
|
164 |
myEvent.commit(); |
|
165 |
||
166 |
// Trigger class initialization failure |
|
167 |
for (Class<?> c : APIClasses) { |
|
168 |
assertNoClassInitFailure(c); |
|
169 |
} |
|
170 |
||
171 |
// jdk.jfr.consumer.* |
|
172 |
// Only run this part of tests if we are on VM |
|
173 |
// that can produce a recording file |
|
174 |
if (Files.exists(RECORDING_FILE)) { |
|
175 |
for(RecordedEvent re : RecordingFile.readAllEvents(RECORDING_FILE)) { |
|
176 |
System.out.println(re); |
|
177 |
} |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
private static void assertNoClassInitFailure(Class<?> clazz) { |
|
182 |
try { |
|
183 |
Class.forName(clazz.getName(), true, clazz.getClassLoader()); |
|
184 |
} catch (ClassNotFoundException e) { |
|
185 |
throw new AssertionError("Could not find public API class on unsupported VM"); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
private static void assertInternalError(Runnable r) { |
|
190 |
try { |
|
191 |
r.run(); |
|
192 |
} catch (InternalError e) { |
|
193 |
// OK, as expected |
|
194 |
return; |
|
195 |
} |
|
196 |
throw new AssertionError("Expected InternalError on an unsupported JVM"); |
|
197 |
} |
|
198 |
||
199 |
private static void assertIOException(Callable<?> c) { |
|
200 |
try { |
|
201 |
c.call(); |
|
202 |
} catch (Exception e) { |
|
203 |
if (e.getClass() == IOException.class) { |
|
204 |
return; |
|
205 |
} |
|
206 |
} |
|
207 |
throw new AssertionError("Expected IOException on an unsupported JVM"); |
|
208 |
} |
|
209 |
||
210 |
private static void assertIllegalStateException(Runnable r) throws Exception { |
|
211 |
try { |
|
212 |
r.run(); |
|
213 |
} catch (IllegalStateException ise) { |
|
214 |
if (!ise.getMessage().equals("Flight Recorder is not supported on this VM")) { |
|
215 |
throw new AssertionError("Expected 'Flight Recorder is not supported on this VM'"); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
private static void assertSwallow(Runnable r) throws Exception { |
|
221 |
try { |
|
222 |
r.run(); |
|
223 |
} catch (Exception e) { |
|
224 |
throw new AssertionError("Unexpected exception '" + e.getMessage() + " on an unspported VM"); |
|
225 |
} |
|
226 |
} |
|
227 |
} |