1 7 package com.inversoft.beans.test; 8 9 10 import java.util.ArrayList ; 11 import java.util.HashSet ; 12 import java.util.List ; 13 14 import junit.framework.TestCase; 15 16 import com.inversoft.beans.BeanException; 17 import com.inversoft.beans.BeanProperty; 18 import com.inversoft.beans.IndexedBeanProperty; 19 import com.inversoft.beans.JavaBean; 20 import com.inversoft.beans.NestedBeanProperty; 21 import com.inversoft.util.typeconverter.TypeConversionException; 22 23 24 30 public class JavaBeanTest extends TestCase { 31 32 33 public JavaBeanTest(String name) { 34 super(name); 35 } 36 37 38 41 public void testConstructors() { 42 43 try { 44 JavaBean jb = new JavaBean(Bean1.class); 45 assertEquals("com.inversoft.beans.test.Bean1", jb.getClassName()); 46 jb = new JavaBean("com.inversoft.beans.test.Bean1"); 47 assertEquals("com.inversoft.beans.test.Bean1", jb.getClassName()); 48 } catch (BeanException e) { 49 fail(e.toString()); 50 } 51 52 try { 53 new JavaBean("bad.class"); 54 fail("Should have failed"); 55 } catch (BeanException e) { 56 } 58 } 59 60 63 public void testGetLocalProperty() { 64 65 try { 66 JavaBean jb = new JavaBean(Bean1.class); 67 jb.getBeanProperty("property1"); 68 BeanProperty bp = jb.getBeanProperty("property1"); 69 assertTrue("Should never be null", bp != null); 70 } catch (BeanException e) { 71 fail(e.toString()); 72 } 73 } 74 75 78 public void testGetDeepProperty() { 79 80 try { 82 JavaBean jb = new JavaBean(Bean1.class); 83 jb.getBeanProperty("property1.property2.property3"); 84 } catch (BeanException e) { 85 fail(e.toString()); 86 } 87 88 try { 89 JavaBean jb = new JavaBean(Bean1.class); 90 jb.getBeanProperty("property1"); 92 jb.getBeanProperty("property1.property2"); 93 jb.getBeanProperty("property1.property2.property3"); 94 } catch (BeanException e) { 96 fail(e.toString()); 97 } 98 } 99 100 103 public void testChildJavaBeans() { 104 105 try { 108 JavaBean jb = new JavaBean(Bean1.class); 109 jb.getBeanProperty("property1.property2.property3"); 110 JavaBean c1 = jb.getChildJavaBean("property1"); 111 assertTrue("children should never be null", c1 != null); 112 assertTrue("children should be of type Bean2", c1.getBeanClass() == Bean2.class); 113 JavaBean c2 = jb.getChildJavaBean("property1.property2"); 114 assertTrue("children should never be null 2", c2 != null); 115 assertTrue("children should be of type Bean3", c2.getBeanClass() == Bean3.class); 116 JavaBean c3 = jb.getChildJavaBean("property1.property2.property3"); 117 assertTrue("children should never be null 3", c3 != null); 118 assertTrue("children should be of type String", c3.getBeanClass() == String .class); 119 } catch (BeanException e) { 120 fail(e.toString()); 121 } 122 } 123 124 127 public void testChildBenProperties() { 128 129 try { 132 JavaBean jb = new JavaBean(Bean1.class); 133 jb.getBeanProperty("property1.property2.property3"); 134 BeanProperty bp = jb.getBeanProperty("property1"); 135 assertTrue("Should never be null", bp != null); 136 assertTrue("Should be of type Bean2", bp.getPropertyType() == Bean2.class); 137 BeanProperty bp2 = jb.getBeanProperty("property1.property2"); 138 assertTrue("Should never be null 2", bp2 != null); 139 assertTrue("Should be of type Bean3", bp2.getPropertyType() == Bean3.class); 140 BeanProperty bp3 = jb.getBeanProperty("property1.property2.property3"); 141 assertTrue("Should never be null 3", bp3 != null); 142 assertTrue("Should be of type String", bp3.getPropertyType() == String .class); 143 } catch (BeanException e) { 144 fail(e.toString()); 145 } 146 } 147 148 151 public void testChildJavaBeanErrors() { 152 153 try { 154 JavaBean jb = new JavaBean(Bean1.class); 155 jb.getBeanProperty("property2"); 156 fail("Should have failed because Bean1 has no property2"); 157 } catch (BeanException e) { 158 assertTrue("Should have a root cause of NoSuchMethodException", 160 e.getCause() != null && e.getCause() instanceof NoSuchMethodException ); 161 } 162 163 try { 164 JavaBean jb = new JavaBean(Bean1.class); 165 jb.getBeanProperty("property1.property3"); 166 fail("Should have failed because Bean2 has no property3"); 167 } catch (BeanException e) { 168 assertTrue("Should have a root cause of NoSuchMethodException", 170 e.getCause() != null && e.getCause() instanceof NoSuchMethodException ); 171 assertTrue("Should NOT have a target", e.getTarget() == null); 172 } 173 } 174 175 178 public void testLocalGetting() { 179 180 try { 182 Bean1 bean = new Bean1(); 183 JavaBean jb = new JavaBean(Bean1.class); 184 jb.getBeanProperty("property1"); 185 assertTrue("Should be null", jb.getPropertyValue("property1", bean, true) == null); 186 } catch (BeanException e) { 187 fail(e.toString()); 188 } 189 190 try { 192 Bean1 bean = new Bean1(); 193 Bean2 bean2 = new Bean2(); 194 bean.setProperty1(bean2); 195 JavaBean jb = new JavaBean(Bean1.class); 196 jb.getBeanProperty("property1"); 197 assertTrue("Should be bean2 using prepopulated JavaBean", 198 jb.getPropertyValue("property1", bean) == bean2); 199 } catch (BeanException e) { 200 fail(e.toString()); 201 } 202 203 try { 205 Bean1 bean = new Bean1(); 206 Bean2 bean2 = new Bean2(); 207 bean.setProperty1(bean2); 208 JavaBean jb = new JavaBean(Bean1.class); 209 assertTrue("Should be bean2 using prepopulated JavaBean again", 210 jb.getPropertyValue("property1", bean) == bean2); 211 } catch (BeanException e) { 212 fail(e.toString()); 213 } 214 } 215 216 219 public void testNestedGetting() { 220 221 try { 223 Bean1 bean = new Bean1(); 224 JavaBean jb = new JavaBean(Bean1.class); 225 jb.getBeanProperty("property1.property2.property3"); 226 jb.getPropertyValue("property1.property2.property3", bean, true); 227 fail("Should have error out because property1 is null"); 228 } catch (BeanException e) { 229 assertTrue("Should not have a root cause or target", 231 e.getCause() == null && e.getTarget() == null); 232 } 233 234 try { 236 Bean1 bean = new Bean1(); 237 bean.setProperty1(new Bean2()); 238 bean.getProperty1().setProperty2(new Bean3()); 239 bean.getProperty1().getProperty2().setProperty3("foo"); 240 JavaBean jb = new JavaBean(Bean1.class); 241 jb.getBeanProperty("property1.property2.property3"); 242 Object obj = jb.getPropertyValue("property1.property2", bean); 243 assertTrue("Should be a Bean3 instance prepopulated JavaBean", obj instanceof Bean3); 244 } catch (BeanException e) { 245 fail(e.toString()); 247 } 248 249 try { 251 Bean1 bean = new Bean1(); 252 bean.setProperty1(new Bean2()); 253 bean.getProperty1().setProperty2(new Bean3()); 254 bean.getProperty1().getProperty2().setProperty3("foo"); 255 JavaBean jb = new JavaBean(Bean1.class); 256 jb.getBeanProperty("property1.property2.property3"); 257 String value = (String ) jb.getPropertyValue("property1.property2.property3", bean); 258 assertTrue("Should be foo using prepopulated JavaBean", value.equals("foo")); 259 } catch (BeanException e) { 260 fail(e.toString()); 262 } 263 264 try { 266 Bean1 bean = new Bean1(); 267 bean.setProperty1(new Bean2()); 268 bean.getProperty1().setProperty2(new Bean3()); 269 bean.getProperty1().getProperty2().setProperty3("foo"); 270 271 JavaBean jb = new JavaBean(Bean1.class); 272 String value = (String ) jb.getPropertyValue("property1.property2.property3", bean); 273 assertTrue("Should be foo using prepopulated JavaBean", value.equals("foo")); 274 } catch (BeanException e) { 275 fail(e.toString()); 277 } 278 } 279 280 283 public void testLocalSetting() { 284 285 try { 287 Bean1 bean = new Bean1(); 288 JavaBean jb = new JavaBean(Bean1.class); 289 bean.setProperty1(new Bean2()); 290 jb.getBeanProperty("property1"); 291 jb.setPropertyValue("property1", bean, null, false, true); 292 assertTrue("Should have been set to null", bean.getProperty1() == null); 293 } catch (BeanException e) { 294 fail(e.toString()); 295 } catch (TypeConversionException tce) { 296 fail(tce.toString()); 297 } 298 299 try { 301 Bean1 bean = new Bean1(); 302 Bean2 bean2 = new Bean2(); 303 JavaBean jb = new JavaBean(Bean1.class); 304 jb.getBeanProperty("property1"); 305 jb.setPropertyValue("property1", bean, bean2); 306 assertTrue("Should be set to same object", bean.getProperty1() == bean2); 307 } catch (BeanException e) { 308 fail(e.toString()); 309 } 310 311 try { 313 Bean1 bean = new Bean1(); 314 Bean2 bean2 = new Bean2(); 315 JavaBean jb = new JavaBean(Bean1.class); 316 jb.setPropertyValue("property1", bean, bean2); 317 assertTrue("Should be set to same object", bean.getProperty1() == bean2); 318 } catch (BeanException e) { 319 fail(e.toString()); 320 } 321 } 322 323 326 public void testNestedSetting() { 327 328 try { 330 Bean1 bean = new Bean1(); 331 JavaBean jb = new JavaBean(Bean1.class); 332 jb.getBeanProperty("property1.property2.property3"); 333 jb.setPropertyValue("property1.property2.property3", bean, "foo", false, true); 334 fail("Should have error out because property1 is null"); 335 } catch (BeanException e) { 336 assertTrue("Should not have a root cause or target", 338 e.getCause() == null && e.getTarget() == null); 339 } catch (TypeConversionException tce) { 340 fail(tce.toString()); 341 } 342 343 try { 345 Bean1 bean = new Bean1(); 346 JavaBean jb = new JavaBean(Bean1.class); 347 jb.getBeanProperty("property1.property2.property3"); 348 jb.setPropertyValue("property1.property2.property3", bean, "foo", false); 349 assertTrue("Should be set to foo", bean.getProperty1().getProperty2().getProperty3().equals("foo")); 350 } catch (BeanException e) { 351 fail(e.toString()); 352 } catch (TypeConversionException tce) { 353 fail(tce.toString()); 354 } 355 356 try { 358 Bean1 bean = new Bean1(); 359 JavaBean jb = new JavaBean(Bean1.class); 360 jb.getBeanProperty("property1.property2.property3"); 361 jb.setPropertyValue("property1", bean, new Bean2(), false); 362 assertTrue("Should have a bean2", bean.getProperty1() != null); 363 assertTrue("Should not have a bean3", bean.getProperty1().getProperty2() == null); 364 } catch (BeanException e) { 365 fail(e.toString()); 366 } catch (TypeConversionException tce) { 367 fail(tce.toString()); 368 } 369 370 try { 372 Bean1 bean = new Bean1(); 373 JavaBean jb = new JavaBean(Bean1.class); 374 jb.setPropertyValue("property1.property2.property3", bean, "foo", false); 375 assertTrue("Should be set to foo", bean.getProperty1().getProperty2().getProperty3().equals("foo")); 376 } catch (BeanException e) { 377 fail(e.toString()); 378 } catch (TypeConversionException tce) { 379 fail(tce.toString()); 380 } 381 } 382 383 386 public void testGettingIndexed() { 387 388 try { 390 Bean1 bean = new Bean1(); 391 Bean2 bean2 = new Bean2(); 392 bean.setIndexed(0, bean2); 393 JavaBean jb = new JavaBean(Bean1.class); 394 jb.getBeanProperty("indexed"); 395 assertTrue("Should be same object", jb.getPropertyValue("indexed[0]", bean) == bean2); 396 } catch (BeanException be) { 397 fail(be.toString()); 398 } 399 400 try { 402 Bean1 bean = new Bean1(); 403 bean.setIndexed(0, new Bean2()); 404 bean.getIndexed(0).setName("foo"); 405 JavaBean jb = new JavaBean(Bean1.class); 406 jb.getBeanProperty("indexed.name"); 407 assertTrue("Should be foo", jb.getPropertyValue("indexed[0].name", bean).equals("foo")); 408 } catch (BeanException be) { 409 fail(be.toString()); 410 } 411 412 try { 414 Bean1 bean = new Bean1(); 415 bean.setIndexed(0, new Bean2()); 416 bean.getIndexed(0).setName("foo"); 417 JavaBean jb = new JavaBean(Bean1.class); 418 jb.getBeanProperty("indexed.name"); 419 jb.getPropertyValue("indexed[1].name", bean, true); 420 fail("Should have errored out because indexed[1] is null"); 421 } catch (BeanException be) { 422 assertTrue("Should not have a root cause or target", 424 be.getCause() == null && be.getTarget() == null); 425 } 426 427 try { 429 Bean1 bean = new Bean1(); 430 bean.setIndexed(0, new Bean2()); 431 bean.getIndexed(0).setProperty2(new Bean3()); 432 bean.getIndexed(0).getProperty2().setProperty3("foo"); 433 JavaBean jb = new JavaBean(Bean1.class); 434 jb.getBeanProperty("indexed.property2.property3"); 435 assertTrue("Should have been foo", 436 jb.getPropertyValue("indexed[0].property2.property3", bean, true).equals("foo")); 437 } catch (BeanException be) { 438 fail(be.toString()); 439 } 440 } 441 442 445 public void testGettingIndexedIndices() { 446 447 try { 449 Bean1 bean = new Bean1(); 450 Bean2 bean2 = new Bean2(); 451 bean.setIndexed(0, bean2); 452 JavaBean jb = new JavaBean(Bean1.class); 453 jb.getBeanProperty("indexed"); 454 assertTrue("Should be same object", jb.getPropertyValue("indexed", bean, new int[][]{{0}}) == bean2); 455 } catch (BeanException be) { 456 fail(be.toString()); 457 } 458 459 try { 461 Bean1 bean = new Bean1(); 462 bean.setIndexed(0, new Bean2()); 463 bean.getIndexed(0).setName("foo"); 464 JavaBean jb = new JavaBean(Bean1.class); 465 jb.getBeanProperty("indexed.name"); 466 assertTrue("Should be foo", jb.getPropertyValue("indexed.name", bean, new int[][]{{0}}).equals("foo")); 467 } catch (BeanException be) { 468 fail(be.toString()); 469 } 470 471 try { 473 Bean1 bean = new Bean1(); 474 bean.setIndexed(0, new Bean2()); 475 bean.getIndexed(0).setName("foo"); 476 JavaBean jb = new JavaBean(Bean1.class); 477 jb.getBeanProperty("indexed.name"); 478 jb.getPropertyValue("indexed.name", bean, new int[][]{{1}}, true); 479 fail("Should have errored out because indexed[1] is null"); 480 } catch (BeanException be) { 481 assertTrue("Should not have a root cause or target", 483 be.getCause() == null && be.getTarget() == null); 484 } 485 486 try { 488 Bean1 bean = new Bean1(); 489 bean.setIndexed(0, new Bean2()); 490 bean.getIndexed(0).setProperty2(new Bean3()); 491 bean.getIndexed(0).getProperty2().setProperty3("foo"); 492 JavaBean jb = new JavaBean(Bean1.class); 493 jb.getBeanProperty("indexed.property2.property3"); 494 assertTrue("Should have been foo", 495 jb.getPropertyValue("indexed.property2.property3", bean, new int[][]{{0}}, true).equals("foo")); 496 } catch (BeanException be) { 497 fail(be.toString()); 498 } 499 500 try { 502 Bean1 bean = new Bean1(); 503 bean.setIndexed(0, new Bean2()); 504 bean.getIndexed(0).setIndexed2(0, new Bean3()); 505 bean.getIndexed(0).getIndexed2(0).setProperty3("foo"); 506 JavaBean jb = new JavaBean(Bean1.class); 507 jb.getBeanProperty("indexed.indexed2.property3"); 508 assertTrue("Should have been foo", 509 jb.getPropertyValue("indexed.indexed2.property3", bean, new int[][]{{0}, {0}}, true).equals("foo")); 510 } catch (BeanException be) { 511 fail(be.toString()); 512 } 513 514 try { 516 List list = new ArrayList (); 517 List list1 = new ArrayList (); 518 List list2 = new ArrayList (); 519 list.add(list1); 520 list.add(list2); 521 list1.add(new Integer (0)); 522 list2.add(new Integer (0)); 523 Bean1 bean = new Bean1(); 524 bean.setIndexed(0, new Bean2()); 525 bean.getIndexed(0).setIndexed2(0, new Bean3()); 526 bean.getIndexed(0).getIndexed2(0).setProperty3("foo"); 527 JavaBean jb = new JavaBean(Bean1.class); 528 jb.getBeanProperty("indexed.indexed2.property3"); 529 assertTrue("Should have been foo", 530 jb.getPropertyValue("indexed.indexed2.property3", bean, list, true).equals("foo")); 531 } catch (BeanException be) { 532 fail(be.toString()); 533 } 534 535 try { 537 List list = new ArrayList (); 538 List list1 = new ArrayList (); 539 List list2 = new ArrayList (); 540 list.add(list1); 541 list.add(list2); 542 list1.add(new Integer (0)); 543 list2.add(new String ("foo")); 544 Bean1 bean = new Bean1(); 545 bean.setIndexed(0, new Bean2()); 546 bean.getIndexed(0).setIndexed2(0, new Bean3()); 547 bean.getIndexed(0).getIndexed2(0).setProperty3("foo"); 548 JavaBean jb = new JavaBean(Bean1.class); 549 jb.getBeanProperty("indexed.indexed2.property3"); 550 jb.getPropertyValue("indexed.indexed2.property3", bean, list, true); 551 fail("Should have thrown an exception because the indices list contains a String"); 552 } catch (BeanException be) { 553 System.err.println(be.toString()); 554 assertTrue("Should not have a root cause or target", be.getCause() == null && be.getTarget() == null); 555 } 556 } 557 558 561 public void testSettingIndexed() { 562 563 try { 565 Bean1 bean = new Bean1(); 566 Bean2 bean2 = new Bean2(); 567 JavaBean jb = new JavaBean(Bean1.class); 568 jb.getBeanProperty("indexed"); 569 jb.setPropertyValue("indexed[0]", bean, bean2, false, true); 570 assertTrue("Should be same object", bean.getIndexed(0) == bean2); 571 } catch (BeanException be) { 572 fail(be.toString()); 573 } 574 575 try { 577 Bean1 bean = new Bean1(); 578 JavaBean jb = new JavaBean(Bean1.class); 579 jb.getBeanProperty("indexed.name"); 580 jb.setPropertyValue("indexed[0].name", bean, "foo", true, true); 581 fail("Should have thrown and exception because indexed[0] is null"); 582 } catch (BeanException be) { 583 assertTrue("Should not have a root cause or target", 585 be.getCause() == null && be.getTarget() == null); 586 } 587 588 try { 590 Bean1 bean = new Bean1(); 591 JavaBean jb = new JavaBean(Bean1.class); 592 jb.getBeanProperty("indexed.name"); 593 jb.setPropertyValue("indexed[0].name", bean, "foo", false); 594 assertTrue("Should be foo", bean.getIndexed(0).getName().equals("foo")); 595 } catch (BeanException be) { 596 fail(be.toString()); 597 } 598 599 try { 601 Bean1 bean = new Bean1(); 602 JavaBean jb = new JavaBean(Bean1.class); 603 jb.getBeanProperty("indexed.property2.integer3"); 604 jb.setPropertyValue("indexed[0].property2.integer3", bean, "16", true, false); 605 assertTrue("Should be 16", bean.getIndexed(0).getProperty2().getInteger3().equals(new Integer (16))); 606 } catch (BeanException be) { 607 fail(be.toString()); 608 } catch (TypeConversionException tce) { 609 fail(tce.toString()); 610 } 611 } 612 613 616 public void testInstantiate() { 617 618 try { 619 JavaBean jb = new JavaBean(Bean1.class); 620 Object bean1 = jb.instantiate(); 621 assertTrue("Should be an instance of Bean1", bean1 instanceof Bean1); 622 } catch (BeanException be) { 623 fail(be.toString()); 624 } 625 } 626 627 630 public void testIndexedProperty() { 631 632 try { 634 Bean1 bean = new Bean1(); 635 Bean2 bean2 = new Bean2(); 636 JavaBean jb = new JavaBean(Bean1.class); 637 IndexedBeanProperty ibp = jb.getIndexedBeanProperty("indexed"); 638 jb.setPropertyValue("indexed[0]", bean, bean2, true, true); 639 assertTrue("Should be same object", ibp.getPropertyValue(bean, 0) == bean2); 640 } catch (BeanException be) { 641 be.printStackTrace(); 642 fail(be.toString()); 643 } 644 645 try { 647 JavaBean jb = new JavaBean(Bean1.class); 648 assertTrue("Should be indexed", jb.isBeanPropertyIndexed("indexed")); 649 } catch (BeanException be) { 650 be.printStackTrace(); 651 fail(be.toString()); 652 } 653 654 try { 656 Bean1 bean = new Bean1(); 657 Bean2 bean2 = new Bean2(); 658 Bean3 bean3 = new Bean3(); 659 JavaBean jb = new JavaBean(Bean1.class); 660 IndexedBeanProperty ibp = jb.getIndexedBeanProperty("property1.indexed2"); 661 662 bean.setProperty1(bean2); 663 jb.setPropertyValue("property1.indexed2[0]", bean, bean3, true, true); 664 assertTrue("Should be same object", ibp.getPropertyValue(bean2, 0) == bean3); 665 } catch (BeanException be) { 666 be.printStackTrace(); 667 fail(be.toString()); 668 } 669 670 try { 672 JavaBean jb = new JavaBean(Bean1.class); 673 assertTrue("Should be indexed", jb.isBeanPropertyIndexed("property1.indexed2")); 674 } catch (BeanException be) { 675 be.printStackTrace(); 676 fail(be.toString()); 677 } 678 } 679 680 683 public void testNestedBeanProperty() { 684 685 try { 687 Bean1 bean = new Bean1(); 688 Bean2 bean2 = new Bean2(); 689 JavaBean jb = new JavaBean(Bean1.class); 690 NestedBeanProperty nbp = jb.getNestedBeanProperty("indexed[0]"); 691 nbp.setPropertyValue(bean, bean2); 692 assertEquals("Should be same object", nbp.getPropertyValue(bean), bean2); 693 assertEquals("Should contain root property", nbp.getRootProperty(), 694 jb.getBeanProperty("indexed")); 695 } catch (BeanException be) { 696 be.printStackTrace(); 697 fail(be.toString()); 698 } 699 700 try { 702 Bean1 bean1 = new Bean1(); 703 Bean2 bean2 = new Bean2(); 704 Bean3 bean3 = new Bean3(); 705 JavaBean jb = new JavaBean(Bean1.class); 706 NestedBeanProperty nbp = jb.getNestedBeanProperty("property1.indexed2[0]"); 707 708 bean1.setProperty1(bean2); 709 nbp.setPropertyValue(bean1, bean3); 710 assertEquals("Should be same object", nbp.getPropertyValue(bean1), bean3); 711 assertEquals("Should contain root property", nbp.getRootProperty(), 712 jb.getBeanProperty("property1")); 713 } catch (BeanException be) { 714 be.printStackTrace(); 715 fail(be.toString()); 716 } 717 718 try { 720 Bean1 bean1 = new Bean1(); 721 Bean2 bean2 = new Bean2(); 722 JavaBean jb = new JavaBean(Bean1.class); 723 NestedBeanProperty nbp = jb.getNestedBeanProperty("property1.name"); 724 725 bean1.setProperty1(bean2); 726 nbp.setPropertyValue(bean1, "fred"); 727 assertEquals("Should be same object", nbp.getPropertyValue(bean1), "fred"); 728 assertEquals("Should contain root property", nbp.getRootProperty(), 729 jb.getBeanProperty("property1")); 730 assertEquals("Should be right type", Bean1.class, nbp.getBeanClass()); 731 assertEquals("Should have property name", "property1.name", nbp.getPropertyName()); 732 assertEquals("Should have property name", "property1.name", nbp.getFullName()); 733 } catch (BeanException be) { 734 be.printStackTrace(); 735 fail(be.toString()); 736 } 737 } 738 739 742 public void testSingleArrayGet() { 743 try { 744 Bean1 bean1 = new Bean1(); 745 Bean2 bean2 = new Bean2(); 746 JavaBean jb = new JavaBean(Bean1.class); 747 748 bean1.getSingleArray()[0] = bean2; 749 750 assertEquals("Should have value bean2", jb.getPropertyValue("singleArray[0]", bean1), bean2); 751 } catch (BeanException be) { 752 fail(be.toString()); 753 } 754 755 try { 756 Bean1 bean1 = new Bean1(); 757 Bean2 bean2 = new Bean2(); 758 JavaBean jb = new JavaBean(Bean1.class); 759 760 bean1.getSingleArray()[0] = bean2; 761 bean2.setName("fred"); 762 763 assertEquals("Should have value fred", jb.getPropertyValue("singleArray[0].name", bean1), "fred"); 764 } catch (BeanException be) { 765 fail(be.toString()); 766 } 767 768 try { 770 Bean1 bean1 = new Bean1(); 771 JavaBean jb = new JavaBean(Bean1.class); 772 773 jb.getPropertyValue("singleArray[0].name", bean1, true); 774 fail("Should have failed because of strictness"); 775 } catch (BeanException be) { 776 } 778 779 try { 780 Bean1 bean1 = new Bean1(); 781 JavaBean jb = new JavaBean(Bean1.class); 782 783 assertNull("Should have value null", jb.getPropertyValue("singleArray[0].name", bean1)); 784 } catch (BeanException be) { 785 fail(be.toString()); 786 } 787 788 try { 789 Bean1 bean1 = new Bean1(); 790 Bean2 bean2 = new Bean2(); 791 JavaBean jb = new JavaBean(Bean1.class); 792 793 bean1.getSingleArray()[0] = bean2; 794 795 assertNull("Should be null in nesting", jb.getPropertyValue("singleArray[0].name", bean1)); 796 } catch (BeanException be) { 797 fail(be.toString()); 798 } 799 } 800 801 804 public void testSingleArraySet() { 805 try { 806 Bean1 bean1 = new Bean1(); 807 Bean2 bean2 = new Bean2(); 808 JavaBean jb = new JavaBean(Bean1.class); 809 810 jb.setPropertyValue("singleArray[0]", bean1, bean2, true, true); 811 assertEquals("Should have value bean2", bean1.getSingleArray()[0], bean2); 812 } catch (BeanException be) { 813 fail(be.toString()); 814 } 815 816 try { 817 Bean1 bean1 = new Bean1(); 818 Bean2 bean2 = new Bean2(); 819 JavaBean jb = new JavaBean(Bean1.class); 820 821 bean1.getSingleArray()[0] = bean2; 822 823 jb.setPropertyValue("singleArray[0].name", bean1, "fred", true, true); 824 assertEquals("Should have value fred", bean1.getSingleArray()[0].getName(), "fred"); 825 } catch (BeanException be) { 826 fail(be.toString()); 827 } 828 829 try { 831 Bean1 bean1 = new Bean1(); 832 JavaBean jb = new JavaBean(Bean1.class); 833 834 jb.setPropertyValue("singleArray[0].name", bean1, "fred", true, true); 835 fail("Should have failed because of strictness"); 836 } catch (BeanException be) { 837 } 839 840 try { 842 Bean1 bean1 = new Bean1(); 843 JavaBean jb = new JavaBean(Bean1.class); 844 845 bean1.setSingleArray(null); 846 847 jb.setPropertyValue("singleArray[2].name", bean1, "fred", true, false); 848 assertEquals("Should have an array of length 3", bean1.getSingleArray().length, 3); 849 assertNotNull("Should have a Bean2 at index 2", bean1.getSingleArray()[2]); 850 assertEquals("Should be fred", bean1.getSingleArray()[2].getName(), "fred"); 851 } catch (BeanException be) { 852 fail(be.toString()); 853 } 854 855 try { 857 Bean1 bean1 = new Bean1(); 858 Bean2 bean2 = new Bean2(); 859 JavaBean jb = new JavaBean(Bean1.class); 860 861 bean1.setSingleArrayObject(null); 862 863 jb.setPropertyValue("singleArrayObject[2]", bean1, bean2, true, false); 864 assertEquals("Should have an array of length 3", bean1.getSingleArrayObject().length, 3); 865 assertNotNull("Should have a Bean2 at index 2", bean1.getSingleArrayObject()[2]); 866 assertTrue("Should have a Bean2 at index 2", bean1.getSingleArrayObject()[2] instanceof Bean2); 867 assertEquals("Should be fred", bean1.getSingleArrayObject()[2], bean2); 868 } catch (BeanException be) { 869 fail(be.toString()); 870 } 871 872 try { 874 Bean1 bean1 = new Bean1(); 875 Bean2 bean2 = new Bean2(); 876 JavaBean jb = new JavaBean(Bean1.class); 877 878 bean1.setSingleArrayObject(null); 879 880 jb.setPropertyValue("singleArrayObject[2].name", bean1, bean2, true, false); 881 fail("Should have failed because name is not on Object"); 882 } catch (BeanException be) { 883 System.out.println(be.toString()); 884 } 885 } 886 887 890 public void testMultiArrayGet() { 891 try { 892 Bean1 bean1 = new Bean1(); 893 Bean2 bean2 = new Bean2(); 894 JavaBean jb = new JavaBean(Bean1.class); 895 896 bean1.setMultiArray(new Bean2[1][2][3]); 897 bean1.getMultiArray()[0][1][2] = bean2; 898 899 assertEquals("Should have value bean2", jb.getPropertyValue("multiArray[0][1][2]", bean1), bean2); 900 } catch (BeanException be) { 901 fail(be.toString()); 902 } 903 904 try { 905 Bean1 bean1 = new Bean1(); 906 Bean2 bean2 = new Bean2(); 907 JavaBean jb = new JavaBean(Bean1.class); 908 909 bean1.setMultiArray(new Bean2[1][2][5]); 910 bean1.getMultiArray()[0][1][4] = bean2; 911 bean2.setName("fred"); 912 913 assertEquals("Should have value fred", jb.getPropertyValue("multiArray[0][1][4].name", bean1), "fred"); 914 } catch (BeanException be) { 915 fail(be.toString()); 916 } 917 918 try { 920 Bean1 bean1 = new Bean1(); 921 JavaBean jb = new JavaBean(Bean1.class); 922 923 jb.getPropertyValue("multiArray[0].name", bean1, true); 924 fail("Should have failed because of strictness"); 925 } catch (BeanException be) { 926 } 928 929 try { 930 Bean1 bean1 = new Bean1(); 931 JavaBean jb = new JavaBean(Bean1.class); 932 933 assertNull("Should have value null", jb.getPropertyValue("multiArray[0][1][1].name", bean1)); 934 } catch (BeanException be) { 935 fail(be.toString()); 936 } 937 938 try { 939 Bean1 bean1 = new Bean1(); 940 Bean2 bean2 = new Bean2(); 941 JavaBean jb = new JavaBean(Bean1.class); 942 943 bean1.setMultiArray(new Bean2[3][1][6]); 944 bean1.getMultiArray()[2][0][5] = bean2; 945 946 assertNull("Should be null in nesting", jb.getPropertyValue("multiArray[2][0][5].name", bean1)); 947 } catch (BeanException be) { 948 fail(be.toString()); 949 } 950 } 951 952 955 public void testMultiArraySet() { 956 try { 957 Bean1 bean1 = new Bean1(); 958 Bean2 bean2 = new Bean2(); 959 JavaBean jb = new JavaBean(Bean1.class); 960 961 bean1.setMultiArray(new Bean2[1][4][2]); 962 jb.setPropertyValue("multiArray[0][3][1]", bean1, bean2, true, true); 963 assertEquals("Should have value bean2", bean1.getMultiArray()[0][3][1], bean2); 964 } catch (BeanException be) { 965 fail(be.toString()); 966 } 967 968 try { 969 Bean1 bean1 = new Bean1(); 970 Bean2 bean2 = new Bean2(); 971 JavaBean jb = new JavaBean(Bean1.class); 972 973 bean1.setMultiArray(new Bean2[1][3][5]); 974 bean1.getMultiArray()[0][2][4] = bean2; 975 976 jb.setPropertyValue("multiArray[0][2][4].name", bean1, "fred", true, true); 977 assertEquals("Should have value fred", bean1.getMultiArray()[0][2][4].getName(), "fred"); 978 } catch (BeanException be) { 979 fail(be.toString()); 980 } 981 982 try { 984 Bean1 bean1 = new Bean1(); 985 JavaBean jb = new JavaBean(Bean1.class); 986 987 bean1.setMultiArray(new Bean2[1][4][2]); 988 jb.setPropertyValue("multiArray[0][3][1].name", bean1, "fred", true, true); 989 fail("Should have failed because of strictness"); 990 } catch (BeanException be) { 991 } 993 994 try { 996 Bean1 bean1 = new Bean1(); 997 JavaBean jb = new JavaBean(Bean1.class); 998 999 bean1.setMultiArray(null); 1000 1001 jb.setPropertyValue("multiArray[2][0][3].name", bean1, "fred", true, false); 1002 assertEquals("Should have an array of length 3", bean1.getMultiArray().length, 3); 1003 assertNotNull("Should have a Bean2 at index 2,0,3", bean1.getMultiArray()[2][0][3]); 1004 assertEquals("Should be fred", bean1.getMultiArray()[2][0][3].getName(), "fred"); 1005 } catch (BeanException be) { 1006 fail(be.toString()); 1007 } 1008 1009 try { 1011 Bean1 bean1 = new Bean1(); 1012 Bean2 bean2 = new Bean2(); 1013 JavaBean jb = new JavaBean(Bean1.class); 1014 1015 bean1.setMultiArrayObject(null); 1016 1017 jb.setPropertyValue("multiArrayObject[2][3][1]", bean1, bean2, true, false); 1018 assertEquals("Should have an array of length 3", bean1.getMultiArrayObject().length, 3); 1019 assertNotNull("Should have a Bean2 at index 2", bean1.getMultiArrayObject()[2][3][1]); 1020 assertTrue("Should have a Bean2 at index 2", bean1.getMultiArrayObject()[2][3][1] instanceof Bean2); 1021 assertEquals("Should be bean2", bean1.getMultiArrayObject()[2][3][1], bean2); 1022 } catch (BeanException be) { 1023 fail(be.toString()); 1024 } 1025 1026 try { 1028 Bean1 bean1 = new Bean1(); 1029 Bean2 bean2 = new Bean2(); 1030 JavaBean jb = new JavaBean(Bean1.class); 1031 1032 bean1.setMultiArrayObject(null); 1033 1034 jb.setPropertyValue("multiArrayObject[2][3]", bean1, bean2, true, false); 1035 fail("should have failed because not correct type"); 1036 } catch (BeanException be) { 1037 fail("Should be a type conversion exception"); 1038 } catch (TypeConversionException tce) { 1039 } 1041 } 1042 1043 1046 public void testCollection() { 1047 try { 1048 Bean3 bean3 = new Bean3(); 1049 Bean2 bean2 = new Bean2(); 1050 JavaBean jb = new JavaBean(Bean3.class); 1051 1052 bean3.getCollection().add(bean2); 1053 bean2.setName("fred"); 1054 assertEquals("Should have value fred", "fred", jb.getPropertyValue("collection[0].name", bean3)); 1055 1056 List newList = new ArrayList (); 1057 jb.setPropertyValue("collection", bean3, newList, true, true); 1058 assertSame("Should have list", bean3.getCollection(), newList); 1059 assertSame("Should have list", jb.getPropertyValue("collection", bean3), newList); 1060 1061 bean3.setCollection(null); 1062 assertNull("Should be null", jb.getPropertyValue("collection[0].foo.bar", bean3)); 1063 } catch (BeanException be) { 1064 fail(be.toString()); 1065 } 1066 1067 try { 1069 Bean3 bean3 = new Bean3(); 1070 JavaBean jb = new JavaBean(Bean3.class); 1071 1072 bean3.setCollection(new HashSet ()); 1073 jb.setPropertyValue("collection[0]", bean3, "fred", true, true); 1074 fail("Should fail because you can't set a collection"); 1075 } catch (BeanException be) { 1076 System.out.println(be.toString()); 1077 } 1078 1079 try { 1081 Bean3 bean3 = new Bean3(); 1082 JavaBean jb = new JavaBean(Bean3.class); 1083 1084 jb.setPropertyValue("collection[0].property1.name", bean3, "fred", true, true); 1086 fail("Should fail for strictness"); 1087 } catch (BeanException be) { 1088 System.out.println(be.toString()); 1089 } 1090 1091 try { 1093 Bean3 bean3 = new Bean3(); 1094 JavaBean jb = new JavaBean(Bean3.class); 1095 1096 jb.setPropertyValue("collection[0].property1.name", bean3, "fred", true, false); 1097 fail("Should fail to Collection"); 1098 } catch (BeanException be) { 1099 System.out.println(be.toString()); 1100 } 1101 1102 try { 1104 Bean3 bean3 = new Bean3(); 1105 JavaBean jb = new JavaBean(Bean3.class); 1106 1107 bean3.setCollection(new ArrayList ()); 1108 bean3.getCollection().add(new Bean2[1][1]); 1109 jb.setPropertyValue("collection[0][0][0].name", bean3, "fred", true, false); 1110 assertEquals("fred", ((Bean2[][]) ((ArrayList ) bean3.getCollection()).get(0))[0][0].getName()); 1111 1112 bean3.setCollection(new ArrayList ()); 1113 Object [] array = new Object [] {new ArrayList (), new ArrayList (), new ArrayList ()}; 1114 bean3.getCollection().add(array); 1115 ((ArrayList ) array[1]).add(new Bean2()); 1116 jb.setPropertyValue("collection[0][1][0].name", bean3, "fred", true, false); 1117 assertEquals("fred", 1118 ((Bean2) ((ArrayList ) ((Object []) ((ArrayList ) bean3.getCollection()).get(0))[1]).get(0)).getName()); 1119 } catch (BeanException be) { 1120 } 1122 } 1123 1124 1127 public void testMap() { 1128 try { 1129 Bean3 bean3 = new Bean3(); 1130 Bean2 bean2 = new Bean2(); 1131 JavaBean jb = new JavaBean(Bean3.class); 1132 1133 bean3.getMap().put("b2", bean2); 1134 bean2.setName("fred"); 1135 assertNotNull("Should have Bean2", jb.getPropertyValue("map[b2]", bean3)); 1136 assertSame("Should have Bean2", bean2, jb.getPropertyValue("map[b2]", bean3)); 1137 assertEquals("Should have value fred", "fred", jb.getPropertyValue("map[b2].name", bean3)); 1138 1139 List newList = new ArrayList (); 1140 newList.add(bean2); 1141 newList.add(bean2); 1142 jb.setPropertyValue("map[l1]", bean3, newList, true, true); 1143 assertSame("Should have list", newList, bean3.getMap().get("l1")); 1144 assertSame("Should have list", newList, jb.getPropertyValue("map[l1]", bean3)); 1145 assertSame("Should have bean2", "fred", jb.getPropertyValue("map[l1][1].name", bean3)); 1146 assertSame("Should have fred", "fred", jb.getPropertyValue("map[l1][1].name", bean3)); 1147 1148 bean3.setMap(null); 1149 assertNull("Should be null", jb.getPropertyValue("map[0].foo.bar", bean3)); 1150 } catch (BeanException be) { 1151 fail(be.toString()); 1152 } 1153 } 1154} | Popular Tags |