| 1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.beans.PropertyDescriptor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.beanutils.priv.PrivateBeanFactory; 29 import org.apache.commons.beanutils.priv.PrivateDirect; 30 31 import junit.framework.TestCase; 32 import junit.framework.Test; 33 import junit.framework.TestSuite; 34 35 36 65 66 public class PropertyUtilsTestCase extends TestCase { 67 68 69 71 72 76 private static final String PRIVATE_DIRECT_CLASS = 77 "org.apache.commons.beanutils.priv.PrivateDirect"; 78 79 80 84 private static final String PRIVATE_INDIRECT_CLASS = 85 "org.apache.commons.beanutils.priv.PrivateIndirect"; 86 87 88 91 private static final String TEST_BEAN_CLASS = 92 "org.apache.commons.beanutils.TestBean"; 93 94 95 98 protected TestBean bean = null; 99 100 101 104 protected TestBeanPackageSubclass beanPackageSubclass = null; 105 106 107 110 protected PrivateDirect beanPrivate = null; 111 112 113 116 protected PrivateDirect beanPrivateSubclass = null; 117 118 119 122 protected TestBeanPublicSubclass beanPublicSubclass = null; 123 124 125 128 protected String describes[] = 129 { "booleanProperty", 130 "booleanSecond", 131 "doubleProperty", 132 "floatProperty", 133 "intArray", 134 "intProperty", 136 "listIndexed", 137 "longProperty", 138 "nested", 142 "nullProperty", 143 "shortProperty", 145 "stringArray", 146 "stringProperty" 148 }; 149 150 151 156 protected final static String [] properties = { 157 "booleanProperty", 158 "booleanSecond", 159 "doubleProperty", 160 "dupProperty", 161 "floatProperty", 162 "intArray", 163 "intIndexed", 164 "intProperty", 165 "listIndexed", 166 "longProperty", 167 "nested", 168 "nullProperty", 169 "readOnlyProperty", 170 "shortProperty", 171 "stringArray", 172 "stringIndexed", 173 "stringProperty", 174 "writeOnlyProperty", 175 }; 176 177 178 180 181 186 public PropertyUtilsTestCase(String name) { 187 188 super(name); 189 190 } 191 192 193 195 196 199 public void setUp() { 200 201 bean = new TestBean(); 202 beanPackageSubclass = new TestBeanPackageSubclass(); 203 beanPrivate = PrivateBeanFactory.create(); 204 beanPrivateSubclass = PrivateBeanFactory.createSubclass(); 205 beanPublicSubclass = new TestBeanPublicSubclass(); 206 207 } 208 209 210 213 public static Test suite() { 214 215 return (new TestSuite(PropertyUtilsTestCase.class)); 216 217 } 218 219 220 223 public void tearDown() { 224 225 bean = null; 226 beanPackageSubclass = null; 227 beanPrivate = null; 228 beanPrivateSubclass = null; 229 beanPublicSubclass = null; 230 231 } 232 233 234 235 237 238 241 public void testCopyPropertiesMap() { 242 243 Map map = new HashMap (); 244 map.put("booleanProperty", Boolean.FALSE); 245 map.put("doubleProperty", new Double (333.0)); 246 map.put("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 247 map.put("floatProperty", new Float ((float) 222.0)); 248 map.put("intArray", new int[] { 0, 100, 200 }); 249 map.put("intProperty", new Integer (111)); 250 map.put("longProperty", new Long (444)); 251 map.put("shortProperty", new Short ((short) 555)); 252 map.put("stringProperty", "New String Property"); 253 254 try { 255 PropertyUtils.copyProperties(bean, map); 256 } catch (Throwable t) { 257 fail("Threw " + t.toString()); 258 } 259 260 assertEquals("booleanProperty", false, 262 bean.getBooleanProperty()); 263 assertEquals("doubleProperty", 333.0, 264 bean.getDoubleProperty(), 0.005); 265 assertEquals("floatProperty", (float) 222.0, 266 bean.getFloatProperty(), (float) 0.005); 267 assertEquals("intProperty", 111, 268 bean.getIntProperty()); 269 assertEquals("longProperty", (long) 444, 270 bean.getLongProperty()); 271 assertEquals("shortProperty", (short) 555, 272 bean.getShortProperty()); 273 assertEquals("stringProperty", "New String Property", 274 bean.getStringProperty()); 275 276 String dupProperty[] = bean.getDupProperty(); 278 assertNotNull("dupProperty present", dupProperty); 279 assertEquals("dupProperty length", 3, dupProperty.length); 280 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 281 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 282 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 283 int intArray[] = bean.getIntArray(); 284 assertNotNull("intArray present", intArray); 285 assertEquals("intArray length", 3, intArray.length); 286 assertEquals("intArray[0]", 0, intArray[0]); 287 assertEquals("intArray[1]", 100, intArray[1]); 288 assertEquals("intArray[2]", 200, intArray[2]); 289 290 } 291 292 293 296 public void testDescribe() { 297 298 Map map = null; 299 try { 300 map = PropertyUtils.describe(bean); 301 } catch (Exception e) { 302 fail("Threw exception " + e); 303 } 304 305 for (int i = 0; i < describes.length; i++) { 307 assertTrue("Property '" + describes[i] + "' is present", 308 map.containsKey(describes[i])); 309 } 310 assertTrue("Property 'writeOnlyProperty' is not present", 311 !map.containsKey("writeOnlyProperty")); 312 313 assertEquals("Value of 'booleanProperty'", 315 Boolean.TRUE, 316 (Boolean ) map.get("booleanProperty")); 317 assertEquals("Value of 'doubleProperty'", 318 new Double (321.0), 319 (Double ) map.get("doubleProperty")); 320 assertEquals("Value of 'floatProperty'", 321 new Float ((float) 123.0), 322 (Float ) map.get("floatProperty")); 323 assertEquals("Value of 'intProperty'", 324 new Integer (123), 325 (Integer ) map.get("intProperty")); 326 assertEquals("Value of 'longProperty'", 327 new Long (321), 328 (Long ) map.get("longProperty")); 329 assertEquals("Value of 'shortProperty'", 330 new Short ((short) 987), 331 (Short ) map.get("shortProperty")); 332 assertEquals("Value of 'stringProperty'", 333 "This is a string", 334 (String ) map.get("stringProperty")); 335 336 } 337 338 339 342 public void testGetDescriptorArguments() { 343 344 try { 345 PropertyUtils.getPropertyDescriptor(null, "stringProperty"); 346 fail("Should throw IllegalArgumentException 1"); 347 } catch (IllegalArgumentException e) { 348 ; } catch (Throwable t) { 350 fail("Threw " + t + " instead of IllegalArgumentException 1"); 351 } 352 353 try { 354 PropertyUtils.getPropertyDescriptor(bean, null); 355 fail("Should throw IllegalArgumentException 2"); 356 } catch (IllegalArgumentException e) { 357 ; } catch (Throwable t) { 359 fail("Threw " + t + " instead of IllegalArgumentException 2"); 360 } 361 362 } 363 364 365 368 public void testGetDescriptorBoolean() { 369 370 testGetDescriptorBase("booleanProperty", "getBooleanProperty", 371 "setBooleanProperty"); 372 373 } 374 375 376 379 public void testGetDescriptorDouble() { 380 381 testGetDescriptorBase("doubleProperty", "getDoubleProperty", 382 "setDoubleProperty"); 383 384 } 385 386 387 390 public void testGetDescriptorFloat() { 391 392 testGetDescriptorBase("floatProperty", "getFloatProperty", 393 "setFloatProperty"); 394 395 } 396 397 398 401 public void testGetDescriptorInt() { 402 403 testGetDescriptorBase("intProperty", "getIntProperty", 404 "setIntProperty"); 405 406 } 407 408 409 421 public void testGetDescriptorInvalidBoolean() throws Exception { 422 423 PropertyDescriptor pd = 424 PropertyUtils.getPropertyDescriptor(bean, "invalidBoolean"); 425 assertNotNull("invalidBoolean is a property", pd); 426 assertNotNull("invalidBoolean has a getter method", 427 pd.getReadMethod()); 428 assertNull("invalidBoolean has no write method", 429 pd.getWriteMethod()); 430 assertTrue("invalidBoolean getter method is isInvalidBoolean", 431 "isInvalidBoolean".equals(pd.getReadMethod().getName())); 432 433 } 434 435 436 439 public void testGetDescriptorLong() { 440 441 testGetDescriptorBase("longProperty", "getLongProperty", 442 "setLongProperty"); 443 444 } 445 446 447 451 public void testGetDescriptorReadOnly() { 452 453 testGetDescriptorBase("readOnlyProperty", "getReadOnlyProperty", 454 null); 455 456 } 457 458 459 463 public void testGetDescriptorSecond() { 464 465 testGetDescriptorBase("booleanSecond", "isBooleanSecond", 466 "setBooleanSecond"); 467 468 } 469 470 471 474 public void testGetDescriptorShort() { 475 476 testGetDescriptorBase("shortProperty", "getShortProperty", 477 "setShortProperty"); 478 479 } 480 481 482 485 public void testGetDescriptorString() { 486 487 testGetDescriptorBase("stringProperty", "getStringProperty", 488 "setStringProperty"); 489 490 } 491 492 493 496 public void testGetDescriptorUnknown() { 497 498 testGetDescriptorBase("unknown", null, null); 499 500 } 501 502 503 507 public void testGetDescriptorWriteOnly() { 508 509 testGetDescriptorBase("writeOnlyProperty", null, 510 "setWriteOnlyProperty"); 511 512 } 513 514 515 519 public void testGetDescriptors() { 520 521 PropertyDescriptor pd[] = 522 PropertyUtils.getPropertyDescriptors(bean); 523 assertNotNull("Got descriptors", pd); 524 int count[] = new int[properties.length]; 525 for (int i = 0; i < pd.length; i++) { 526 String name = pd[i].getName(); 527 for (int j = 0; j < properties.length; j++) { 528 if (name.equals(properties[j])) 529 count[j]++; 530 } 531 } 532 for (int j = 0; j < properties.length; j++) { 533 if (count[j] < 0) 534 fail("Missing property " + properties[j]); 535 else if (count[j] > 1) 536 fail("Duplicate property " + properties[j]); 537 } 538 539 } 540 541 542 545 public void testGetDescriptorsArguments() { 546 547 try { 548 PropertyUtils.getPropertyDescriptors(null); 549 fail("Should throw IllegalArgumentException"); 550 } catch (IllegalArgumentException e) { 551 ; } catch (Throwable t) { 553 fail("Threw " + t + " instead of IllegalArgumentException"); 554 } 555 556 } 557 558 559 562 public void testGetIndexedArguments() { 563 564 566 try { 567 PropertyUtils.getIndexedProperty(null, "intArray", 0); 568 fail("Should throw IllegalArgumentException 1"); 569 } catch (IllegalArgumentException e) { 570 ; } catch (Throwable t) { 572 fail("Threw " + t + " instead of IllegalArgumentException 1"); 573 } 574 575 try { 576 PropertyUtils.getIndexedProperty(bean, null, 0); 577 fail("Should throw IllegalArgumentException 2"); 578 } catch (IllegalArgumentException e) { 579 ; } catch (Throwable t) { 581 fail("Threw " + t + " instead of IllegalArgumentException 2"); 582 } 583 584 586 try { 587 PropertyUtils.getIndexedProperty(null, 588 "intArray[0]"); 589 fail("Should throw IllegalArgumentException 3"); 590 } catch (IllegalArgumentException e) { 591 ; } catch (Throwable t) { 593 fail("Threw " + t + " instead of IllegalArgumentException 3"); 594 } 595 596 try { 597 PropertyUtils.getIndexedProperty(bean, "[0]"); 598 fail("Should throw NoSuchMethodException 4"); 599 } catch (NoSuchMethodException e) { 600 ; } catch (Throwable t) { 602 fail("Threw " + t + " instead of NoSuchMethodException 4"); 603 } 604 605 try { 606 PropertyUtils.getIndexedProperty(bean, "intArray"); 607 fail("Should throw IllegalArgumentException 5"); 608 } catch (IllegalArgumentException e) { 609 ; } catch (Throwable t) { 611 fail("Threw " + t + " instead of IllegalArgumentException 5"); 612 } 613 614 616 try { 617 PropertyUtils.getIndexedProperty(null, "intIndexed", 0); 618 fail("Should throw IllegalArgumentException 1"); 619 } catch (IllegalArgumentException e) { 620 ; } catch (Throwable t) { 622 fail("Threw " + t + " instead of IllegalArgumentException 1"); 623 } 624 625 try { 626 PropertyUtils.getIndexedProperty(bean, null, 0); 627 fail("Should throw IllegalArgumentException 2"); 628 } catch (IllegalArgumentException e) { 629 ; } catch (Throwable t) { 631 fail("Threw " + t + " instead of IllegalArgumentException 2"); 632 } 633 634 636 try { 637 PropertyUtils.getIndexedProperty(null, 638 "intIndexed[0]"); 639 fail("Should throw IllegalArgumentException 3"); 640 } catch (IllegalArgumentException e) { 641 ; } catch (Throwable t) { 643 fail("Threw " + t + " instead of IllegalArgumentException 3"); 644 } 645 646 try { 647 PropertyUtils.getIndexedProperty(bean, "[0]"); 648 fail("Should throw NoSuchMethodException 4"); 649 } catch (NoSuchMethodException e) { 650 ; } catch (Throwable t) { 652 fail("Threw " + t + " instead of NoSuchMethodException 4"); 653 } 654 655 try { 656 PropertyUtils.getIndexedProperty(bean, "intIndexed"); 657 fail("Should throw IllegalArgumentException 5"); 658 } catch (IllegalArgumentException e) { 659 ; } catch (Throwable t) { 661 fail("Threw " + t + " instead of IllegalArgumentException 5"); 662 } 663 664 } 665 666 667 670 public void testGetIndexedValues() { 671 672 Object value = null; 673 674 676 for (int i = 0; i < 5; i++) { 677 678 try { 679 value = PropertyUtils.getIndexedProperty 680 (bean, "dupProperty", i); 681 assertNotNull("dupProperty returned value " + i, value); 682 assertTrue("dupProperty returned String " + i, 683 value instanceof String ); 684 assertEquals("dupProperty returned correct " + i, 685 "Dup " + i, 686 (String ) value); 687 } catch (Throwable t) { 688 fail("dupProperty " + i + " threw " + t); 689 } 690 691 try { 692 value = 693 PropertyUtils.getIndexedProperty(bean, "intArray", i); 694 assertNotNull("intArray returned value " + i, value); 695 assertTrue("intArray returned Integer " + i, 696 value instanceof Integer ); 697 assertEquals("intArray returned correct " + i, i * 10, 698 ((Integer ) value).intValue()); 699 } catch (Throwable t) { 700 fail("intArray " + i + " threw " + t); 701 } 702 703 try { 704 value = 705 PropertyUtils.getIndexedProperty(bean, "intIndexed", i); 706 assertNotNull("intIndexed returned value " + i, value); 707 assertTrue("intIndexed returned Integer " + i, 708 value instanceof Integer ); 709 assertEquals("intIndexed returned correct " + i, i * 10, 710 ((Integer ) value).intValue()); 711 } catch (Throwable t) { 712 fail("intIndexed " + i + " threw " + t); 713 } 714 715 try { 716 value = 717 PropertyUtils.getIndexedProperty(bean, "listIndexed", i); 718 assertNotNull("listIndexed returned value " + i, value); 719 assertTrue("list returned String " + i, 720 value instanceof String ); 721 assertEquals("listIndexed returned correct " + i, 722 "String " + i, (String ) value); 723 } catch (Throwable t) { 724 fail("listIndexed " + i + " threw " + t); 725 } 726 727 try { 728 value = 729 PropertyUtils.getIndexedProperty(bean, "stringArray", i); 730 assertNotNull("stringArray returned value " + i, value); 731 assertTrue("stringArray returned String " + i, 732 value instanceof String ); 733 assertEquals("stringArray returned correct " + i, 734 "String " + i, (String ) value); 735 } catch (Throwable t) { 736 fail("stringArray " + i + " threw " + t); 737 } 738 739 try { 740 value = 741 PropertyUtils.getIndexedProperty(bean, "stringIndexed", i); 742 assertNotNull("stringIndexed returned value " + i, value); 743 assertTrue("stringIndexed returned String " + i, 744 value instanceof String ); 745 assertEquals("stringIndexed returned correct " + i, 746 "String " + i, (String ) value); 747 } catch (Throwable t) { 748 fail("stringIndexed " + i + " threw " + t); 749 } 750 751 } 752 753 755 for (int i = 0; i < 5; i++) { 756 757 try { 758 value = PropertyUtils.getIndexedProperty 759 (bean, "dupProperty[" + i + "]"); 760 assertNotNull("dupProperty returned value " + i, value); 761 assertTrue("dupProperty returned String " + i, 762 value instanceof String ); 763 assertEquals("dupProperty returned correct " + i, 764 "Dup " + i, 765 (String ) value); 766 } catch (Throwable t) { 767 fail("dupProperty " + i + " threw " + t); 768 } 769 770 try { 771 value = 772 PropertyUtils.getIndexedProperty(bean, 773 "intArray[" + i + "]"); 774 assertNotNull("intArray returned value " + i, value); 775 assertTrue("intArray returned Integer " + i, 776 value instanceof Integer ); 777 assertEquals("intArray returned correct " + i, i * 10, 778 ((Integer ) value).intValue()); 779 } catch (Throwable t) { 780 fail("intArray " + i + " threw " + t); 781 } 782 783 try { 784 value = 785 PropertyUtils.getIndexedProperty(bean, 786 "intIndexed[" + i + "]"); 787 assertNotNull("intIndexed returned value " + i, value); 788 assertTrue("intIndexed returned Integer " + i, 789 value instanceof Integer ); 790 assertEquals("intIndexed returned correct " + i, i * 10, 791 ((Integer ) value).intValue()); 792 } catch (Throwable t) { 793 fail("intIndexed " + i + " threw " + t); 794 } 795 796 try { 797 value = 798 PropertyUtils.getIndexedProperty(bean, 799 "listIndexed[" + i + "]"); 800 assertNotNull("listIndexed returned value " + i, value); 801 assertTrue("listIndexed returned String " + i, 802 value instanceof String ); 803 assertEquals("listIndexed returned correct " + i, 804 "String " + i, (String ) value); 805 } catch (Throwable t) { 806 fail("listIndexed " + i + " threw " + t); 807 } 808 809 try { 810 value = 811 PropertyUtils.getIndexedProperty(bean, 812 "stringArray[" + i + "]"); 813 assertNotNull("stringArray returned value " + i, value); 814
|