| 1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import junit.framework.TestCase; 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 32 38 39 public class DynaPropertyUtilsTestCase extends TestCase { 40 41 42 44 45 48 protected DynaBean bean = null; 49 50 51 54 protected String describes[] = 55 { "booleanProperty", 56 "booleanSecond", 57 "doubleProperty", 58 "floatProperty", 59 "intArray", 60 "intIndexed", 61 "intProperty", 62 "listIndexed", 63 "longProperty", 64 "mappedObjects", 65 "mappedProperty", 66 "mappedIntProperty", 67 "nested", 68 "nullProperty", 69 "shortProperty", 71 "stringArray", 72 "stringIndexed", 73 "stringProperty" 74 }; 75 76 77 80 protected TestBean nested = null; 81 82 83 85 86 91 public DynaPropertyUtilsTestCase(String name) { 92 93 super(name); 94 95 } 96 97 98 100 101 104 public void setUp() throws Exception { 105 106 DynaClass dynaClass = createDynaClass(); 108 bean = dynaClass.newInstance(); 109 110 bean.set("booleanProperty", new Boolean (true)); 112 bean.set("booleanSecond", new Boolean (true)); 113 bean.set("doubleProperty", new Double (321.0)); 114 bean.set("floatProperty", new Float ((float) 123.0)); 115 int intArray[] = { 0, 10, 20, 30, 40 }; 116 bean.set("intArray", intArray); 117 int intIndexed[] = { 0, 10, 20, 30, 40 }; 118 bean.set("intIndexed", intIndexed); 119 bean.set("intProperty", new Integer (123)); 120 List listIndexed = new ArrayList (); 121 listIndexed.add("String 0"); 122 listIndexed.add("String 1"); 123 listIndexed.add("String 2"); 124 listIndexed.add("String 3"); 125 listIndexed.add("String 4"); 126 bean.set("listIndexed", listIndexed); 127 bean.set("longProperty", new Long ((long) 321)); 128 HashMap mapProperty = new HashMap (); 129 mapProperty.put("First Key", "First Value"); 130 mapProperty.put("Second Key", "Second Value"); 131 bean.set("mapProperty", mapProperty); 132 HashMap mappedObjects = new HashMap (); 133 mappedObjects.put("First Key", "First Value"); 134 mappedObjects.put("Second Key", "Second Value"); 135 bean.set("mappedObjects", mappedObjects); 136 HashMap mappedProperty = new HashMap (); 137 mappedProperty.put("First Key", "First Value"); 138 mappedProperty.put("Second Key", "Second Value"); 139 bean.set("mappedProperty", mappedProperty); 140 HashMap mappedIntProperty = new HashMap (); 141 mappedIntProperty.put("One", new Integer (1)); 142 mappedIntProperty.put("Two", new Integer (2)); 143 bean.set("mappedIntProperty", mappedIntProperty); 144 nested = new TestBean(); 145 bean.set("nested", nested); 146 bean.set("shortProperty", new Short ((short) 987)); 148 String stringArray[] = 149 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 150 bean.set("stringArray", stringArray); 151 String stringIndexed[] = 152 { "String 0", "String 1", "String 2", "String 3", "String 4" }; 153 bean.set("stringIndexed", stringIndexed); 154 bean.set("stringProperty", "This is a string"); 155 156 } 157 158 159 162 public static Test suite() { 163 164 return (new TestSuite(DynaPropertyUtilsTestCase.class)); 165 166 } 167 168 169 172 public void tearDown() { 173 174 bean = null; 175 nested = null; 176 177 } 178 179 180 181 183 184 187 public void testCopyPropertiesMap() { 188 189 Map map = new HashMap (); 190 map.put("booleanProperty", Boolean.FALSE); 191 map.put("doubleProperty", new Double (333.0)); 192 map.put("dupProperty", new String [] { "New 0", "New 1", "New 2" }); 193 map.put("floatProperty", new Float ((float) 222.0)); 194 map.put("intArray", new int[] { 0, 100, 200 }); 195 map.put("intProperty", new Integer (111)); 196 map.put("longProperty", new Long (444)); 197 map.put("shortProperty", new Short ((short) 555)); 198 map.put("stringProperty", "New String Property"); 199 200 try { 201 PropertyUtils.copyProperties(bean, map); 202 } catch (Throwable t) { 203 fail("Threw " + t.toString()); 204 } 205 206 assertEquals("booleanProperty", false, 208 ((Boolean ) bean.get("booleanProperty")).booleanValue()); 209 assertEquals("doubleProperty", 333.0, 210 ((Double ) bean.get("doubleProperty")).doubleValue(), 211 0.005); 212 assertEquals("floatProperty", (float) 222.0, 213 ((Float ) bean.get("floatProperty")).floatValue(), 214 (float) 0.005); 215 assertEquals("intProperty", 111, 216 ((Integer ) bean.get("intProperty")).intValue()); 217 assertEquals("longProperty", (long) 444, 218 ((Long ) bean.get("longProperty")).longValue()); 219 assertEquals("shortProperty", (short) 555, 220 ((Short ) bean.get("shortProperty")).shortValue()); 221 assertEquals("stringProperty", "New String Property", 222 (String ) bean.get("stringProperty")); 223 224 String dupProperty[] = (String []) bean.get("dupProperty"); 226 assertNotNull("dupProperty present", dupProperty); 227 assertEquals("dupProperty length", 3, dupProperty.length); 228 assertEquals("dupProperty[0]", "New 0", dupProperty[0]); 229 assertEquals("dupProperty[1]", "New 1", dupProperty[1]); 230 assertEquals("dupProperty[2]", "New 2", dupProperty[2]); 231 int intArray[] = (int[]) bean.get("intArray"); 232 assertNotNull("intArray present", intArray); 233 assertEquals("intArray length", 3, intArray.length); 234 assertEquals("intArray[0]", 0, intArray[0]); 235 assertEquals("intArray[1]", 100, intArray[1]); 236 assertEquals("intArray[2]", 200, intArray[2]); 237 238 } 239 240 241 244 public void testDescribe() { 245 246 Map map = null; 247 try { 248 map = PropertyUtils.describe(bean); 249 } catch (Exception e) { 250 fail("Threw exception " + e); 251 } 252 253 for (int i = 0; i < describes.length; i++) { 255 assertTrue("Property '" + describes[i] + "' is present", 256 map.containsKey(describes[i])); 257 } 258 assertTrue("Property 'writeOnlyProperty' is not present", 259 !map.containsKey("writeOnlyProperty")); 260 261 assertEquals("Value of 'booleanProperty'", 263 Boolean.TRUE, 264 (Boolean ) map.get("booleanProperty")); 265 assertEquals("Value of 'doubleProperty'", 266 new Double (321.0), 267 (Double ) map.get("doubleProperty")); 268 assertEquals("Value of 'floatProperty'", 269 new Float ((float) 123.0), 270 (Float ) map.get("floatProperty")); 271 assertEquals("Value of 'intProperty'", 272 new Integer (123), 273 (Integer ) map.get("intProperty")); 274 assertEquals("Value of 'longProperty'", 275 new Long (321), 276 (Long ) map.get("longProperty")); 277 assertEquals("Value of 'shortProperty'", 278 new Short ((short) 987), 279 (Short ) map.get("shortProperty")); 280 assertEquals("Value of 'stringProperty'", 281 "This is a string", 282 (String ) map.get("stringProperty")); 283 284 } 285 286 287 290 public void testGetIndexedArguments() { 291 292 294 try { 295 PropertyUtils.getIndexedProperty(null, "intArray", 0); 296 fail("Should throw IllegalArgumentException 1"); 297 } catch (IllegalArgumentException e) { 298 ; } catch (Throwable t) { 300 fail("Threw " + t + " instead of IllegalArgumentException 1"); 301 } 302 303 try { 304 PropertyUtils.getIndexedProperty(bean, null, 0); 305 fail("Should throw IllegalArgumentException 2"); 306 } catch (IllegalArgumentException e) { 307 ; } catch (Throwable t) { 309 fail("Threw " + t + " instead of IllegalArgumentException 2"); 310 } 311 312 314 try { 315 PropertyUtils.getIndexedProperty(null, 316 "intArray[0]"); 317 fail("Should throw IllegalArgumentException 3"); 318 } catch (IllegalArgumentException e) { 319 ; } catch (Throwable t) { 321 fail("Threw " + t + " instead of IllegalArgumentException 3"); 322 } 323 324 try { 325 PropertyUtils.getIndexedProperty(bean, "[0]"); 326 fail("Should throw NoSuchMethodException 4"); 327 } catch (NoSuchMethodException e) { 328 ; } catch (Throwable t) { 330 fail("Threw " + t + " instead of NoSuchMethodException 4"); 331 } 332 333 try { 334 PropertyUtils.getIndexedProperty(bean, "intArray"); 335 fail("Should throw IllegalArgumentException 5"); 336 } catch (IllegalArgumentException e) { 337 ; } catch (Throwable t) { 339 fail("Threw " + t + " instead of IllegalArgumentException 5"); 340 } 341 342 344 try { 345 PropertyUtils.getIndexedProperty(null, "intIndexed", 0); 346 fail("Should throw IllegalArgumentException 1"); 347 } catch (IllegalArgumentException e) { 348 ; } catch (Throwable t) { 350 fail("Threw " + t + " instead of IllegalArgumentException 1"); 351 } 352 353 try { 354 PropertyUtils.getIndexedProperty(bean, null, 0); 355 fail("Should throw IllegalArgumentException 2"); 356 } catch (IllegalArgumentException e) { 357 ; } catch (Throwable t) { 359 fail("Threw " + t + " instead of IllegalArgumentException 2"); 360 } 361 362 364 try { 365 PropertyUtils.getIndexedProperty(null, 366 "intIndexed[0]"); 367 fail("Should throw IllegalArgumentException 3"); 368 } catch (IllegalArgumentException e) { 369 ; } catch (Throwable t) { 371 fail("Threw " + t + " instead of IllegalArgumentException 3"); 372 } 373 374 try { 375 PropertyUtils.getIndexedProperty(bean, "[0]"); 376 fail("Should throw NoSuchMethodException 4"); 377 } catch (NoSuchMethodException e) { 378 ; } catch (Throwable t) { 380 fail("Threw " + t + " instead of NoSuchMethodException 4"); 381 } 382 383 try { 384 PropertyUtils.getIndexedProperty(bean, "intIndexed"); 385 fail("Should throw IllegalArgumentException 5"); 386 } catch (IllegalArgumentException e) { 387 ; } catch (Throwable t) { 389 fail("Threw " + t + " instead of IllegalArgumentException 5"); 390 } 391 392 } 393 394 395 398 public void testGetIndexedValues() { 399 400 Object value = null; 401 402 404 for (int i = 0; i < 5; i++) { 405 406 try { 407 value = 408 PropertyUtils.getIndexedProperty(bean, "intArray", i); 409 assertNotNull("intArray returned value " + i, value); 410 assertTrue("intArray returned Integer " + i, 411 value instanceof Integer ); 412 assertEquals("intArray returned correct " + i, i * 10, 413 ((Integer ) value).intValue()); 414 } catch (Throwable t) { 415 fail("intArray " + i + " threw " + t); 416 } 417 418 try { 419 value = 420 PropertyUtils.getIndexedProperty(bean, "intIndexed", i); 421 assertNotNull("intIndexed returned value " + i, value); 422 assertTrue("intIndexed returned Integer " + i, 423 value instanceof Integer ); 424 assertEquals("intIndexed returned correct " + i, i * 10, 425 ((Integer ) value).intValue()); 426 } catch (Throwable t) { 427 fail("intIndexed " + i + " threw " + t); 428 } 429 430 try { 431 value = 432 PropertyUtils.getIndexedProperty(bean, "listIndexed", i); 433 assertNotNull("listIndexed returned value " + i, value); 434 assertTrue("list returned String " + i, 435 value instanceof String ); 436 assertEquals("listIndexed returned correct " + i, 437 "String " + i, (String ) value); 438 } catch (Throwable t) { 439 fail("listIndexed " + i + " threw " + t); 440 } 441 442 try { 443 value = 444 PropertyUtils.getIndexedProperty(bean, "stringArray", i); 445 assertNotNull("stringArray returned value " + i, value); 446 assertTrue("stringArray returned String " + i, 447 value instanceof String ); 448 assertEquals("stringArray returned correct " + i, 449 "String " + i, (String ) value); 450 } catch (Throwable t) { 451 fail("stringArray " + i + " threw " + t); 452 } 453 454 try { 455 value = 456 PropertyUtils.getIndexedProperty(bean, "stringIndexed", i); 457 assertNotNull("stringIndexed returned value " + i, value); 458 assertTrue("stringIndexed returned String " + i, 459 value instanceof String ); 460 assertEquals("stringIndexed returned correct " + i, 461 "String " + i, (String ) value); 462 } catch (Throwable t) { 463 fail("stringIndexed " + i + " threw " + t); 464 } 465 466 } 467 468 470 for (int i = 0; i < 5; i++) { 471 472 try { 473 value = 474 PropertyUtils.getIndexedProperty(bean, 475 "intArray[" + i + "]"); 476 assertNotNull("intArray returned value " + i, value); 477 assertTrue("intArray returned Integer " + i, 478 value instanceof Integer ); 479 assertEquals("intArray returned correct " + i, i * 10, 480 ((Integer ) value).intValue()); 481 } catch (Throwable t) { 482 fail("intArray " + i + " threw " + t); 483 } 484 485 try { 486 value = 487 PropertyUtils.getIndexedProperty(bean, 488 "intIndexed[" + i + "]"); 489 assertNotNull("intIndexed returned value " + i, value); 490 assertTrue("intIndexed returned Integer " + i, 491 value instanceof Integer ); 492 assertEquals("intIndexed returned correct " + i, i * 10, 493 ((Integer ) value).intValue()); 494 } catch (Throwable t) { 495 fail("intIndexed " + i + " threw " + t); 496 } 497 498 try { 499 value = 500 PropertyUtils.getIndexedProperty(bean, 501 "listIndexed[" + i + "]"); 502 assertNotNull("listIndexed returned value " + i, value); 503 assertTrue("listIndexed returned String " + i, 504 value instanceof String ); 505 assertEquals("listIndexed returned correct " + i, 506 "String " + i, (String ) value); 507 } catch (Throwable t) { 508 fail("listIndexed " + i + " threw " + t); 509 } 510 511 try { 512 value = 513 PropertyUtils.getIndexedProperty(bean, 514 "stringArray[" + i + "]"); 515 assertNotNull("stringArray returned value " + i, value); 516 assertTrue("stringArray returned String " + i, 517 value instanceof String ); 518 assertEquals("stringArray returned correct " + i, 519 "String " + i, (String ) value); 520 } catch (Throwable t) { 521 fail("stringArray " + i + " threw " + t); 522 } 523 524 try { 525 value = 526 PropertyUtils.getIndexedProperty(bean, 527 "stringIndexed[" + i + "]"); 528 assertNotNull("stringIndexed returned value " + i, value); 529 assertTrue("stringIndexed returned String " + i, 530 value instanceof String ); 531 assertEquals("stringIndexed returned correct " + i, 532 "String " + i, (String ) value); 533 } catch (Throwable t) { 534 fail("stringIndexed " + i + " threw " + t); 535 } 536 537 } 538 539 541 try { 542 value = 543 PropertyUtils.getIndexedProperty(bean, 544 "intArray", -1); 545 fail("Should have thrown ArrayIndexOutOfBoundsException"); 546 } catch (ArrayIndexOutOfBoundsException t) { 547 ; } catch (Throwable t) { 549 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 550 } 551 552 try { 553 value = 554 PropertyUtils.getIndexedProperty(bean, 555 "intArray", 5); 556 fail("Should have thrown ArrayIndexOutOfBoundsException"); 557 } catch (ArrayIndexOutOfBoundsException t) { 558 ; } catch (Throwable t) { 560 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 561 } 562 563 try { 564 value = 565 PropertyUtils.getIndexedProperty(bean, 566 "intIndexed", -1); 567 fail("Should have thrown ArrayIndexOutOfBoundsException"); 568 } catch (ArrayIndexOutOfBoundsException t) { 569 ; } catch (Throwable t) { 571 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 572 } 573 574 try { 575 value = 576 PropertyUtils.getIndexedProperty(bean, 577 "intIndexed", 5); 578 fail("Should have thrown ArrayIndexOutOfBoundsException"); 579 } catch (ArrayIndexOutOfBoundsException t) { 580 ; } catch (Throwable t) { 582 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 583 } 584 585 try { 586 value = 587 PropertyUtils.getIndexedProperty(bean, 588 "listIndexed", -1); 589 fail("Should have thrown IndexOutOfBoundsException"); 590 } catch (IndexOutOfBoundsException t) { 591 ; } catch (Throwable t) { 593 fail("Threw " + t + " instead of IndexOutOfBoundsException"); 594 } 595 596 try { 597 value = 598 PropertyUtils.getIndexedProperty(bean, 599 "listIndexed", 5); 600 fail("Should have thrown IndexOutOfBoundsException"); 601 } catch (IndexOutOfBoundsException t) { 602 ; } catch (Throwable t) { 604 fail("Threw " + t + " instead of IndexOutOfBoundsException"); 605 } 606 607 try { 608 value = 609 PropertyUtils.getIndexedProperty(bean, 610 "stringArray", -1); 611 fail("Should have thrown ArrayIndexOutOfBoundsException"); 612 } catch (ArrayIndexOutOfBoundsException t) { 613 ; } catch (Throwable t) { 615 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 616 } 617 618 try { 619 value = 620 PropertyUtils.getIndexedProperty(bean, 621 "stringArray", 5); 622 fail("Should have thrown ArrayIndexOutOfBoundsException"); 623 } catch (ArrayIndexOutOfBoundsException t) { 624 ; } catch (Throwable t) { 626 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 627 } 628 629 try { 630 value = 631 PropertyUtils.getIndexedProperty(bean, 632 "stringIndexed", -1); 633 fail("Should have thrown ArrayIndexOutOfBoundsException"); 634 } catch (ArrayIndexOutOfBoundsException t) { 635 ; } catch (Throwable t) { 637 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 638 } 639 640 try { 641 value = 642 PropertyUtils.getIndexedProperty(bean, 643 "stringIndexed", 5); 644 fail("Should have thrown ArrayIndexOutOfBoundsException"); 645 } catch (ArrayIndexOutOfBoundsException t) { 646 ; } catch (Throwable t) { 648 fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); 649 } 650 651 } 652 653 654 657 public void testGetMappedArguments() { 658 659 661 try { 662 PropertyUtils.getMappedProperty(null, "mappedProperty", 663 "First Key"); 664 fail("Should throw IllegalArgumentException 1"); 665 } catch (IllegalArgumentException e) { 666 ; } catch (Throwable t) { 668 fail("Threw " + t + " instead of IllegalArgumentException 1"); 669 } 670 671 try { 672 PropertyUtils.getMappedProperty(bean, null, "First Key"); 673 fail("Should throw IllegalArgumentException 2"); 674 } catch (IllegalArgumentException e) { 675 ; } catch (Throwable t) { 677 fail("Threw " + t + " instead of IllegalArgumentException 2"); 678 } 679 680 try { 681 PropertyUtils.getMappedProperty(bean, "mappedProperty", null); 682 fail("Should throw IllegalArgumentException 3"); 683 } catch (IllegalArgumentException e) { 684 ; |