1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import junit.framework.TestCase; 32 import junit.framework.Test; 33 import junit.framework.TestSuite; 34 35 36 44 45 public class BasicDynaBeanTestCase extends TestCase { 46 47 48 50 51 54 protected DynaBean bean = null; 55 56 57 62 protected final static String [] properties = { 63 "booleanProperty", 64 "booleanSecond", 65 "doubleProperty", 66 "floatProperty", 67 "intArray", 68 "intIndexed", 69 "intProperty", 70 "listIndexed", 71 "longProperty", 72 "mappedProperty", 73 "mappedIntProperty", 74 "nullProperty", 75 "shortProperty", 76 "stringArray", 77 "stringIndexed", 78 "stringProperty", 79 }; 80 81 82 84 85 90 public BasicDynaBeanTestCase(String name) { 91 92 super(name); 93 94 } 95 96 97 99 100 103 public void setUp() throws Exception { 104 105 DynaClass dynaClass = createDynaClass(); 107 bean = dynaClass.newInstance(); 108 109 bean.set("booleanProperty", new Boolean (true)); 111 bean.set("booleanSecond", new Boolean (true)); 112 bean.set("doubleProperty", new Double (321.0)); 113 bean.set("floatProperty", new Float ((float) 123.0)); 114 int intArray[] = { 0, 10, 20, 30, 40 }; 115 bean.set("intArray", intArray); 116 int intIndexed[] = { 0, 10, 20, 30, 40 }; 117 bean.set("intIndexed", intIndexed); 118 bean.set("intProperty", new Integer (123)); 119 List listIndexed = new ArrayList (); 120 listIndexed.add("String 0"); 121 listIndexed.add("String 1"); 122 listIndexed.add("String 2"); 123 listIndexed.add("String 3"); 124 listIndexed.add("String 4"); 125 bean.set("listIndexed", listIndexed); 126 bean.set("longProperty", new Long ((long) 321)); 127 HashMap mappedProperty = new HashMap (); 128 mappedProperty.put("First Key", "First Value"); 129 mappedProperty.put("Second Key", "Second Value"); 130 bean.set("mappedProperty", mappedProperty); 131 HashMap mappedIntProperty = new HashMap (); 132 mappedIntProperty.put("One", new Integer (1)); 133 mappedIntProperty.put("Two", new Integer (2)); 134 bean.set("mappedIntProperty", mappedIntProperty); 135 bean.set("shortProperty", new Short ((short) 987)); 137 String stringArray[] = 138 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 139 bean.set("stringArray", stringArray); 140 String stringIndexed[] = 141 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 142 bean.set("stringIndexed", stringIndexed); 143 bean.set("stringProperty", "This is a string"); 144 145 } 146 147 148 151 public static Test suite() { 152 153 return (new TestSuite(BasicDynaBeanTestCase.class)); 154 155 } 156 157 158 161 public void tearDown() { 162 163 bean = null; 164 165 } 166 167 168 169 171 172 175 public void testGetDescriptorArguments() { 176 177 try { 178 DynaProperty descriptor = 179 bean.getDynaClass().getDynaProperty("unknown"); 180 assertNull("Unknown property descriptor should be null", 181 descriptor); 182 } catch (Throwable t) { 183 fail("Threw " + t + " instead of returning null"); 184 } 185 186 try { 187 bean.getDynaClass().getDynaProperty(null); 188 fail("Should throw IllegalArgumentException"); 189 } catch (IllegalArgumentException e) { 190 ; } catch (Throwable t) { 192 fail("Threw " + t + " instead of IllegalArgumentException"); 193 } 194 195 } 196 197 198 201 public void testGetDescriptorBoolean() { 202 203 testGetDescriptorBase("booleanProperty", Boolean.TYPE); 204 205 } 206 207 208 211 public void testGetDescriptorDouble() { 212 213 testGetDescriptorBase("doubleProperty", Double.TYPE); 214 215 } 216 217 218 221 public void testGetDescriptorFloat() { 222 223 testGetDescriptorBase("floatProperty", Float.TYPE); 224 225 } 226 227 228 231 public void testGetDescriptorInt() { 232 233 testGetDescriptorBase("intProperty", Integer.TYPE); 234 235 } 236 237 238 241 public void testGetDescriptorLong() { 242 243 testGetDescriptorBase("longProperty", Long.TYPE); 244 245 } 246 247 248 252 public void testGetDescriptorSecond() { 253 254 testGetDescriptorBase("booleanSecond", Boolean.TYPE); 255 256 } 257 258 259 262 public void testGetDescriptorShort() { 263 264 testGetDescriptorBase("shortProperty", Short.TYPE); 265 266 } 267 268 269 272 public void testGetDescriptorString() { 273 274 testGetDescriptorBase("stringProperty", String .class); 275 276 } 277 278 279 283 public void testGetDescriptors() { 284 285 DynaProperty pd[] = bean.getDynaClass().getDynaProperties(); 286 assertNotNull("Got descriptors", pd); 287 int count[] = new int[properties.length]; 288 for (int i = 0; i < pd.length; i++) { 289 String name = pd[i].getName(); 290 for (int j = 0; j < properties.length; j++) { 291 if (name.equals(properties[j])) 292 count[j]++; 293 } 294 } 295 for (int j = 0; j < properties.length; j++) { 296 if (count[j] < 0) 297 fail("Missing property " + properties[j]); 298 else if (count[j] > 1) 299 fail("Duplicate property " + properties[j]); 300 } 301 302 } 303 304 305 308 public void testGetIndexedArguments() { 309 310 try { 311 bean.get("intArray", -1); 312 fail("Should throw IndexOutOfBoundsException"); 313 } catch (IndexOutOfBoundsException e) { 314 ; } catch (Throwable t) { 316 fail("Threw " + t + " instead of IndexOutOfBoundsException"); 317 } 318 319 320 } 321 322 323 326 public void testGetIndexedValues() { 327 328 Object value = null; 329 330 for (int i = 0; i < 5; i++) { 331 332 try { 333 value = bean.get("intArray", i); 334 assertNotNull("intArray returned value " + i, value); 335 assertTrue("intArray returned Integer " + i, 336 value instanceof Integer ); 337 assertEquals("intArray returned correct " + i, i * 10, 338 ((Integer ) value).intValue()); 339 } catch (Throwable t) { 340 fail("intArray " + i + " threw " + t); 341 } 342 343 try { 344 value = bean.get("intIndexed", i); 345 assertNotNull("intIndexed returned value " + i, value); 346 assertTrue("intIndexed returned Integer " + i, 347 value instanceof Integer ); 348 assertEquals("intIndexed returned correct " + i, i * 10, 349 ((Integer ) value).intValue()); 350 } catch (Throwable t) { 351 fail("intIndexed " + i + " threw " + t); 352 } 353 354 try { 355 value = bean.get("listIndexed", i); 356 assertNotNull("listIndexed returned value " + i, value); 357 assertTrue("list returned String " + i, 358 value instanceof String ); 359 assertEquals("listIndexed returned correct " + i, 360 "String " + i, (String ) value); 361 } catch (Throwable t) { 362 fail("listIndexed " + i + " threw " + t); 363 } 364 365 try { 366 value = bean.get("stringArray", i); 367 assertNotNull("stringArray returned value " + i, value); 368 assertTrue("stringArray returned String " + i, 369 value instanceof String ); 370 assertEquals("stringArray returned correct " + i, 371 "String " + i, (String ) value); 372 } catch (Throwable t) { 373 fail("stringArray " + i + " threw " + t); 374 } 375 376 try { 377 value = bean.get("stringIndexed", i); 378 assertNotNull("stringIndexed returned value " + i, value); 379 assertTrue("stringIndexed returned String " + i, 380 value instanceof String ); 381 assertEquals("stringIndexed returned correct " + i, 382 "String " + i, (String ) value); 383 } catch (Throwable t) { 384 fail("stringIndexed " + i + " threw " + t); 385 } 386 387 } 388 389 390 } 391 392 393 396 public void testGetMappedArguments() { 397 398 399 try { 400 Object value = bean.get("mappedProperty", "unknown"); 401 assertNull("Should not return a value", value); 402 } catch (Throwable t) { 403 fail("Threw " + t + " instead of returning null"); 404 } 405 406 407 } 408 409 410 413 public void testGetMappedValues() { 414 415 Object value = null; 416 417 try { 418 value = bean.get("mappedProperty", "First Key"); 419 assertEquals("Can find first value", "First Value", value); 420 } catch (Throwable t) { 421 fail("Finding first value threw " + t); 422 } 423 424 try { 425 value = bean.get("mappedProperty", "Second Key"); 426 assertEquals("Can find second value", "Second Value", value); 427 } catch (Throwable t) { 428 fail("Finding second value threw " + t); 429 } 430 431 try { 432 value = bean.get("mappedProperty", "Third Key"); 433 assertNull("Can not find third value", value); 434 } catch (Throwable t) { 435 fail("Finding third value threw " + t); 436 } 437 438 } 439 440 441 444 public void testGetSimpleArguments() { 445 446 try { 447 bean.get(null); 448 fail("Should throw IllegalArgumentException"); 449 } catch (IllegalArgumentException e) { 450 ; } catch (Throwable t) { 452 fail("Threw " + t + " instead of IllegalArgumentException"); 453 } 454 455 } 456 457 458 461 public void testGetSimpleBoolean() { 462 463 try { 464 Object value = bean.get("booleanProperty"); 465 assertNotNull("Got a value", value); 466 assertTrue("Got correct type", (value instanceof Boolean )); 467 assertTrue("Got correct value", 468 ((Boolean ) value).booleanValue() == true); 469 } catch (Throwable e) { 470 fail("Exception: " + e); 471 } 472 473 } 474 475 476 479 public void testGetSimpleDouble() { 480 481 try { 482 Object value = bean.get("doubleProperty"); 483 assertNotNull("Got a value", value); 484 assertTrue("Got correct type", (value instanceof Double )); 485 assertEquals("Got correct value", 486 ((Double ) value).doubleValue(), 487 (double) 321.0, 488 (double) 0.005); 489 } catch (Throwable t) { 490 fail("Exception: " + t); 491 } 492 493 } 494 495 496 499 public void testGetSimpleFloat() { 500 501 try { 502 Object value = bean.get("floatProperty"); 503 assertNotNull("Got a value", value); 504 assertTrue("Got correct type", (value instanceof Float )); 505 assertEquals("Got correct value", 506 ((Float ) value).floatValue(), 507 (float) 123.0, 508 (float) 0.005); 509 } catch (Throwable t) { 510 fail("Exception: " + t); 511 } 512 513 } 514 515 516 519 public void testGetSimpleInt() { 520 521 try { 522 Object value = bean.get("intProperty"); 523 assertNotNull("Got a value", value); 524 assertTrue("Got correct type", (value instanceof Integer )); 525 assertEquals("Got correct value", 526 ((Integer ) value).intValue(), 527 (int) 123); 528 } catch (Throwable t) { 529 fail("Exception: " + t); 530 } 531 532 } 533 534 535 538 public void testGetSimpleLong() { 539 540 try { 541 Object value = bean.get("longProperty"); 542 assertNotNull("Got a value", value); 543 assertTrue("Got correct type", (value instanceof Long )); 544 assertEquals("Got correct value", 545 ((Long ) value).longValue(), 546 (long) 321); 547 } catch (Throwable t) { 548 fail("Exception: " + t); 549 } 550 551 } 552 553 554 557 public void testGetSimpleShort() { 558 559 try { 560 Object value = bean.get("shortProperty"); 561 assertNotNull("Got a value", value); 562 assertTrue("Got correct type", (value instanceof Short )); 563 assertEquals("Got correct value", 564 ((Short ) value).shortValue(), 565 (short) 987); 566 } catch (Throwable t) { 567 fail("Exception: " + t); 568 } 569 570 } 571 572 573 576 public void testGetSimpleString() { 577 578 try { 579 Object value = bean.get("stringProperty"); 580 assertNotNull("Got a value", value); 581 assertTrue("Got correct type", (value instanceof String )); 582 assertEquals("Got correct value", 583 (String ) value, 584 "This is a string"); 585 } catch (Throwable t) { 586 fail("Exception: " + t); 587 } 588 589 } 590 591 592 595 public void testMappedContains() { 596 597 try { 598 assertTrue("Can see first key", 599 bean.contains("mappedProperty", "First Key")); 600 } catch (Throwable t) { 601 fail("Exception: " + t); 602 } 603 604 605 try { 606 assertTrue("Can not see unknown key", 607 !bean.contains("mappedProperty", "Unknown Key")); 608 } catch (Throwable t) { 609 fail("Exception: " + t); 610 } 611 612 } 613 614 615 618 public void testMappedRemove() { 619 620 try { 621 assertTrue("Can see first key", 622 bean.contains("mappedProperty", "First Key")); 623 bean.remove("mappedProperty", "First Key"); 624 assertTrue("Can not see first key", 625 !bean.contains("mappedProperty", "First Key")); 626 } catch (Throwable t) { 627 fail("Exception: " + t); 628 } 629 630 try { 631 assertTrue("Can not see unknown key", 632 !bean.contains("mappedProperty", "Unknown Key")); 633 bean.remove("mappedProperty", "Unknown Key"); 634 assertTrue("Can not see unknown key", 635 !bean.contains("mappedProperty", "Unknown Key")); 636 } catch (Throwable t) { 637 fail("Exception: " + t); 638 } 639 640 } 641 642 643 646 public void testSerialization() { 647 648 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 650 try { 651 ObjectOutputStream oos = new ObjectOutputStream (baos); 652 oos.writeObject(bean); 653 oos.flush(); 654 oos.close(); 655 } catch (Exception e) { 656 fail("Exception during serialization: " + e); 657 } 658 659 try { 661 bean = null; 662 ByteArrayInputStream bais = 663 new ByteArrayInputStream (baos.toByteArray()); 664 ObjectInputStream ois = new ObjectInputStream (bais); 665 bean = (DynaBean) ois.readObject(); 666 bais.close(); 667 } catch (Exception e) { 668 fail("Exception during deserialization: " + e); 669 } 670 671 testGetDescriptorArguments(); 673 testGetDescriptorBoolean(); 674 testGetDescriptorDouble(); 675 testGetDescriptorFloat(); 676 testGetDescriptorInt(); 677 testGetDescriptorLong(); 678 testGetDescriptorSecond(); 679 testGetDescriptorShort(); 680 testGetDescriptorString(); 681 testGetDescriptors(); 682 testGetIndexedArguments(); 683 testGetIndexedValues(); 684 testGetMappedArguments(); 685 testGetMappedValues(); 686 testGetSimpleArguments(); 687 testGetSimpleBoolean(); 688 testGetSimpleDouble(); 689 testGetSimpleFloat(); 690 testGetSimpleInt(); 691 testGetSimpleLong(); 692 testGetSimpleShort(); 693 testGetSimpleString(); 694 testMappedContains(); 695 testMappedRemove(); 696 697 try { 699 bean = bean.getDynaClass().newInstance(); 700 } catch (Exception e) { 701 fail("Exception creating new instance: " + e); 702 } 703 testGetDescriptorArguments(); 704 testGetDescriptorBoolean(); 705 testGetDescriptorDouble(); 706 testGetDescriptorFloat(); 707 testGetDescriptorInt(); 708 testGetDescriptorLong(); 709 testGetDescriptorSecond(); 710 testGetDescriptorShort(); 711 testGetDescriptorString(); 712 testGetDescriptors(); 713 714 } 715 716 717 720 public void testSetIndexedArguments() { 721 722 try { 723 bean.set("intArray", -1, new Integer (0)); 724 fail("Should throw IndexOutOfBoundsException"); 725 } catch (IndexOutOfBoundsException e) { 726 ; } catch (Throwable t) { 728 fail("Threw " + t + " instead of IndexOutOfBoundsException"); 729 } 730 731 } 732 733 734 737 public void testSetIndexedValues() { 738 739 Object value = null; 740 741 try { 742 bean.set("intArray", 0, new Integer (1)); 743 value = (Integer ) bean.get("intArray", 0); 744 assertNotNull("Returned new value 0", value); 745 assertTrue("Returned Integer new value 0", 746 value instanceof Integer ); 747 assertEquals("Returned correct new value 0", 1, 748 ((Integer ) value).intValue()); 749 } catch (Throwable t) { 750 fail("Threw " + t); 751 } 752 753 try { 754 bean.set("intIndexed", 1, new Integer (11)); 755 value = (Integer ) bean.get("intIndexed", 1); 756 assertNotNull("Returned new value 1", value); 757 assertTrue("Returned Integer new value 1", 758 value instanceof Integer ); 759 assertEquals("Returned correct new value 1", 11, 760 ((Integer ) value).intValue()); 761 } catch (Throwable t) { 762 fail("Threw " + t); 763 } 764 765 try { 766 bean.set("listIndexed", 2, "New Value 2"); 767 value = (String ) bean.get("listIndexed", 2); 768 assertNotNull("Returned new value 2", value); 769 assertTrue("Returned String new value 2", 770 value instanceof String ); 771 assertEquals("Returned correct new value 2", "New Value 2", 772 (String ) value); 773 } catch (Throwable t) { 774 fail("Threw " + t); 775 } 776 777 try { 778 bean.set("stringArray", 3, "New Value 3"); 779 value = (String ) bean.get("stringArray", 3); 780 assertNotNull("Returned new value 3", value); 781 assertTrue("Returned String new value 3", 782 value instanceof String ); 783 assertEquals("Returned correct new value 3", "New Value 3", 784 (String ) value); 785 } catch (Throwable t) { 786 fail("Threw " + t); 787 } 788 789 try { 790 bean.set("stringIndexed", 4, "New Value 4"); 791 value = (String ) bean.get("stringIndexed", 4); 792 assertNotNull("Returned new value 4", value); 793 assertTrue("Returned String new value 4", 794 value instanceof String ); 795 assertEquals("Returned correct new value 4", "New Value 4", 796 (String ) value); 797 } catch (Throwable t) { 798 fail("Threw " + t); 799 } 800 801 802 } 803 804 805 808 public void testSetMappedValues() { 809 810 try { 811 bean.set("mappedProperty", "First Key", "New First Value"); 812 assertEquals("Can replace old value", 813 "New First Value", 814 (String ) bean.get("mappedProperty", "First Key")); 815 } catch (Throwable t) { 816 fail("Finding fourth value threw " + t); 817 } 818 819 try { 820 bean.set("mappedProperty", "Fourth Key", "Fourth Value"); 821 assertEquals("Can set new value", 822 "Fourth Value", 823 (String ) bean.get("mappedProperty", "Fourth Key")); 824 } catch (Throwable t) { 825 fail("Finding fourth value threw " + t); 826 } 827 828 829 } 830 831 832 835 public void testSetSimpleBoolean() { 836 837 try { 838 boolean oldValue = 839 ((Boolean ) bean.get("booleanProperty")).booleanValue(); 840 boolean newValue = !oldValue; 841 bean.set("booleanProperty", new Boolean (newValue)); 842 assertTrue("Matched new value", 843 newValue == 844 ((Boolean ) bean.get("booleanProperty")).booleanValue()); 845 } catch (Throwable e) { 846 fail("Exception: " + e); 847 } 848 849 } 850 851 852 855 public void testSetSimpleDouble() { 856 857 try { 858 double oldValue = 859 ((Double ) bean.get("doubleProperty")).doubleValue(); 860 double newValue = oldValue + 1.0; 861 bean.set("doubleProperty", new Double (newValue)); 862 assertEquals("Matched new value", 863 newValue, 864 ((Double ) bean.get("doubleProperty")).doubleValue(), 865 (double) 0.005); 866 } catch (Throwable e) { 867 fail("Exception: " + e); 868 } 869 870 } 871 872 873 876 public void testSetSimpleFloat() { 877 878 try { 879 float oldValue = 880 ((Float ) bean.get("floatProperty")).floatValue(); 881 float newValue = oldValue + (float) 1.0; 882 bean.set("floatProperty", new Float (newValue)); 883 assertEquals("Matched new value", 884 newValue, 885 ((Float ) bean.get("floatProperty")).floatValue(), 886 (float) 0.005); 887 } catch (Throwable e) { 888 fail("Exception: " + e); 889 } 890 891 } 892 893 894 897 public void testSetSimpleInt() { 898 899 try { 900 int oldValue = 901 ((Integer ) bean.get("intProperty")).intValue(); 902 int newValue = oldValue + 1; 903 bean.set("intProperty", new Integer (newValue)); 904 assertEquals("Matched new value", 905 newValue, 906 ((Integer ) bean.get("intProperty")).intValue()); 907 } catch (Throwable e) { 908 fail("Exception: " + e); 909 } 910 911 } 912 913 914 917 public void testSetSimpleLong() { 918 919 try { 920 long oldValue = 921 ((Long ) bean.get("longProperty")).longValue(); 922 long newValue = oldValue + 1; 923 bean.set("longProperty", new Long (newValue)); 924 assertEquals("Matched new value", 925 newValue, 926 ((Long ) bean.get("longProperty")).longValue()); 927 } catch (Throwable e) { 928 fail("Exception: " + e); 929 } 930 931 } 932 933 934 937 public void testSetSimpleShort() { 938 939 try { 940 short oldValue = 941 ((Short ) bean.get("shortProperty")).shortValue(); 942 short newValue = (short) (oldValue + 1); 943 bean.set("shortProperty", new Short (newValue)); 944 assertEquals("Matched new value", 945 newValue, 946 ((Short ) bean.get("shortProperty")).shortValue()); 947 } catch (Throwable e) { 948 fail("Exception: " + e); 949 } 950 951 } 952 953 954 957 public void testSetSimpleString() { 958 959 try { 960 String oldValue = (String ) bean.get("stringProperty"); 961 String newValue = oldValue + " Extra Value"; 962 bean.set("stringProperty", newValue); 963 assertEquals("Matched new value", 964 newValue, 965 (String ) bean.get("stringProperty")); 966 } catch (Throwable e) { 967 fail("Exception: " + e); 968 } 969 970 } 971 972 973 975 976 980 protected DynaClass createDynaClass() { 981 982 int intArray[] = new int[0]; 983 String stringArray[] = new String [0]; 984 985 DynaClass dynaClass = new BasicDynaClass 986 ("TestDynaClass", null, 987 new DynaProperty[]{ 988 new DynaProperty("booleanProperty", Boolean.TYPE), 989 new DynaProperty("booleanSecond", Boolean.TYPE), 990 new DynaProperty("doubleProperty", Double.TYPE), 991 new DynaProperty("floatProperty", Float.TYPE), 992 new DynaProperty("intArray", intArray.getClass()), 993 new DynaProperty("intIndexed", intArray.getClass()), 994 new DynaProperty("intProperty", Integer.TYPE), 995 new DynaProperty("listIndexed", List .class), 996 new DynaProperty("longProperty", Long.TYPE), 997 new DynaProperty("mappedProperty", Map .class), 998 new DynaProperty("mappedIntProperty", Map .class), 999 new DynaProperty("nullProperty", String .class), 1000 new DynaProperty("shortProperty", Short.TYPE), 1001 new DynaProperty("stringArray", stringArray.getClass()), 1002 new DynaProperty("stringIndexed", stringArray.getClass()), 1003 new DynaProperty("stringProperty", String .class), 1004 }); 1005 return (dynaClass); 1006 1007 } 1008 1009 1010 1016 protected void testGetDescriptorBase(String name, Class type) { 1017 1018 try { 1019 DynaProperty descriptor = 1020 bean.getDynaClass().getDynaProperty(name); 1021 assertNotNull("Got descriptor", descriptor); 1022 assertEquals("Got correct type", type, descriptor.getType()); 1023 } catch (Throwable t) { 1024 fail("Threw an exception: " + t); 1025 } 1026 1027 } 1028 1029 1030} 1031 | Popular Tags |