1 16 17 package org.apache.commons.beanutils; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import junit.framework.TestCase; 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 32 38 39 public class DynaBeanUtilsTestCase extends TestCase { 40 41 42 44 45 48 protected DynaBean bean = null; 49 50 51 54 protected TestBean nested = null; 55 56 57 60 protected String describes[] = 61 { "booleanProperty", 62 "booleanSecond", 63 "byteProperty", 64 "doubleProperty", 65 "dupProperty", 66 "floatProperty", 67 "intArray", 68 "intIndexed", 69 "intProperty", 70 "listIndexed", 71 "longProperty", 72 "mapProperty", 73 "mappedProperty", 74 "mappedIntProperty", 75 "nested", 76 "nullProperty", 77 "shortProperty", 79 "stringArray", 80 "stringIndexed", 81 "stringProperty" 82 }; 83 84 85 87 88 93 public DynaBeanUtilsTestCase(String name) { 94 95 super(name); 96 97 } 98 99 100 102 103 106 public void setUp() throws Exception { 107 108 DynaClass dynaClass = createDynaClass(); 110 bean = dynaClass.newInstance(); 111 112 bean.set("booleanProperty", new Boolean (true)); 114 bean.set("booleanSecond", new Boolean (true)); 115 bean.set("byteProperty", new Byte ((byte) 121)); 116 bean.set("doubleProperty", new Double (321.0)); 117 bean.set("floatProperty", new Float ((float) 123.0)); 118 String dupProperty[] = { "Dup 0", "Dup 1", "Dup 2", "Dup 3", "Dup 4"}; 119 bean.set("dupProperty", dupProperty); 120 int intArray[] = { 0, 10, 20, 30, 40 }; 121 bean.set("intArray", intArray); 122 int intIndexed[] = { 0, 10, 20, 30, 40 }; 123 bean.set("intIndexed", intIndexed); 124 bean.set("intProperty", new Integer (123)); 125 List listIndexed = new ArrayList (); 126 listIndexed.add("String 0"); 127 listIndexed.add("String 1"); 128 listIndexed.add("String 2"); 129 listIndexed.add("String 3"); 130 listIndexed.add("String 4"); 131 bean.set("listIndexed", listIndexed); 132 bean.set("longProperty", new Long ((long) 321)); 133 HashMap mapProperty = new HashMap (); 134 mapProperty.put("First Key", "First Value"); 135 mapProperty.put("Second Key", "Second Value"); 136 bean.set("mapProperty", mapProperty); 137 HashMap mappedProperty = new HashMap (); 138 mappedProperty.put("First Key", "First Value"); 139 mappedProperty.put("Second Key", "Second Value"); 140 bean.set("mappedProperty", mappedProperty); 141 HashMap mappedIntProperty = new HashMap (); 142 mappedIntProperty.put("One", new Integer (1)); 143 mappedIntProperty.put("Two", new Integer (2)); 144 bean.set("mappedIntProperty", mappedIntProperty); 145 nested = new TestBean(); 146 bean.set("nested", nested); 147 bean.set("shortProperty", new Short ((short) 987)); 149 String stringArray[] = 150 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 151 bean.set("stringArray", stringArray); 152 String stringIndexed[] = 153 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 154 bean.set("stringIndexed", stringIndexed); 155 bean.set("stringProperty", "This is a string"); 156 157 } 158 159 160 163 public static Test suite() { 164 165 return (new TestSuite(DynaBeanUtilsTestCase.class)); 166 167 } 168 169 170 173 public void tearDown() { 174 175 bean = null; 176 nested = null; 177 178 } 179 180 181 182 184 187 public void testCloneDynaBean() { 188 189 DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); 191 DynaBean orig = null; 192 try { 193 orig = dynaClass.newInstance(); 194 } catch (Exception e) { 195 fail("newInstance(): " + e); 196 } 197 orig.set("booleanProperty", Boolean.FALSE); 198 orig.set("byteProperty", new Byte ((byte)111)); 199 orig.set("doubleProperty", new Double (333.33)); 200 orig.set("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 201 orig.set("intArray", new int[] { 100, 200, 300 }); 202 orig.set("intProperty", new Integer (333)); 203 orig.set("longProperty", new Long (3333)); 204 orig.set("shortProperty", new Short ((short) 33)); 205 orig.set("stringArray", new String [] { "New 0", "New 1" }); 206 orig.set("stringProperty", "Custom string"); 207 208 DynaBean clonedBean = null; 210 try { 211 clonedBean = (DynaBean) BeanUtils.cloneBean(orig); 212 } catch (Exception e) { 213 fail("Threw exception: " + e); 214 } 215 216 assertEquals("Cloned boolean property", 218 false, 219 ((Boolean ) clonedBean.get("booleanProperty")).booleanValue()); 220 assertEquals("Cloned byte property", 221 (byte) 111, 222 ((Byte ) clonedBean.get("byteProperty")).byteValue()); 223 assertEquals("Cloned double property", 224 333.33, 225 ((Double ) clonedBean.get("doubleProperty")).doubleValue(), 226 0.005); 227 assertEquals("Cloned int property", 228 333, 229 ((Integer ) clonedBean.get("intProperty")).intValue()); 230 assertEquals("Cloned long property", 231 (long) 3333, 232 ((Long ) clonedBean.get("longProperty")).longValue()); 233 assertEquals("Cloned short property", 234 (short) 33, 235 ((Short ) clonedBean.get("shortProperty")).shortValue()); 236 assertEquals("Cloned string property", 237 "Custom string", 238 (String ) clonedBean.get("stringProperty")); 239 240 String dupProperty[] = (String []) clonedBean.get("dupProperty"); 242 assertNotNull("dupProperty present", dupProperty); 243 assertEquals("dupProperty length", 3, dupProperty.length); 244 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 245 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 246 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 247 int intArray[] = (int[]) clonedBean.get("intArray"); 248 assertNotNull("intArray present", intArray); 249 assertEquals("intArray length", 3, intArray.length); 250 assertEquals("intArray[0]", 100, intArray[0]); 251 assertEquals("intArray[1]", 200, intArray[1]); 252 assertEquals("intArray[2]", 300, intArray[2]); 253 String stringArray[] = (String []) clonedBean.get("stringArray"); 254 assertNotNull("stringArray present", stringArray); 255 assertEquals("stringArray length", 2, stringArray.length); 256 assertEquals("stringArray[0]", "New 0", stringArray[0]); 257 assertEquals("stringArray[1]", "New 1", stringArray[1]); 258 259 } 260 261 264 public void testCopyPropertiesDynaBean() { 265 266 DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); 268 DynaBean orig = null; 269 try { 270 orig = dynaClass.newInstance(); 271 } catch (Exception e) { 272 fail("newInstance(): " + e); 273 } 274 orig.set("booleanProperty", Boolean.FALSE); 275 orig.set("byteProperty", new Byte ((byte)111)); 276 orig.set("doubleProperty", new Double (333.33)); 277 orig.set("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 278 orig.set("intArray", new int[] { 100, 200, 300 }); 279 orig.set("intProperty", new Integer (333)); 280 orig.set("longProperty", new Long (3333)); 281 orig.set("shortProperty", new Short ((short) 33)); 282 orig.set("stringArray", new String [] { "New 0", "New 1" }); 283 orig.set("stringProperty", "Custom string"); 284 285 try { 287 BeanUtils.copyProperties(bean, orig); 288 } catch (Exception e) { 289 fail("Threw exception: " + e); 290 } 291 292 assertEquals("Copied boolean property", 294 false, 295 ((Boolean ) bean.get("booleanProperty")).booleanValue()); 296 assertEquals("Copied byte property", 297 (byte) 111, 298 ((Byte ) bean.get("byteProperty")).byteValue()); 299 assertEquals("Copied double property", 300 333.33, 301 ((Double ) bean.get("doubleProperty")).doubleValue(), 302 0.005); 303 assertEquals("Copied int property", 304 333, 305 ((Integer ) bean.get("intProperty")).intValue()); 306 assertEquals("Copied long property", 307 (long) 3333, 308 ((Long ) bean.get("longProperty")).longValue()); 309 assertEquals("Copied short property", 310 (short) 33, 311 ((Short ) bean.get("shortProperty")).shortValue()); 312 assertEquals("Copied string property", 313 "Custom string", 314 (String ) bean.get("stringProperty")); 315 316 String dupProperty[] = (String []) bean.get("dupProperty"); 318 assertNotNull("dupProperty present", dupProperty); 319 assertEquals("dupProperty length", 3, dupProperty.length); 320 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 321 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 322 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 323 int intArray[] = (int[]) bean.get("intArray"); 324 assertNotNull("intArray present", intArray); 325 assertEquals("intArray length", 3, intArray.length); 326 assertEquals("intArray[0]", 100, intArray[0]); 327 assertEquals("intArray[1]", 200, intArray[1]); 328 assertEquals("intArray[2]", 300, intArray[2]); 329 String stringArray[] = (String []) bean.get("stringArray"); 330 assertNotNull("stringArray present", stringArray); 331 assertEquals("stringArray length", 2, stringArray.length); 332 assertEquals("stringArray[0]", "New 0", stringArray[0]); 333 assertEquals("stringArray[1]", "New 1", stringArray[1]); 334 335 } 336 337 338 341 public void testCopyPropertiesMap() { 342 343 Map map = new HashMap (); 344 map.put("booleanProperty", "false"); 345 map.put("byteProperty", "111"); 346 map.put("doubleProperty", "333.0"); 347 map.put("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 348 map.put("floatProperty", "222.0"); 349 map.put("intArray", new String [] { "0", "100", "200" }); 350 map.put("intProperty", "111"); 351 map.put("longProperty", "444"); 352 map.put("shortProperty", "555"); 353 map.put("stringProperty", "New String Property"); 354 355 try { 356 BeanUtils.copyProperties(bean, map); 357 } catch (Throwable t) { 358 fail("Threw " + t.toString()); 359 } 360 361 assertEquals("booleanProperty", false, 363 ((Boolean ) bean.get("booleanProperty")).booleanValue()); 364 assertEquals("byteProperty", (byte) 111, 365 ((Byte ) bean.get("byteProperty")).byteValue()); 366 assertEquals("doubleProperty", 333.0, 367 ((Double ) bean.get("doubleProperty")).doubleValue(), 368 0.005); 369 assertEquals("floatProperty", (float) 222.0, 370 ((Float ) bean.get("floatProperty")).floatValue(), 371 (float) 0.005); 372 assertEquals("intProperty", 111, 373 ((Integer ) bean.get("intProperty")).intValue()); 374 assertEquals("longProperty", (long) 444, 375 ((Long ) bean.get("longProperty")).longValue()); 376 assertEquals("shortProperty", (short) 555, 377 ((Short ) bean.get("shortProperty")).shortValue()); 378 assertEquals("stringProperty", "New String Property", 379 (String ) bean.get("stringProperty")); 380 381 String dupProperty[] = (String []) bean.get("dupProperty"); 383 assertNotNull("dupProperty present", dupProperty); 384 assertEquals("dupProperty length", 3, dupProperty.length); 385 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 386 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 387 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 388 int intArray[] = (int[]) bean.get("intArray"); 389 assertNotNull("intArray present", intArray); 390 assertEquals("intArray length", 3, intArray.length); 391 assertEquals("intArray[0]", 0, intArray[0]); 392 assertEquals("intArray[1]", 100, intArray[1]); 393 assertEquals("intArray[2]", 200, intArray[2]); 394 395 } 396 397 398 401 public void testCopyPropertiesStandard() { 402 403 TestBean orig = new TestBean(); 405 orig.setBooleanProperty(false); 406 orig.setByteProperty((byte) 111); 407 orig.setDoubleProperty(333.33); 408 orig.setDupProperty(new String [] { "New 0", "New 1", "New 2" }); 409 orig.setIntArray(new int[] { 100, 200, 300 }); 410 orig.setIntProperty(333); 411 orig.setLongProperty(3333); 412 orig.setShortProperty((short) 33); 413 orig.setStringArray(new String [] { "New 0", "New 1" }); 414 orig.setStringProperty("Custom string"); 415 416 try { 418 BeanUtils.copyProperties(bean, orig); 419 } catch (Exception e) { 420 fail("Threw exception: " + e); 421 } 422 423 assertEquals("Copied boolean property", 425 false, 426 ((Boolean ) bean.get("booleanProperty")).booleanValue()); 427 assertEquals("Copied byte property", 428 (byte) 111, 429 ((Byte ) bean.get("byteProperty")).byteValue()); 430 assertEquals("Copied double property", 431 333.33, 432 ((Double ) bean.get("doubleProperty")).doubleValue(), 433 0.005); 434 assertEquals("Copied int property", 435 333, 436 ((Integer ) bean.get("intProperty")).intValue()); 437 assertEquals("Copied long property", 438 (long) 3333, 439 ((Long ) bean.get("longProperty")).longValue()); 440 assertEquals("Copied short property", 441 (short) 33, 442 ((Short ) bean.get("shortProperty")).shortValue()); 443 assertEquals("Copied string property", 444 "Custom string", 445 (String ) bean.get("stringProperty")); 446 447 String dupProperty[] = (String []) bean.get("dupProperty"); 449 assertNotNull("dupProperty present", dupProperty); 450 assertEquals("dupProperty length", 3, dupProperty.length); 451 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 452 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 453 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 454 int intArray[] = (int[]) bean.get("intArray"); 455 assertNotNull("intArray present", intArray); 456 assertEquals("intArray length", 3, intArray.length); 457 assertEquals("intArray[0]", 100, intArray[0]); 458 assertEquals("intArray[1]", 200, intArray[1]); 459 assertEquals("intArray[2]", 300, intArray[2]); 460 String stringArray[] = (String []) bean.get("stringArray"); 461 assertNotNull("stringArray present", stringArray); 462 assertEquals("stringArray length", 2, stringArray.length); 463 assertEquals("stringArray[0]", "New 0", stringArray[0]); 464 assertEquals("stringArray[1]", "New 1", stringArray[1]); 465 466 } 467 468 469 472 public void testDescribe() { 473 474 Map map = null; 475 try { 476 map = PropertyUtils.describe(bean); 477 } catch (Exception e) { 478 fail("Threw exception " + e); 479 } 480 481 for (int i = 0; i < describes.length; i++) { 483 assertTrue("Property '" + describes[i] + "' is present", 484 map.containsKey(describes[i])); 485 } 486 assertTrue("Property 'writeOnlyProperty' is not present", 487 !map.containsKey("writeOnlyProperty")); 488 489 assertEquals("Value of 'booleanProperty'", 491 Boolean.TRUE, 492 (Boolean ) map.get("booleanProperty")); 493 assertEquals("Value of 'byteProperty'", 494 new Byte ((byte) 121), 495 (Byte ) map.get("byteProperty")); 496 assertEquals("Value of 'doubleProperty'", 497 new Double (321.0), 498 (Double ) map.get("doubleProperty")); 499 assertEquals("Value of 'floatProperty'", 500 new Float ((float) 123.0), 501 (Float ) map.get("floatProperty")); 502 assertEquals("Value of 'intProperty'", 503 new Integer (123), 504 (Integer ) map.get("intProperty")); 505 assertEquals("Value of 'longProperty'", 506 new Long (321), 507 (Long ) map.get("longProperty")); 508 assertEquals("Value of 'shortProperty'", 509 new Short ((short) 987), 510 (Short ) map.get("shortProperty")); 511 assertEquals("Value of 'stringProperty'", 512 "This is a string", 513 (String ) map.get("stringProperty")); 514 515 } 516 517 518 521 public void testPopulateArrayProperties() { 522 523 try { 524 525 HashMap map = new HashMap (); 526 String intArrayIn[] = new String [] { "123", "456", "789" }; 528 map.put("intArray", intArrayIn); 529 String stringArray[] = new String [] 530 { "New String 0", "New String 1" }; 531 map.put("stringArray", stringArray); 532 533 BeanUtils.populate(bean, map); 534 535 int intArray[] = (int[]) bean.get("intArray"); 536 assertNotNull("intArray is present", intArray); 537 assertEquals("intArray length", 538 3, intArray.length); 539 assertEquals("intArray[0]", 123, intArray[0]); 540 assertEquals("intArray[1]", 456, intArray[1]); 541 assertEquals("intArray[2]", 789, intArray[2]); 542 stringArray = (String []) bean.get("stringArray"); 543 assertNotNull("stringArray is present", stringArray); 544 assertEquals("stringArray length", 2, stringArray.length); 545 assertEquals("stringArray[0]", "New String 0", stringArray[0]); 546 assertEquals("stringArray[1]", "New String 1", stringArray[1]); 547 548 } catch (IllegalAccessException e) { 549 fail("IllegalAccessException"); 550 } catch (InvocationTargetException e) { 551 fail("InvocationTargetException"); 552 } 553 554 } 555 556 557 560 public void testGetArrayProperty() { 561 try { 562 String arr[] = BeanUtils.getArrayProperty(bean, "stringArray"); 563 String comp[] = (String []) bean.get("stringArray"); 564 565 assertTrue("String array length = " + comp.length, 566 (comp.length == arr.length)); 567 568 arr = BeanUtils.getArrayProperty(bean, "intArray"); 569 int iarr[] = (int[]) bean.get("intArray"); 570 571 assertTrue("String array length = " + iarr.length, 572 (iarr.length == arr.length)); 573 } catch (IllegalAccessException e) { 574 fail("IllegalAccessException"); 575 } catch (InvocationTargetException e) { 576 fail("InvocationTargetException"); 577 } catch (NoSuchMethodException e) { 578 fail("NoSuchMethodException"); 579 } 580 581 } 582 583 584 587 public void testGetIndexedProperty1() { 588 try { 589 String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]"); 590 String comp = String.valueOf(bean.get("intIndexed", 3)); 591 assertTrue("intIndexed[3] == " + comp, val.equals(comp)); 592 593 val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]"); 594 comp = (String ) bean.get("stringIndexed", 3); 595 assertTrue("stringIndexed[3] == " + comp, val.equals(comp)); 596 } catch (IllegalAccessException e) { 597 fail("IllegalAccessException"); 598 } catch (InvocationTargetException e) { 599 fail("InvocationTargetException"); 600 } catch (NoSuchMethodException e) { 601 fail("NoSuchMethodException"); 602 } 603 } 604 605 606 609 public void testGetIndexedProperty2() { 610 try { 611 String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3); 612 String comp = String.valueOf(bean.get("intIndexed", 3)); 613 614 assertTrue("intIndexed,3 == " + comp, val.equals(comp)); 615 616 val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3); 617 comp = (String ) bean.get("stringIndexed", 3); 618 619 assertTrue("stringIndexed,3 == " + comp, val.equals(comp)); 620 621 } catch (IllegalAccessException e) { 622 fail("IllegalAccessException"); 623 } catch (InvocationTargetException e) { 624 fail("InvocationTargetException"); 625 } catch (NoSuchMethodException e) { 626 fail("NoSuchMethodException"); 627 } 628 } 629 630 631 634 public void testGetNestedProperty() { 635 try { 636 String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty"); 637 String comp = nested.getStringProperty(); 638 assertTrue("nested.StringProperty == " + comp, 639 val.equals(comp)); 640 } catch (IllegalAccessException e) { 641 fail("IllegalAccessException"); 642 } catch (InvocationTargetException e) { 643 fail("InvocationTargetException"); 644 } catch (NoSuchMethodException e) { 645 fail("NoSuchMethodException"); 646 } 647 } 648 649 650 653 public void testGetGeneralProperty() { 654 try { 655 String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]"); 656 String comp = String.valueOf(bean.get("intIndexed", 2)); 657 658 assertTrue("nested.intIndexed[2] == " + comp, 659 val.equals(comp)); 660 } catch (IllegalAccessException e) { 661 fail("IllegalAccessException"); 662 } catch (InvocationTargetException e) { 663 fail("InvocationTargetException"); 664 } catch (NoSuchMethodException e) { 665 fail("NoSuchMethodException"); 666 } 667 } 668 669 670 673 public void testGetSimpleProperty() { 674 try { 675 String val = BeanUtils.getSimpleProperty(bean, "shortProperty"); 676 String comp = String.valueOf(bean.get("shortProperty")); 677 678 assertTrue("shortProperty == " + comp, 679 val.equals(comp)); 680 } catch (IllegalAccessException e) { 681 fail("IllegalAccessException"); 682 } catch (InvocationTargetException e) { 683 fail("InvocationTargetException"); 684 } catch (NoSuchMethodException e) { 685 fail("NoSuchMethodException"); 686 } 687 } 688 689 690 693 public void testPopulateArrayElements() { 694 695 try { 696 697 HashMap map = new HashMap (); 698 map.put("intIndexed[0]", "100"); 699 map.put("intIndexed[2]", "120"); 700 map.put("intIndexed[4]", "140"); 701 702 BeanUtils.populate(bean, map); 703 Integer intIndexed0 = (Integer ) bean.get("intIndexed", 0); 704 assertEquals("intIndexed[0] is 100", 705 100, intIndexed0.intValue()); 706 Integer intIndexed1 = (Integer ) bean.get("intIndexed", 1); 707 assertEquals("intIndexed[1] is 10", 708 10, intIndexed1.intValue()); 709 Integer intIndexed2 = (Integer ) bean.get("intIndexed", 2); 710 assertEquals("intIndexed[2] is 120", 711 120, intIndexed2.intValue()); 712 Integer intIndexed3 = (Integer ) bean.get("intIndexed", 3); 713 assertEquals("intIndexed[3] is 30", 714 30, intIndexed3.intValue()); 715 Integer intIndexed4 = (Integer ) bean.get("intIndexed", 4); 716 assertEquals("intIndexed[4] is 140", 717 140, intIndexed4.intValue()); 718 719 map.clear(); 720 map.put("stringIndexed[1]", "New String 1"); 721 map.put("stringIndexed[3]", "New String 3"); 722 723 BeanUtils.populate(bean, map); 724 725 assertEquals("stringIndexed[0] is \"String 0\"", 726 "String 0", 727 (String ) bean.get("stringIndexed", 0)); 728 assertEquals("stringIndexed[1] is \"New String 1\"", 729 "New String 1", 730 (String ) bean.get("stringIndexed", 1)); 731 assertEquals("stringIndexed[2] is \"String 2\"", 732 "String 2", 733 (String ) bean.get("stringIndexed", 2)); 734 assertEquals("stringIndexed[3] is \"New String 3\"", 735 "New String 3", 736 (String ) bean.get("stringIndexed", 3)); 737 assertEquals("stringIndexed[4] is \"String 4\"", 738 "String 4", 739 (String ) bean.get("stringIndexed", 4)); 740 741 } catch (IllegalAccessException e) { 742 fail("IllegalAccessException"); 743 } catch (InvocationTargetException e) { 744 fail("InvocationTargetException"); 745 } 746 747 } 748 749 750 753 public void testPopulateMapped() { 754 755 try { 756 757 HashMap map = new HashMap (); 758 map.put("mappedProperty(First Key)", "New First Value"); 759 map.put("mappedProperty(Third Key)", "New Third Value"); 760 761 BeanUtils.populate(bean, map); 762 763 assertEquals("mappedProperty(First Key)", 764 "New First Value", 765 (String ) bean.get("mappedProperty", "First Key")); 766 assertEquals("mappedProperty(Second Key)", 767 "Second Value", 768 (String ) bean.get("mappedProperty", "Second Key")); 769 assertEquals("mappedProperty(Third Key)", 770 "New Third Value", 771 (String ) bean.get("mappedProperty", "Third Key")); 772 assertNull("mappedProperty(Fourth Key", 773 (String ) bean.get("mappedProperty", "Fourth Key")); 774 775 } catch (IllegalAccessException e) { 776 fail("IllegalAccessException"); 777 } catch (InvocationTargetException e) { 778 fail("InvocationTargetException"); 779 } 780 781 } 782 783 784 787 public void testPopulateNested() { 788 789 try { 790 791 HashMap map = new HashMap (); 792 map.put("nested.booleanProperty", "false"); 793 map.put("nested.doubleProperty", "432.0"); 795 map.put("nested.intProperty", "543"); 797 map.put("nested.shortProperty", "654"); 799 801 BeanUtils.populate(bean, map); 802 803 TestBean nested = (TestBean) bean.get("nested"); 804 assertTrue("booleanProperty is false", 805 !nested.getBooleanProperty()); 806 assertTrue("booleanSecond is true", 807 nested.isBooleanSecond()); 808 assertEquals("doubleProperty is 432.0", 809 (double) 432.0, 810 nested.getDoubleProperty(), 811 (double) 0.005); 812 assertEquals("floatProperty is 123.0", 813 (float) 123.0, 814 nested.getFloatProperty(), 815 (float) 0.005); 816 assertEquals("intProperty is 543", 817 543, nested.getIntProperty()); 818 assertEquals("longProperty is 321", 819 (long) 321, nested.getLongProperty()); 820 assertEquals("shortProperty is 654", 821 (short) 654, nested.getShortProperty()); 822 assertEquals("stringProperty is \"This is a string\"", 823 "This is a string", 824 nested.getStringProperty()); 825 826 } catch (IllegalAccessException e) { 827 fail("IllegalAccessException"); 828 } catch (InvocationTargetException e) { 829 fail("InvocationTargetException"); 830 } 831 832 } 833 834 835 838 public void testPopulateScalar() { 839 840 try { 841 842 bean.set("nullProperty", "non-null value"); 843 844 HashMap map = new HashMap (); 845 map.put("booleanProperty", "false"); 846 map.put("doubleProperty", "432.0"); 848 map.put("intProperty", "543"); 850 map.put("nullProperty", null); 852 map.put("shortProperty", "654"); 853 855 BeanUtils.populate(bean, map); 856 857 Boolean booleanProperty = (Boolean ) bean.get("booleanProperty"); 858 assertTrue("booleanProperty is false", !booleanProperty.booleanValue()); 859 Boolean booleanSecond = (Boolean ) bean.get("booleanSecond"); 860 assertTrue("booleanSecond is true", booleanSecond.booleanValue()); 861 Double doubleProperty = (Double ) bean.get("doubleProperty"); 862 assertEquals("doubleProperty is 432.0", 863 (double) 432.0, doubleProperty.doubleValue(), 864 (double) 0.005); 865 Float floatProperty = (Float ) bean.get("floatProperty"); 866 assertEquals("floatProperty is 123.0", 867 (float) 123.0, floatProperty.floatValue(), 868 (float) 0.005); 869 Integer intProperty = (Integer ) bean.get("intProperty"); 870 assertEquals("intProperty is 543", 871 543, intProperty.intValue()); 872 Long longProperty = (Long ) bean.get("longProperty"); 873 assertEquals("longProperty is 321", 874 (long) 321, longProperty.longValue()); 875 assertNull("nullProperty is null", bean.get("nullProperty")); 876 Short shortProperty = (Short ) bean.get("shortProperty"); 877 assertEquals("shortProperty is 654", 878 (short) 654, shortProperty.shortValue()); 879 assertEquals("stringProperty is \"This is a string\"", 880 "This is a string", 881 (String ) bean.get("stringProperty")); 882 883 } catch (IllegalAccessException e) { 884 fail("IllegalAccessException"); 885 } catch (InvocationTargetException e) { 886 fail("InvocationTargetException"); 887 } 888 889 } 890 891 892 895 public void testSetPropertyNullValues() throws Exception { 896 897 Object oldValue = null; 898 Object newValue = null; 899 900 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray"); 902 BeanUtils.setProperty(bean, "stringArray", (String ) null); 903 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray"); 904 assertNotNull("stringArray is not null", newValue); 905 assertTrue("stringArray of correct type", 906 newValue instanceof String []); 907 assertEquals("stringArray length", 908 1, ((String []) newValue).length); 909 assertTrue("stringArray[0] is null", 910 ((String []) newValue)[0] == null); 911 PropertyUtils.setProperty(bean, "stringArray", oldValue); 912 913 oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray"); 915 BeanUtils.setProperty(bean, "stringArray[2]", (String ) null); 916 newValue = PropertyUtils.getSimpleProperty(bean, "stringArray"); 917 assertNotNull("stringArray is not null", newValue); 918 assertTrue("stringArray of correct type", 919 newValue instanceof String []); 920 assertEquals("stringArray length", 921 5, ((String []) newValue).length); 922 assertTrue("stringArray[2] is null", 923 ((String []) newValue)[2] == null); 924 PropertyUtils.setProperty(bean, "stringArray", oldValue); 925 926 BeanUtils.setProperty(bean, "stringProperty", null); 928 assertTrue("stringProperty is now null", 929 BeanUtils.getProperty(bean, "stringProperty") == null); 930 931 } 932 933 934 937 public void testSetPropertyOnPrimitiveWrappers() throws Exception { 938 939 BeanUtils.setProperty(bean,"intProperty", new Integer (1)); 940 assertEquals(1,((Integer ) bean.get("intProperty")).intValue()); 941 BeanUtils.setProperty(bean,"stringProperty", new Integer (1)); 942 assertEquals(1, Integer.parseInt((String ) bean.get("stringProperty"))); 943 944 } 945 946 947 950 public void testSetPropertyNull() throws Exception { 951 952 bean.set("nullProperty", "non-null value"); 953 BeanUtils.setProperty(bean, "nullProperty", null); 954 assertNull("nullProperty is null", bean.get("nullProperty")); 955 956 } 957 958 959 962 public void testCopyPropertyByte() throws Exception { 963 964 BeanUtils.setProperty(bean, "byteProperty", new Byte ((byte) 123)); 965 assertEquals((byte) 123, ((Byte ) bean.get("byteProperty")).byteValue()); 966 972 BeanUtils.setProperty(bean, "byteProperty", new Integer ((int) 123)); 973 assertEquals((byte) 123, ((Byte ) bean.get("byteProperty")).byteValue()); 974 BeanUtils.setProperty(bean, "byteProperty", new Long ((long) 123)); 975 assertEquals((byte) 123, ((Byte ) bean.get("byteProperty")).byteValue()); 976 BeanUtils.setProperty(bean, "byteProperty", new Short ((short) 123)); 977 assertEquals((byte) 123, ((Byte ) bean.get("byteProperty")).byteValue()); 978 979 } 980 981 982 985 public void testCopyPropertyDouble() throws Exception { 986 987 BeanUtils.setProperty(bean, "doubleProperty", new Byte ((byte) 123)); 988 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 989 BeanUtils.setProperty(bean, "doubleProperty", new Double ((double) 123)); 990 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 991 BeanUtils.setProperty(bean, "doubleProperty", new Float ((float) 123)); 992 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 993 BeanUtils.setProperty(bean, "doubleProperty", new Integer ((int) 123)); 994 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 995 BeanUtils.setProperty(bean, "doubleProperty", new Long ((long) 123)); 996 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 997 BeanUtils.setProperty(bean, "doubleProperty", new Short ((short) 123)); 998 assertEquals((double) 123, ((Double ) bean.get("doubleProperty")).doubleValue(), 0.005); 999 1000 } 1001 1002 1003 1006 public void testCopyPropertyFloat() throws Exception { 1007 1008 BeanUtils.setProperty(bean, "floatProperty", new Byte ((byte) 123)); 1009 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1010 BeanUtils.setProperty(bean, "floatProperty", new Double ((double) 123)); 1011 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1012 BeanUtils.setProperty(bean, "floatProperty", new Float ((float) 123)); 1013 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1014 BeanUtils.setProperty(bean, "floatProperty", new Integer ((int) 123)); 1015 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1016 BeanUtils.setProperty(bean, "floatProperty", new Long ((long) 123)); 1017 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1018 BeanUtils.setProperty(bean, "floatProperty", new Short ((short) 123)); 1019 assertEquals((float) 123, ((Float ) bean.get("floatProperty")).floatValue(), 0.005); 1020 1021 } 1022 1023 1024 1027 public void testCopyPropertyInteger() throws Exception { 1028 1029 BeanUtils.setProperty(bean, "longProperty", new Byte ((byte) 123)); 1030 assertEquals((int) 123, ((Integer ) bean.get("intProperty")).intValue()); 1031 1037 BeanUtils.setProperty(bean, "longProperty", new Integer ((int) 123)); 1038 assertEquals((int) 123, ((Integer ) bean.get("intProperty")).intValue()); 1039 BeanUtils.setProperty(bean, "longProperty", new Long ((long) 123)); 1040 assertEquals((int) 123, ((Integer ) bean.get("intProperty")).intValue()); 1041 BeanUtils.setProperty(bean, "longProperty", new Short ((short) 123)); 1042 assertEquals((int) 123, ((Integer ) bean.get("intProperty")).intValue()); 1043 1044 } 1045 1046 1047 1050 public void testCopyPropertyLong() throws Exception { 1051 1052 BeanUtils.setProperty(bean, "longProperty", new Byte ((byte) 123)); 1053 assertEquals((long) 123, ((Long ) bean.get("longProperty")).longValue()); 1054 1060 BeanUtils.setProperty(bean, "longProperty", new Integer ((int) 123)); 1061 assertEquals((long) 123, ((Long ) bean.get("longProperty")).longValue()); 1062 BeanUtils.setProperty(bean, "longProperty", new Long ((long) 123)); 1063 assertEquals((long) 123, ((Long ) bean.get("longProperty")).longValue()); 1064 BeanUtils.setProperty(bean, "longProperty", new Short ((short) 123)); 1065 assertEquals((long) 123, ((Long ) bean.get("longProperty")).longValue()); 1066 1067 } 1068 1069 1070 1073 public void testCopyPropertyNull() throws Exception { 1074 1075 bean.set("nullProperty", "non-null value"); 1076 BeanUtils.copyProperty(bean, "nullProperty", null); 1077 assertNull("nullProperty is null", bean.get("nullProperty")); 1078 1079 } 1080 1081 1082 1085 public void testCopyPropertyShort() throws Exception { 1086 1087 BeanUtils.setProperty(bean, "shortProperty", new Byte ((byte) 123)); 1088 assertEquals((short) 123, ((Short ) bean.get("shortProperty")).shortValue()); 1089 1095 BeanUtils.setProperty(bean, "shortProperty", new Integer ((int) 123)); 1096 assertEquals((short) 123, ((Short ) bean.get("shortProperty")).shortValue()); 1097 BeanUtils.setProperty(bean, "shortProperty", new Long ((long) 123)); 1098 assertEquals((short) 123, ((Short ) bean.get("shortProperty")).shortValue()); 1099 BeanUtils.setProperty(bean, "shortProperty", new Short ((short) 123)); 1100 assertEquals((short) 123, ((Short ) bean.get("shortProperty")).shortValue()); 1101 1102 } 1103 1104 1105 1109 public void testCopyPropertyNestedIndexedArray() throws Exception { 1110 1111 int origArray[] = { 0, 10, 20, 30, 40}; 1112 int intArray[] = { 0, 0, 0 }; 1113 ((TestBean) bean.get("nested")).setIntArray(intArray); 1114 int intChanged[] = { 0, 0, 0 }; 1115 1116 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer (1)); 1118 checkIntArray((int[]) bean.get("intArray"), origArray); 1119 intChanged[1] = 1; 1120 checkIntArray(((TestBean) bean.get("nested")).getIntArray(), 1121 intChanged); 1122 1123 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte ((byte) 2)); 1125 checkIntArray((int[]) bean.get("intArray"), origArray); 1126 intChanged[1] = 2; 1127 checkIntArray(((TestBean) bean.get("nested")).getIntArray(), 1128 intChanged); 1129 1130 BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long ((long) 3)); 1132 checkIntArray((int[]) bean.get("intArray"), origArray); 1133 intChanged[1] = 3; 1134 checkIntArray(((TestBean) bean.get("nested")).getIntArray(), 1135 intChanged); 1136 1137 BeanUtils.copyProperty(bean, "nested.intArray[1]", "4"); 1139 checkIntArray((int[]) bean.get("intArray"), origArray); 1140 intChanged[1] = 4; 1141 checkIntArray(((TestBean) bean.get("nested")).getIntArray(), 1142 intChanged); 1143 1144 } 1145 1146 1147 1150 public void testCopyPropertyNestedMappedMap() throws Exception { 1151 1152 Map origMap = new HashMap (); 1153 origMap.put("First Key", "First Value"); 1154 origMap.put("Second Key", "Second Value"); 1155 Map changedMap = new HashMap (); 1156 changedMap.put("First Key", "First Value"); 1157 changedMap.put("Second Key", "Second Value"); 1158 1159 BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)", 1161 "New Second Value"); 1162 checkMap((Map ) bean.get("mapProperty"), origMap); 1163 changedMap.put("Second Key", "New Second Value"); 1164 checkMap(((TestBean) bean.get("nested")).getMapProperty(), changedMap); 1165 1166 } 1167 1168 1169 1173 public void testCopyPropertyNestedSimple() throws Exception { 1174 1175 bean.set("intProperty", new Integer (0)); 1176 nested.setIntProperty(0); 1177 1178 BeanUtils.copyProperty(bean, "nested.intProperty", new Integer (1)); 1180 assertEquals(0, ((Integer ) bean.get("intProperty")).intValue()); 1181 assertEquals(1, nested.getIntProperty()); 1182 1183 BeanUtils.copyProperty(bean, "nested.intProperty", new Byte ((byte) 2)); 1185 assertEquals(0, ((Integer ) bean.get("intProperty")).intValue()); 1186 assertEquals(2, nested.getIntProperty()); 1187 1188 BeanUtils.copyProperty(bean, "nested.intProperty", new Long ((long) 3)); 1190 assertEquals(0, ((Integer ) bean.get("intProperty")).intValue()); 1191 assertEquals(3, nested.getIntProperty()); 1192 1193 BeanUtils.copyProperty(bean, "nested.intProperty", "4"); 1195 assertEquals(0, ((Integer ) bean.get("intProperty")).intValue()); 1196 assertEquals(4, nested.getIntProperty()); 1197 1198 } 1199 1200 1201 1203 1204 protected void checkIntArray(int actual[], int expected[]) { 1206 assertNotNull("actual array not null", actual); 1207 assertEquals("actual array length", expected.length, actual.length); 1208 for (int i = 0; i < actual.length; i++) { 1209 assertEquals("actual array value[" + i + "]", 1210 expected[i], actual[i]); 1211 } 1212 } 1213 1214 1215 protected void checkMap(Map actual, Map expected) { 1217 assertNotNull("actual map not null", actual); 1218 assertEquals("actual map size", expected.size(), actual.size()); 1219 Iterator keys = expected.keySet().iterator(); 1220 while (keys.hasNext()) { 1221 Object key = keys.next(); 1222 assertEquals("actual map value(" + key + ")", 1223 expected.get(key), actual.get(key)); 1224 } 1225 } 1226 1227 1228 1232 protected static DynaClass createDynaClass() { 1233 1234 int intArray[] = new int[0]; 1235 String stringArray[] = new String [0]; 1236 1237 DynaClass dynaClass = new BasicDynaClass 1238 ("TestDynaClass", null, 1239 new DynaProperty[]{ 1240 new DynaProperty("booleanProperty", Boolean.TYPE), 1241 new DynaProperty("booleanSecond", Boolean.TYPE), 1242 new DynaProperty("byteProperty", Byte.TYPE), 1243 new DynaProperty("doubleProperty", Double.TYPE), 1244 new DynaProperty("dupProperty", stringArray.getClass()), 1245 new DynaProperty("floatProperty", Float.TYPE), 1246 new DynaProperty("intArray", intArray.getClass()), 1247 new DynaProperty("intIndexed", intArray.getClass()), 1248 new DynaProperty("intProperty", Integer.TYPE), 1249 new DynaProperty("listIndexed", List .class), 1250 new DynaProperty("longProperty", Long.TYPE), 1251 new DynaProperty("mapProperty", Map .class), 1252 new DynaProperty("mappedProperty", Map .class), 1253 new DynaProperty("mappedIntProperty", Map .class), 1254 new DynaProperty("nested", TestBean.class), 1255 new DynaProperty("nullProperty", String .class), 1256 new DynaProperty("shortProperty", Short.TYPE), 1257 new DynaProperty("stringArray", stringArray.getClass()), 1258 new DynaProperty("stringIndexed", stringArray.getClass()), 1259 new DynaProperty("stringProperty", String .class), 1260 }); 1261 return (dynaClass); 1262 1263 } 1264 1265 1266} 1267 | Popular Tags |