blob: 17cad8a5ea2daf61d690603590a8af847af08f57 [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 6175517 6304996
27 * @summary General MXBean test: createMBean, registerMBean, immutableInfo,
28 * interfaceClassName, openType, originalType, StandardMBean,
29 * StandardEmitterMBean.
30 * @author Luis-Miguel Alventosa
31 * @run clean MiscTest
32 * @run build MiscTest
33 * @run main MiscTest
34 */
35
36import java.io.*;
37import java.lang.management.*;
38import javax.management.*;
39import javax.management.openmbean.*;
40
41public class MiscTest {
42
43 private static final MBeanNotificationInfo notifs[] =
44 new MBeanNotificationInfo[] {
45 new MBeanNotificationInfo(
46 new String[] {AttributeChangeNotification.ATTRIBUTE_CHANGE},
47 AttributeChangeNotification.class.getName(),
48 "This notification is emitted when the reset() method is called.")
49 };
50
51 private static Class<?> testClasses[] = {
52 Test11.class, Test12.class,
53 Test21.class, Test22.class,
54 Test31.class, Test32.class,
55 Test33.class, Test34.class,
56 Test41.class, Test42.class,
57 Test43.class, Test44.class,
58 };
59
60 private static Class<?> testIntfs[] = {
61 Test11MBean.class, Test12MBean.class,
62 Test21MXBean.class, Test22MXBean.class,
63 Test31SMB.class, Test32SMB.class,
64 Test33SMB.class, Test34SMB.class,
65 Test41SMX.class, Test42SMX.class,
66 Test43SMX.class, Test44SMX.class,
67 };
68
69 public interface SuperInterface {
70 public String getState();
71 public void setState(String s);
72 public int getNbChanges();
73 public void reset();
74 public void close(boolean force);
75 public MemoryUsage getMemoryUsage();
76 }
77
78 public static class BaseTest {
79
80 public String getState() {
81 return state;
82 }
83
84 public void setState(String s) {
85 state = s;
86 nbChanges++;
87 }
88
89 public int getNbChanges() {
90 return nbChanges;
91 }
92
93 public void reset() {
94 state = "initial state";
95 nbChanges = 0;
96 nbResets++;
97 }
98
99 public String getName() {
100 return "name";
101 }
102
103 public void setName(String s) {
104 }
105
106 public void close(boolean force) {
107 }
108
109 public MemoryUsage getMemoryUsage() {
110 return new MemoryUsage(10, 20, 30, 40);
111 }
112
113 public int getNbResets() {
114 return nbResets;
115 }
116
117 private String state = "initial state";
118 private int nbChanges = 0;
119 private int nbResets = 0;
120 }
121
122 public static class BaseEmitterTest
123 extends NotificationBroadcasterSupport {
124
125 public String getState() {
126 return state;
127 }
128
129 public void setState(String s) {
130 state = s;
131 nbChanges++;
132 }
133
134 public int getNbChanges() {
135 return nbChanges;
136 }
137
138 public void reset() {
139 state = "initial state";
140 nbChanges = 0;
141 nbResets++;
142 }
143
144 public String getName() {
145 return "name";
146 }
147
148 public void setName(String s) {
149 }
150
151 public void close(boolean force) {
152 }
153
154 public MemoryUsage getMemoryUsage() {
155 return new MemoryUsage(10, 20, 30, 40);
156 }
157
158 public int getNbResets() {
159 return nbResets;
160 }
161
162 public MBeanNotificationInfo[] getNotificationInfo() {
163 return notifs;
164 }
165
166 private String state = "initial state";
167 private int nbChanges = 0;
168 private int nbResets = 0;
169 }
170
171 public static interface Test11MBean extends SuperInterface {
172 }
173
174 public static interface Test12MBean extends SuperInterface {
175 }
176
177 public static interface Test21MXBean extends SuperInterface {
178 }
179
180 public static interface Test22MXBean extends SuperInterface {
181 }
182
183 public static interface Test31SMB extends SuperInterface {
184 }
185
186 public static interface Test32SMB extends SuperInterface {
187 }
188
189 public static interface Test33SMB extends SuperInterface {
190 }
191
192 public static interface Test34SMB extends SuperInterface {
193 }
194
195 public static interface Test41SMX extends SuperInterface {
196 }
197
198 public static interface Test42SMX extends SuperInterface {
199 }
200
201 public static interface Test43SMX extends SuperInterface {
202 }
203
204 public static interface Test44SMX extends SuperInterface {
205 }
206
207 public static class Test11 extends BaseTest
208 implements Test11MBean {
209 }
210
211 public static class Test12 extends BaseEmitterTest
212 implements Test12MBean {
213 }
214
215 public static class Test21 extends BaseTest
216 implements Test21MXBean {
217 }
218
219 public static class Test22 extends BaseEmitterTest
220 implements Test22MXBean {
221 }
222
223 public static class Test31 extends BaseTest
224 implements Test31SMB {
225 }
226
227 public static class Test32 extends BaseEmitterTest
228 implements Test32SMB {
229 }
230
231 public static class Test33 extends StandardMBean
232 implements Test33SMB {
233
234 public Test33() {
235 super(Test33SMB.class, false);
236 }
237
238 public String getState() {
239 return state;
240 }
241
242 public void setState(String s) {
243 state = s;
244 nbChanges++;
245 }
246
247 public int getNbChanges() {
248 return nbChanges;
249 }
250
251 public void reset() {
252 state = "initial state";
253 nbChanges = 0;
254 nbResets++;
255 }
256
257 public String getName() {
258 return "name";
259 }
260
261 public void setName(String s) {
262 }
263
264 public void close(boolean force) {
265 }
266
267 public MemoryUsage getMemoryUsage() {
268 return new MemoryUsage(10, 20, 30, 40);
269 }
270
271 public int getNbResets() {
272 return nbResets;
273 }
274
275 private String state = "initial state";
276 private int nbChanges = 0;
277 private int nbResets = 0;
278 }
279
280 public static class Test34 extends StandardEmitterMBean
281 implements Test34SMB {
282
283 public Test34() {
284 super(Test34SMB.class, false,
285 new NotificationBroadcasterSupport(notifs));
286 }
287
288 public String getState() {
289 return state;
290 }
291
292 public void setState(String s) {
293 state = s;
294 nbChanges++;
295 }
296
297 public int getNbChanges() {
298 return nbChanges;
299 }
300
301 public void reset() {
302 state = "initial state";
303 nbChanges = 0;
304 nbResets++;
305 }
306
307 public String getName() {
308 return "name";
309 }
310
311 public void setName(String s) {
312 }
313
314 public void close(boolean force) {
315 }
316
317 public MemoryUsage getMemoryUsage() {
318 return new MemoryUsage(10, 20, 30, 40);
319 }
320
321 public int getNbResets() {
322 return nbResets;
323 }
324
325 private String state = "initial state";
326 private int nbChanges = 0;
327 private int nbResets = 0;
328 }
329
330 public static class Test41 extends BaseTest
331 implements Test41SMX {
332 }
333
334 public static class Test42 extends BaseEmitterTest
335 implements Test42SMX {
336 }
337
338 public static class Test43 extends StandardMBean
339 implements Test43SMX {
340
341 public Test43() {
342 super(Test43SMX.class, true);
343 }
344
345 public String getState() {
346 return state;
347 }
348
349 public void setState(String s) {
350 state = s;
351 nbChanges++;
352 }
353
354 public int getNbChanges() {
355 return nbChanges;
356 }
357
358 public void reset() {
359 state = "initial state";
360 nbChanges = 0;
361 nbResets++;
362 }
363
364 public String getName() {
365 return "name";
366 }
367
368 public void setName(String s) {
369 }
370
371 public void close(boolean force) {
372 }
373
374 public MemoryUsage getMemoryUsage() {
375 return new MemoryUsage(10, 20, 30, 40);
376 }
377
378 public int getNbResets() {
379 return nbResets;
380 }
381
382 private String state = "initial state";
383 private int nbChanges = 0;
384 private int nbResets = 0;
385 }
386
387 public static class Test44 extends StandardEmitterMBean
388 implements Test44SMX {
389
390 public Test44() {
391 super(Test44SMX.class, true,
392 new NotificationBroadcasterSupport(notifs));
393 }
394
395 public String getState() {
396 return state;
397 }
398
399 public void setState(String s) {
400 state = s;
401 nbChanges++;
402 }
403
404 public int getNbChanges() {
405 return nbChanges;
406 }
407
408 public void reset() {
409 state = "initial state";
410 nbChanges = 0;
411 nbResets++;
412 }
413
414 public String getName() {
415 return "name";
416 }
417
418 public void setName(String s) {
419 }
420
421 public void close(boolean force) {
422 }
423
424 public MemoryUsage getMemoryUsage() {
425 return new MemoryUsage(10, 20, 30, 40);
426 }
427
428 public int getNbResets() {
429 return nbResets;
430 }
431
432 private String state = "initial state";
433 private int nbChanges = 0;
434 private int nbResets = 0;
435 }
436
437 public static void main(String[] args) throws Exception {
438 // Instantiate the MBean server
439 //
440 echo("\n>>> Create the MBean server");
441 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
442
443 // Get default domain
444 //
445 echo("\n>>> Get the MBean server's default domain");
446 String domain = mbs.getDefaultDomain();
447 echo("\tDefault Domain = " + domain);
448
449 for (int i = 0; i < testClasses.length; i++) {
450 // Create and register the Test MBean
451 //
452 String cn = testClasses[i].getName();
453 String ons = domain + ":type=" + cn;
454 echo("\n>>> Create the " + cn +
455 " MBean within the MBeanServer");
456 echo("\tObjectName = " + ons);
457 ObjectName on = ObjectName.getInstance(ons);
458 if (testClasses[i] == Test31.class ||
459 testClasses[i] == Test41.class) {
460 StandardMBean s = new StandardMBean(
461 testClasses[i].newInstance(),
462 (Class) testIntfs[i],
463 testClasses[i] == Test41.class);
464 mbs.registerMBean(s, on);
465 } else if (testClasses[i] == Test32.class ||
466 testClasses[i] == Test42.class) {
467 Object o = testClasses[i].newInstance();
468 StandardEmitterMBean s = new StandardEmitterMBean(
469 o,
470 (Class) testIntfs[i],
471 testClasses[i] == Test42.class,
472 (NotificationEmitter) o);
473 mbs.registerMBean(s, on);
474 } else {
475 mbs.createMBean(cn, on);
476 }
477
478 // Check notifs
479 //
480 MBeanInfo mbi = mbs.getMBeanInfo(on);
481 MBeanNotificationInfo mbni[] = mbi.getNotifications();
482 if (i % 2 == 0) {
483 if (mbni.length != 0) {
484 throw new IllegalArgumentException(
485 "Should not be a NotificationEmitter");
486 }
487 } else {
488 if (mbni.length != 1) {
489 throw new IllegalArgumentException(
490 "Should not a NotificationEmitter with one notification");
491 }
492 }
493 // Manage the Test MBean
494 //
495 manageMBean(mbs, on, cn);
496 }
497 }
498
499 private static void manageMBean(MBeanServer mbs,
500 ObjectName on,
501 String cn)
502 throws Exception {
503
504 echo("\n>>> Manage the " + cn +
505 " MBean using its attributes ");
506 echo(" and operations exposed for management");
507
508 // Get attribute values
509 printAttributes(mbs, on);
510
511 // Change State attribute
512 echo("\n Setting State attribute to value \"new state\"...");
513 Attribute stateAttribute = new Attribute("State","new state");
514 mbs.setAttribute(on, stateAttribute);
515
516 // Get attribute values
517 printAttributes(mbs, on);
518
519 // Invoking reset operation
520 echo("\n Invoking reset operation...");
521 mbs.invoke(on, "reset", null, null);
522
523 // Invoking close operation
524 echo("\n Invoking close operation...");
525 String type = on.getKeyProperty("type");
526 String signature[] = {"boolean"};
527 mbs.invoke(on, "close", new Object[] {true}, signature);
528
529 // Get attribute values
530 printAttributes(mbs, on);
531
532 // Create proxy
533 if (type.equals(Test11.class.getName())) {
534 Test11MBean p = JMX.newMBeanProxy(mbs,
535 on,
536 Test11MBean.class);
537 // Get attribute values
538 echo("\n Getting attribute values through proxies:");
539 echo("\tState = \"" + p.getState() + "\"");
540 echo("\tNbChanges = " + p.getNbChanges());
541 echo("\tMemoryUsage = " + p.getMemoryUsage());
542 checkDescriptor(mbs, on, "true", Test11MBean.class.getName());
543 } else if (type.equals(Test12.class.getName())) {
544 Test12MBean p = JMX.newMBeanProxy(mbs,
545 on,
546 Test12MBean.class,
547 true);
548 // Get attribute values
549 echo("\n Getting attribute values through proxies:");
550 echo("\tState = \"" + p.getState() + "\"");
551 echo("\tNbChanges = " + p.getNbChanges());
552 echo("\tMemoryUsage = " + p.getMemoryUsage());
553 checkDescriptor(mbs, on, "false", Test12MBean.class.getName());
554 } else if (type.equals(Test21.class.getName())) {
555 Test21MXBean p = JMX.newMXBeanProxy(mbs,
556 on,
557 Test21MXBean.class);
558 // Get attribute values
559 echo("\n Getting attribute values through proxies:");
560 echo("\tState = \"" + p.getState() + "\"");
561 echo("\tNbChanges = " + p.getNbChanges());
562 echo("\tMemoryUsage = " + p.getMemoryUsage());
563 checkDescriptor(mbs, on, "true", Test21MXBean.class.getName());
564 } else if (type.equals(Test22.class.getName())) {
565 Test22MXBean p = JMX.newMXBeanProxy(mbs,
566 on,
567 Test22MXBean.class,
568 true);
569 // Get attribute values
570 echo("\n Getting attribute values through proxies:");
571 echo("\tState = \"" + p.getState() + "\"");
572 echo("\tNbChanges = " + p.getNbChanges());
573 echo("\tMemoryUsage = " + p.getMemoryUsage());
574 checkDescriptor(mbs, on, "true", Test22MXBean.class.getName());
575 } else if (type.equals(Test31.class.getName())) {
576 Test31SMB p = JMX.newMBeanProxy(mbs,
577 on,
578 Test31SMB.class);
579 // Get attribute values
580 echo("\n Getting attribute values through proxies:");
581 echo("\tState = \"" + p.getState() + "\"");
582 echo("\tNbChanges = " + p.getNbChanges());
583 echo("\tMemoryUsage = " + p.getMemoryUsage());
584 checkDescriptor(mbs, on, "true", Test31SMB.class.getName());
585 } else if (type.equals(Test32.class.getName())) {
586 Test32SMB p = JMX.newMBeanProxy(mbs,
587 on,
588 Test32SMB.class,
589 true);
590 // Get attribute values
591 echo("\n Getting attribute values through proxies:");
592 echo("\tState = \"" + p.getState() + "\"");
593 echo("\tNbChanges = " + p.getNbChanges());
594 echo("\tMemoryUsage = " + p.getMemoryUsage());
595 checkDescriptor(mbs, on, "true", Test32SMB.class.getName());
596 } else if (type.equals(Test33.class.getName())) {
597 Test33SMB p = JMX.newMBeanProxy(mbs,
598 on,
599 Test33SMB.class,
600 true);
601 // Get attribute values
602 echo("\n Getting attribute values through proxies:");
603 echo("\tState = \"" + p.getState() + "\"");
604 echo("\tNbChanges = " + p.getNbChanges());
605 echo("\tMemoryUsage = " + p.getMemoryUsage());
606 checkDescriptor(mbs, on, "true", Test33SMB.class.getName());
607 } else if (type.equals(Test34.class.getName())) {
608 Test34SMB p = JMX.newMBeanProxy(mbs,
609 on,
610 Test34SMB.class,
611 true);
612 // Get attribute values
613 echo("\n Getting attribute values through proxies:");
614 echo("\tState = \"" + p.getState() + "\"");
615 echo("\tNbChanges = " + p.getNbChanges());
616 echo("\tMemoryUsage = " + p.getMemoryUsage());
617 checkDescriptor(mbs, on, "true", Test34SMB.class.getName());
618 } else if (type.equals(Test41.class.getName())) {
619 Test41SMX p = JMX.newMXBeanProxy(mbs,
620 on,
621 Test41SMX.class);
622 // Get attribute values
623 echo("\n Getting attribute values through proxies:");
624 echo("\tState = \"" + p.getState() + "\"");
625 echo("\tNbChanges = " + p.getNbChanges());
626 echo("\tMemoryUsage = " + p.getMemoryUsage());
627 checkDescriptor(mbs, on, "true", Test41SMX.class.getName());
628 } else if (type.equals(Test42.class.getName())) {
629 Test42SMX p = JMX.newMXBeanProxy(mbs,
630 on,
631 Test42SMX.class,
632 true);
633 // Get attribute values
634 echo("\n Getting attribute values through proxies:");
635 echo("\tState = \"" + p.getState() + "\"");
636 echo("\tNbChanges = " + p.getNbChanges());
637 echo("\tMemoryUsage = " + p.getMemoryUsage());
638 checkDescriptor(mbs, on, "true", Test42SMX.class.getName());
639 } else if (type.equals(Test43.class.getName())) {
640 Test43SMX p = JMX.newMXBeanProxy(mbs,
641 on,
642 Test43SMX.class);
643 // Get attribute values
644 echo("\n Getting attribute values through proxies:");
645 echo("\tState = \"" + p.getState() + "\"");
646 echo("\tNbChanges = " + p.getNbChanges());
647 echo("\tMemoryUsage = " + p.getMemoryUsage());
648 checkDescriptor(mbs, on, "true", Test43SMX.class.getName());
649 } else if (type.equals(Test44.class.getName())) {
650 Test44SMX p = JMX.newMXBeanProxy(mbs,
651 on,
652 Test44SMX.class,
653 true);
654 // Get attribute values
655 echo("\n Getting attribute values through proxies:");
656 echo("\tState = \"" + p.getState() + "\"");
657 echo("\tNbChanges = " + p.getNbChanges());
658 echo("\tMemoryUsage = " + p.getMemoryUsage());
659 checkDescriptor(mbs, on, "true", Test44SMX.class.getName());
660 } else {
661 throw new IllegalArgumentException("Invalid MBean type");
662 }
663 }
664
665 private static void printAttributes(MBeanServer mbs,
666 ObjectName on)
667 throws Exception {
668 echo("\n Getting attribute values:");
669 String state = (String) mbs.getAttribute(on, "State");
670 Integer nbChanges = (Integer) mbs.getAttribute(on,"NbChanges");
671 echo("\tState = \"" + state + "\"");
672 echo("\tNbChanges = " + nbChanges);
673 String type = on.getKeyProperty("type");
674 if (type.indexOf("Test2") != -1 || type.indexOf("Test4") != -1) {
675 CompositeData memoryUsage =
676 (CompositeData) mbs.getAttribute(on, "MemoryUsage");
677 echo("\tMemoryUsage = " + memoryUsage);
678 } else {
679 MemoryUsage memoryUsage =
680 (MemoryUsage) mbs.getAttribute(on, "MemoryUsage");
681 echo("\tMemoryUsage = " + memoryUsage);
682 }
683 }
684
685 public static void checkDescriptor(MBeanServer mbs,
686 ObjectName on,
687 String immutable,
688 String intf)
689 throws Exception {
690
691 MBeanInfo mbi = mbs.getMBeanInfo(on);
692
693 Descriptor d = mbi.getDescriptor();
694 if (d == null || d.getFieldNames().length == 0)
695 throw new IllegalArgumentException("Empty descriptor");
696 if (!d.getFieldValue("immutableInfo").equals(immutable)) {
697 final String msg =
698 "Bad descriptor: expected immutableInfo=" + immutable + ": " + d;
699 throw new IllegalArgumentException(msg);
700 }
701 if (!d.getFieldValue("interfaceClassName").equals(intf)) {
702 final String msg =
703 "Bad descriptor: expected interfaceClassName=" + intf + ": " + d;
704 throw new IllegalArgumentException(msg);
705 }
706
707 if (intf.indexOf("MX") != -1) {
708 MBeanAttributeInfo attrs[] = mbi.getAttributes();
709 if (attrs == null || attrs.length == 0)
710 throw new IllegalArgumentException("No attributes");
711 boolean nbChangesFound = false;
712 for (MBeanAttributeInfo attr : attrs) {
713 if (attr.getName().equals("NbChanges")) {
714 nbChangesFound = true;
715 Descriptor ad = attr.getDescriptor();
716 OpenType<?> opty = (OpenType<?>)
717 ad.getFieldValue("openType");
718 if (!opty.equals(SimpleType.INTEGER)) {
719 throw new IllegalArgumentException("Open type should " +
720 "be INTEGER: " + opty);
721 }
722 String orty =
723 (String) ad.getFieldValue("originalType");
724 if (!orty.equals(Integer.TYPE.getName())) {
725 throw new IllegalArgumentException("Orig type should " +
726 "be int: " + orty);
727 }
728 }
729 }
730 if (!nbChangesFound)
731 throw new IllegalArgumentException("Did not find NbChanges");
732 }
733 }
734
735 private static void echo(String msg) {
736 System.out.println(msg);
737 }
738}