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