1 7 8 package test.compliance.query; 9 10 import junit.framework.TestCase; 11 12 import test.compliance.query.support.Trivial; 13 14 import java.util.Iterator; 15 import java.util.Set; 16 import java.util.HashSet; 17 18 import javax.management.MBeanServer; 19 import javax.management.MBeanServerFactory; 20 import javax.management.ObjectInstance; 21 import javax.management.ObjectName; 22 23 30 public class QueryMBeansObjectNameOnlyTestCase 31 extends TestCase 32 { 33 35 38 int implSize; 39 40 42 45 public QueryMBeansObjectNameOnlyTestCase(String s) 46 { 47 super(s); 48 49 MBeanServer server = MBeanServerFactory.createMBeanServer(); 51 implSize = server.getMBeanCount().intValue(); 52 MBeanServerFactory.releaseMBeanServer(server); 53 } 54 55 57 60 public void testExactFound() 61 { 62 MBeanServer server = null; 63 HashSet instances = new HashSet(); 64 Set resultMBeans = null; 65 Set resultNames = null; 66 try 67 { 68 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 69 instances.add(server.registerMBean(new Trivial(), 70 new ObjectName("Domain1:type=instance1"))); 71 server.registerMBean(new Trivial(), 72 new ObjectName("Domain1:type=instance2")); 73 server.registerMBean(new Trivial(), 74 new ObjectName("Domain2:type=instance1")); 75 server.registerMBean(new Trivial(), 76 new ObjectName("Domain3:type=instance1")); 77 resultMBeans = server.queryMBeans(new ObjectName("Domain1:type=instance1"), null); 78 resultNames = server.queryNames(new ObjectName("Domain1:type=instance1"), null); 79 } 80 catch (Exception e) 81 { 82 fail(e.toString()); 83 } 84 85 checkit(resultMBeans, resultNames, instances, 0); 86 87 if (server != null) 88 MBeanServerFactory.releaseMBeanServer(server); 89 } 90 91 94 public void testExactNotFound() 95 { 96 MBeanServer server = null; 97 HashSet instances = new HashSet(); 98 Set resultMBeans = null; 99 Set resultNames = null; 100 try 101 { 102 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 103 server.registerMBean(new Trivial(), 104 new ObjectName("Domain1:type=instance1")); 105 server.registerMBean(new Trivial(), 106 new ObjectName("Domain1:type=instance2")); 107 server.registerMBean(new Trivial(), 108 new ObjectName("Domain2:type=instance1")); 109 server.registerMBean(new Trivial(), 110 new ObjectName("Domain3:type=instance1")); 111 resultMBeans = server.queryMBeans(new ObjectName("Domain2:type=instance2"), null); 112 resultNames = server.queryNames(new ObjectName("Domain2:type=instance2"), null); 113 } 114 catch (Exception e) 115 { 116 fail(e.toString()); 117 } 118 119 checkit(resultMBeans, resultNames, instances, 0); 120 121 if (server != null) 122 MBeanServerFactory.releaseMBeanServer(server); 123 } 124 125 128 public void testGetAllMBeans() 129 { 130 MBeanServer server = null; 131 HashSet instances = new HashSet(); 132 Set resultMBeans = null; 133 Set resultNames = null; 134 try 135 { 136 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 137 instances.add(server.registerMBean(new Trivial(), 138 new ObjectName("Domain1:type=instance1"))); 139 instances.add(server.registerMBean(new Trivial(), 140 new ObjectName("Domain1:type=instance2"))); 141 instances.add(server.registerMBean(new Trivial(), 142 new ObjectName("Domain2:type=instance1"))); 143 instances.add(server.registerMBean(new Trivial(), 144 new ObjectName("Domain3:type=instance1"))); 145 resultMBeans = server.queryMBeans(new ObjectName("*:*"), null); 146 resultNames = server.queryNames(new ObjectName("*:*"), null); 147 } 148 catch (Exception e) 149 { 150 fail(e.toString()); 151 } 152 153 checkit(resultMBeans, resultNames, instances, implSize); 154 155 if (server != null) 156 MBeanServerFactory.releaseMBeanServer(server); 157 } 158 159 162 public void testGetAllMBeans2() 163 { 164 MBeanServer server = null; 165 HashSet instances = new HashSet(); 166 Set resultMBeans = null; 167 Set resultNames = null; 168 try 169 { 170 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 171 instances.add(server.registerMBean(new Trivial(), 172 new ObjectName("Domain1:type=instance1"))); 173 instances.add(server.registerMBean(new Trivial(), 174 new ObjectName("Domain1:type=instance2"))); 175 instances.add(server.registerMBean(new Trivial(), 176 new ObjectName("Domain2:type=instance1"))); 177 instances.add(server.registerMBean(new Trivial(), 178 new ObjectName("Domain3:type=instance1"))); 179 resultMBeans = server.queryMBeans(new ObjectName(""), null); 180 resultNames = server.queryNames(new ObjectName(""), null); 181 } 182 catch (Exception e) 183 { 184 fail(e.toString()); 185 } 186 187 checkit(resultMBeans, resultNames, instances, implSize); 188 189 if (server != null) 190 MBeanServerFactory.releaseMBeanServer(server); 191 } 192 193 196 public void testGetAllMBeans3() 197 { 198 MBeanServer server = null; 199 HashSet instances = new HashSet(); 200 Set resultMBeans = null; 201 Set resultNames = null; 202 try 203 { 204 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 205 instances.add(server.registerMBean(new Trivial(), 206 new ObjectName("Domain1:type=instance1"))); 207 instances.add(server.registerMBean(new Trivial(), 208 new ObjectName("Domain1:type=instance2"))); 209 instances.add(server.registerMBean(new Trivial(), 210 new ObjectName("Domain2:type=instance1"))); 211 instances.add(server.registerMBean(new Trivial(), 212 new ObjectName("Domain3:type=instance1"))); 213 resultMBeans = server.queryMBeans(null, null); 214 resultNames = server.queryNames(null, null); 215 } 216 catch (Exception e) 217 { 218 fail(e.toString()); 219 } 220 221 checkit(resultMBeans, resultNames, instances, implSize); 222 223 if (server != null) 224 MBeanServerFactory.releaseMBeanServer(server); 225 } 226 227 230 public void testGetOneDomain() 231 { 232 MBeanServer server = null; 233 HashSet instances = new HashSet(); 234 Set resultMBeans = null; 235 Set resultNames = null; 236 try 237 { 238 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 239 instances.add(server.registerMBean(new Trivial(), 240 new ObjectName("Domain1:type=instance1"))); 241 instances.add(server.registerMBean(new Trivial(), 242 new ObjectName("Domain1:type=instance2"))); 243 server.registerMBean(new Trivial(), 244 new ObjectName("Domain2:type=instance1")); 245 server.registerMBean(new Trivial(), 246 new ObjectName("Domain3:type=instance1")); 247 resultMBeans = server.queryMBeans(new ObjectName("Domain1:*"), null); 248 resultNames = server.queryNames(new ObjectName("Domain1:*"), null); 249 } 250 catch (Exception e) 251 { 252 fail(e.toString()); 253 } 254 255 checkit(resultMBeans, resultNames, instances, 0); 256 257 if (server != null) 258 MBeanServerFactory.releaseMBeanServer(server); 259 } 260 261 264 public void testGetDefaultDomain() 265 { 266 MBeanServer server = null; 267 HashSet instances = new HashSet(); 268 Set resultMBeans = null; 269 Set resultNames = null; 270 try 271 { 272 server = MBeanServerFactory.createMBeanServer("Domain1"); 273 instances.add(server.registerMBean(new Trivial(), 274 new ObjectName("Domain1:type=instance1"))); 275 instances.add(server.registerMBean(new Trivial(), 276 new ObjectName("Domain1:type=instance2"))); 277 server.registerMBean(new Trivial(), 278 new ObjectName("Domain2:type=instance1")); 279 server.registerMBean(new Trivial(), 280 new ObjectName("Domain3:type=instance1")); 281 resultMBeans = server.queryMBeans(new ObjectName(":*"), null); 282 resultNames = server.queryNames(new ObjectName(":*"), null); 283 } 284 catch (Exception e) 285 { 286 fail(e.toString()); 287 } 288 289 checkit(resultMBeans, resultNames, instances, 0); 290 291 if (server != null) 292 MBeanServerFactory.releaseMBeanServer(server); 293 } 294 295 298 public void testGetOneProperty() 299 { 300 MBeanServer server = null; 301 HashSet instances = new HashSet(); 302 Set resultMBeans = null; 303 Set resultNames = null; 304 try 305 { 306 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 307 instances.add(server.registerMBean(new Trivial(), 308 new ObjectName("Domain1:type=instance1"))); 309 server.registerMBean(new Trivial(), 310 new ObjectName("Domain1:type=instance2")); 311 instances.add(server.registerMBean(new Trivial(), 312 new ObjectName("Domain2:type=instance1"))); 313 instances.add(server.registerMBean(new Trivial(), 314 new ObjectName("Domain3:type=instance1"))); 315 resultMBeans = server.queryMBeans(new ObjectName("*:type=instance1"), null); 316 resultNames = server.queryNames(new ObjectName("*:type=instance1"), null); 317 } 318 catch (Exception e) 319 { 320 fail(e.toString()); 321 } 322 323 checkit(resultMBeans, resultNames, instances, 0); 324 325 if (server != null) 326 MBeanServerFactory.releaseMBeanServer(server); 327 } 328 329 332 public void testSpecAll() 333 { 334 MBeanServer server = null; 335 HashSet instances = new HashSet(); 336 Set resultMBeans = null; 337 Set resultNames = null; 338 try 339 { 340 server = MBeanServerFactory.createMBeanServer(); 341 instances.add(server.registerMBean(new Trivial(), 342 new ObjectName("MyDomain:description=Printer,type=laser"))); 343 instances.add(server.registerMBean(new Trivial(), 344 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993"))); 345 instances.add(server.registerMBean(new Trivial(), 346 new ObjectName("Socrates:description=Printer,type=laser,date=1993"))); 347 instances.add(server.registerMBean(new Trivial(), 348 new ObjectName("DefaultDomain:description=Printer,type=ink"))); 349 instances.add(server.registerMBean(new Trivial(), 350 new ObjectName("MyDomain:description=Disk,capacity=2"))); 351 instances.add(server.registerMBean(new Trivial(), 352 new ObjectName("DefaultDomain:description=Disk,capacity=1"))); 353 resultMBeans = server.queryMBeans(new ObjectName("*:*"), null); 354 resultNames = server.queryNames(new ObjectName("*:*"), null); 355 } 356 catch (Exception e) 357 { 358 fail(e.toString()); 359 } 360 361 checkit(resultMBeans, resultNames, instances, implSize); 362 363 if (server != null) 364 MBeanServerFactory.releaseMBeanServer(server); 365 } 366 367 370 public void testSpecDefault() 371 { 372 MBeanServer server = null; 373 HashSet instances = new HashSet(); 374 Set resultMBeans = null; 375 Set resultNames = null; 376 try 377 { 378 server = MBeanServerFactory.createMBeanServer(); 379 server.registerMBean(new Trivial(), 380 new ObjectName("MyDomain:description=Printer,type=laser")); 381 instances.add(server.registerMBean(new Trivial(), 382 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993"))); 383 server.registerMBean(new Trivial(), 384 new ObjectName("Socrates:description=Printer,type=laser,date=1993")); 385 instances.add(server.registerMBean(new Trivial(), 386 new ObjectName("DefaultDomain:description=Printer,type=ink"))); 387 server.registerMBean(new Trivial(), 388 new ObjectName("MyDomain:description=Disk,capacity=2")); 389 instances.add(server.registerMBean(new Trivial(), 390 new ObjectName("DefaultDomain:description=Disk,capacity=1"))); 391 resultMBeans = server.queryMBeans(new ObjectName(":*"), null); 392 resultNames = server.queryNames(new ObjectName(":*"), null); 393 } 394 catch (Exception e) 395 { 396 fail(e.toString()); 397 } 398 399 checkit(resultMBeans, resultNames, instances, 0); 400 401 if (server != null) 402 MBeanServerFactory.releaseMBeanServer(server); 403 } 404 405 408 public void testSpecMyDomain() 409 { 410 MBeanServer server = null; 411 HashSet instances = new HashSet(); 412 Set resultMBeans = null; 413 Set resultNames = null; 414 try 415 { 416 server = MBeanServerFactory.createMBeanServer(); 417 instances.add(server.registerMBean(new Trivial(), 418 new ObjectName("MyDomain:description=Printer,type=laser"))); 419 server.registerMBean(new Trivial(), 420 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993")); 421 server.registerMBean(new Trivial(), 422 new ObjectName("Socrates:description=Printer,type=laser,date=1993")); 423 server.registerMBean(new Trivial(), 424 new ObjectName("DefaultDomain:description=Printer,type=ink")); 425 instances.add(server.registerMBean(new Trivial(), 426 new ObjectName("MyDomain:description=Disk,capacity=2"))); 427 server.registerMBean(new Trivial(), 428 new ObjectName("DefaultDomain:description=Disk,capacity=1")); 429 resultMBeans = server.queryMBeans(new ObjectName("MyDomain:*"), null); 430 resultNames = server.queryNames(new ObjectName("MyDomain:*"), null); 431 } 432 catch (Exception e) 433 { 434 fail(e.toString()); 435 } 436 437 checkit(resultMBeans, resultNames, instances, 0); 438 439 if (server != null) 440 MBeanServerFactory.releaseMBeanServer(server); 441 } 442 443 446 public void testSpecAnyAnyDomain() 447 { 448 MBeanServer server = null; 449 HashSet instances = new HashSet(); 450 Set resultMBeans = null; 451 Set resultNames = null; 452 try 453 { 454 server = MBeanServerFactory.createMBeanServer(); 455 instances.add(server.registerMBean(new Trivial(), 456 new ObjectName("MyDomain:description=Printer,type=laser"))); 457 server.registerMBean(new Trivial(), 458 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993")); 459 server.registerMBean(new Trivial(), 460 new ObjectName("Socrates:description=Printer,type=laser,date=1993")); 461 server.registerMBean(new Trivial(), 462 new ObjectName("DefaultDomain:description=Printer,type=ink")); 463 instances.add(server.registerMBean(new Trivial(), 464 new ObjectName("MyDomain:description=Disk,capacity=2"))); 465 server.registerMBean(new Trivial(), 466 new ObjectName("DefaultDomain:description=Disk,capacity=1")); 467 resultMBeans = server.queryMBeans(new ObjectName("??Domain:*"), null); 468 resultNames = server.queryNames(new ObjectName("??Domain:*"), null); 469 } 470 catch (Exception e) 471 { 472 fail(e.toString()); 473 } 474 475 checkit(resultMBeans, resultNames, instances, 0); 476 477 if (server != null) 478 MBeanServerFactory.releaseMBeanServer(server); 479 } 480 481 484 public void testAsteriskDomAsterisk() 485 { 486 MBeanServer server = null; 487 HashSet instances = new HashSet(); 488 Set resultMBeans = null; 489 Set resultNames = null; 490 try 491 { 492 server = MBeanServerFactory.createMBeanServer(); 493 instances.add(server.registerMBean(new Trivial(), 494 new ObjectName("MyDomain:description=Printer,type=laser"))); 495 instances.add(server.registerMBean(new Trivial(), 496 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993"))); 497 server.registerMBean(new Trivial(), 498 new ObjectName("Socrates:description=Printer,type=laser,date=1993")); 499 instances.add(server.registerMBean(new Trivial(), 500 new ObjectName("DefaultDomain:description=Printer,type=ink"))); 501 instances.add(server.registerMBean(new Trivial(), 502 new ObjectName("MyDomain:description=Disk,capacity=2"))); 503 instances.add(server.registerMBean(new Trivial(), 504 new ObjectName("DefaultDomain:description=Disk,capacity=1"))); 505 resultMBeans = server.queryMBeans(new ObjectName("*Dom*:*"), null); 506 resultNames = server.queryNames(new ObjectName("*Dom*:*"), null); 507 } 508 catch (Exception e) 509 { 510 fail(e.toString()); 511 } 512 513 checkit(resultMBeans, resultNames, instances, 0); 514 515 if (server != null) 516 MBeanServerFactory.releaseMBeanServer(server); 517 } 518 519 522 public void testSpecLaserPrinters() 523 { 524 MBeanServer server = null; 525 HashSet instances = new HashSet(); 526 Set resultMBeans = null; 527 Set resultNames = null; 528 try 529 { 530 server = MBeanServerFactory.createMBeanServer(); 531 instances.add(server.registerMBean(new Trivial(), 532 new ObjectName("MyDomain:description=Printer,type=laser"))); 533 instances.add(server.registerMBean(new Trivial(), 534 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993"))); 535 instances.add(server.registerMBean(new Trivial(), 536 new ObjectName("Socrates:description=Printer,type=laser,date=1993"))); 537 server.registerMBean(new Trivial(), 538 new ObjectName("DefaultDomain:description=Printer,type=ink")); 539 server.registerMBean(new Trivial(), 540 new ObjectName("MyDomain:description=Disk,capacity=2")); 541 server.registerMBean(new Trivial(), 542 new ObjectName("DefaultDomain:description=Disk,capacity=1")); 543 resultMBeans = server.queryMBeans(new ObjectName("*:description=Printer,type=laser,*"), null); 544 resultNames = server.queryNames(new ObjectName("*:description=Printer,type=laser,*"), null); 545 } 546 catch (Exception e) 547 { 548 fail(e.toString()); 549 } 550 551 checkit(resultMBeans, resultNames, instances, 0); 552 553 if (server != null) 554 MBeanServerFactory.releaseMBeanServer(server); 555 } 556 557 560 public void testSpecPrinters() 561 { 562 MBeanServer server = null; 563 HashSet instances = new HashSet(); 564 Set resultMBeans = null; 565 Set resultNames = null; 566 try 567 { 568 server = MBeanServerFactory.createMBeanServer(); 569 instances.add(server.registerMBean(new Trivial(), 570 new ObjectName("MyDomain:description=Printer,type=laser"))); 571 instances.add(server.registerMBean(new Trivial(), 572 new ObjectName("DefaultDomain:description=Printer,type=laser,date=1993"))); 573 instances.add(server.registerMBean(new Trivial(), 574 new ObjectName("Socrates:description=Printer,type=laser,date=1993"))); 575 instances.add(server.registerMBean(new Trivial(), 576 new ObjectName("DefaultDomain:description=Printer,type=ink"))); 577 server.registerMBean(new Trivial(), 578 new ObjectName("MyDomain:description=Disk,capacity=2")); 579 server.registerMBean(new Trivial(), 580 new ObjectName("DefaultDomain:description=Disk,capacity=2")); 581 resultMBeans = server.queryMBeans(new ObjectName("*:description=Printer,*"), null); 582 resultNames = server.queryNames(new ObjectName("*:description=Printer,*"), null); 583 } 584 catch (Exception e) 585 { 586 fail(e.toString()); 587 } 588 589 checkit(resultMBeans, resultNames, instances, 0); 590 591 if (server != null) 592 MBeanServerFactory.releaseMBeanServer(server); 593 } 594 595 598 public void testSuffixMatchManyDomain() 599 { 600 MBeanServer server = null; 601 HashSet instances = new HashSet(); 602 Set resultMBeans = null; 603 Set resultNames = null; 604 try 605 { 606 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 607 instances.add(server.registerMBean(new Trivial(), 608 new ObjectName("DomainA123:type=instance1"))); 609 instances.add(server.registerMBean(new Trivial(), 610 new ObjectName("DomainA321:type=instance2"))); 611 instances.add(server.registerMBean(new Trivial(), 612 new ObjectName("DomainA2224:type=instance1"))); 613 instances.add(server.registerMBean(new Trivial(), 614 new ObjectName("DomainA3:type=instance1"))); 615 instances.add(server.registerMBean(new Trivial(), 616 new ObjectName("DomainA:type=instance1"))); 617 server.registerMBean(new Trivial(), 618 new ObjectName("Domain:type=instance1")); 619 server.registerMBean(new Trivial(), 620 new ObjectName("Dom:type=instance1")); 621 resultMBeans = server.queryMBeans(new ObjectName("DomainA*:*"), null); 622 resultNames = server.queryNames(new ObjectName("DomainA*:*"), null); 623 } 624 catch (Exception e) 625 { 626 fail(e.toString()); 627 } 628 629 checkit(resultMBeans, resultNames, instances, 0); 630 631 if (server != null) 632 MBeanServerFactory.releaseMBeanServer(server); 633 } 634 635 638 public void testPrefixMatchManyDomain() 639 { 640 MBeanServer server = null; 641 HashSet instances = new HashSet(); 642 Set resultMBeans = null; 643 Set resultNames = null; 644 try 645 { 646 server = MBeanServerFactory.createMBeanServer("QueryMBeans"); 647 instances.add(server.registerMBean(new Trivial(), 648 new ObjectName("123ADomain:type=instance1"))); 649 instances.add(server.registerMBean(new Trivial(), 650 new ObjectName("321ADomain:type=instance2"))); 651 instances.add(server.registerMBean(new Trivial(), 652 new ObjectName("2224ADomain:type=instance1"))); 653 instances.add(server.registerMBean(new Trivial(), 654 new ObjectName("3ADomain:type=instance1"))); 655 instances.add(server.registerMBean(new Trivial(), 656 new ObjectName("3ADomai123ADomain:type=instance1"))); 657 instances.add(server.registerMBean(new Trivial(), 658 new ObjectName("ADomain:type=instance1"))); 659 server.registerMBean(new Trivial(), 660 new ObjectName("Domain:type=instance1")); 661 server.registerMBean(new Trivial(), 662 new ObjectName("ADomai:type=instance1")); 663 server.registerMBean(new Trivial(), 664 new ObjectName("main:type=instance1")); 665 resultMBeans = server.queryMBeans(new ObjectName("*ADomain:*"), null); 666 resultNames = server.queryNames(new ObjectName("*ADomain:*"), null); 667 } 668 catch (Exception e) 669 { 670 fail(e.toString()); 671 } 672 673 checkit(resultMBeans, resultNames, instances, 0); 674 675 if (server != null) 676 MBeanServerFactory.releaseMBeanServer(server); 677 } 678 679 681 684 private void checkit(Set resultMBeans, Set resultNames, HashSet expected, 685 int others) 686 { 687 assertEquals(expected.size() + others, resultMBeans.size()); 689 assertEquals(expected.size() + others, resultNames.size()); 690 691 HashSet expectedNames = new HashSet(); 693 Iterator iterator = expected.iterator(); 694 while (iterator.hasNext()) 695 { 696 expectedNames.add(((ObjectInstance) iterator.next()).getObjectName()); 697 } 698 699 iterator = resultMBeans.iterator(); 701 while (iterator.hasNext()) 702 { 703 ObjectInstance instance = (ObjectInstance) iterator.next(); 704 Iterator iterator2 = expected.iterator(); 705 boolean found = false; 706 while (iterator2.hasNext()) 707 { 708 if (iterator2.next().equals(instance)) 709 { 710 iterator2.remove(); 711 found = true; 712 break; 713 } 714 } 715 if (found == false && 716 instance.getObjectName().getDomain().equals("JMImplementation") == false) 717 fail("Unexpected instance " + instance.getObjectName()); 718 } 719 720 iterator = resultNames.iterator(); 722 while (iterator.hasNext()) 723 { 724 ObjectName name = (ObjectName) iterator.next(); 725 Iterator iterator2 = expectedNames.iterator(); 726 boolean found = false; 727 while (iterator2.hasNext()) 728 { 729 if (iterator2.next().equals(name)) 730 { 731 iterator2.remove(); 732 found = true; 733 break; 734 } 735 } 736 if (found == false && 737 name.getDomain().equals("JMImplementation") == false) 738 fail("Unexpected name " + name); 739 } 740 } 741 } 742 | Popular Tags |