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