blob: ee52aee9a62c3f99bcd003519b21a9533714a20f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6222961
27 * @summary Test that the counter/gauge/string monitors
28 * support attributes of arbitrary data types.
29 * @author Luis-Miguel Alventosa
30 * @run clean AttributeArbitraryDataTypeTest
31 * @run build AttributeArbitraryDataTypeTest
32 * @run main AttributeArbitraryDataTypeTest
33 */
34
35import java.beans.IntrospectionException;
36import java.beans.PropertyDescriptor;
37import java.beans.SimpleBeanInfo;
38import javax.management.MBeanServer;
39import javax.management.MBeanServerFactory;
40import javax.management.Notification;
41import javax.management.NotificationListener;
42import javax.management.ObjectName;
43import javax.management.monitor.CounterMonitor;
44import javax.management.monitor.GaugeMonitor;
45import javax.management.monitor.MonitorNotification;
46import javax.management.monitor.StringMonitor;
47import javax.management.openmbean.CompositeData;
48import javax.management.openmbean.CompositeDataSupport;
49import javax.management.openmbean.CompositeType;
50import javax.management.openmbean.OpenDataException;
51import javax.management.openmbean.OpenType;
52import javax.management.openmbean.SimpleType;
53
54public class AttributeArbitraryDataTypeTest implements NotificationListener {
55
56 // Flag to notify that a message has been received
57 private boolean counterMessageReceived = false;
58 private boolean gaugeMessageReceived = false;
59 private boolean stringMessageReceived = false;
60
61 // Match enum
62 public enum Match { do_not_match_0,
63 do_not_match_1,
64 do_not_match_2,
65 do_match_now };
66
67 // MatchBeanInfo class
68 public static class MatchBeanInfo extends SimpleBeanInfo {
69 public PropertyDescriptor[] getPropertyDescriptors() {
70 try {
71 return new PropertyDescriptor[] {
72 new PropertyDescriptor("name", Match.class, "name", null) };
73 } catch (IntrospectionException e ) {
74 e.printStackTrace();
75 return null;
76 }
77 }
78 }
79
80 // ComplexAttribute class
81 public class ComplexAttribute {
82
83 public Integer getIntegerAttribute() {
84 return i;
85 }
86
87 public void setIntegerAttribute(Integer i) {
88 this.i = i;
89 }
90
91 public Double getDoubleAttribute() {
92 return d;
93 }
94
95 public void setDoubleAttribute(Double d) {
96 this.d = d;
97 }
98
99 public String getStringAttribute() {
100 return s;
101 }
102
103 public void setStringAttribute(String s) {
104 this.s = s;
105 }
106
107 public Integer[] getArrayAttribute() {
108 return a;
109 }
110
111 public void setArrayAttribute(Integer[] a) {
112 this.a = a;
113 }
114
115 public Match getEnumAttribute() {
116 return e;
117 }
118
119 public void setEnumAttribute(Match e) {
120 this.e = e;
121 }
122
123 private Integer i;
124 private Double d;
125 private String s;
126 private Integer[] a;
127 private Match e;
128 }
129
130 // MBean class
131 public class ObservedObject implements ObservedObjectMBean {
132
133 // Simple type buried in complex getter
134 //
135 public ComplexAttribute getComplexAttribute() {
136 return ca;
137 }
138
139 public void setComplexAttribute(ComplexAttribute ca) {
140 this.ca = ca;
141 }
142
143 private ComplexAttribute ca = null;
144
145 // Simple type buried in CompositeData
146 //
147 public CompositeData getCompositeDataAttribute()
148 throws OpenDataException {
149 CompositeType ct = new CompositeType("CompositeDataAttribute",
150 "Composite Data Attribute",
151 itemNames,
152 itemDescriptions,
153 itemTypes);
154 Object itemValues[] = { ia, da, sa };
155 return new CompositeDataSupport(ct, itemNames, itemValues);
156 }
157
158 public Integer ia;
159 public Double da;
160 public String sa;
161
162 private String itemNames[] = { "IntegerAttribute",
163 "DoubleAttribute",
164 "StringAttribute" };
165 private String itemDescriptions[] = { "Integer Attribute",
166 "Double Attribute",
167 "String Attribute" };
168 private OpenType itemTypes[] = { SimpleType.INTEGER,
169 SimpleType.DOUBLE,
170 SimpleType.STRING };
171 }
172
173 // MBean interface
174 public interface ObservedObjectMBean {
175 public ComplexAttribute getComplexAttribute();
176 public void setComplexAttribute(ComplexAttribute ca);
177 public CompositeData getCompositeDataAttribute()
178 throws OpenDataException;
179 }
180
181 // Notification handler
182 public void handleNotification(Notification notification,
183 Object handback) {
184 MonitorNotification n = (MonitorNotification) notification;
185 echo("\tInside handleNotification...");
186 String type = n.getType();
187 try {
188 if (type.equals(MonitorNotification.
189 THRESHOLD_VALUE_EXCEEDED)) {
190 echo("\t\t" + n.getObservedAttribute() +
191 " has reached or exceeded the threshold");
192 echo("\t\tDerived Gauge = " + n.getDerivedGauge());
193 echo("\t\tTrigger = " + n.getTrigger());
194 counterMessageReceived = true;
195 } else if (type.equals(MonitorNotification.
196 THRESHOLD_HIGH_VALUE_EXCEEDED)) {
197 echo("\t\t" + n.getObservedAttribute() +
198 " has reached or exceeded the high threshold");
199 echo("\t\tDerived Gauge = " + n.getDerivedGauge());
200 echo("\t\tTrigger = " + n.getTrigger());
201 gaugeMessageReceived = true;
202 } else if (type.equals(MonitorNotification.
203 STRING_TO_COMPARE_VALUE_MATCHED)) {
204 echo("\t\t" + n.getObservedAttribute() +
205 " matches the string-to-compare value");
206 echo("\t\tDerived Gauge = " + n.getDerivedGauge());
207 echo("\t\tTrigger = " + n.getTrigger());
208 stringMessageReceived = true;
209 } else {
210 echo("\t\tSkipping notification of type: " + type);
211 }
212 } catch (Exception e) {
213 echo("\tError in handleNotification!");
214 e.printStackTrace(System.out);
215 }
216 }
217
218 /**
219 * Update the counter and check for notifications
220 */
221 public int counterMonitorNotification(int testCase)
222 throws Exception {
223
224 counterMessageReceived = false;
225 CounterMonitor counterMonitor = null;
226 try {
227 MBeanServer server = MBeanServerFactory.newMBeanServer();
228
229 String domain = server.getDefaultDomain();
230
231 // Create a new CounterMonitor MBean and add it to the MBeanServer.
232 //
233 echo(">>> CREATE a new CounterMonitor MBean");
234 ObjectName counterMonitorName = new ObjectName(
235 domain + ":type=" + CounterMonitor.class.getName());
236 counterMonitor = new CounterMonitor();
237 server.registerMBean(counterMonitor, counterMonitorName);
238
239 echo(">>> ADD a listener to the CounterMonitor");
240 counterMonitor.addNotificationListener(this, null, null);
241
242 //
243 // MANAGEMENT OF A STANDARD MBEAN
244 //
245
246 echo(">>> CREATE a new ObservedObject MBean");
247
248 ObjectName obsObjName =
249 ObjectName.getInstance(domain + ":type=ObservedObject");
250 ObservedObject obsObj = new ObservedObject();
251 ComplexAttribute ca = new ComplexAttribute();
252 switch (testCase) {
253 case 1:
254 obsObj.ia = 0;
255 break;
256 case 2:
257 ca.setIntegerAttribute(0);
258 obsObj.setComplexAttribute(ca);
259 break;
260 case 3:
261 ca.setArrayAttribute(new Integer[0]);
262 obsObj.setComplexAttribute(ca);
263 break;
264 }
265 server.registerMBean(obsObj, obsObjName);
266
267 echo(">>> SET the attributes of the CounterMonitor:");
268
269 counterMonitor.addObservedObject(obsObjName);
270 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
271
272 switch (testCase) {
273 case 1:
274 counterMonitor.setObservedAttribute(
275 "CompositeDataAttribute.IntegerAttribute");
276 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
277 "CompositeDataAttribute.IntegerAttribute");
278 break;
279 case 2:
280 counterMonitor.setObservedAttribute(
281 "ComplexAttribute.integerAttribute");
282 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
283 "ComplexAttribute.integerAttribute");
284 break;
285 case 3:
286 counterMonitor.setObservedAttribute(
287 "ComplexAttribute.arrayAttribute.length");
288 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
289 "ComplexAttribute.arrayAttribute.length");
290 break;
291 }
292
293 counterMonitor.setNotify(true);
294 echo("\tATTRIBUTE \"NotifyFlag\" = true");
295
296 Integer threshold = 2;
297 counterMonitor.setInitThreshold(threshold);
298 echo("\tATTRIBUTE \"Threshold\" = " + threshold);
299
300 int granularityperiod = 500;
301 counterMonitor.setGranularityPeriod(granularityperiod);
302 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
303
304 echo(">>> START the CounterMonitor");
305 counterMonitor.start();
306
307 // Wait for granularity period (multiplied by 2 for sure)
308 //
309 Thread.sleep(granularityperiod * 2);
310
311 switch (testCase) {
312 case 1:
313 obsObj.ia = 1;
314 break;
315 case 2:
316 ca.setIntegerAttribute(1);
317 break;
318 case 3:
319 ca.setArrayAttribute(new Integer[1]);
320 break;
321 }
322
323 // Wait for granularity period (multiplied by 2 for sure)
324 //
325 Thread.sleep(granularityperiod * 2);
326
327 switch (testCase) {
328 case 1:
329 obsObj.ia = 2;
330 break;
331 case 2:
332 ca.setIntegerAttribute(2);
333 break;
334 case 3:
335 ca.setArrayAttribute(new Integer[2]);
336 break;
337 }
338
339 // Wait for granularity period (multiplied by 2 for sure)
340 //
341 Thread.sleep(granularityperiod * 2);
342
343 switch (testCase) {
344 case 1:
345 obsObj.ia = 3;
346 break;
347 case 2:
348 ca.setIntegerAttribute(3);
349 break;
350 case 3:
351 ca.setArrayAttribute(new Integer[3]);
352 break;
353 }
354
355 // Check if notification was received
356 //
357 if (counterMessageReceived) {
358 echo("\tOK: CounterMonitor notification received");
359 } else {
360 echo("\tKO: CounterMonitor notification missed or not emitted");
361 return 1;
362 }
363 } finally {
364 if (counterMonitor != null)
365 counterMonitor.stop();
366 }
367
368 return 0;
369 }
370
371 /**
372 * Update the gauge and check for notifications
373 */
374 public int gaugeMonitorNotification(int testCase)
375 throws Exception {
376
377 gaugeMessageReceived = false;
378 GaugeMonitor gaugeMonitor = null;
379 try {
380 MBeanServer server = MBeanServerFactory.newMBeanServer();
381
382 String domain = server.getDefaultDomain();
383
384 // Create a new GaugeMonitor MBean and add it to the MBeanServer.
385 //
386 echo(">>> CREATE a new GaugeMonitor MBean");
387 ObjectName gaugeMonitorName = new ObjectName(
388 domain + ":type=" + GaugeMonitor.class.getName());
389 gaugeMonitor = new GaugeMonitor();
390 server.registerMBean(gaugeMonitor, gaugeMonitorName);
391
392 echo(">>> ADD a listener to the GaugeMonitor");
393 gaugeMonitor.addNotificationListener(this, null, null);
394
395 //
396 // MANAGEMENT OF A STANDARD MBEAN
397 //
398
399 echo(">>> CREATE a new ObservedObject MBean");
400
401 ObjectName obsObjName =
402 ObjectName.getInstance(domain + ":type=ObservedObject");
403 ObservedObject obsObj = new ObservedObject();
404 ComplexAttribute ca = new ComplexAttribute();
405 switch (testCase) {
406 case 1:
407 obsObj.da = 0.0;
408 break;
409 case 2:
410 ca.setDoubleAttribute(0.0);
411 obsObj.setComplexAttribute(ca);
412 break;
413 case 3:
414 ca.setArrayAttribute(new Integer[0]);
415 obsObj.setComplexAttribute(ca);
416 break;
417 }
418 server.registerMBean(obsObj, obsObjName);
419
420 echo(">>> SET the attributes of the GaugeMonitor:");
421
422 gaugeMonitor.addObservedObject(obsObjName);
423 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
424
425 switch (testCase) {
426 case 1:
427 gaugeMonitor.setObservedAttribute(
428 "CompositeDataAttribute.DoubleAttribute");
429 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
430 "CompositeDataAttribute.DoubleAttribute");
431 break;
432 case 2:
433 gaugeMonitor.setObservedAttribute(
434 "ComplexAttribute.doubleAttribute");
435 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
436 "ComplexAttribute.doubleAttribute");
437 break;
438 case 3:
439 gaugeMonitor.setObservedAttribute(
440 "ComplexAttribute.arrayAttribute.length");
441 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
442 "ComplexAttribute.arrayAttribute.length");
443 break;
444 }
445
446 gaugeMonitor.setNotifyLow(false);
447 gaugeMonitor.setNotifyHigh(true);
448 echo("\tATTRIBUTE \"Notify Low Flag\" = false");
449 echo("\tATTRIBUTE \"Notify High Flag\" = true");
450
451 switch (testCase) {
452 case 1:
453 case 2:
454 Double highThresholdD = 3.0, lowThresholdD = 2.5;
455 gaugeMonitor.setThresholds(highThresholdD, lowThresholdD);
456 echo("\tATTRIBUTE \"Low Threshold\" = " + lowThresholdD);
457 echo("\tATTRIBUTE \"High Threshold\" = " + highThresholdD);
458 break;
459 case 3:
460 Integer highThreshold = 2, lowThreshold = 1;
461 gaugeMonitor.setThresholds(highThreshold, lowThreshold);
462 echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);
463 echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);
464 break;
465 }
466
467 int granularityperiod = 500;
468 gaugeMonitor.setGranularityPeriod(granularityperiod);
469 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
470
471 echo(">>> START the GaugeMonitor");
472 gaugeMonitor.start();
473
474 // Wait for granularity period (multiplied by 2 for sure)
475 //
476 Thread.sleep(granularityperiod * 2);
477
478 switch (testCase) {
479 case 1:
480 obsObj.da = 2.0;
481 break;
482 case 2:
483 ca.setDoubleAttribute(2.0);
484 break;
485 case 3:
486 ca.setArrayAttribute(new Integer[2]);
487 break;
488 }
489
490 // Wait for granularity period (multiplied by 2 for sure)
491 //
492 Thread.sleep(granularityperiod * 2);
493
494 switch (testCase) {
495 case 1:
496 obsObj.da = 4.0;
497 break;
498 case 2:
499 ca.setDoubleAttribute(4.0);
500 break;
501 case 3:
502 ca.setArrayAttribute(new Integer[4]);
503 break;
504 }
505
506 // Wait for granularity period (multiplied by 2 for sure)
507 //
508 Thread.sleep(granularityperiod * 2);
509
510 switch (testCase) {
511 case 1:
512 obsObj.da = 6.0;
513 break;
514 case 2:
515 ca.setDoubleAttribute(6.0);
516 break;
517 case 3:
518 ca.setArrayAttribute(new Integer[6]);
519 break;
520 }
521
522 // Check if notification was received
523 //
524 if (gaugeMessageReceived) {
525 echo("\tOK: GaugeMonitor notification received");
526 } else {
527 echo("\tKO: GaugeMonitor notification missed or not emitted");
528 return 1;
529 }
530 } finally {
531 if (gaugeMonitor != null)
532 gaugeMonitor.stop();
533 }
534
535 return 0;
536 }
537
538 /**
539 * Update the string and check for notifications
540 */
541 public int stringMonitorNotification(int testCase)
542 throws Exception {
543
544 stringMessageReceived = false;
545 StringMonitor stringMonitor = null;
546 try {
547 MBeanServer server = MBeanServerFactory.newMBeanServer();
548
549 String domain = server.getDefaultDomain();
550
551 // Create a new StringMonitor MBean and add it to the MBeanServer.
552 //
553 echo(">>> CREATE a new StringMonitor MBean");
554 ObjectName stringMonitorName = new ObjectName(
555 domain + ":type=" + StringMonitor.class.getName());
556 stringMonitor = new StringMonitor();
557 server.registerMBean(stringMonitor, stringMonitorName);
558
559 echo(">>> ADD a listener to the StringMonitor");
560 stringMonitor.addNotificationListener(this, null, null);
561
562 //
563 // MANAGEMENT OF A STANDARD MBEAN
564 //
565
566 echo(">>> CREATE a new ObservedObject MBean");
567
568 ObjectName obsObjName =
569 ObjectName.getInstance(domain + ":type=ObservedObject");
570 ObservedObject obsObj = new ObservedObject();
571 ComplexAttribute ca = new ComplexAttribute();
572 switch (testCase) {
573 case 1:
574 obsObj.sa = "do_not_match_0";
575 break;
576 case 2:
577 ca.setStringAttribute("do_not_match_0");
578 obsObj.setComplexAttribute(ca);
579 break;
580 case 3:
581 ca.setEnumAttribute(Match.do_not_match_0);
582 obsObj.setComplexAttribute(ca);
583 break;
584 }
585 server.registerMBean(obsObj, obsObjName);
586
587 echo(">>> SET the attributes of the StringMonitor:");
588
589 stringMonitor.addObservedObject(obsObjName);
590 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
591
592 switch (testCase) {
593 case 1:
594 stringMonitor.setObservedAttribute(
595 "CompositeDataAttribute.StringAttribute");
596 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
597 "CompositeDataAttribute.StringAttribute");
598 break;
599 case 2:
600 stringMonitor.setObservedAttribute(
601 "ComplexAttribute.stringAttribute");
602 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
603 "ComplexAttribute.stringAttribute");
604 break;
605 case 3:
606 stringMonitor.setObservedAttribute(
607 "ComplexAttribute.enumAttribute.name");
608 echo("\tATTRIBUTE \"ObservedAttribute\" = " +
609 "ComplexAttribute.enumAttribute.name");
610 break;
611 }
612
613 stringMonitor.setNotifyMatch(true);
614 echo("\tATTRIBUTE \"NotifyMatch\" = true");
615
616 stringMonitor.setNotifyDiffer(false);
617 echo("\tATTRIBUTE \"NotifyDiffer\" = false");
618
619 stringMonitor.setStringToCompare("do_match_now");
620 echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\"");
621
622 int granularityperiod = 500;
623 stringMonitor.setGranularityPeriod(granularityperiod);
624 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
625
626 echo(">>> START the StringMonitor");
627 stringMonitor.start();
628
629 // Wait for granularity period (multiplied by 2 for sure)
630 //
631 Thread.sleep(granularityperiod * 2);
632
633 switch (testCase) {
634 case 1:
635 obsObj.sa = "do_not_match_1";
636 break;
637 case 2:
638 ca.setStringAttribute("do_not_match_1");
639 break;
640 case 3:
641 ca.setEnumAttribute(Match.do_not_match_1);
642 break;
643 }
644
645 // Wait for granularity period (multiplied by 2 for sure)
646 //
647 Thread.sleep(granularityperiod * 2);
648
649 switch (testCase) {
650 case 1:
651 obsObj.sa = "do_match_now";
652 break;
653 case 2:
654 ca.setStringAttribute("do_match_now");
655 break;
656 case 3:
657 ca.setEnumAttribute(Match.do_match_now);
658 break;
659 }
660
661 // Wait for granularity period (multiplied by 2 for sure)
662 //
663 Thread.sleep(granularityperiod * 2);
664
665 switch (testCase) {
666 case 1:
667 obsObj.sa = "do_not_match_2";
668 break;
669 case 2:
670 ca.setStringAttribute("do_not_match_2");
671 break;
672 case 3:
673 ca.setEnumAttribute(Match.do_not_match_2);
674 break;
675 }
676
677 // Check if notification was received
678 //
679 if (stringMessageReceived) {
680 echo("\tOK: StringMonitor notification received");
681 } else {
682 echo("\tKO: StringMonitor notification missed or not emitted");
683 return 1;
684 }
685 } finally {
686 if (stringMonitor != null)
687 stringMonitor.stop();
688 }
689
690 return 0;
691 }
692
693 /**
694 * Test the monitor notifications.
695 */
696 public int monitorNotifications() throws Exception {
697 echo(">>> ----------------------------------------");
698 int error = counterMonitorNotification(1);
699 echo(">>> ----------------------------------------");
700 error += counterMonitorNotification(2);
701 echo(">>> ----------------------------------------");
702 error += counterMonitorNotification(3);
703 echo(">>> ----------------------------------------");
704 error += gaugeMonitorNotification(1);
705 echo(">>> ----------------------------------------");
706 error += gaugeMonitorNotification(2);
707 echo(">>> ----------------------------------------");
708 error += gaugeMonitorNotification(3);
709 echo(">>> ----------------------------------------");
710 error += stringMonitorNotification(1);
711 echo(">>> ----------------------------------------");
712 error += stringMonitorNotification(2);
713 echo(">>> ----------------------------------------");
714 error += stringMonitorNotification(3);
715 echo(">>> ----------------------------------------");
716 return error;
717 }
718
719 /*
720 * Print message
721 */
722 private static void echo(String message) {
723 System.out.println(message);
724 }
725
726 /*
727 * Standalone entry point.
728 *
729 * Run the test and report to stdout.
730 */
731 public static void main (String args[]) throws Exception {
732 AttributeArbitraryDataTypeTest test =
733 new AttributeArbitraryDataTypeTest();
734 int error = test.monitorNotifications();
735 if (error > 0) {
736 echo(">>> Unhappy Bye, Bye!");
737 throw new IllegalStateException("Test FAILED: Didn't get all " +
738 "the notifications that were " +
739 "expected by the test!");
740 } else {
741 echo(">>> Happy Bye, Bye!");
742 }
743 }
744}