1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 31 import javax.management.openmbean.CompositeDataSupport ; 32 import javax.management.openmbean.CompositeType ; 33 import javax.management.openmbean.InvalidKeyException ; 34 import javax.management.openmbean.OpenDataException ; 35 import javax.management.openmbean.OpenType ; 36 import javax.management.openmbean.SimpleType ; 37 38 import junit.framework.TestCase; 39 40 45 public class CompositeDataSupportTestCase 46 extends TestCase 47 { 48 50 52 54 57 public CompositeDataSupportTestCase(String s) 58 { 59 super(s); 60 } 61 62 64 public void testCompositeDataSupport() 65 throws Exception 66 { 67 String [] itemNames = new String [] { "name1", "name2" }; 68 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 69 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 70 CompositeType compositeType = new CompositeType ("typeName", "description", 71 itemNames, itemDescriptions, itemTypes); 72 HashMap map = new HashMap (); 73 map.put("name1", "value1"); 74 map.put("name2", new Integer (2)); 75 new CompositeDataSupport (compositeType, map); 76 new CompositeDataSupport (compositeType, new String [] { "name1", "name2" }, new Object [] { "value1", new Integer (2) }); 77 } 78 79 public void testGetCompositeType() 80 throws Exception 81 { 82 String [] itemNames = new String [] { "name1", "name2" }; 83 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 84 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 85 CompositeType compositeType = new CompositeType ("typeName", "description", 86 itemNames, itemDescriptions, itemTypes); 87 HashMap map = new HashMap (); 88 map.put("name1", "value1"); 89 map.put("name2", new Integer (2)); 90 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 91 assertEquals(compositeType, data.getCompositeType()); 92 } 93 94 public void testGet() 95 throws Exception 96 { 97 String [] itemNames = new String [] { "name1", "name2" }; 98 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 99 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 100 CompositeType compositeType = new CompositeType ("typeName", "description", 101 itemNames, itemDescriptions, itemTypes); 102 HashMap map = new HashMap (); 103 map.put("name1", "value1"); 104 map.put("name2", new Integer (2)); 105 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 106 assertEquals("value1", data.get("name1")); 107 assertEquals(new Integer (2), data.get("name2")); 108 } 109 110 public void testGetAll() 111 throws Exception 112 { 113 String [] itemNames = new String [] { "name1", "name2" }; 114 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 115 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 116 CompositeType compositeType = new CompositeType ("typeName", "description", 117 itemNames, itemDescriptions, itemTypes); 118 HashMap map = new HashMap (); 119 map.put("name1", "value1"); 120 map.put("name2", new Integer (2)); 121 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 122 Object [] result = data.getAll(new String [] { "name1", "name2" }); 123 assertEquals("value1", result[0]); 124 assertEquals(new Integer (2), result[1]); 125 result = data.getAll(new String [] { "name2", "name1" }); 126 assertEquals("value1", result[1]); 127 assertEquals(new Integer (2), result[0]); 128 result = data.getAll(new String [] { "name1" }); 129 assertEquals("value1", result[0]); 130 result = data.getAll(new String [] { "name2" }); 131 assertEquals(new Integer (2), result[0]); 132 } 133 134 public void testContainsKey() 135 throws Exception 136 { 137 String [] itemNames = new String [] { "name1", "name2" }; 138 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 139 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 140 CompositeType compositeType = new CompositeType ("typeName", "description", 141 itemNames, itemDescriptions, itemTypes); 142 HashMap map = new HashMap (); 143 map.put("name1", "value1"); 144 map.put("name2", new Integer (2)); 145 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 146 assertTrue("data should contain key name1", data.containsKey("name1") == true); 147 assertTrue("data should contain key name2", data.containsKey("name2") == true); 148 assertTrue("data should not contain key nameX", data.containsKey("nameX") == false); 149 assertTrue("data should not contain key null", data.containsKey(null) == false); 150 assertTrue("data should not contain key <empty>", data.containsKey("") == false); 151 } 152 153 public void testContainsValue() 154 throws Exception 155 { 156 String [] itemNames = new String [] { "name1", "name2" }; 157 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 158 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 159 CompositeType compositeType = new CompositeType ("typeName", "description", 160 itemNames, itemDescriptions, itemTypes); 161 HashMap map = new HashMap (); 162 map.put("name1", "value1"); 163 map.put("name2", new Integer (2)); 164 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 165 assertTrue("data should contain value value1", data.containsValue("value1") == true); 166 assertTrue("data should contain value 2", data.containsValue(new Integer (2)) == true); 167 assertTrue("data should not contain value name1", data.containsValue("name1") == false); 168 assertTrue("data should not contain key null", data.containsValue(null) == false); 169 assertTrue("data should not contain key <empty>", data.containsValue("") == false); 170 171 map.clear(); 172 map.put("name1", "value1"); 173 map.put("name2", null); 174 data = new CompositeDataSupport (compositeType, map); 175 assertTrue("data should contain value null", data.containsValue(null) == true); 176 } 177 178 public void testValues() 179 throws Exception 180 { 181 String [] itemNames = new String [] { "name1", "name2" }; 182 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 183 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 184 CompositeType compositeType = new CompositeType ("typeName", "description", 185 itemNames, itemDescriptions, itemTypes); 186 HashMap map = new HashMap (); 187 map.put("name1", "value1"); 188 map.put("name2", new Integer (2)); 189 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 190 Collection values = data.values(); 191 assertTrue("data values contain 2 elements", values.size() == 2); 192 assertTrue("data values should have value1", values.contains("value1")); 193 assertTrue("data values should have 2", values.contains(new Integer (2))); 194 assertTrue("data values should not have name1", values.contains("name1") == false); 195 assertTrue("data values should not have null", values.contains(null) == false); 196 assertTrue("data values should not have <empty>", values.contains("") == false); 197 198 map.clear(); 199 map.put("name1", "value1"); 200 map.put("name2", null); 201 data = new CompositeDataSupport (compositeType, map); 202 values = data.values(); 203 assertTrue("data values should contain value null", values.contains(null) == true); 204 } 205 206 public void testEquals() 207 throws Exception 208 { 209 String [] itemNames = new String [] { "name1", "name2" }; 210 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 211 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 212 CompositeType compositeType = new CompositeType ("typeName", "description", 213 itemNames, itemDescriptions, itemTypes); 214 HashMap map = new HashMap (); 215 map.put("name1", "value1"); 216 map.put("name2", new Integer (2)); 217 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 218 219 assertTrue("data should equal itself", data.equals(data)); 220 assertTrue("data should not equal null", data.equals(null) == false); 221 assertTrue("data should not equal non CompositeData", data.equals(new Object ()) == false); 222 223 String [] itemNames2 = new String [] { "name1", "name2" }; 224 String [] itemDescriptions2 = new String [] { "desc1", "desc2" }; 225 OpenType [] itemTypes2 = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 226 CompositeType compositeType2 = new CompositeType ("typeName", "description", 227 itemNames2, itemDescriptions2, itemTypes2); 228 HashMap map2 = new HashMap (); 229 map2.put("name1", "value1"); 230 map2.put("name2", new Integer (2)); 231 CompositeDataSupport data2 = new CompositeDataSupport (compositeType2, map2); 232 233 assertTrue("data should equal with data2 with different instance of the same composite type", data.equals(data2)); 234 assertTrue("data2 should equal with data with different instance of the same composite type", data2.equals(data)); 235 236 compositeType2 = new CompositeType ("typeName2", "description", 237 itemNames2, itemDescriptions2, itemTypes2); 238 data2 = new CompositeDataSupport (compositeType2, map2); 239 240 assertTrue("data should not be equal with data2 with different composite type", data.equals(data2) == false); 241 assertTrue("data2 should not be equal with data with different composite type", data2.equals(data) == false); 242 243 map2 = new HashMap (); 244 map2.put("name1", "value1"); 245 map2.put("name2", new Integer (3)); 246 data2 = new CompositeDataSupport (compositeType, map2); 247 248 assertTrue("data should not be equal with data2 with different values", data.equals(data2) == false); 249 assertTrue("data2 should not be equal with data with different value", data2.equals(data) == false); 250 } 251 252 public void testHashCode() 253 throws Exception 254 { 255 String [] itemNames = new String [] { "name1", "name2" }; 256 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 257 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 258 CompositeType compositeType = new CompositeType ("typeName", "description", 259 itemNames, itemDescriptions, itemTypes); 260 HashMap map = new HashMap (); 261 map.put("name1", "value1"); 262 map.put("name2", new Integer (2)); 263 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 264 265 int myHashCode = compositeType.hashCode() + "value1".hashCode() + new Integer (2).hashCode(); 266 assertTrue("Wrong hash code generated", myHashCode == data.hashCode()); 267 } 268 269 public void testToString() 270 throws Exception 271 { 272 String [] itemNames = new String [] { "name1", "name2" }; 273 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 274 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 275 CompositeType compositeType = new CompositeType ("typeName", "description", 276 itemNames, itemDescriptions, itemTypes); 277 HashMap map = new HashMap (); 278 map.put("name1", "value1"); 279 map.put("name2", new Integer (2)); 280 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 281 282 String toString = data.toString(); 283 284 assertTrue("toString() should contain the composite type", 285 toString.indexOf(compositeType.toString()) != -1); 286 assertTrue("toString() should contain name1=value1", 287 toString.indexOf("name1=value1") != -1); 288 assertTrue("toString() should contain name2=" + new Integer (2), 289 toString.indexOf("name2=" + new Integer (2)) != -1); 290 } 291 292 public void testSerialization() 293 throws Exception 294 { 295 String [] itemNames = new String [] { "name1", "name2" }; 296 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 297 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 298 CompositeType compositeType = new CompositeType ("typeName", "description", 299 itemNames, itemDescriptions, itemTypes); 300 HashMap map = new HashMap (); 301 map.put("name1", "value1"); 302 map.put("name2", new Integer (2)); 303 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 304 305 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 307 ObjectOutputStream oos = new ObjectOutputStream (baos); 308 oos.writeObject(data); 309 310 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 312 ObjectInputStream ois = new ObjectInputStream (bais); 313 Object result = ois.readObject(); 314 315 assertEquals(data, result); 316 } 317 318 public void testErrorsArray() 319 throws Exception 320 { 321 String [] itemNames = new String [] { "name1", "name2" }; 322 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 323 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 324 CompositeType compositeType = new CompositeType ("typeName", "description", 325 itemNames, itemDescriptions, itemTypes); 326 Object [] itemValues = new Object [] { "value1", new Integer (2) }; 327 328 boolean caught = false; 329 try 330 { 331 new CompositeDataSupport (null, itemNames, itemValues); 332 } 333 catch (IllegalArgumentException e) 334 { 335 caught = true; 336 } 337 if (caught == false) 338 fail("Excepted IllegalArgumentException for null composite type"); 339 340 caught = false; 341 try 342 { 343 new CompositeDataSupport (compositeType, null, itemValues); 344 } 345 catch (IllegalArgumentException e) 346 { 347 caught = true; 348 } 349 if (caught == false) 350 fail("Excepted IllegalArgumentException for null item names"); 351 352 caught = false; 353 try 354 { 355 new CompositeDataSupport (compositeType, new String [0], itemValues); 356 } 357 catch (IllegalArgumentException e) 358 { 359 caught = true; 360 } 361 if (caught == false) 362 fail("Excepted IllegalArgumentException for empty item names"); 363 364 caught = false; 365 try 366 { 367 new CompositeDataSupport (compositeType, itemNames, null); 368 } 369 catch (IllegalArgumentException e) 370 { 371 caught = true; 372 } 373 if (caught == false) 374 fail("Excepted IllegalArgumentException for null item values"); 375 376 caught = false; 377 try 378 { 379 new CompositeDataSupport (compositeType, itemNames, new Object [0]); 380 } 381 catch (IllegalArgumentException e) 382 { 383 caught = true; 384 } 385 if (caught == false) 386 fail("Excepted IllegalArgumentException for empty item values"); 387 388 caught = false; 389 try 390 { 391 new CompositeDataSupport (compositeType, new String [] { "name1", null }, itemValues); 392 } 393 catch (IllegalArgumentException e) 394 { 395 caught = true; 396 } 397 if (caught == false) 398 fail("Excepted IllegalArgumentException for a null item name"); 399 400 caught = false; 401 try 402 { 403 new CompositeDataSupport (compositeType, new String [] { "name1", "" }, itemValues); 404 } 405 catch (IllegalArgumentException e) 406 { 407 caught = true; 408 } 409 if (caught == false) 410 fail("Excepted IllegalArgumentException for an empty item name"); 411 412 caught = false; 413 try 414 { 415 new CompositeDataSupport (compositeType, itemNames, new Object [] { "wrong" }); 416 } 417 catch (IllegalArgumentException e) 418 { 419 caught = true; 420 } 421 if (caught == false) 422 fail("Excepted IllegalArgumentException for mismatch in number of itemNames/itemValues"); 423 424 caught = false; 425 try 426 { 427 new CompositeDataSupport (compositeType, new String [] { "name1" }, new Object [] { "value1" }); 428 } 429 catch (OpenDataException e) 430 { 431 caught = true; 432 } 433 if (caught == false) 434 fail("Excepted OpenDataException for mismatch in number of itemNames for CompositeType/CompositeData"); 435 436 caught = false; 437 try 438 { 439 new CompositeDataSupport (compositeType, new String [] { "name1", "wrongName" }, itemValues); 440 } 441 catch (OpenDataException e) 442 { 443 caught = true; 444 } 445 if (caught == false) 446 fail("Excepted OpenDataException for an item name not in the composite type"); 447 448 caught = false; 449 try 450 { 451 new CompositeDataSupport (compositeType, itemNames, new Object [] { "value1", "wrong" }); 452 } 453 catch (OpenDataException e) 454 { 455 caught = true; 456 } 457 if (caught == false) 458 fail("Excepted OpenDataException for an item value of the wrong type"); 459 460 new CompositeDataSupport (compositeType, itemNames, new Object [] { "value1", null }); 461 } 462 463 public void testErrorsMap() 464 throws Exception 465 { 466 String [] itemNames = new String [] { "name1", "name2" }; 467 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 468 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 469 CompositeType compositeType = new CompositeType ("typeName", "description", 470 itemNames, itemDescriptions, itemTypes); 471 HashMap map = new HashMap (); 472 map.put("name1", "value1"); 473 map.put("name2", new Integer (2)); 474 475 boolean caught = false; 476 try 477 { 478 new CompositeDataSupport (null, map); 479 } 480 catch (IllegalArgumentException e) 481 { 482 caught = true; 483 } 484 if (caught == false) 485 fail("Excepted IllegalArgumentException for null composite type"); 486 487 caught = false; 488 try 489 { 490 new CompositeDataSupport (compositeType, null); 491 } 492 catch (IllegalArgumentException e) 493 { 494 caught = true; 495 } 496 if (caught == false) 497 fail("Excepted IllegalArgumentException for null map"); 498 499 caught = false; 500 try 501 { 502 new CompositeDataSupport (compositeType, new HashMap ()); 503 } 504 catch (IllegalArgumentException e) 505 { 506 caught = true; 507 } 508 if (caught == false) 509 fail("Excepted IllegalArgumentException for empty map"); 510 511 caught = false; 512 try 513 { 514 HashMap map2 = new HashMap (); 515 map2.put("name1", "value1"); 516 map2.put(null, new Integer (2)); 517 new CompositeDataSupport (compositeType, map2); 518 } 519 catch (IllegalArgumentException e) 520 { 521 caught = true; 522 } 523 if (caught == false) 524 fail("Excepted IllegalArgumentException for a null key in map"); 525 526 caught = false; 527 try 528 { 529 HashMap map2 = new HashMap (); 530 map2.put("name1", "value1"); 531 map2.put("", new Integer (2)); 532 new CompositeDataSupport (compositeType, map2); 533 } 534 catch (IllegalArgumentException e) 535 { 536 caught = true; 537 } 538 if (caught == false) 539 fail("Excepted IllegalArgumentException for an empty key in map"); 540 541 caught = false; 542 try 543 { 544 HashMap map2 = new HashMap (); 545 map2.put("name1", "value1"); 546 new CompositeDataSupport (compositeType, map2); 547 } 548 catch (OpenDataException e) 549 { 550 caught = true; 551 } 552 if (caught == false) 553 fail("Excepted OpenDataException for mismatch in number of items for CompositeType/CompositeData"); 554 555 caught = false; 556 try 557 { 558 HashMap map2 = new HashMap (); 559 map2.put("name1", "value1"); 560 map2.put("wrongName", new Integer (2)); 561 new CompositeDataSupport (compositeType, map2); 562 } 563 catch (OpenDataException e) 564 { 565 caught = true; 566 } 567 if (caught == false) 568 fail("Excepted OpenDataException for an item name not in the composite type"); 569 570 caught = false; 571 try 572 { 573 HashMap map2 = new HashMap (); 574 map2.put("name1", "value1"); 575 map2.put("name2", "wrong"); 576 new CompositeDataSupport (compositeType, map2); 577 } 578 catch (OpenDataException e) 579 { 580 caught = true; 581 } 582 if (caught == false) 583 fail("Excepted OpenDataException for an item value of the wrong type"); 584 585 caught = false; 586 try 587 { 588 HashMap map2 = new HashMap (); 589 map2.put("name1", "value1"); 590 map2.put(new Integer (2), new Integer (2)); 591 new CompositeDataSupport (compositeType, map2); 592 } 593 catch (ArrayStoreException e) 594 { 595 caught = true; 596 } 597 if (caught == false) 598 fail("Excepted ArrayStoreException for a non String key in map"); 599 600 HashMap map2 = new HashMap (); 601 map2.put("name1", "value1"); 602 map2.put("name2", null); 603 new CompositeDataSupport (compositeType, map2); 604 } 605 606 public void testErrors() 607 throws Exception 608 { 609 String [] itemNames = new String [] { "name1", "name2" }; 610 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 611 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 612 CompositeType compositeType = new CompositeType ("typeName", "description", 613 itemNames, itemDescriptions, itemTypes); 614 HashMap map = new HashMap (); 615 map.put("name1", "value1"); 616 map.put("name2", new Integer (2)); 617 CompositeDataSupport data = new CompositeDataSupport (compositeType, map); 618 619 boolean caught = false; 620 try 621 { 622 data.get(null); 623 } 624 catch (IllegalArgumentException e) 625 { 626 caught = true; 627 } 628 if (caught == false) 629 fail("Excepted IllegalArgumentException for get and a null key"); 630 631 caught = false; 632 try 633 { 634 data.get(""); 635 } 636 catch (IllegalArgumentException e) 637 { 638 caught = true; 639 } 640 if (caught == false) 641 fail("Excepted IllegalArgumentException for get and an empty key"); 642 643 caught = false; 644 try 645 { 646 data.get("wrong"); 647 } 648 catch (InvalidKeyException e) 649 { 650 caught = true; 651 } 652 if (caught == false) 653 fail("Excepted InvalidKeyException for get and a wrong key"); 654 655 caught = false; 656 try 657 { 658 data.getAll(new String [] { "name1", null }); 659 } 660 catch (IllegalArgumentException e) 661 { 662 caught = true; 663 } 664 if (caught == false) 665 fail("Excepted IllegalArgumentException for getAll and a null key"); 666 667 caught = false; 668 try 669 { 670 data.getAll(new String [] { "name1", "" }); 671 } 672 catch (IllegalArgumentException e) 673 { 674 caught = true; 675 } 676 if (caught == false) 677 fail("Excepted IllegalArgumentException for getAll and an empty key"); 678 679 caught = false; 680 try 681 { 682 data.getAll(new String [] { "name1", "wrong" }); 683 } 684 catch (IllegalArgumentException e) 685 { 686 caught = true; 687 } 688 if (caught == false) 689 fail("Excepted InvalidKeyException for getAll and an invalid key"); 690 } 691 } 692 | Popular Tags |