| 1 16 17 package org.apache.commons.beanutils; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 30 58 59 public class BeanUtilsTestCase extends TestCase { 60 61 63 66 protected TestBean bean = null; 67 68 69 72 protected String describes[] = 73 { "booleanProperty", 74 "booleanSecond", 75 "byteProperty", 76 "doubleProperty", 77 "dupProperty", 78 "floatProperty", 79 "intArray", 80 "longProperty", 82 "listIndexed", 83 "longProperty", 84 "nested", 87 "nullProperty", 88 "readOnlyProperty", 89 "shortProperty", 90 "stringArray", 91 "stringProperty" 93 }; 94 95 96 98 103 public BeanUtilsTestCase(String name) { 104 super(name); 105 } 106 107 108 110 111 114 public void setUp() { 115 bean = new TestBean(); 116 } 117 118 119 122 public static Test suite() { 123 return (new TestSuite(BeanUtilsTestCase.class)); 124 } 125 126 129 public void tearDown() { 130 bean = null; 131 } 132 133 134 136 137 140 public void testCopyPropertiesDynaBean() { 141 142 DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); 144 DynaBean orig = null; 145 try { 146 orig = dynaClass.newInstance(); 147 } catch (Exception e) { 148 fail("newInstance(): " + e); 149 } 150 orig.set("booleanProperty", Boolean.FALSE); 151 orig.set("byteProperty", new Byte ((byte) 111)); 152 orig.set("doubleProperty", new Double (333.33)); 153 orig.set("dupProperty", 154 new String [] { "New 0", "New 1", "New 2" }); 155 orig.set("intArray", new int[] { 100, 200, 300 }); 156 orig.set("intProperty", new Integer (333)); 157 orig.set("longProperty", new Long (3333)); 158 orig.set("shortProperty", new Short ((short) 33)); 159 orig.set("stringArray", new String [] { "New 0", "New 1" }); 160 orig.set("stringProperty", "Custom string"); 161 162 try { 164 BeanUtils.copyProperties(bean, orig); 165 } catch (Exception e) { 166 fail("Threw exception: " + e); 167 } 168 169 assertEquals("Copied boolean property", 171 false, 172 bean.getBooleanProperty()); 173 assertEquals("Copied byte property", 174 (byte) 111, 175 bean.getByteProperty()); 176 assertEquals("Copied double property", 177 333.33, 178 bean.getDoubleProperty(), 179 0.005); 180 assertEquals("Copied int property", 181 333, 182 bean.getIntProperty()); 183 assertEquals("Copied long property", 184 (long) 3333, 185 bean.getLongProperty()); 186 assertEquals("Copied short property", 187 (short) 33, 188 bean.getShortProperty()); 189 assertEquals("Copied string property", 190 "Custom string", 191 bean.getStringProperty()); 192 193 String dupProperty[] = bean.getDupProperty(); 195 assertNotNull("dupProperty present", dupProperty); 196 assertEquals("dupProperty length", 3, dupProperty.length); 197 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 198 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 199 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 200 int intArray[] = bean.getIntArray(); 201 assertNotNull("intArray present", intArray); 202 assertEquals("intArray length", 3, intArray.length); 203 assertEquals("intArray[0]", 100, intArray[0]); 204 assertEquals("intArray[1]", 200, intArray[1]); 205 assertEquals("intArray[2]", 300, intArray[2]); 206 String stringArray[] = bean.getStringArray(); 207 assertNotNull("stringArray present", stringArray); 208 assertEquals("stringArray length", 2, stringArray.length); 209 assertEquals("stringArray[0]", "New 0", stringArray[0]); 210 assertEquals("stringArray[1]", "New 1", stringArray[1]); 211 212 } 213 214 215 218 public void testCopyPropertiesMap() { 219 220 Map map = new HashMap (); 221 map.put("booleanProperty", "false"); 222 map.put("byteProperty", "111"); 223 map.put("doubleProperty", "333.0"); 224 map.put("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 225 map.put("floatProperty", "222.0"); 226 map.put("intArray", new String [] { "0", "100", "200" }); 227 map.put("intProperty", "111"); 228 map.put("longProperty", "444"); 229 map.put("shortProperty", "555"); 230 map.put("stringProperty", "New String Property"); 231 232 try { 233 BeanUtils.copyProperties(bean, map); 234 } catch (Throwable t) { 235 fail("Threw " + t.toString()); 236 } 237 238 assertEquals("booleanProperty", false, 240 bean.getBooleanProperty()); 241 assertEquals("byteProperty", (byte) 111, 242 bean.getByteProperty()); 243 assertEquals("doubleProperty", 333.0, 244 bean.getDoubleProperty(), 0.005); 245 assertEquals("floatProperty", (float) 222.0, 246 bean.getFloatProperty(), (float) 0.005); 247 assertEquals("longProperty", 111, 248 bean.getIntProperty()); 249 assertEquals("longProperty", (long) 444, 250 bean.getLongProperty()); 251 assertEquals("shortProperty", (short) 555, 252 bean.getShortProperty()); 253 assertEquals("stringProperty", "New String Property", 254 bean.getStringProperty()); 255 256 String dupProperty[] = bean.getDupProperty(); 258 assertNotNull("dupProperty present", dupProperty); 259 assertEquals("dupProperty length", 3, dupProperty.length); 260 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 261 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 262 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 263 int intArray[] = bean.getIntArray(); 264 assertNotNull("intArray present", intArray); 265 assertEquals("intArray length", 3, intArray.length); 266 assertEquals("intArray[0]", 0, intArray[0]); 267 assertEquals("intArray[1]", 100, intArray[1]); 268 assertEquals("intArray[2]", 200, intArray[2]); 269 270 } 271 272 273 276 public void testCopyPropertiesStandard() { 277 278 TestBean orig = new TestBean(); 280 orig.setBooleanProperty(false); 281 orig.setByteProperty((byte) 111); 282 orig.setDoubleProperty(333.33); 283 orig.setDupProperty(new String [] { "New 0", "New 1", "New 2" }); 284 orig.setIntArray(new int[] { 100, 200, 300 }); 285 orig.setIntProperty(333); 286 orig.setLongProperty(3333); 287 orig.setShortProperty((short) 33); 288 orig.setStringArray(new String [] { "New 0", "New 1" }); 289 orig.setStringProperty("Custom string"); 290 291 try { 293 BeanUtils.copyProperties(bean, orig); 294 } catch (Exception e) { 295 fail("Threw exception: " + e); 296 } 297 298 assertEquals("Copied boolean property", 300 false, 301 bean.getBooleanProperty()); 302 assertEquals("Copied byte property", 303 (byte) 111, 304 bean.getByteProperty()); 305 assertEquals("Copied double property", 306 333.33, 307 bean.getDoubleProperty(), 308 0.005); 309 assertEquals("Copied int property", 310 333, 311 bean.getIntProperty()); 312 assertEquals("Copied long property", 313 (long) 3333, 314 bean.getLongProperty()); 315 assertEquals("Copied short property", 316 (short) 33, 317 bean.getShortProperty()); 318 assertEquals("Copied string property", 319 "Custom string", 320 bean.getStringProperty()); 321 322 String dupProperty[] = bean.getDupProperty(); 324 assertNotNull("dupProperty present", dupProperty); 325 assertEquals("dupProperty length", 3, dupProperty.length); 326 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 327 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 328 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 329 int intArray[] = bean.getIntArray(); 330 assertNotNull("intArray present", intArray); 331 assertEquals("intArray length", 3, intArray.length); 332 assertEquals("intArray[0]", 100, intArray[0]); 333 assertEquals("intArray[1]", 200, intArray[1]); 334 assertEquals("intArray[2]", 300, intArray[2]); 335 String stringArray[] = bean.getStringArray(); 336 assertNotNull("stringArray present", stringArray); 337 assertEquals("stringArray length", 2, stringArray.length); 338 assertEquals("stringArray[0]", "New 0", stringArray[0]); 339 assertEquals("stringArray[1]", "New 1", stringArray[1]); 340 341 } 342 343 344 347 public void testDescribe() { 348 349 Map map = null; 350 try { 351 map = BeanUtils.describe(bean); 352 } catch (Exception e) { 353 fail("Threw exception " + e); 354 } 355 356 for (int i = 0; i < describes.length; i++) { 358 assertTrue("Property '" + describes[i] + "' is present", 359 map.containsKey(describes[i])); 360 } 361 assertTrue("Property 'writeOnlyProperty' is not present", 362 !map.containsKey("writeOnlyProperty")); 363 364 assertEquals("Value of 'booleanProperty'", 366 "true", 367 (String ) map.get("booleanProperty")); 368 assertEquals("Value of 'byteProperty'", 369 "121", 370 (String ) map.get("byteProperty")); 371 assertEquals("Value of 'doubleProperty'", 372 "321.0", 373 (String ) map.get("doubleProperty")); 374 assertEquals("Value of 'floatProperty'", 375 "123.0", 376 (String ) map.get("floatProperty")); 377 assertEquals("Value of 'intProperty'", 378 "123", 379 (String ) map.get("intProperty")); 380 assertEquals("Value of 'longProperty'", 381 "321", 382 (String ) map.get("longProperty")); 383 assertEquals("Value of 'shortProperty'", 384 "987", 385 (String ) map.get("shortProperty")); 386 assertEquals("Value of 'stringProperty'", 387 "This is a string", 388 (String ) map.get("stringProperty")); 389 390 } 391 392 393 396 public void testGetArrayProperty() { 397 try { 398 String arr[] = BeanUtils.getArrayProperty(bean, "stringArray"); 399 String comp[] = bean.getStringArray(); 400 401 assertTrue("String array length = " + comp.length, 402 (comp.length == arr.length)); 403 404 arr = BeanUtils.getArrayProperty(bean, "intArray"); 405 int iarr[] = bean.getIntArray(); 406 407 assertTrue("String array length = " + iarr.length, 408 (iarr.length == arr.length)); 409 } catch (IllegalAccessException e) { 410 fail("IllegalAccessException"); 411 } catch (InvocationTargetException e) { 412 fail("InvocationTargetException"); 413 } catch (NoSuchMethodException e) { 414 fail("NoSuchMethodException"); 415 } 416 417 } 418 419 420 423 public void testGetIndexedProperty1() { 424 try { 425 String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]"); 426 String comp = String.valueOf(bean.getIntIndexed(3)); 427 assertTrue("intIndexed[3] == " + comp, val.equals(comp)); 428 429 val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]"); 430 comp = bean.getStringIndexed(3); 431 assertTrue("stringIndexed[3] == " + comp, val.equals(comp)); 432 } catch (IllegalAccessException e) { 433 fail("IllegalAccessException"); 434 } catch (InvocationTargetException e) { 435 fail("InvocationTargetException"); 436 } catch (NoSuchMethodException e) { 437 fail("NoSuchMethodException"); 438 } 439 } 440 441 442 445 public void testGetIndexedProperty2() { 446 try { 447 String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3); 448 String comp = String.valueOf(bean.getIntIndexed(3)); 449 450 assertTrue("intIndexed,3 == " + comp, val.equals(comp)); 451 452 val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3); 453 comp = bean.getStringIndexed(3); 454 455 assertTrue("stringIndexed,3 == " + comp, val.equals(comp)); 456 457 } catch (IllegalAccessException e) { 458 fail("IllegalAccessException"); 459 } catch (InvocationTargetException e) { 460 fail("InvocationTargetException"); 461 } catch (NoSuchMethodException e) { 462 fail("NoSuchMethodException"); 463 } 464 } 465 466 467 470 public void testGetNestedProperty() { 471 try { 472 String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty"); 473 String comp = bean.getNested().getStringProperty(); 474 assertTrue("nested.StringProperty == " + comp, 475 val.equals(comp)); 476 } catch (IllegalAccessException e) { 477 fail("IllegalAccessException"); 478 } catch (InvocationTargetException e) { 479 fail("InvocationTargetException"); 480 } catch (NoSuchMethodException e) { 481 fail("NoSuchMethodException"); 482 } 483 } 484 485 486 489 public void testGetGeneralProperty() { 490 try { 491 String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]"); 492 String comp = String.valueOf(bean.getIntIndexed(2)); 493 494 assertTrue("nested.intIndexed[2] == " + comp, 495 val.equals(comp)); 496 } catch (IllegalAccessException e) { 497 fail("IllegalAccessException"); 498 } catch (InvocationTargetException e) { 499 fail("InvocationTargetException"); 500 } catch (NoSuchMethodException e) { 501 fail("NoSuchMethodException"); 502 } 503 } 504 505 506 509 public void testGetSimpleProperty() { 510 try { 511 String val = BeanUtils.getSimpleProperty(bean, "shortProperty"); 512 String comp = String.valueOf(bean.getShortProperty()); 513 514 assertTrue("shortProperty == " + comp, 515 val.equals(comp)); 516 } catch (IllegalAccessException e) { 517 fail("IllegalAccessException"); 518 } catch (InvocationTargetException e) { 519 fail("InvocationTargetException"); 520 } catch (NoSuchMethodException e) { 521 fail("NoSuchMethodException"); 522 } 523 } 524 525 526 529 public void testPopulateArrayElements() { 530 531 try { 532 533 HashMap map = new HashMap (); 534 map.put("intIndexed[0]", "100"); 535 map.put("intIndexed[2]", "120"); 536 map.put("intIndexed[4]", "140"); 537 538 BeanUtils.populate(bean, map); 539 540 assertEquals("intIndexed[0] is 100", 541 100, bean.getIntIndexed(0)); 542 assertEquals("intIndexed[1] is 10", 543 10, bean.getIntIndexed(1)); 544 assertEquals("intIndexed[2] is 120", 545 120, bean.getIntIndexed(2)); 546 assertEquals("intIndexed[3] is 30", 547 30, bean.getIntIndexed(3)); 548 assertEquals("intIndexed[4] is 140", 549 140, bean.getIntIndexed(4)); 550 551 map.clear(); 552 map.put("stringIndexed[1]", "New String 1"); 553 map.put("stringIndexed[3]", "New String 3"); 554 555 BeanUtils.populate(bean, map); 556 557 assertEquals("stringIndexed[0] is \"String 0\"", 558 "String 0", bean.getStringIndexed(0)); 559 assertEquals("stringIndexed[1] is \"New String 1\"", 560 "New String 1", bean.getStringIndexed(1)); 561 assertEquals("stringIndexed[2] is \"String 2\"", 562 "String 2", bean.getStringIndexed(2)); 563 assertEquals("stringIndexed[3] is \"New String 3\"", 564 "New String 3", bean.getStringIndexed(3)); 565 assertEquals("stringIndexed[4] is \"String 4\"", 566 "String 4", bean.getStringIndexed(4)); 567 568 } catch (IllegalAccessException e) { 569 fail("IllegalAccessException"); 570 } catch (InvocationTargetException e) { 571 fail("InvocationTargetException"); 572 } 573 574 } 575 576 577 580 public void testPopulateArrayProperties() { 581 582 try { 583 584 HashMap map = new HashMap (); 585 int intArray[] = new int[] { 123, 456, 789 }; 586 map.put("intArray", intArray); 587 String stringArray[] = new String [] 588 { "New String 0", "New String 1" }; 589 map.put("stringArray", stringArray); 590 591 BeanUtils.populate(bean, map); 592 593 intArray = bean.getIntArray(); 594 assertNotNull("intArray is present", intArray); 595 assertEquals("intArray length", 596 3, intArray.length); 597 assertEquals("intArray[0]", 123, intArray[0]); 598 assertEquals("intArray[1]", 456, intArray[1]); 599 assertEquals("intArray[2]", 789, intArray[2]); 600 stringArray = bean.getStringArray(); 601 assertNotNull("stringArray is present", stringArray); 602 assertEquals("stringArray length", 2, stringArray.length); 603 assertEquals("stringArray[0]", "New String 0", stringArray[0]); 604 assertEquals("stringArray[1]", "New String 1", stringArray[1]); 605 606 } catch (IllegalAccessException e) { 607 fail("IllegalAccessException"); 608 } catch (InvocationTargetException e) { 609 fail("InvocationTargetException"); 610 } 611 612 } 613 614 615 618 public void testPopulateMapped() { 619 620 try { 621 622 HashMap map = new HashMap (); 623 map.put("mappedProperty(First Key)", "New First Value"); 624 map.put("mappedProperty(Third Key)", "New Third Value"); 625 626 BeanUtils.populate(bean, map); 627 628 assertEquals("mappedProperty(First Key)", 629 "New First Value", 630 bean.getMappedProperty("First Key")); 631 assertEquals("mappedProperty(Second Key)", 632 "Second Value", 633 bean.getMappedProperty("Second Key")); 634 assertEquals("mappedProperty(Third Key)", 635 "New Third Value", 636 bean.getMappedProperty("Third Key")); 637 assertNull("mappedProperty(Fourth Key", 638 bean.getMappedProperty("Fourth Key")); 639 640 } catch (IllegalAccessException e) { 641 fail("IllegalAccessException"); 642 } catch (InvocationTargetException e) { 643 fail("InvocationTargetException"); 644 } 645 646 } 647 648 649 652 public void testPopulateNested() { 653 654 try { 655 656 HashMap map = new HashMap (); 657 map.put("nested.booleanProperty", "false"); 658 map.put("nested.doubleProperty", "432.0"); 660 map.put("nested.intProperty", "543"); 662 map.put("nested.shortProperty", "654"); 664 map.put("nested.writeOnlyProperty", "New writeOnlyProperty value"); 666 667 BeanUtils.populate(bean, map); 668 669 assertTrue("booleanProperty is false", 670 !bean.getNested().getBooleanProperty()); 671 assertTrue("booleanSecond is true", 672 bean.getNested().isBooleanSecond()); 673 assertEquals("doubleProperty is 432.0", 674 (double) 432.0, 675 bean.getNested().getDoubleProperty(), 676 (double) 0.005); 677 assertEquals("floatProperty is 123.0", 678 (float) 123.0, 679 bean.getNested().getFloatProperty(), 680 (float) 0.005); 681 assertEquals("intProperty is 543", 682 543, bean.getNested().getIntProperty()); 683 assertEquals("longProperty is 321", 684 (long) 321, bean.getNested().getLongProperty()); 685 assertEquals("shortProperty is 654", 686 (short) 654, bean.getNested().getShortProperty()); 687 assertEquals("stringProperty is \"This is a string\"", 688 "This is a string", 689 bean.getNested().getStringProperty()); 690 assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"", 691 "New writeOnlyProperty value", 692 bean.getNested().getWriteOnlyPropertyValue()); 693 694 } catch (IllegalAccessException e) { 695 fail("IllegalAccessException"); 696 } catch (InvocationTargetException e) { 697 fail("InvocationTargetException"); 698 } 699 700 } 701 702 703 706 public void testPopulateScalar() { 707 708 try { 709 710 bean.setNullProperty("Non-null value"); 711 712 HashMap map = new HashMap (); 713 map.put("booleanProperty", "false"); 714 map.put("byteProperty", "111"); 716 map.put("doubleProperty", "432.0"); 717 map.put("intProperty", "543"); 719 map.put("longProperty", ""); 720 map.put("nullProperty", null); 721 map.put("shortProperty", "654"); 722 map.put("writeOnlyProperty", "New writeOnlyProperty value"); 724 map.put("readOnlyProperty", "New readOnlyProperty value"); 725 726 BeanUtils.populate(bean, map); 727 728 assertTrue("booleanProperty is false", !bean.getBooleanProperty()); 729 assertTrue("booleanSecond is true", bean.isBooleanSecond()); 730 assertEquals("byteProperty is 111", 731 (byte) 111, bean.getByteProperty()); 732 assertEquals("doubleProperty is 432.0", 733 (double) 432.0, bean.getDoubleProperty(), 734 (double) 0.005); 735 assertEquals("floatProperty is 123.0", 736 (float) 123.0, bean.getFloatProperty(), 737 (float) 0.005); 738 assertEquals("intProperty is 543", 739 543, bean.getIntProperty()); 740 assertEquals("longProperty is 0", 741 (long) 0, bean.getLongProperty()); 742 assertNull("nullProperty is null", 743 bean.getNullProperty()); 744 assertEquals("shortProperty is 654", 745 (short) 654, bean.getShortProperty()); 746 assertEquals("stringProperty is \"This is a string\"", 747 |