1 18 package org.apache.struts.action; 19 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 import org.apache.commons.beanutils.DynaProperty; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.config.FormBeanConfig; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.config.impl.ModuleConfigImpl; 35 36 37 41 public class TestDynaActionForm extends TestDynaActionFormClass { 42 43 44 49 public TestDynaActionForm(String theName) 50 { 51 super(theName); 52 } 53 54 55 60 public static void main(String [] theArgs) 61 { 62 junit.awtui.TestRunner.main 63 (new String [] {TestDynaActionForm.class.getName()}); 64 } 65 66 67 71 public static Test suite() 72 { 73 return new TestSuite(TestDynaActionForm.class); 75 } 76 77 78 80 81 84 protected ModuleConfig moduleConfig = null; 85 86 87 90 protected DynaActionForm dynaForm = null; 91 92 93 96 protected ActionMapping mapping = null; 97 98 99 protected Log log = null; 100 101 106 protected final static String [] properties = { 107 "booleanProperty", 108 "booleanSecond", 109 "doubleProperty", 110 "floatProperty", 111 "intArray", 112 "intIndexed", 113 "intProperty", 114 "listIndexed", 115 "longProperty", 116 "mappedProperty", 117 "mappedIntProperty", 118 "shortProperty", 120 "stringArray", 121 "stringIndexed", 122 "stringProperty", 123 }; 124 125 126 128 129 public void setUp() { 130 131 super.setUp(); 132 try { 133 dynaForm = (DynaActionForm) dynaClass.newInstance(); 134 } catch (IllegalAccessException e) { 135 throw new RuntimeException (e.getMessage()); 136 } catch (InstantiationException e) { 137 throw new RuntimeException (e.getMessage()); 138 } 139 setupComplexProperties(); 140 moduleConfig = new DynaActionFormConfig(beanConfig); 141 mapping = new DynaActionFormMapping(moduleConfig); 142 log = LogFactory.getLog(this.getClass().getName() + "." + this.getName()); 143 144 } 145 146 147 public void tearDown() { 148 149 super.tearDown(); 150 moduleConfig = null; 151 dynaForm = null; 152 mapping = null; 153 154 } 155 156 157 159 160 public void testBeanCreate() { 162 163 assertEquals("booleanProperty", Boolean.TRUE, 164 (Boolean ) dynaForm.get("booleanProperty")); 165 assertEquals("booleanSecond", Boolean.TRUE, 166 (Boolean ) dynaForm.get("booleanSecond")); 167 assertEquals("doubleProperty", new Double (321.0), 168 (Double ) dynaForm.get("doubleProperty")); 169 assertEquals("floatProperty", new Float ((float) 123.0), 170 (Float ) dynaForm.get("floatProperty")); 171 assertEquals("intProperty", new Integer (123), 172 (Integer ) dynaForm.get("intProperty")); 173 assertEquals("longProperty", new Long ((long) 321), 175 (Long ) dynaForm.get("longProperty")); 176 assertEquals("shortProperty", new Short ((short) 987), 181 (Short ) dynaForm.get("shortProperty")); 182 assertEquals("stringProperty", "This is a string", 183 (String ) dynaForm.get("stringProperty")); 184 185 } 186 187 188 public void testIndexedInitialize() { 191 192 dynaForm.set("intArray", 1, new Integer (111)); 194 assertEquals("intArray[1]", new Integer (111), 195 (Integer ) dynaForm.get("intArray", 1)); 196 dynaForm.set("intIndexed", 2, new Integer (222)); 197 assertEquals("intIndexed[2]", new Integer (222), 198 (Integer ) dynaForm.get("intIndexed", 2)); 199 dynaForm.set("stringArray", 3, "New String 3"); 200 assertEquals("stringArray[3]", "New String 3", 201 (String ) dynaForm.get("stringArray", 3)); 202 dynaForm.set("stringIndexed", 4, "New String 4"); 203 assertEquals("stringIndexed[4]", "New String 4", 204 (String ) dynaForm.get("stringIndexed", 4)); 205 206 dynaForm.initialize(mapping); 209 setupComplexProperties(); 210 testGetIndexedValues(); 211 212 } 213 214 215 public void testScalarInitialize() { 217 218 dynaForm.set("booleanProperty", Boolean.FALSE); 220 assertEquals("booleanProperty", Boolean.FALSE, 221 (Boolean ) dynaForm.get("booleanProperty")); 222 dynaForm.set("booleanSecond", Boolean.FALSE); 223 dynaForm.set("doubleProperty", new Double (654.0)); 224 dynaForm.set("floatProperty", new Float ((float) 543.0)); 225 dynaForm.set("intProperty", new Integer (555)); 226 dynaForm.set("longProperty", new Long ((long) 777)); 227 dynaForm.set("shortProperty", new Short ((short) 222)); 228 dynaForm.set("stringProperty", "New String Value"); 229 assertEquals("stringProperty", "New String Value", 230 (String ) dynaForm.get("stringProperty")); 231 232 dynaForm.initialize(mapping); 234 setupComplexProperties(); 235 testBeanCreate(); 236 237 } 238 239 240 242 243 246 public void testGetDescriptorArguments() { 247 248 DynaProperty descriptor = 249 dynaForm.getDynaClass().getDynaProperty("unknown"); 250 assertNull("Unknown property descriptor should be null", 251 descriptor); 252 253 try { 254 dynaForm.getDynaClass().getDynaProperty(null); 255 fail("Should throw IllegalArgumentException"); 256 } catch (IllegalArgumentException e) { 257 ; } 259 260 } 261 262 263 266 public void testGetDescriptorBoolean() { 267 268 testGetDescriptorBase("booleanProperty", Boolean.TYPE); 269 270 } 271 272 273 276 public void testGetDescriptorDouble() { 277 278 testGetDescriptorBase("doubleProperty", Double.TYPE); 279 280 } 281 282 283 286 public void testGetDescriptorFloat() { 287 288 testGetDescriptorBase("floatProperty", Float.TYPE); 289 290 } 291 292 293 296 public void testGetDescriptorInt() { 297 298 testGetDescriptorBase("intProperty", Integer.TYPE); 299 300 } 301 302 303 306 public void testGetDescriptorLong() { 307 308 testGetDescriptorBase("longProperty", Long.TYPE); 309 310 } 311 312 313 317 public void testGetDescriptorSecond() { 318 319 testGetDescriptorBase("booleanSecond", Boolean.TYPE); 320 321 } 322 323 324 327 public void testGetDescriptorShort() { 328 329 testGetDescriptorBase("shortProperty", Short.TYPE); 330 331 } 332 333 334 337 public void testGetDescriptorString() { 338 339 testGetDescriptorBase("stringProperty", String .class); 340 341 } 342 343 344 348 public void testGetDescriptors() { 349 350 DynaProperty pd[] = dynaForm.getDynaClass().getDynaProperties(); 351 assertNotNull("Got descriptors", pd); 352 int count[] = new int[properties.length]; 353 for (int i = 0; i < pd.length; i++) { 354 String name = pd[i].getName(); 355 for (int j = 0; j < properties.length; j++) { 356 if (name.equals(properties[j])) 357 count[j]++; 358 } 359 } 360 for (int j = 0; j < properties.length; j++) { 361 if (count[j] < 0) 362 fail("Missing property " + properties[j]); 363 else if (count[j] > 1) 364 fail("Duplicate property " + properties[j]); 365 } 366 367 } 368 369 370 373 public void testGetIndexedArguments() { 374 375 try { 376 dynaForm.get("intArray", -1); 377 fail("Should throw IndexOutOfBoundsException"); 378 } catch (IndexOutOfBoundsException e) { 379 ; } 381 382 } 383 384 385 388 public void testGetIndexedValues() { 389 390 Object value = null; 391 392 for (int i = 0; i < 5; i++) { 393 394 value = dynaForm.get("intArray", i); 395 assertNotNull("intArray returned value " + i, value); 396 assertTrue("intArray returned Integer " + i, 397 value instanceof Integer ); 398 assertEquals("intArray returned correct " + i, i * 10, 399 ((Integer ) value).intValue()); 400 401 value = dynaForm.get("intIndexed", i); 402 assertNotNull("intIndexed returned value " + i, value); 403 assertTrue("intIndexed returned Integer " + i, 404 value instanceof Integer ); 405 assertEquals("intIndexed returned correct " + i, i * 100, 406 ((Integer ) value).intValue()); 407 408 value = dynaForm.get("listIndexed", i); 409 assertNotNull("listIndexed returned value " + i, value); 410 assertTrue("list returned String " + i, 411 value instanceof String ); 412 assertEquals("listIndexed returned correct " + i, 413 "String " + i, (String ) value); 414 415 value = dynaForm.get("stringArray", i); 416 assertNotNull("stringArray returned value " + i, value); 417 assertTrue("stringArray returned String " + i, 418 value instanceof String ); 419 assertEquals("stringArray returned correct " + i, 420 "String " + i, (String ) value); 421 422 value = dynaForm.get("stringIndexed", i); 423 assertNotNull("stringIndexed returned value " + i, value); 424 assertTrue("stringIndexed returned String " + i, 425 value instanceof String ); 426 assertEquals("stringIndexed returned correct " + i, 427 "String " + i, (String ) value); 428 429 } 430 431 432 } 433 434 435 438 public void testGetMappedArguments() { 439 Object value = dynaForm.get("mappedProperty", "unknown"); 440 assertNull("Should not return a value", value); 441 } 442 443 444 447 public void testGetMappedValues() { 448 449 Object value = null; 450 451 value = dynaForm.get("mappedProperty", "First Key"); 452 assertEquals("Can find first value", "First Value", value); 453 454 value = dynaForm.get("mappedProperty", "Second Key"); 455 assertEquals("Can find second value", "Second Value", value); 456 457 value = dynaForm.get("mappedProperty", "Third Key"); 458 assertNull("Can not find third value", value); 459 460 } 461 462 463 466 public void testGetSimpleArguments() { 467 468 try { 469 dynaForm.get(null); 470 fail("Should throw IllegalArgumentException"); 471 } catch (IllegalArgumentException e) { 472 ; } 474 475 } 476 477 478 481 public void testGetSimpleBoolean() { 482 483 Object value = dynaForm.get("booleanProperty"); 484 assertNotNull("Got a value", value); 485 assertTrue("Got correct type", (value instanceof Boolean )); 486 assertTrue("Got correct value", 487 ((Boolean ) value).booleanValue() == true); 488 } 489 490 491 494 public void testGetSimpleDouble() { 495 496 Object value = dynaForm.get("doubleProperty"); 497 assertNotNull("Got a value", value); 498 assertTrue("Got correct type", (value instanceof Double )); 499 assertEquals("Got correct value", 500 ((Double ) value).doubleValue(), 501 (double) 321.0, 502 (double) 0.005); 503 504 } 505 506 507 510 public void testGetSimpleFloat() { 511 512 Object value = dynaForm.get("floatProperty"); 513 assertNotNull("Got a value", value); 514 assertTrue("Got correct type", (value instanceof Float )); 515 assertEquals("Got correct value", 516 ((Float ) value).floatValue(), 517 (float) 123.0, 518 (float) 0.005); 519 520 } 521 522 523 526 public void testGetSimpleInt() { 527 528 Object value = dynaForm.get("intProperty"); 529 assertNotNull("Got a value", value); 530 assertTrue("Got correct type", (value instanceof Integer )); 531 assertEquals("Got correct value", 532 ((Integer ) value).intValue(), 533 (int) 123); 534 535 } 536 537 538 541 public void testGetSimpleLong() { 542 543 Object value = dynaForm.get("longProperty"); 544 assertNotNull("Got a value", value); 545 assertTrue("Got correct type", (value instanceof Long )); 546 assertEquals("Got correct value", 547 ((Long ) value).longValue(), 548 (long) 321); 549 550 } 551 552 553 556 public void testGetSimpleShort() { 557 558 Object value = dynaForm.get("shortProperty"); 559 assertNotNull("Got a value", value); 560 assertTrue("Got correct type", (value instanceof Short )); 561 assertEquals("Got correct value", 562 ((Short ) value).shortValue(), 563 (short) 987); 564 565 } 566 567 568 571 public void testGetSimpleString() { 572 573 Object value = dynaForm.get("stringProperty"); 574 assertNotNull("Got a value", value); 575 assertTrue("Got correct type", (value instanceof String )); 576 assertEquals("Got correct value", 577 (String ) value, 578 "This is a string"); 579 580 } 581 582 583 586 public void testMappedContains() { 587 588 assertTrue("Can see first key", 589 dynaForm.contains("mappedProperty", "First Key")); 590 591 assertTrue("Can not see unknown key", 592 !dynaForm.contains("mappedProperty", "Unknown Key")); 593 594 } 595 596 597 600 public void testMappedRemove() { 601 602 assertTrue("Can see first key", 603 dynaForm.contains("mappedProperty", "First Key")); 604 dynaForm.remove("mappedProperty", "First Key"); 605 assertTrue("Can not see first key", 606 !dynaForm.contains("mappedProperty", "First Key")); 607 608 assertTrue("Can not see unknown key", 609 !dynaForm.contains("mappedProperty", "Unknown Key")); 610 dynaForm.remove("mappedProperty", "Unknown Key"); 611 assertTrue("Can not see unknown key", 612 !dynaForm.contains("mappedProperty", "Unknown Key")); 613 614 } 615 616 617 620 public void testSetIndexedArguments() { 621 622 try { 623 dynaForm.set("intArray", -1, new Integer (0)); 624 fail("Should throw IndexOutOfBoundsException"); 625 } catch (IndexOutOfBoundsException e) { 626 ; } 628 629 } 630 631 632 635 public void testSetIndexedValues() { 636 637 Object value = null; 638 639 dynaForm.set("intArray", 0, new Integer (1)); 640 value = (Integer ) dynaForm.get("intArray", 0); 641 assertNotNull("Returned new value 0", value); 642 assertTrue("Returned Integer new value 0", 643 value instanceof Integer ); 644 assertEquals("Returned correct new value 0", 1, 645 ((Integer ) value).intValue()); 646 647 dynaForm.set("intIndexed", 1, new Integer (11)); 648 value = (Integer ) dynaForm.get("intIndexed", 1); 649 assertNotNull("Returned new value 1", value); 650 assertTrue("Returned Integer new value 1", 651 value instanceof Integer ); 652 assertEquals("Returned correct new value 1", 11, 653 ((Integer ) value).intValue()); 654 dynaForm.set("listIndexed", 2, "New Value 2"); 655 value = (String ) dynaForm.get("listIndexed", 2); 656 assertNotNull("Returned new value 2", value); 657 assertTrue("Returned String new value 2", 658 value instanceof String ); 659 assertEquals("Returned correct new value 2", "New Value 2", 660 (String ) value); 661 662 dynaForm.set("stringArray", 3, "New Value 3"); 663 value = (String ) dynaForm.get("stringArray", 3); 664 assertNotNull("Returned new value 3", value); 665 assertTrue("Returned String new value 3", 666 value instanceof String ); 667 assertEquals("Returned correct new value 3", "New Value 3", 668 (String ) value); 669 670 dynaForm.set("stringIndexed", 4, "New Value 4"); 671 value = (String ) dynaForm.get("stringIndexed", 4); 672 assertNotNull("Returned new value 4", value); 673 assertTrue("Returned String new value 4", 674 value instanceof String ); 675 assertEquals("Returned correct new value 4", "New Value 4", 676 (String ) value); 677 678 679 } 680 681 682 685 public void testSetMappedValues() { 686 687 688 dynaForm.set("mappedProperty", "First Key", "New First Value"); 689 assertEquals("Can replace old value", 690 "New First Value", 691 (String ) dynaForm.get("mappedProperty", "First Key")); 692 693 dynaForm.set("mappedProperty", "Fourth Key", "Fourth Value"); 694 assertEquals("Can set new value", 695 "Fourth Value", 696 (String ) dynaForm.get("mappedProperty", "Fourth Key")); 697 698 699 } 700 701 702 705 public void testSetSimpleBoolean() { 706 707 boolean oldValue = 708 ((Boolean ) dynaForm.get("booleanProperty")).booleanValue(); 709 boolean newValue = !oldValue; 710 dynaForm.set("booleanProperty", new Boolean (newValue)); 711 assertTrue("Matched new value", 712 newValue == 713 ((Boolean ) dynaForm.get("booleanProperty")).booleanValue()); 714 715 } 716 717 718 721 public void testSetSimpleDouble() { 722 723 double oldValue = 724 ((Double ) dynaForm.get("doubleProperty")).doubleValue(); 725 double newValue = oldValue + 1.0; 726 dynaForm.set("doubleProperty", new Double (newValue)); 727 assertEquals("Matched new value", 728 newValue, 729 ((Double ) dynaForm.get("doubleProperty")).doubleValue(), 730 (double) 0.005); 731 732 } 733 734 735 738 public void testSetSimpleFloat() { 739 740 float oldValue = 741 ((Float ) dynaForm.get("floatProperty")).floatValue(); 742 float newValue = oldValue + (float) 1.0; 743 dynaForm.set("floatProperty", new Float (newValue)); 744 assertEquals("Matched new value", 745 newValue, 746 ((Float ) dynaForm.get("floatProperty")).floatValue(), 747 (float) 0.005); 748 749 } 750 751 752 755 public void testSetSimpleInt() { 756 757 int oldValue = 758 ((Integer ) dynaForm.get("intProperty")).intValue(); 759 int newValue = oldValue + 1; 760 dynaForm.set("intProperty", new Integer (newValue)); 761 assertEquals("Matched new value", 762 newValue, 763 ((Integer ) dynaForm.get("intProperty")).intValue()); 764 } 765 766 767 770 public void testSetSimpleLong() { 771 772 long oldValue = 773 ((Long ) dynaForm.get("longProperty")).longValue(); 774 long newValue = oldValue + 1; 775 dynaForm.set("longProperty", new Long (newValue)); 776 assertEquals("Matched new value", 777 newValue, 778 ((Long ) dynaForm.get("longProperty")).longValue()); 779 780 } 781 782 783 786 public void testSetSimpleShort() { 787 788 short oldValue = 789 ((Short ) dynaForm.get("shortProperty")).shortValue(); 790 short newValue = (short) (oldValue + 1); 791 dynaForm.set("shortProperty", new Short (newValue)); 792 assertEquals("Matched new value", 793 newValue, 794 ((Short ) dynaForm.get("shortProperty")).shortValue()); 795 796 } 797 798 799 802 public void testSetSimpleString() { 803 804 String oldValue = (String ) dynaForm.get("stringProperty"); 805 String newValue = oldValue + " Extra Value"; 806 dynaForm.set("stringProperty", newValue); 807 assertEquals("Matched new value", 808 newValue, 809 (String ) dynaForm.get("stringProperty")); 810 811 } 812 813 814 816 817 821 protected void setupComplexProperties() { 822 823 List listIndexed = new ArrayList (); 824 listIndexed.add("String 0"); 825 listIndexed.add("String 1"); 826 listIndexed.add("String 2"); 827 listIndexed.add("String 3"); 828 listIndexed.add("String 4"); 829 dynaForm.set("listIndexed", listIndexed); 830 831 Map mappedProperty = new HashMap (); 832 mappedProperty.put("First Key", "First Value"); 833 mappedProperty.put("Second Key", "Second Value"); 834 dynaForm.set("mappedProperty", mappedProperty); 835 836 Map mappedIntProperty = new HashMap (); 837 mappedIntProperty.put("One", new Integer (1)); 838 mappedIntProperty.put("Two", new Integer (2)); 839 dynaForm.set("mappedIntProperty", mappedIntProperty); 840 841 } 842 843 844 845 851 protected void testGetDescriptorBase(String name, Class type) { 852 853 DynaProperty descriptor = 854 dynaForm.getDynaClass().getDynaProperty(name); 855 assertNotNull("Got descriptor", descriptor); 856 assertEquals("Got correct type", type, descriptor.getType()); 857 858 } 859 860 861 } 862 863 864 class DynaActionFormMapping extends ActionMapping { 865 866 public DynaActionFormMapping(ModuleConfig appConfig) { 867 this.appConfig = appConfig; 868 } 869 870 private ModuleConfig appConfig = null; 871 872 public ModuleConfig getModuleConfig() { 873 return (this.appConfig); 874 } 875 876 public String getName() { 877 return ("dynaForm"); 878 } 879 880 } 881 882 883 884 class DynaActionFormConfig extends ModuleConfigImpl { 885 886 public DynaActionFormConfig(FormBeanConfig beanConfig) { 887 super(""); 888 this.beanConfig = beanConfig; 889 } 890 891 private FormBeanConfig beanConfig = null; 892 893 public FormBeanConfig findFormBeanConfig(String name) { 894 return (this.beanConfig); 895 } 896 897 898 } 899 900 901 | Popular Tags |