Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/NullAttributeValueTest.java
38839 views
1
/*
2
* Copyright (c) 2005, 2013, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6200031 8025206
27
* @summary Test that the counter/gauge/string monitors emit a
28
* jmx.monitor.error.type notification when the attribute
29
* being monitored returns a null value.
30
* @author Luis-Miguel Alventosa
31
* @author Shanliang JIANG
32
* @run clean NullAttributeValueTest
33
* @run build NullAttributeValueTest
34
* @run main NullAttributeValueTest
35
*/
36
37
import javax.management.*;
38
import javax.management.monitor.*;
39
40
public class NullAttributeValueTest implements NotificationListener {
41
42
// Flag to notify that a message has been received
43
private volatile boolean messageReceived = false;
44
45
// MBean class
46
public class ObservedObject implements ObservedObjectMBean {
47
public Integer getIntegerAttribute() {
48
return null;
49
}
50
public String getStringAttribute() {
51
return null;
52
}
53
}
54
55
// MBean interface
56
public interface ObservedObjectMBean {
57
public Integer getIntegerAttribute();
58
public String getStringAttribute();
59
}
60
61
// Notification handler
62
public void handleNotification(Notification notification,
63
Object handback) {
64
MonitorNotification n = (MonitorNotification) notification;
65
echo("\tInside handleNotification...");
66
String type = n.getType();
67
try {
68
if (type.equals(
69
MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
70
echo("\t\t" + n.getObservedAttribute() + " is null");
71
echo("\t\tDerived Gauge = " + n.getDerivedGauge());
72
echo("\t\tTrigger = " + n.getTrigger());
73
messageReceived = true;
74
} else {
75
echo("\t\tSkipping notification of type: " + type);
76
}
77
} catch (Exception e) {
78
echo("\tError in handleNotification!");
79
e.printStackTrace(System.out);
80
}
81
}
82
83
/**
84
* Update the counter and check for notifications
85
*/
86
public int counterMonitorNotification() throws Exception {
87
CounterMonitor counterMonitor = null;
88
try {
89
MBeanServer server = MBeanServerFactory.newMBeanServer();
90
91
String domain = server.getDefaultDomain();
92
93
// Create a new CounterMonitor MBean and add it to the MBeanServer.
94
//
95
echo(">>> CREATE a new CounterMonitor MBean");
96
ObjectName counterMonitorName = new ObjectName(
97
domain + ":type=" + CounterMonitor.class.getName());
98
counterMonitor = new CounterMonitor();
99
server.registerMBean(counterMonitor, counterMonitorName);
100
101
echo(">>> ADD a listener to the CounterMonitor");
102
counterMonitor.addNotificationListener(this, null, null);
103
104
//
105
// MANAGEMENT OF A STANDARD MBEAN
106
//
107
108
echo(">>> CREATE a new ObservedObject MBean");
109
110
ObjectName obsObjName =
111
ObjectName.getInstance(domain + ":type=ObservedObject");
112
ObservedObject obsObj = new ObservedObject();
113
server.registerMBean(obsObj, obsObjName);
114
115
echo(">>> SET the attributes of the CounterMonitor:");
116
117
counterMonitor.addObservedObject(obsObjName);
118
echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
119
120
counterMonitor.setObservedAttribute("IntegerAttribute");
121
echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
122
123
counterMonitor.setNotify(true);
124
echo("\tATTRIBUTE \"NotifyFlag\" = true");
125
126
Integer threshold = 2;
127
counterMonitor.setInitThreshold(threshold);
128
echo("\tATTRIBUTE \"Threshold\" = " + threshold);
129
130
int granularityperiod = 500;
131
counterMonitor.setGranularityPeriod(granularityperiod);
132
echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
133
134
echo(">>> START the CounterMonitor");
135
counterMonitor.start();
136
137
return checkReceived(granularityperiod, "CounterMonitor");
138
} finally {
139
if (counterMonitor != null)
140
counterMonitor.stop();
141
}
142
}
143
144
/**
145
* Update the gauge and check for notifications
146
*/
147
public int gaugeMonitorNotification() throws Exception {
148
GaugeMonitor gaugeMonitor = null;
149
try {
150
MBeanServer server = MBeanServerFactory.newMBeanServer();
151
152
String domain = server.getDefaultDomain();
153
154
// Create a new GaugeMonitor MBean and add it to the MBeanServer.
155
//
156
echo(">>> CREATE a new GaugeMonitor MBean");
157
ObjectName gaugeMonitorName = new ObjectName(
158
domain + ":type=" + GaugeMonitor.class.getName());
159
gaugeMonitor = new GaugeMonitor();
160
server.registerMBean(gaugeMonitor, gaugeMonitorName);
161
162
echo(">>> ADD a listener to the GaugeMonitor");
163
gaugeMonitor.addNotificationListener(this, null, null);
164
165
//
166
// MANAGEMENT OF A STANDARD MBEAN
167
//
168
169
echo(">>> CREATE a new ObservedObject MBean");
170
171
ObjectName obsObjName =
172
ObjectName.getInstance(domain + ":type=ObservedObject");
173
ObservedObject obsObj = new ObservedObject();
174
server.registerMBean(obsObj, obsObjName);
175
176
echo(">>> SET the attributes of the GaugeMonitor:");
177
178
gaugeMonitor.addObservedObject(obsObjName);
179
echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
180
181
gaugeMonitor.setObservedAttribute("IntegerAttribute");
182
echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
183
184
gaugeMonitor.setNotifyLow(false);
185
gaugeMonitor.setNotifyHigh(true);
186
echo("\tATTRIBUTE \"Notify Low Flag\" = false");
187
echo("\tATTRIBUTE \"Notify High Flag\" = true");
188
189
Double highThreshold = 3.0, lowThreshold = 2.5;
190
gaugeMonitor.setThresholds(highThreshold, lowThreshold);
191
echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);
192
echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);
193
194
int granularityperiod = 500;
195
gaugeMonitor.setGranularityPeriod(granularityperiod);
196
echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
197
198
echo(">>> START the GaugeMonitor");
199
gaugeMonitor.start();
200
201
return checkReceived(granularityperiod, "GaugeMonitor");
202
} finally {
203
if (gaugeMonitor != null)
204
gaugeMonitor.stop();
205
}
206
}
207
208
/**
209
* Update the string and check for notifications
210
*/
211
public int stringMonitorNotification() throws Exception {
212
StringMonitor stringMonitor = null;
213
try {
214
MBeanServer server = MBeanServerFactory.newMBeanServer();
215
216
String domain = server.getDefaultDomain();
217
218
// Create a new StringMonitor MBean and add it to the MBeanServer.
219
//
220
echo(">>> CREATE a new StringMonitor MBean");
221
ObjectName stringMonitorName = new ObjectName(
222
domain + ":type=" + StringMonitor.class.getName());
223
stringMonitor = new StringMonitor();
224
server.registerMBean(stringMonitor, stringMonitorName);
225
226
echo(">>> ADD a listener to the StringMonitor");
227
stringMonitor.addNotificationListener(this, null, null);
228
229
//
230
// MANAGEMENT OF A STANDARD MBEAN
231
//
232
233
echo(">>> CREATE a new ObservedObject MBean");
234
235
ObjectName obsObjName =
236
ObjectName.getInstance(domain + ":type=ObservedObject");
237
ObservedObject obsObj = new ObservedObject();
238
server.registerMBean(obsObj, obsObjName);
239
240
echo(">>> SET the attributes of the StringMonitor:");
241
242
stringMonitor.addObservedObject(obsObjName);
243
echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
244
245
stringMonitor.setObservedAttribute("StringAttribute");
246
echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
247
248
stringMonitor.setNotifyMatch(true);
249
echo("\tATTRIBUTE \"NotifyMatch\" = true");
250
251
stringMonitor.setNotifyDiffer(false);
252
echo("\tATTRIBUTE \"NotifyDiffer\" = false");
253
254
stringMonitor.setStringToCompare("do_match_now");
255
echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");
256
257
int granularityperiod = 500;
258
stringMonitor.setGranularityPeriod(granularityperiod);
259
echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
260
261
echo(">>> START the StringMonitor");
262
stringMonitor.start();
263
264
return checkReceived(granularityperiod, "StringMonitor");
265
} finally {
266
if (stringMonitor != null)
267
stringMonitor.stop();
268
}
269
}
270
271
/**
272
* Test the monitor notifications.
273
*/
274
public int monitorNotifications() throws Exception {
275
echo(">>> ----------------------------------------");
276
messageReceived = false;
277
int error = counterMonitorNotification();
278
echo(">>> ----------------------------------------");
279
messageReceived = false;
280
error += gaugeMonitorNotification();
281
echo(">>> ----------------------------------------");
282
messageReceived = false;
283
error += stringMonitorNotification();
284
echo(">>> ----------------------------------------");
285
return error;
286
}
287
288
private int checkReceived(long granularityperiod, String caller) throws InterruptedException {
289
int i = 100;
290
do {
291
Thread.sleep(granularityperiod);
292
} while (!messageReceived && i-- > 0);
293
294
if (messageReceived) {
295
echo("\tOK: " + caller + " notification received");
296
} else {
297
echo("\tKO: " + caller + " notification missed or not emitted");
298
}
299
300
return messageReceived ? 0 : 1;
301
}
302
303
/*
304
* Print message
305
*/
306
private static void echo(String message) {
307
System.out.println(message);
308
}
309
310
/*
311
* Standalone entry point.
312
*
313
* Run the test and report to stdout.
314
*/
315
public static void main (String args[]) throws Exception {
316
NullAttributeValueTest test = new NullAttributeValueTest();
317
int error = test.monitorNotifications();
318
if (error > 0) {
319
echo(">>> Unhappy Bye, Bye!");
320
throw new IllegalStateException("Test FAILED: Didn't get all " +
321
"the notifications that were " +
322
"expected by the test!");
323
} else {
324
echo(">>> Happy Bye, Bye!");
325
}
326
}
327
}
328
329