1 7 8 package test.compliance.modelmbean; 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.ArrayList; 15 import java.util.Arrays; 16 import java.util.HashMap; 17 import java.util.Iterator; 18 import java.util.List; 19 import java.util.Map; 20 import java.util.Set; 21 22 import junit.framework.TestCase; 23 24 import javax.management.RuntimeOperationsException; 25 import javax.management.modelmbean.DescriptorSupport; 26 27 34 public class DescriptorTEST 35 extends TestCase 36 { 37 public DescriptorTEST(String s) 38 { 39 super(s); 40 } 41 42 44 public void testDefaultConstructor() 45 throws Exception 46 { 47 DescriptorSupport descriptor = new DescriptorSupport(); 48 assertTrue("Should be empty", descriptor.getFields().length == 0); 49 boolean caught = false; 50 try 51 { 52 descriptor.isValid(); 53 } 54 catch (RuntimeOperationsException expected) 55 { 56 caught = true; 57 } 58 assertTrue("Should not be valid", caught); 59 } 60 61 public void testSizeConstructor() 62 throws Exception 63 { 64 DescriptorSupport descriptor = new DescriptorSupport(100); 65 assertTrue("Should be empty", descriptor.getFields().length == 0); 66 boolean caught = false; 67 try 68 { 69 descriptor.isValid(); 70 } 71 catch (RuntimeOperationsException expected) 72 { 73 caught = true; 74 } 75 assertTrue("Should not be valid", caught); 76 77 caught = false; 78 try 79 { 80 descriptor = new DescriptorSupport(-1); 81 } 82 catch (RuntimeOperationsException e) 83 { 84 caught = true; 85 } 86 assertTrue("Expected an exception for a negative size", caught); 87 } 88 89 public void testCopyConstructor() 90 throws Exception 91 { 92 DescriptorSupport descriptor = new DescriptorSupport((DescriptorSupport) null); 93 assertTrue("Should be empty", descriptor.getFields().length == 0); 94 boolean caught = false; 95 try 96 { 97 descriptor.isValid(); 98 } 99 catch (RuntimeOperationsException expected) 100 { 101 caught = true; 102 } 103 assertTrue("Should not be valid", caught); 104 105 descriptor = new DescriptorSupport(descriptor); 106 assertTrue("Should be empty", descriptor.getFields().length == 0); 107 caught = false; 108 try 109 { 110 descriptor.isValid(); 111 } 112 catch (RuntimeOperationsException expected) 113 { 114 caught = true; 115 } 116 assertTrue("Should not be valid", caught); 117 118 descriptor.setField("name", "testName"); 119 descriptor.setField("descriptorType", "testType"); 120 descriptor = new DescriptorSupport(descriptor); 121 assertTrue("Should be two fields", descriptor.getFields().length == 2); 122 assertEquals("testName", descriptor.getFieldValue("name")); 123 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 124 assertTrue("Should be valid", descriptor.isValid()); 125 } 126 127 public void testNamesValuesConstructor() 128 throws Exception 129 { 130 String[] names = new String[] { "name", "descriptorType" }; 131 Object[] values = new Object[] { "testName", "testType" }; 132 DescriptorSupport descriptor = new DescriptorSupport(names, values); 133 assertTrue("Should be two fields", descriptor.getFields().length == 2); 134 assertEquals("testName", descriptor.getFieldValue("name")); 135 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 136 assertTrue("Should be valid", descriptor.isValid()); 137 138 descriptor = new DescriptorSupport(new String[0], new Object[0]); 139 assertTrue("Should be empty", descriptor.getFields().length == 0); 140 boolean caught = false; 141 try 142 { 143 descriptor.isValid(); 144 } 145 catch (RuntimeOperationsException expected) 146 { 147 caught = true; 148 } 149 assertTrue("Should not be valid", caught); 150 151 caught = false; 152 try 153 { 154 descriptor = new DescriptorSupport(null, null); 155 } 156 catch (RuntimeOperationsException e) 157 { 158 caught = true; 159 } 160 assertTrue("Expected an exception for null names and values", caught); 161 162 caught = false; 163 try 164 { 165 descriptor = new DescriptorSupport(null, values); 166 } 167 catch (RuntimeOperationsException e) 168 { 169 caught = true; 170 } 171 assertTrue("Expected an exception for null names", caught); 172 173 caught = false; 174 try 175 { 176 descriptor = new DescriptorSupport(names, null); 177 } 178 catch (RuntimeOperationsException e) 179 { 180 caught = true; 181 } 182 assertTrue("Expected an exception for null values", caught); 183 184 Object[] tooManyValues = new Object[] { "testName", "testType", "tooMany" }; 185 caught = false; 186 try 187 { 188 descriptor = new DescriptorSupport(names, tooManyValues); 189 } 190 catch (RuntimeOperationsException e) 191 { 192 caught = true; 193 } 194 assertTrue("Expected an exception for too many values", caught); 195 196 Object[] tooFewValues = new Object[] { "testName" }; 197 caught = false; 198 try 199 { 200 descriptor = new DescriptorSupport(names, tooFewValues); 201 } 202 catch (RuntimeOperationsException e) 203 { 204 caught = true; 205 } 206 assertTrue("Expected an exception for too few values", caught); 207 208 String[] nullName = new String[] { "name", null }; 209 caught = false; 210 try 211 { 212 descriptor = new DescriptorSupport(nullName, values); 213 } 214 catch (RuntimeOperationsException e) 215 { 216 caught = true; 217 } 218 assertTrue("Expected an exception for null name", caught); 219 220 String[] emptyName = new String[] { "name", "" }; 221 caught = false; 222 try 223 { 224 descriptor = new DescriptorSupport(emptyName, values); 225 } 226 catch (RuntimeOperationsException e) 227 { 228 caught = true; 229 } 230 assertTrue("Expected an exception for empty name ", caught); 231 232 String[] notEmptyName = new String[] { "name", " " }; 234 descriptor = new DescriptorSupport(notEmptyName, values); 235 assertTrue("Should be two fields", descriptor.getFields().length == 2); 236 assertEquals("testName", descriptor.getFieldValue("name")); 237 assertEquals("testType", descriptor.getFieldValue(" ")); 238 caught = false; 239 try 240 { 241 descriptor.isValid(); 242 } 243 catch (RuntimeOperationsException expected) 244 { 245 caught = true; 246 } 247 assertTrue("Should not be valid", caught); 248 249 names = new String[] { "name", "descriptorType", "another" }; 250 values = new Object[] { "testName", "testType", null }; 251 descriptor = new DescriptorSupport(names, values); 252 assertTrue("Should be three fields", descriptor.getFields().length == 3); 253 assertEquals("testName", descriptor.getFieldValue("name")); 254 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 255 assertTrue("another should be null", descriptor.getFieldValue("another") == null); 256 assertTrue("Should be valid", descriptor.isValid()); 257 } 258 259 public void testNameEqualsValueConstructor() 260 throws Exception 261 { 262 String[] fields = new String[] { "name=testName", "descriptorType=testType" }; 263 DescriptorSupport descriptor = new DescriptorSupport(fields); 264 assertTrue("Should be two fields", descriptor.getFields().length == 2); 265 assertEquals("testName", descriptor.getFieldValue("name")); 266 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 267 assertTrue("Should be valid", descriptor.isValid()); 268 269 descriptor = new DescriptorSupport((String[]) null); 270 assertTrue("Should be empty", descriptor.getFields().length == 0); 271 boolean caught = false; 272 try 273 { 274 descriptor.isValid(); 275 } 276 catch (RuntimeOperationsException expected) 277 { 278 caught = true; 279 } 280 assertTrue("Should not be valid", caught); 281 282 descriptor = new DescriptorSupport(new String[0]); 283 assertTrue("Should be empty", descriptor.getFields().length == 0); 284 caught = false; 285 try 286 { 287 descriptor.isValid(); 288 } 289 catch (RuntimeOperationsException expected) 290 { 291 caught = true; 292 } 293 assertTrue("Should not be valid", caught); 294 295 String[] nullName = new String[] { "name=testName", "=rubbish" }; 296 caught = false; 297 try 298 { 299 descriptor = new DescriptorSupport(nullName); 300 } 301 catch (RuntimeOperationsException e) 302 { 303 caught = true; 304 } 305 assertTrue("Expected an exception for '=rubbish'", caught); 306 307 String[] notEmptyName = new String[] { "name=testName", " =rubbish" }; 309 descriptor = new DescriptorSupport(notEmptyName); 310 assertTrue("Should be two fields", descriptor.getFields().length == 2); 311 assertEquals("testName", descriptor.getFieldValue("name")); 312 assertEquals("rubbish", descriptor.getFieldValue(" ")); 313 caught = false; 314 try 315 { 316 descriptor.isValid(); 317 } 318 catch (RuntimeOperationsException expected) 319 { 320 caught = true; 321 } 322 assertTrue("Should not be valid", caught); 323 } 324 325 public void testNameEqualsValueConstructorError() 326 throws Exception 327 { 328 try 329 { 330 String[] fields = new String[] { "name=testName", "descriptorType=testType", "another=" }; 331 DescriptorSupport descriptor = new DescriptorSupport(fields); 332 assertTrue("Should be three fields", descriptor.getFields().length == 3); 333 assertEquals("testName", descriptor.getFieldValue("name")); 334 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 335 assertTrue("another should be null", descriptor.getFieldValue("another") == null); 336 assertTrue("Should be valid", descriptor.isValid()); 337 } 338 catch (Exception e) 339 { 340 fail("FAILS IN RI: 'another=' should be valid according to the javadoc " + e.toString()); 341 } 342 } 343 344 public void testGetFieldValue() 345 throws Exception 346 { 347 String[] fields = new String[] { "name=testName", "descriptorType=testType" }; 348 DescriptorSupport descriptor = new DescriptorSupport(fields); 349 assertEquals("testName", descriptor.getFieldValue("name")); 350 assertTrue("Field names are not case sensitive", "testName".equals(descriptor.getFieldValue("NAME"))); 351 assertTrue("Non existent field should be null", descriptor.getFieldValue("nonExistent") == null); 352 353 boolean caught = false; 354 try 355 { 356 descriptor.getFieldValue(null); 357 } 358 catch (RuntimeOperationsException e) 359 { 360 caught = true; 361 } 362 assertTrue("Expected an exception for null name", caught); 363 364 caught = false; 365 try 366 { 367 descriptor.getFieldValue(""); 368 } 369 catch (RuntimeOperationsException e) 370 { 371 caught = true; 372 } 373 assertTrue("Expected an exception for empty name", caught); 374 375 assertTrue("Blank field name is allowed", descriptor.getFieldValue(" ") == null); 377 } 378 379 public void testSetFieldValue() 380 throws Exception 381 { 382 String[] fields = new String[] { "name=testName", "descriptorType=testType" }; 383 DescriptorSupport descriptor = new DescriptorSupport(fields); 384 assertEquals("testName", descriptor.getFieldValue("name")); 385 descriptor.setField("name", "newName"); 386 assertEquals("newName", descriptor.getFieldValue("name")); 387 descriptor.setField("NAME", "newNAME"); 388 assertEquals("newNAME", descriptor.getFieldValue("name")); 389 390 boolean caught = false; 391 try 392 { 393 descriptor.setField(null, "null"); 394 } 395 catch (RuntimeOperationsException e) 396 { 397 caught = true; 398 } 399 assertTrue("Expected an exception for null name", caught); 400 401 caught = false; 402 try 403 { 404 descriptor.setField("", "empty"); 405 } 406 catch (RuntimeOperationsException e) 407 { 408 caught = true; 409 } 410 assertTrue("Expected an exception for empty name", caught); 411 412 descriptor.setField(" ", "blank"); 414 assertEquals("blank", descriptor.getFieldValue(" ")); 415 416 descriptor.setField("newField", "newValue"); 417 assertEquals("newValue", descriptor.getFieldValue("newField")); 418 } 419 420 public void testGetFields() 421 throws Exception 422 { 423 String[] fields = new String[] { "name=testName", "descriptorType=testType" }; 424 DescriptorSupport descriptor = new DescriptorSupport(fields); 425 String[] getFields = descriptor.getFields(); 426 compareFields(fields, getFields); 427 } 428 429 public void testGetFieldNames() 430 throws Exception 431 { 432 String[] fields = new String[] { "name=testName", "descriptorType=testType" }; 433 DescriptorSupport descriptor = new DescriptorSupport(fields); 434 String[] names = descriptor.getFieldNames(); 435 compareFieldNames(fields, names); 436 } 437 438 public void testGetFieldValues() 439 throws Exception 440 { 441 String[] names = new String[] { "name", "descriptorType" }; 442 String[] values = new String[] { "testName", "testType" }; 443 DescriptorSupport descriptor = new DescriptorSupport(names, values); 444 445 Object[] result = descriptor.getFieldValues(null); 446 compareFieldValuesUnsorted(values, result); 447 448 result = descriptor.getFieldValues(new String[0]); 449 compareFieldValues(new Object[0], result); 450 451 result = descriptor.getFieldValues(names); 452 compareFieldValues(values, result); 453 454 result = descriptor.getFieldValues(new String[] { "name" }); 455 compareFieldValues(new Object[] { "testName" }, result); 456 457 result = descriptor.getFieldValues(new String[] { "descriptorType", "name" }); 458 compareFieldValues(new Object[] { "testType", "testName" }, result); 459 460 result = descriptor.getFieldValues(new String[] { "NAME" }); 461 compareFieldValues(new Object[] { "testName" }, result); 462 463 result = descriptor.getFieldValues(new String[] { null }); 464 compareFieldValues(new Object[] { null }, result); 465 466 result = descriptor.getFieldValues(new String[] { "" }); 467 compareFieldValues(new Object[] { null }, result); 468 } 469 470 public void testSetFieldValues() 471 throws Exception 472 { 473 DescriptorSupport descriptor = new DescriptorSupport(); 474 String[] names = new String[] { "name", "descriptorType" }; 475 Object[] values = new Object[] { "testName", "testType" }; 476 descriptor.setFields(names, values); 477 assertTrue("Should be two fields", descriptor.getFields().length == 2); 478 assertEquals("testName", descriptor.getFieldValue("name")); 479 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 480 assertTrue("Should be valid", descriptor.isValid()); 481 482 descriptor = new DescriptorSupport(); 483 descriptor.setFields(new String[0], new Object[0]); 484 assertTrue("Should be empty", descriptor.getFields().length == 0); 485 boolean caught = false; 486 try 487 { 488 descriptor.isValid(); 489 } 490 catch (RuntimeOperationsException expected) 491 { 492 caught = true; 493 } 494 495 caught = false; 496 try 497 { 498 descriptor = new DescriptorSupport(); 499 descriptor.setFields(null, null); 500 } 501 catch (RuntimeOperationsException e) 502 { 503 caught = true; 504 } 505 assertTrue("Expected an exception for null names and values", caught); 506 507 caught = false; 508 try 509 { 510 descriptor = new DescriptorSupport(); 511 descriptor.setFields(null, values); 512 } 513 catch (RuntimeOperationsException e) 514 { 515 caught = true; 516 } 517 assertTrue("Expected an exception for null names", caught); 518 519 caught = false; 520 try 521 { 522 descriptor = new DescriptorSupport(); 523 descriptor.setFields(names, null); 524 } 525 catch (RuntimeOperationsException e) 526 { 527 caught = true; 528 } 529 assertTrue("Expected an exception for null values", caught); 530 531 Object[] tooManyValues = new Object[] { "testName", "testType", "tooMany" }; 532 caught = false; 533 try 534 { 535 descriptor = new DescriptorSupport(); 536 descriptor.setFields(names, tooManyValues); 537 } 538 catch (RuntimeOperationsException e) 539 { 540 caught = true; 541 } 542 assertTrue("Expected an exception for too many values", caught); 543 544 Object[] tooFewValues = new Object[] { "testName" }; 545 caught = false; 546 try 547 { 548 descriptor = new DescriptorSupport(); 549 descriptor.setFields(names, tooFewValues); 550 } 551 catch (RuntimeOperationsException e) 552 { 553 caught = true; 554 } 555 assertTrue("Expected an exception for too few values", caught); 556 557 String[] nullName = new String[] { "name", null }; 558 caught = false; 559 try 560 { 561 descriptor = new DescriptorSupport(); 562 descriptor.setFields(nullName, values); 563 } 564 catch (RuntimeOperationsException e) 565 { 566 caught = true; 567 } 568 assertTrue("Expected an exception for null name", caught); 569 570 String[] emptyName = new String[] { "name", "" }; 571 caught = false; 572 try 573 { 574 descriptor = new DescriptorSupport(); 575 descriptor.setFields(emptyName, values); 576 } 577 catch (RuntimeOperationsException e) 578 { 579 caught = true; 580 } 581 assertTrue("Expected an exception for empty name ", caught); 582 583 String[] notEmptyName = new String[] { "name", " " }; 585 descriptor = new DescriptorSupport(); 586 descriptor.setFields(notEmptyName, values); 587 assertTrue("Should be two fields", descriptor.getFields().length == 2); 588 assertEquals("testName", descriptor.getFieldValue("name")); 589 assertEquals("testType", descriptor.getFieldValue(" ")); 590 caught = false; 591 try 592 { 593 descriptor.isValid(); 594 } 595 catch (RuntimeOperationsException expected) 596 { 597 caught = true; 598 } 599 assertTrue("Should not be valid", caught); 600 601 names = new String[] { "name", "descriptorType", "another" }; 602 values = new Object[] { "testName", "testType", null }; 603 descriptor = new DescriptorSupport(); 604 descriptor.setFields(names, values); 605 assertTrue("Should be three fields", descriptor.getFields().length == 3); 606 assertEquals("testName", descriptor.getFieldValue("name")); 607 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 608 assertTrue("another should be null", descriptor.getFieldValue("another") == null); 609 assertTrue("Should be valid", descriptor.isValid()); 610 } 611 612 public void testClone() 613 throws Exception 614 { 615 String[] names = new String[] { "name", "descriptorType" }; 616 Object[] values = new Object[] { "testName", "testType" }; 617 DescriptorSupport descriptor = new DescriptorSupport(names, values); 618 619 DescriptorSupport clone = (DescriptorSupport) descriptor.clone(); 620 compareFields(descriptor.getFields(), clone.getFields()); 621 } 622 623 public void testRemove() 624 throws Exception 625 { 626 String[] names = new String[] { "name", "descriptorType" }; 627 Object[] values = new Object[] { "testName", "testType" }; 628 DescriptorSupport descriptor = new DescriptorSupport(names, values); 629 630 descriptor.removeField("name"); 631 assertTrue("Should be one field", descriptor.getFields().length == 1); 632 assertTrue("name should not be present", descriptor.getFieldValue("name") == null); 633 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 634 635 descriptor = new DescriptorSupport(names, values); 636 descriptor.removeField("NAME"); 637 assertTrue("Should be one field", descriptor.getFields().length == 1); 638 assertTrue("name should not be present", descriptor.getFieldValue("name") == null); 639 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 640 641 descriptor = new DescriptorSupport(names, values); 642 descriptor.removeField("notPresent"); 643 assertTrue("Should be two fields", descriptor.getFields().length == 2); 644 assertEquals("testName", descriptor.getFieldValue("name")); 645 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 646 647 descriptor.removeField(null); 648 assertTrue("Should be two fields", descriptor.getFields().length == 2); 649 assertEquals("testName", descriptor.getFieldValue("name")); 650 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 651 652 descriptor.removeField(""); 653 assertTrue("Should be two fields", descriptor.getFields().length == 2); 654 assertEquals("testName", descriptor.getFieldValue("name")); 655 assertEquals("testType", descriptor.getFieldValue("descriptorType")); 656 } 657 658 public void testIsValidMandatory() 659 throws Exception 660 { 661 String[] names = new String[] { "name", "descriptorType" }; 662 Object[] values = new Object[] { "testName", "testType" }; 663 validate(names, values, true); 664 665 names = new String[] { "name", "descriptorType" }; 666 values = new Object[] { null, "testType" }; 667 validate(names, values, false); 668 669 names = new String[] { "name", "descriptorType" }; 670 values = new Object[] { "", "testType" }; 671 validate(names, values, false); 672 673 names = new String[] { "name", "descriptorType" }; 674 values = new Object[] { "testName", null }; 675 validate(names, values, false); 676 677 names = new String[] { "name", "descriptorType" }; 678 values = new Object[] { "testName", "" }; 679 validate(names, values, false); 680 } 681 682 public void testIsValidRole() 683 throws Exception 684 { 685 doTestString("role", "operation"); 686 } 687 688 public void testIsValidGetMethod() 689 throws Exception 690 { 691 doTestString("getMethod", "getSomething"); 692 } 693 694 public void testIsValidSetMethod() 695 throws Exception 696 { 697 doTestString("setMethod", "setSomething"); 698 } 699 700 public void testIsValidPersistPeriod() 701 throws Exception 702 { 703 doTestInteger("persistPeriod"); 704 } 705 706 public void testIsValidCurrencyTimeLimit() 707 throws Exception 708 { 709 doTestInteger("currencyTimeLimit"); 710 } 711 712 public void testIsValidLastUpdateTimeStamp() 713 throws Exception 714 { 715 doTestInteger("lastUpdatedTimeStamp"); 716 } 717 718 public void testIsValidLog() 719 throws Exception 720 { 721 String[] names = new String[] { "name", "descriptorType", "log" }; 722 Object[] values = new Object[] { "testName", "testType", "true" }; 723 validate(names, values, true); 724 725 names = new String[] { "name", "descriptorType", "log" }; 726 values = new Object[] { "testName", "testType", "false" }; 727 validate(names, values, true); 728 729 names = new String[] { "name", "descriptorType", "log" }; 730 values = new Object[] { "testName", "testType", "t" }; 731 validate(names, values, true); 732 733 names = new String[] { "name", "descriptorType", "log" }; 734 values = new Object[] { "testName", "testType", "f" }; 735 validate(names, values, true); 736 737 names = new String[] { "name", "descriptorType", "log" }; 738 values = new Object[] { "testName", "testType", "TRUE" }; 739 validate(names, values, true); 740 741 names = new String[] { "name", "descriptorType", "log" }; 742 values = new Object[] { "testName", "testType", "FALSE" }; 743 validate(names, values, true); 744 745 names = new String[] { "name", "descriptorType", "log" }; 746 values = new Object[] { "testName", "testType", "T" }; 747 validate(names, values, true); 748 749 names = new String[] { "name", "descriptorType", "log" }; 750 values = new Object[] { "testName", "testType", "F" }; 751 validate(names, values, true); 752 753 names = new String[] { "name", "descriptorType", "log" }; 754 values = new Object[] { "testName", "testType", null }; 755 validate(names, values, false); 756 757 names = new String[] { "name", "descriptorType", "log" }; 758 values = new Object[] { "testName", "testType", "" }; 759 validate(names, values, false); 760 761 names = new String[] { "name", "descriptorType", "log" }; 762 values = new Object[] { "testName", "testType", "rubbish" }; 763 validate(names, values, false); 764 } 765 766 public void testIsValidVisibility() 767 throws Exception 768 { 769 String[] names = new String[] { "name", "descriptorType", "visibility" }; 770 Object[] values = new Object[] { "testName", "testType", "1" }; 771 validate(names, values, true); 772 773 names = new String[] { "name", "descriptorType", "visibility" }; 774 values = new Object[] { "testName", "testType", "2" }; 775 validate(names, values, true); 776 777 names = new String[] { "name", "descriptorType", "visibility" }; 778 values = new Object[] { "testName", "testType", "3" }; 779 validate(names, values, true); 780 781 names = new String[] { "name", "descriptorType", "visibility" }; 782 values = new Object[] { "testName", "testType", "4" }; 783 validate(names, values, true); 784 785 names = new String[] { "name", "descriptorType", "visibility" }; 786 values = new Object[] { "testName", "testType", "0" }; 787 validate(names, values, false); 788 789 names = new String[] { "name", "descriptorType", "visibility" }; 790 values = new Object[] { "testName", "testType", "5" }; 791 validate(names, values, false); 792 793 names = new String[] { "name", "descriptorType", "visibility" }; 794 values = new Object[] { "testName", "testType", new Integer(1) }; 795 validate(names, values, true); 796 797 names = new String[] { "name", "descriptorType", "visibility" }; 798 values = new Object[] { "testName", "testType", new Integer(2) }; 799 validate(names, values, true); 800 801 names = new String[] { "name", "descriptorType", "visibility" }; 802 values = new Object[] { "testName", "testType", new Integer(3) }; 803 validate(names, values, true); 804 805 names = new String[] { "name", "descriptorType", "visibility" }; 806 values = new Object[] { "testName", "testType", new Integer(4) }; 807 validate(names, values, true); 808 809 names = new String[] { "name", "descriptorType", "visibility" }; 810 values = new Object[] { "testName", "testType", new Integer(0) }; 811 validate(names, values, false); 812 813 names = new String[] { "name", "descriptorType", "visibility" }; 814 values = new Object[] { "testName", "testType", new Integer(6) }; 815 validate(names, values, false); 816 817 names = new String[] { "name", "descriptorType", "visibility" }; 818 values = new Object[] { "testName", "testType", null }; 819 validate(names, values, false); 820 821 names = new String[] { "name", "descriptorType", "visibility" }; 822 values = new Object[] { "testName", "testType", "" }; 823 validate(names, values, false); 824 } 825 826 public void testIsValidSeverity() 827 throws Exception 828 { 829 String[] names = new String[] { "name", "descriptorType", "severity" }; 830 Object[] values = new Object[] { "testName", "testType", "1" }; 831 validate(names, values, true); 832 833 names = new String[] { "name", "descriptorType", "severity" }; 834 values = new Object[] { "testName", "testType", "2" }; 835 validate(names, values, true); 836 837 names = new String[] { "name", "descriptorType", "severity" }; 838 values = new Object[] { "testName", "testType", "3" }; 839 validate(names, values, true); 840 841 names = new String[] { "name", "descriptorType", "severity" }; 842 values = new Object[] { "testName", "testType", "4" }; 843 validate(names, values, true); 844 845 names = new String[] { "name", "descriptorType", "severity" }; 846 values = new Object[] { "testName", "testType", "5" }; 847 validate(names, values, true); 848 849 names = new String[] { "name", "descriptorType", "severity" }; 850 values = new Object[] { "testName", "testType", new Integer(1) }; 851 validate(names, values, true); 852 853 names = new String[] { "name", "descriptorType", "severity" }; 854 values = new Object[] { "testName", "testType", new Integer(2) }; 855 validate(names, values, true); 856 857 names = new String[] { "name", "descriptorType", "severity" }; 858 values = new Object[] { "testName", "testType", new Integer(3) }; 859 validate(names, values, true); 860 861 names = new String[] { "name", "descriptorType", "severity" }; 862 values = new Object[] { "testName", "testType", new Integer(4) }; 863 validate(names, values, true); 864 865 names = new String[] { "name", "descriptorType", "severity" }; 866 values = new Object[] { "testName", "testType", new Integer(5) }; 867 validate(names, values, true); 868 869 names = new String[] { "name", "descriptorType", "severity" }; 870 values = new Object[] { "testName", "testType", null }; 871 validate(names, values, false); 872 873 names = new String[] { "name", "descriptorType", "severity" }; 874 values = new Object[] { "testName", "testType", "" }; 875 validate(names, values, false); 876 877 names = new String[] { "name", "descriptorType", "severity" }; 878 values = new Object[] { "testName", "testType", "0" }; 879 validate(names, values, false); 880 881 names = new String[] { "name", "descriptorType", "severity" }; 882 values = new Object[] { "testName", "testType", "7" }; 883 validate(names, values, false); 884 885 names = new String[] { "name", "descriptorType", "severity" }; 886 values = new Object[] { "testName", "testType", new Integer(0) }; 887 validate(names, values, false); 888 889 names = new String[] { "name", "descriptorType", "severity" }; 890 values = new Object[] { "testName", "testType", new Integer(7) }; 891 validate(names, values, false); 892 } 893 894 public void testIsValidError() 895 throws Exception 896 { 897 try 898 { 899 String[] names = new String[] { "name", "descriptorType", "severity" }; 900 Object[] values = new Object[] { "testName", "testType", "6" }; 901 validate(names, values, true); 902 903 names = new String[] { "name", "descriptorType", "severity" }; 904 values = new Object[] { "testName", "testType", new Integer(6) }; 905 validate(names, values, true); 906 } 907 catch (Exception e) 908 { 909 fail("FAILS IN RI: javadoc and spec are inconsistent on whether severity=6 is valid"); 910 } 911 } 912 913 public void testIsValidPersistPolicy() 914 throws Exception 915 { 916 String[] names = new String[] { "name", "descriptorType", "persistPolicy" }; 917 Object[] values = new Object[] { "testName", "testType", "onUpdate" }; 918 validate(names, values, true); 919 920 names = new String[] { "name", "descriptorType", "persistPolicy" }; 921 values = new Object[] { "testName", "testType", "noMoreOftenThan" }; 922 validate(names, values, true); 923 924 names = new String[] { "name", "descriptorType", "persistPolicy" }; 925 values = new Object[] { "testName", "testType", "never" }; 926 validate(names, values, true); 927 928 names = new String[] { "name", "descriptorType", "persistPolicy" }; 929 values = new Object[] { "testName", "testType", "onTimer" }; 930 validate(names, values, true); 931 932 names = new String[] { "name", "descriptorType", "persistPolicy" }; 933 values = new Object[] { "testName", "testType", null }; 934 validate(names, values, false); 935 936 names = new String[] { "name", "descriptorType", "persistPolicy" }; 937 values = new Object[] { "testName", "testType", "" }; 938 validate(names, values, false); 939 940 names = new String[] { "name", "descriptorType", "persistPolicy" }; 941 values = new Object[] { "testName", "testType", "rubbish" }; 942 validate(names, values, false); 943 } 944 945 public void testSerialization() 946 throws Exception 947 { 948 String[] names = new String[] { "name", "descriptorType" }; 949 Object[] values = new Object[] { "testName", "testType" }; 950 DescriptorSupport descriptor = new DescriptorSupport(names, values); 951 952 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 954 ObjectOutputStream oos = new ObjectOutputStream(baos); 955 oos.writeObject(descriptor); 956 957 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 959 ObjectInputStream ois = new ObjectInputStream(bais); 960 Object result = ois.readObject(); 961 962 compareFields(descriptor.getFields(), ((DescriptorSupport) result).getFields()); 963 } 964 965 967 private void doTestString(String field, String value) 968 throws Exception 969 { 970 String[] names = new String[] { "name", "descriptorType", field }; 971 Object[] values = new Object[] { "testName", "testType", value }; 972 validate(names, values, true); 973 974 names = new String[] { "name", "descriptorType", field }; 975 values = new Object[] { "testName", "testType", null }; 976 validate(names, values, false); 977 978 names = new String[] { "name", "descriptorType", field }; 979 values = new Object[] { "testName", "testType", "" }; 980 validate(names, values, false); 981 } 982 983 public void doTestInteger(String field) 984 { 985 String[] names = new String[] { "name", "descriptorType", field }; 986 Object[] values = new Object[] { "testName", "testType", "0" }; 987 validate(names, values, true); 988 989 names = new String[] { "name", "descriptorType", field }; 990 values = new Object[] { "testName", "testType", "-1" }; 991 validate(names, values, true); 992 993 names = new String[] { "name", "descriptorType", field }; 994 values = new Object[] { "testName", "testType", "100" }; 995 validate(names, values, true); 996 997 names = new String[] { "name", "descriptorType", field }; 998 values = new Object[] { "testName", "testType", "-2" }; 999 validate(names, values, false); 1000 1001 names = new String[] { "name", "descriptorType", field }; 1002 values = new Object[] { "testName", "testType", null }; 1003 validate(names, values, false); 1004 1005 names = new String[] { "name", "descriptorType", field }; 1006 values = new Object[] { "testName", "testType", "" }; 1007 validate(names, values, false); 1008 1009 names = new String[] { "name", "descriptorType", field }; 1010 values = new Object[] { "testName", "testType", "rubbish" }; 1011 validate(names, values, false); 1012 1013 names = new String[] { "name", "descriptorType", field }; 1014 values = new Object[] { "testName", "testType", new Integer(0) }; 1015 validate(names, values, true); 1016 1017 names = new String[] { "name", "descriptorType", field }; 1018 values = new Object[] { "testName", "testType", new Integer(-1) }; 1019 validate(names, values, true); 1020 1021 names = new String[] { "name", "descriptorType", field }; 1022 values = new Object[] { "testName", "testType", new Integer(100) }; 1023 validate(names, values, true); 1024 1025 names = new String[] { "name", "descriptorType", field }; 1026 values = new Object[] { "testName", "testType", new Integer(-2) }; 1027 validate(names, values, false); 1028 } 1029 1030 private void validate(String[] names, Object[] values, boolean valid) 1031 { 1032 DescriptorSupport descriptor = null; 1033 RuntimeOperationsException caught = null; 1034 try 1035 { 1036 descriptor = new DescriptorSupport(names, values); 1037 descriptor.isValid(); 1038 } 1039 catch (RuntimeOperationsException e) 1040 { 1041 caught = e; 1042 } 1043 if (valid && caught != null) 1044 throw caught; 1045 if (valid == false && caught == null) 1046 fail("Expected an exception for new Descriptor(String[], String[]) names=" + 1047 Arrays.asList(names) + " values=" + Arrays.asList(values)); 1048 1049 caught = null; 1050 try 1051 { 1052 String[] fields = new String[names.length]; 1053 for (int i = 0; i < fields.length; i++) 1054 { 1055 if (values[i] == null) 1056 fields[i] = names[i] + "="; 1057 else 1058 fields[i] = names[i] + "=" + values[i].toString(); 1059 } 1060 descriptor = new DescriptorSupport(names, values); 1061 descriptor.isValid(); 1062 } 1063 catch (RuntimeOperationsException e) 1064 { 1065 caught = e; 1066 } 1067 if (valid && caught != null) 1068 throw caught; 1069 if (valid == false && caught == null) 1070 fail("Expected an exception for new Descriptor(String[], String[]) names=" + 1071 Arrays.asList(names) + " values=" + Arrays.asList(values)); 1072 1073 caught = null; 1074 try 1075 { 1076 descriptor = new DescriptorSupport(); 1077 for (int i = 0; i < names.length; i++) 1078 descriptor.setField(names[i], values[i]); 1079 descriptor.isValid(); 1080 } 1081 catch (RuntimeOperationsException e) 1082 { 1083 caught = e; 1084 } 1085 if (valid && caught != null) 1086 throw caught; 1087 if (valid == false && caught == null) 1088 fail("Expected an exception for new Descriptor(String[], String[]) names=" + 1089 Arrays.asList(names) + " values=" + Arrays.asList(values)); 1090 1091 caught = null; 1092 try 1093 { 1094 descriptor = new DescriptorSupport(); 1095 descriptor.setFields(names, values); 1096 descriptor.isValid(); 1097 } 1098 catch (RuntimeOperationsException e) 1099 { 1100 caught = e; 1101 } 1102 if (valid && caught != null) 1103 throw caught; 1104 if (valid == false && caught == null) 1105 fail("Expected an exception for new Descriptor(String[], String[]) names=" + 1106 Arrays.asList(names) + " values=" + Arrays.asList(values)); 1107 } 1108 1109 private void compareFieldNames(String[] one, String[] two) 1110 throws Exception 1111 { 1112 Set setOne = makeMap(one).keySet(); 1113 List setTwo = new ArrayList(Arrays.asList(two)); 1114 for (Iterator i = setOne.iterator(); i.hasNext(); ) 1115 { 1116 Object key = i.next(); 1117 if (setTwo.remove(key) == false) 1118 fail("Expected " + Arrays.asList(two) + " to contain field " + key); 1119 } 1120 assertTrue("Didn't expect the following fields " + setTwo, setTwo.isEmpty()); 1121 } 1122 1123 private void compareFieldValuesUnsorted(Object[] one, Object[] two) 1124 throws Exception 1125 { 1126 if (one.length != two.length) 1127 fail("Lengths are different original=" + Arrays.asList(one) + " result=" + Arrays.asList(two)); 1128 1129 List listOne = Arrays.asList(one); 1130 List listTwo = new ArrayList(Arrays.asList(two)); 1131 for (Iterator i = listOne.iterator(); i.hasNext();) 1132 { 1133 Object value = i.next(); 1134 if (listTwo.remove(value) == false) 1135 fail("Expected " + two + " to contain " + value); 1136 } 1137 assertTrue("Didn't expect the following fields " + listTwo, listTwo.isEmpty()); 1138 } 1139 1140 private void compareFieldValues(Object[] one, Object[] two) 1141 throws Exception 1142 { 1143 if (one.length != two.length) 1144 fail("Lengths are different original=" + Arrays.asList(one) + " result=" + Arrays.asList(two)); 1145 for (int i = 0; i < one.length; i++) 1146 { 1147 if (one[i] == null && two[i] != null) 1148 fail("For index " + i + " original=" + one[i] + " result=" + two[i]); 1149 else if (one[i] != null && two[i] == null) 1150 fail("For index " + i + " original=" + one[i] + " result=" + two[i]); 1151 else if (one[i] != null && one[i].equals(two[i]) == false) 1152 fail("For index " + i + " original=" + one[i] + " result=" + two[i]); 1153 } 1154 } 1155 1156 private void compareFields(String[] one, String[] two) 1157 throws Exception 1158 { 1159 Map mapOne = makeMap(one); 1160 Map mapTwo = makeMap(two); 1161 for (Iterator i = mapOne.entrySet().iterator(); i.hasNext(); ) 1162 { 1163 Map.Entry entry = (Map.Entry) i.next(); 1164 Object key = entry.getKey(); 1165 Object value = entry.getValue(); 1166 if (value.equals(mapTwo.remove(key)) == false) 1167 fail("Expected " + Arrays.asList(two) + " to contain field " + key); 1168 } 1169 assertTrue("Didn't expect the following fields " + mapTwo, mapTwo.isEmpty()); 1170 } 1171 1172 private Map makeMap(String[] fields) 1173 throws Exception 1174 { 1175 HashMap result = new HashMap(fields.length); 1176 for (int i = 0; i < fields.length; i++) 1177 { 1178 int index = fields[i].indexOf("="); 1179 String key = fields[i].substring(0, index).toLowerCase(); 1180 String value = null; 1181 if (index != fields[i].length()-1) 1182 value = fields[i].substring(index); 1183 result.put(key, value); 1184 } 1185 return result; 1186 } 1187} 1188 | Popular Tags |