1 3 package jodd.bean; 4 5 import java.math.BigDecimal ; 6 import java.sql.Date ; 7 import java.sql.Time ; 8 import java.sql.Timestamp ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 import jodd.bean.modifier.TrimStringsBeanModifier; 13 import jodd.bean.test.*; 14 import junit.framework.TestCase; 15 16 public class BeanUtilTest extends TestCase { 17 18 public void testSimpleProperty() { 19 FooBean fb = new FooBean(); 20 21 assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", false)); 23 assertTrue(BeanUtil.hasProperty(fb, "fooInteger")); 24 BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer (173), false); 26 assertEquals(new Integer (173), BeanUtil.getSimpleProperty(fb, "fooInteger", false)); 28 29 assertEquals(new Integer (0), BeanUtil.getSimpleProperty(fb, "fooint", false)); 31 assertTrue(BeanUtil.hasProperty(fb, "fooint")); 32 assertFalse(BeanUtil.hasProperty(fb, "fooint-xxx")); 33 34 assertTrue(BeanUtil.hasProperty(fb, "fooByte")); 36 assertEquals(new Byte ((byte)0), BeanUtil.getSimplePropertyForced(fb, "fooByte", false)); 37 38 Map m = new HashMap (); 39 BeanUtil.setSimpleProperty(m, "foo", new Integer (173), false); 41 assertTrue(BeanUtil.hasProperty(m, "foo")); 43 assertEquals(new Integer (173), BeanUtil.getSimpleProperty(m, "foo", false)); 44 45 assertTrue(BeanUtil.hasProperty(fb, "fooMap")); 47 assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", false)); 48 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap", false)); 50 51 assertTrue(BeanUtil.hasProperty(fb, "fooList")); 53 assertNull(BeanUtil.getSimpleProperty(fb, "fooList", false)); 54 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList", false)); 56 57 assertTrue(BeanUtil.hasProperty(fb, "fooStringA")); 59 assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false)); 60 String [] tmp = new String [10]; 61 tmp[2] = "foo"; 62 BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, false); 64 tmp = (String []) BeanUtil.getSimpleProperty(fb, "fooStringA", false); 66 assertEquals("foo", tmp[2]); 67 68 fb.setFooStringA(null); 69 assertTrue(BeanUtil.hasProperty(fb, "fooStringA")); 71 assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false)); 72 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooStringA", false)); 74 } 75 76 public void testSimplePropertySlimPrivate() { 77 FooBeanSlim fb = new FooBeanSlim(); 78 79 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooInteger")); 81 assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", true)); 82 BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer (173), true); 84 assertEquals(new Integer (173), BeanUtil.getSimpleProperty(fb, "fooInteger", true)); 86 87 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooint")); 89 assertEquals(new Integer (0), BeanUtil.getSimpleProperty(fb, "fooint", true)); 90 91 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooByte")); 93 assertEquals(new Byte ((byte)0), BeanUtil.getSimplePropertyForced(fb, "fooByte", true)); 94 95 Map m = new HashMap (); 96 assertFalse(BeanUtil.hasDeclaredProperty(m, "foo")); 98 BeanUtil.setSimpleProperty(m, "foo", new Integer (173), true); 99 assertTrue(BeanUtil.hasDeclaredProperty(m, "foo")); 101 assertEquals(new Integer (173), BeanUtil.getSimpleProperty(m, "foo", true)); 102 103 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap")); 105 assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", true)); 106 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap", true)); 108 109 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList")); 111 assertNull(BeanUtil.getSimpleProperty(fb, "fooList", true)); 112 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList", true)); 114 115 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA")); 117 assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true)); 118 String [] tmp = new String [10]; 119 tmp[2] = "foo"; 120 BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, true); 122 tmp = (String []) BeanUtil.getSimpleProperty(fb, "fooStringA", true); 124 assertEquals("foo", tmp[2]); 125 126 fb = new FooBeanSlim(); 127 assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true)); 129 assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooStringA", true)); 131 } 132 133 public void testIndexProperty() { 134 FooBean fb = new FooBean(); 135 136 assertNull(fb.getFooStringA()); 138 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[0]")); 139 try { 140 BeanUtil.getIndexProperty(fb, "fooStringA[0]", false, true); 141 fail(); 142 } catch (ArrayIndexOutOfBoundsException aioobex) { 143 } 145 assertNotNull(fb.getFooStringA()); 146 assertEquals(0, fb.getFooStringA().length); 147 148 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[7]")); 150 try { 151 BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx", false, false); 152 fail(); 153 } catch(ArrayIndexOutOfBoundsException aioobex) { 154 } 156 157 BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", false, true); 159 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[40]")); 160 assertEquals("zzz", fb.getFooStringA()[40]); 161 assertEquals(41, fb.getFooStringA().length); 162 163 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]")); 165 BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, false, true); 166 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]")); 167 assertNull(fb.getFooStringA()[43]); 168 assertEquals(44, fb.getFooStringA().length); 169 170 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[15]")); 172 assertNotNull(BeanUtil.getIndexProperty(fb, "fooStringA[15]", false, true)); 173 assertNull(fb.getFooStringA()[0]); 174 assertNotNull(fb.getFooStringA()[15]); 175 176 177 fb.setFooStringA(null); 179 BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", false, true); 180 assertEquals("ccc", fb.getFooStringA()[7]); 181 182 183 184 185 186 assertNull(fb.getFooList()); 188 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]")); 189 try { 190 BeanUtil.getIndexProperty(fb, "fooList[1]", false, true); 191 fail(); 192 } catch (IndexOutOfBoundsException ioobex) { 193 } 195 assertNotNull(fb.getFooList()); 196 197 try { 199 BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false, false); 200 fail(); 201 } catch (IndexOutOfBoundsException ioobex) { 202 } 204 205 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[40]")); 207 BeanUtil.setIndexProperty(fb, "fooList[40]", "zzz", false, true); 208 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[40]")); 209 assertEquals("zzz", fb.getFooList().get(40)); 210 assertEquals(41, fb.getFooList().size()); 211 212 fb.setFooList(null); 214 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]")); 215 BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false, true); 216 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[1]")); 217 assertEquals("xxx", fb.getFooList().get(1)); 218 assertEquals(2, fb.getFooList().size()); 219 220 221 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]")); 223 assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", false, true)); 224 assertNotNull(fb.getFooMap()); 225 fb.setFooMap(null); 227 assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]")); 228 BeanUtil.setIndexProperty(fb, "fooMap[foo]", "xxx", false, true); 229 assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]")); 230 assertEquals("xxx", fb.getFooMap().get("foo")); 231 assertEquals(1, fb.getFooMap().size()); 232 } 233 234 public void testIndexPropertySlimPrivate() { 235 FooBeanSlim fb = new FooBeanSlim(); 236 237 assertNull(fb.getStringA()); 239 try { 240 BeanUtil.getIndexProperty(fb, "fooStringA[0]", true, true); 241 fail(); 242 } catch (ArrayIndexOutOfBoundsException aioobex) { 243 } 245 assertNotNull(fb.getStringA()); 246 assertEquals(0, fb.getStringA().length); 247 248 try { 250 BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx", true, false); 251 fail(); 252 } catch(ArrayIndexOutOfBoundsException aioobex) { 253 } 255 256 BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", true, true); 258 assertEquals("zzz", fb.getStringA()[40]); 259 assertEquals(41, fb.getStringA().length); 260 261 BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, true, true); 262 assertNull(fb.getStringA()[43]); 263 assertEquals(44, fb.getStringA().length); 264 265 266 fb = new FooBeanSlim(); 268 assertNull(fb.getStringA()); 269 BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", true, true); 270 assertNotNull(fb.getStringA()); 271 assertEquals("ccc", fb.getStringA()[7]); 272 273 274 assertNull(fb.getList()); 276 try { 277 BeanUtil.getIndexProperty(fb, "fooList[1]", true, true); 278 fail(); 279 } catch(IndexOutOfBoundsException ioobex) { 280 } 282 assertNotNull(fb.getList()); 283 284 try { 286 BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true, false); 287 fail(); 288 } catch(IndexOutOfBoundsException ioobex) { 289 } 291 292 BeanUtil.setIndexProperty(fb, "fooList[40]", "zzz", true, true); 294 assertEquals("zzz", fb.getList().get(40)); 295 assertEquals(41, fb.getList().size()); 296 297 fb = new FooBeanSlim(); 299 BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true, true); 300 assertEquals("xxx", fb.getList().get(1)); 301 302 assertNull(fb.getMap()); 304 assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", true, true)); 305 assertNotNull(fb.getMap()); 306 307 fb = new FooBeanSlim(); 309 assertNull(fb.getMap()); 310 BeanUtil.setIndexProperty(fb, "fooMap[foo]", "xxx", true, true); 311 assertNotNull(fb.getMap()); 312 assertEquals("xxx", fb.getMap().get("foo")); 313 } 314 315 316 318 public void testSetPropertyNumbers() { 319 FooBean fb = new FooBean(); 320 321 String propName = "fooInteger"; 323 BeanUtil.setProperty(fb, propName, new Integer (1)); 324 assertEquals(1, fb.getFooInteger().intValue()); 325 BeanUtil.setProperty(fb, propName, null); 326 assertNull(fb.getFooInteger()); 327 BeanUtil.setProperty(fb, propName, "2"); assertEquals(2, fb.getFooInteger().intValue()); 329 try { 330 BeanUtil.setProperty(fb, propName, "x"); fail(); 332 } catch(Exception ex) {} 333 assertEquals(2, fb.getFooInteger().intValue()); 334 335 propName = "fooint"; 337 BeanUtil.setProperty(fb, propName, new Integer (1)); 338 assertEquals(1, fb.getFooint()); 339 340 try { 341 BeanUtil.setProperty(fb, propName, null); fail(); 343 } catch(Exception ex) {} 344 assertEquals(1, fb.getFooint()); 345 346 BeanUtil.setProperty(fb, propName, "2"); 347 assertEquals(2, fb.getFooint()); 348 349 try { 350 BeanUtil.setProperty(fb, propName, "w"); fail(); 352 } catch (Exception ex) {} 353 assertEquals(2, fb.getFooint()); 354 355 356 propName = "fooLong"; 358 BeanUtil.setProperty(fb, propName, new Long (1)); 359 assertEquals(1L, fb.getFooLong().longValue()); 360 BeanUtil.setProperty(fb, propName, new Integer (3)); 361 assertEquals(3L, fb.getFooLong().longValue()); 362 BeanUtil.setProperty(fb, propName, null); 363 assertNull(fb.getFooLong()); 364 BeanUtil.setProperty(fb, propName, "2"); assertEquals(2L, fb.getFooLong().longValue()); 366 367 try { 368 BeanUtil.setProperty(fb, propName, "x"); fail(); 370 } catch (Exception ex) {} 371 assertEquals(2L, fb.getFooLong().longValue()); 372 373 propName = "foolong"; 375 BeanUtil.setProperty(fb, propName, new Long (1)); 376 assertEquals(1L, fb.getFoolong()); 377 BeanUtil.setProperty(fb, propName, new Integer (3)); 378 assertEquals(3L, fb.getFoolong()); 379 380 try { 381 BeanUtil.setProperty(fb, propName, null); fail(); 383 } catch (Exception ex) {} 384 385 assertEquals(3L, fb.getFoolong()); 386 BeanUtil.setProperty(fb, propName, "2"); assertEquals(2L, fb.getFoolong()); 388 try { 389 BeanUtil.setProperty(fb, propName, "w"); fail(); 391 } catch (Exception ex) {} 392 assertEquals(2L, fb.getFoolong()); 393 394 propName = "fooByte"; 396 BeanUtil.setProperty(fb, propName, new Byte ((byte) 1)); 397 assertEquals(1, fb.getFooByte().byteValue()); 398 BeanUtil.setProperty(fb, propName, new Integer (3)); 399 assertEquals(3, fb.getFooByte().byteValue()); 400 BeanUtil.setProperty(fb, propName, new Integer (257)); 401 assertEquals(1, fb.getFooByte().byteValue()); BeanUtil.setProperty(fb, propName, null); 403 assertNull(fb.getFooByte()); 404 BeanUtil.setProperty(fb, propName, "2"); assertEquals(2, fb.getFooByte().byteValue()); 406 407 try { 408 BeanUtil.setProperty(fb, propName, "x"); fail(); 410 } catch (Exception ex) {} 411 assertEquals(2, fb.getFooByte().byteValue()); 412 413 propName = "foobyte"; 415 BeanUtil.setProperty(fb, propName, new Byte ((byte) 1)); 416 assertEquals(1, fb.getFoobyte()); 417 BeanUtil.setProperty(fb, propName, new Integer (3)); 418 assertEquals(3, fb.getFoobyte()); 419 BeanUtil.setProperty(fb, propName, new Integer (257)); 420 assertEquals(1, fb.getFoobyte()); 421 try { 422 BeanUtil.setProperty(fb, propName, null); fail(); 424 } catch (Exception ex) {} 425 assertEquals(1, fb.getFoobyte()); 426 BeanUtil.setProperty(fb, propName, "2"); assertEquals(2, fb.getFoobyte()); 428 try { 429 BeanUtil.setProperty(fb, propName, "x"); fail(); 431 } catch(Exception ex) {} 432 assertEquals(2, fb.getFoobyte()); 433 434 propName = "fooBoolean"; 436 BeanUtil.setProperty(fb, propName, Boolean.TRUE); 437 assertTrue(fb.getFooBoolean().booleanValue()); 438 BeanUtil.setProperty(fb, propName, Boolean.FALSE); 439 assertFalse(fb.getFooBoolean().booleanValue()); 440 BeanUtil.setProperty(fb, propName, null); 441 assertNull(fb.getFooBoolean()); 442 BeanUtil.setProperty(fb, propName, "yes"); 443 assertTrue(fb.getFooBoolean().booleanValue()); 444 BeanUtil.setProperty(fb, propName, "y"); 445 assertTrue(fb.getFooBoolean().booleanValue()); 446 BeanUtil.setProperty(fb, propName, "true"); 447 assertTrue(fb.getFooBoolean().booleanValue()); 448 BeanUtil.setProperty(fb, propName, "on"); 449 assertTrue(fb.getFooBoolean().booleanValue()); 450 BeanUtil.setProperty(fb, propName, "1"); 451 assertTrue(fb.getFooBoolean().booleanValue()); 452 BeanUtil.setProperty(fb, propName, "no"); 453 assertFalse(fb.getFooBoolean().booleanValue()); 454 BeanUtil.setProperty(fb, propName, "n"); 455 assertFalse(fb.getFooBoolean().booleanValue()); 456 BeanUtil.setProperty(fb, propName, "false"); 457 assertFalse(fb.getFooBoolean().booleanValue()); 458 BeanUtil.setProperty(fb, propName, "off"); 459 assertFalse(fb.getFooBoolean().booleanValue()); 460 BeanUtil.setProperty(fb, propName, "0"); 461 assertFalse(fb.getFooBoolean().booleanValue()); 462 463 propName = "fooboolean"; 465 BeanUtil.setProperty(fb, propName, Boolean.TRUE); 466 assertTrue(fb.getFooboolean()); 467 BeanUtil.setProperty(fb, propName, Boolean.FALSE); 468 assertFalse(fb.getFooboolean()); 469 470 try { 471 BeanUtil.setProperty(fb, propName, null); 472 fail(); 473 } catch(Exception ex) {} 474 475 assertFalse(fb.getFooboolean()); 476 BeanUtil.setProperty(fb, propName, "yes"); 477 assertTrue(fb.getFooboolean()); 478 BeanUtil.setProperty(fb, propName, "y"); 479 assertTrue(fb.getFooboolean()); 480 BeanUtil.setProperty(fb, propName, "true"); 481 assertTrue(fb.getFooboolean()); 482 BeanUtil.setProperty(fb, propName, "on"); 483 assertTrue(fb.getFooboolean()); 484 BeanUtil.setProperty(fb, propName, "1"); 485 assertTrue(fb.getFooboolean()); 486 BeanUtil.setProperty(fb, propName, "no"); 487 assertFalse(fb.getFooboolean()); 488 BeanUtil.setProperty(fb, propName, "n"); 489 assertFalse(fb.getFooboolean()); 490 BeanUtil.setProperty(fb, propName, "false"); 491 assertFalse(fb.getFooboolean()); 492 BeanUtil.setProperty(fb, propName, "off"); 493 assertFalse(fb.getFooboolean()); 494 BeanUtil.setProperty(fb, propName, "0"); 495 assertFalse(fb.getFooboolean()); 496 497 propName = "fooFloat"; 499 BeanUtil.setProperty(fb, propName, new Float (1.1)); 500 assertEquals(1.1, fb.getFooFloat().floatValue(), 0.0005); 501 BeanUtil.setProperty(fb, propName, new Integer (3)); 502 assertEquals(3.0, fb.getFooFloat().floatValue(), 0.0005); 503 BeanUtil.setProperty(fb, propName, null); 504 assertNull(fb.getFooFloat()); 505 BeanUtil.setProperty(fb, propName, "2.2"); assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005); 507 try { 508 BeanUtil.setProperty(fb, propName, "x"); fail(); 510 } catch (Exception ex) {} 511 assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005); 512 513 propName = "foofloat"; 515 BeanUtil.setProperty(fb, propName, new Float (1.1)); 516 assertEquals(1.1, fb.getFoofloat(), 0.0005); 517 BeanUtil.setProperty(fb, propName, new Integer (3)); 518 assertEquals(3.0, fb.getFoofloat(), 0.0005); 519 try { 520 BeanUtil.setProperty(fb, propName, null); fail(); 522 } catch(Exception ex) {} 523 assertEquals(3.0, fb.getFoofloat(), 0.0005); 524 BeanUtil.setProperty(fb, propName, "2.2"); 526 assertEquals(2.2, fb.getFoofloat(), 0.0005); 527 try { 528 BeanUtil.setProperty(fb, propName, "w"); fail(); 530 } catch(Exception ex) {} 531 assertEquals(2.2, fb.getFoofloat(), 0.0005); 532 533 propName = "fooDouble"; 535 BeanUtil.setProperty(fb, propName, new Double (1.1)); 536 assertEquals(1.1, fb.getFooDouble().doubleValue(), 0.0005); 537 BeanUtil.setProperty(fb, propName, new Integer (3)); 538 assertEquals(3.0, fb.getFooDouble().doubleValue(), 0.0005); 539 BeanUtil.setProperty(fb, propName, null); 540 assertNull(fb.getFooDouble()); 541 BeanUtil.setProperty(fb, propName, "2.2"); assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005); 543 try { 544 BeanUtil.setProperty(fb, propName, "x"); fail(); 546 } catch (Exception ex) {} 547 assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005); 548 549 propName = "foodouble"; 551 BeanUtil.setProperty(fb, propName, new Double (1.1)); 552 assertEquals(1.1, fb.getFoodouble(), 0.0005); 553 BeanUtil.setProperty(fb, propName, new Integer (3)); 554 assertEquals(3.0, fb.getFoodouble(), 0.0005); 555 try { 556 BeanUtil.setProperty(fb, propName, null); fail(); 558 } catch(Exception ex) {} 559 assertEquals(3.0, fb.getFoodouble(), 0.0005); 560 BeanUtil.setProperty(fb, propName, "2.2"); 562 assertEquals(2.2, fb.getFoodouble(), 0.0005); 563 try { 564 BeanUtil.setProperty(fb, propName, "w"); fail(); 566 } catch(Exception ex) {} 567 assertEquals(2.2, fb.getFoodouble(), 0.0005); 568 } 569 570 571 public void testSetPropertySql() { 572 FooBean2 fb = new FooBean2(); 573 574 String propName = "fooTimestamp"; 575 Timestamp ts = new Timestamp (101, 0, 17, 1, 2, 3, 4); 577 BeanUtil.setProperty(fb, propName, ts); 578 assertEquals("2001-01-17 01:02:03.000000004", fb.getFooTimestamp().toString()); 579 580 propName = "fooTime"; 581 Time t = new Time (17, 13, 15); 583 BeanUtil.setProperty(fb, propName, t); 584 585 assertEquals("17:13:15", fb.getFooTime().toString()); 586 587 propName = "fooDate"; 588 Date d = new Date (101, 1, 17); 590 BeanUtil.setProperty(fb, propName, d); 591 assertEquals("2001-02-17", fb.getFooDate().toString()); 592 } 593 594 595 public void testSetPropertyMath() { 596 FooBean2 fb = new FooBean2(); 597 String propName = "fooBigDecimal"; 598 BeanUtil.setProperty(fb, propName, new BigDecimal (1.2)); 599 assertEquals(1.2, fb.getFooBigDecimal().doubleValue(), 0.0005); 600 } 601 602 public void testSetPropertyString() { 603 FooBean fb = new FooBean(); 604 605 String propName = "fooString"; 607 BeanUtil.setProperty(fb, propName, "string"); 608 assertEquals("string", fb.getFooString()); 609 BeanUtil.setProperty(fb, propName, null); 610 assertNull(fb.getFooString()); 611 612 propName = "fooStringA"; 614 String [] sa = new String [] {"one", "two", "three"}; 615 BeanUtil.setProperty(fb, propName, sa); 616 assertEquals("one", fb.getFooStringA()[0]); 617 assertEquals("two", fb.getFooStringA()[1]); 618 assertEquals("three", fb.getFooStringA()[2]); 619 BeanUtil.setProperty(fb, propName, "just a string"); 620 sa = (String []) BeanUtil.getProperty(fb, propName); 621 assertEquals(1, sa.length); 622 assertEquals("just a string", sa[0]); 623 624 propName = "fooCharacter"; 626 BeanUtil.setProperty(fb, propName, new Character ('a')); 627 assertEquals('a', fb.getFooCharacter().charValue()); 628 BeanUtil.setProperty(fb, propName, "123"); 629 assertEquals('1', fb.getFooCharacter().charValue()); 630 BeanUtil.setProperty(fb, propName, new Integer (789)); 631 assertEquals('7', fb.getFooCharacter().charValue()); 632 633 propName = "foochar"; 635 BeanUtil.setProperty(fb, propName, new Character ('a')); 636 assertEquals('a', fb.getFoochar()); 637 BeanUtil.setProperty(fb, propName, "123"); 638 assertEquals('1', fb.getFoochar()); 639 BeanUtil.setProperty(fb, propName, new Integer (789)); 640 assertEquals('7', fb.getFoochar()); 641 } 642 643 644 public void testLoaders() { 645 HashMap map = new HashMap (); 646 647 map.put("fooInteger", new Integer (1)); 648 map.put("fooint", new Integer (2)); 649 map.put("fooLong", new Long (3)); 650 map.put("foolong", new Long (4)); 651 map.put("fooByte", new Byte ((byte)5)); 652 map.put("foobyte", new Byte ((byte)6)); 653 map.put("fooCharacter", new Character ('7')); 654 map.put("foochar", new Character ('8')); 655 map.put("fooBoolean", Boolean.TRUE); 656 map.put("fooboolean", Boolean.FALSE); 657 map.put("fooFloat", new Float (9.0)); 658 map.put("foofloat", new Float (10.0)); 659 map.put("fooDouble", new Double (11.0)); 660 map.put("foodouble", new Double (12.0)); 661 map.put("fooString", "13"); 662 map.put("fooStringA", new String []{"14", "15"}); 663 664 FooBean fb = new FooBean(); 665 BeanLoaderManager.load(fb, map); 666 667 assertEquals(1, fb.getFooInteger().intValue()); 668 assertEquals(2, fb.getFooint()); 669 assertEquals(3, fb.getFooLong().longValue()); 670 assertEquals(4, fb.getFoolong()); 671 assertEquals(5, fb.getFooByte().byteValue()); 672 assertEquals(6, fb.getFoobyte()); 673 assertEquals('7', fb.getFooCharacter().charValue()); 674 assertEquals('8', fb.getFoochar()); 675 assertTrue(fb.getFooBoolean().booleanValue()); 676 assertFalse(fb.getFooboolean()); 677 assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005); 678 assertEquals(10.0, fb.getFoofloat(), 0.005); 679 assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005); 680 assertEquals(12.0, fb.getFoodouble(), 0.005); 681 assertEquals("13", fb.getFooString()); 682 assertEquals("14", fb.getFooStringA()[0]); 683 assertEquals("15", fb.getFooStringA()[1]); 684 685 map.put("FooInteger", new Integer (1)); 686 map.put("Fooint", new Integer (2)); 687 map.put("FooLong", new Long (3)); 688 map.put("Foolong", new Long (4)); 689 map.put("FooByte", new Byte ((byte)5)); 690 map.put("Foobyte", new Byte ((byte)6)); 691 map.put("FooCharacter", new Character ('7')); 692 map.put("Foochar", new Character ('8')); 693 map.put("FooBoolean", Boolean.TRUE); 694 map.put("Fooboolean", Boolean.FALSE); 695 map.put("FooFloat", new Float (9.0)); 696 map.put("Foofloat", new Float (10.0)); 697 map.put("FooDouble", new Double (11.0)); 698 map.put("Foodouble", new Double (12.0)); 699 map.put("FooString", "13"); 700 map.put("FooStringA", new String []{"14", "15"}); 701 702 fb = new FooBean(); 703 BeanLoaderManager.load(fb, map); 704 705 assertEquals(1, fb.getFooInteger().intValue()); 706 assertEquals(2, fb.getFooint()); 707 assertEquals(3, fb.getFooLong().longValue()); 708 assertEquals(4, fb.getFoolong()); 709 assertEquals(5, fb.getFooByte().byteValue()); 710 assertEquals(6, fb.getFoobyte()); 711 assertEquals('7', fb.getFooCharacter().charValue()); 712 assertEquals('8', fb.getFoochar()); 713 assertTrue(fb.getFooBoolean().booleanValue()); 714 assertFalse(fb.getFooboolean()); 715 assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005); 716 assertEquals(10.0, fb.getFoofloat(), 0.005); 717 assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005); 718 assertEquals(12.0, fb.getFoodouble(), 0.005); 719 assertEquals("13", fb.getFooString()); 720 assertEquals("14", fb.getFooStringA()[0]); 721 assertEquals("15", fb.getFooStringA()[1]); 722 723 } 724 725 public void testForEach() { 726 FooBean fb = new FooBean(); 727 fb.setFooString(" xxx "); 728 fb.setFooStringA(new String [] {" xxx ", " yy ", " z "}); 729 new TrimStringsBeanModifier().modify(fb); 730 assertEquals("xxx", fb.getFooString()); 731 assertEquals("xxx", fb.getFooStringA()[0]); 732 assertEquals("yy", fb.getFooStringA()[1]); 733 assertEquals("z", fb.getFooStringA()[2]); 734 } 735 736 737 public void testGet() { 738 FooBean fb = new FooBean(); 739 fb.setFooInteger(new Integer (101)); 740 fb.setFooint(102); 741 fb.setFooLong(new Long (103)); 742 fb.setFoolong(104); 743 fb.setFooByte(new Byte ((byte) 105)); 744 fb.setFoobyte((byte) 106); 745 fb.setFooCharacter(new Character ('7')); 746 fb.setFoochar('8'); 747 fb.setFooBoolean(Boolean.TRUE); 748 fb.setFooboolean(false); 749 fb.setFooFloat(new Float (109.0)); 750 fb.setFoofloat((float)110.0); 751 fb.setFooDouble(new Double (111.0)); 752 fb.setFoodouble(112.0); 753 fb.setFooString("113"); 754 fb.setFooStringA(new String [] {"114", "115"} ); 755 756 Integer v = (Integer ) BeanUtil.getProperty(fb, "fooInteger"); 757 assertEquals(101, v.intValue()); 758 v = (Integer ) BeanUtil.getProperty(fb, "fooint"); 759 assertEquals(102, v.intValue()); 760 Long vl = (Long ) BeanUtil.getProperty(fb, "fooLong"); 761 assertEquals(103, vl.longValue()); 762 vl = (Long ) BeanUtil.getProperty(fb, "foolong"); 763 assertEquals(104, vl.longValue()); 764 Byte vb = (Byte ) BeanUtil.getProperty(fb, "fooByte"); 765 assertEquals(105, vb.intValue()); 766 vb = (Byte ) BeanUtil.getProperty(fb, "foobyte"); 767 assertEquals(106, vb.intValue()); 768 Character c = (Character ) BeanUtil.getProperty(fb, "fooCharacter"); 769 assertEquals('7', c.charValue()); 770 c = (Character ) BeanUtil.getProperty(fb, "foochar"); 771 assertEquals('8', c.charValue()); 772 Boolean b = (Boolean ) BeanUtil.getProperty(fb, "fooBoolean"); 773 assertTrue(b.booleanValue()); 774 b = (Boolean ) BeanUtil.getProperty(fb, "fooboolean"); 775 assertFalse(b.booleanValue()); 776 Float f = (Float ) BeanUtil.getProperty(fb, "fooFloat"); 777 assertEquals(109.0, f.floatValue(), 0.005); 778 f = (Float ) BeanUtil.getProperty(fb, "foofloat"); 779 assertEquals(110.0, f.floatValue(), 0.005); 780 Double d = (Double ) BeanUtil.getProperty(fb, "fooDouble"); 781 assertEquals(111.0, d.doubleValue(), 0.005); 782 d = (Double ) BeanUtil.getProperty(fb, "foodouble"); 783 assertEquals(112.0, d.doubleValue(), 0.005); 784 String s = (String ) BeanUtil.getProperty(fb, "fooString"); 785 assertEquals("113", s); 786 String [] sa = (String []) BeanUtil.getProperty(fb, "fooStringA"); 787 assertEquals(2, sa.length); 788 assertEquals("114", sa[0]); 789 assertEquals("115", sa[1]); 790 } 791 792 public void testCopy() { 793 FooBean fb = new FooBean(); 794 fb.setFooInteger(new Integer (201)); 795 fb.setFooint(202); 796 fb.setFooLong(new Long (203)); 797 fb.setFoolong(204); 798 fb.setFooByte(new Byte ((byte) 95)); 799 fb.setFoobyte((byte) 96); 800 fb.setFooCharacter(new Character ('7')); 801 fb.setFoochar('8'); 802 fb.setFooBoolean(Boolean.TRUE); 803 fb.setFooboolean(false); 804 fb.setFooFloat(new Float (209.0)); 805 fb.setFoofloat((float)210.0); 806 fb.setFooDouble(new Double (211.0)); 807 fb.setFoodouble(212.0); 808 fb.setFooString("213"); 809 fb.setFooStringA(new String [] {"214", "215"} ); 810 811 FooBean dest = new FooBean(); 812 BeanCopy.copyProperties(fb, dest); 813 814 Integer v = (Integer ) BeanUtil.getProperty(dest, "fooInteger"); 815 assertEquals(201, v.intValue()); 816 v = (Integer ) BeanUtil.getProperty(dest, "fooint"); 817 assertEquals(202, v.intValue()); 818 Long vl = (Long ) BeanUtil.getProperty(dest, "fooLong"); 819 assertEquals(203, vl.longValue()); 820 vl = (Long ) BeanUtil.getProperty(dest, "foolong"); 821 assertEquals(204, vl.longValue()); 822 Byte vb = (Byte ) BeanUtil.getProperty(dest, "fooByte"); 823 assertEquals(95, vb.intValue()); 824 vb = (Byte ) BeanUtil.getProperty(dest, "foobyte"); 825 assertEquals(96, vb.intValue()); 826 Character c = (Character ) BeanUtil.getProperty(dest, "fooCharacter"); 827 assertEquals('7', c.charValue()); 828 c = (Character ) BeanUtil.getProperty(dest, "foochar"); 829 assertEquals('8', c.charValue()); 830 Boolean b = (Boolean ) BeanUtil.getProperty(dest, "fooBoolean"); 831 assertTrue(b.booleanValue()); 832 b = (Boolean ) BeanUtil.getProperty(dest, "fooboolean"); 833 assertFalse(b.booleanValue()); 834 Float f = (Float ) BeanUtil.getProperty(dest, "fooFloat"); 835 assertEquals(209.0, f.floatValue(), 0.005); 836 f = (Float ) BeanUtil.getProperty(dest, "foofloat"); 837 assertEquals(210.0, f.floatValue(), 0.005); 838 Double d = (Double ) BeanUtil.getProperty(dest, "fooDouble"); 839 assertEquals(211.0, d.doubleValue(), 0.005); 840 d = (Double ) BeanUtil.getProperty(dest, "foodouble"); 841 assertEquals(212.0, d.doubleValue(), 0.005); 842 String s = (String ) BeanUtil.getProperty(dest, "fooString"); 843 assertEquals("213", s); 844 String [] sa = (String []) BeanUtil.getProperty(dest, "fooStringA"); 845 assertEquals(2, sa.length); 846 assertEquals("214", sa[0]); 847 assertEquals("215", sa[1]); 848 849 850 FooBean empty = new FooBean(); 851 BeanCopy.copyProperties(empty, dest); 852 v = (Integer ) BeanUtil.getProperty(dest, "fooInteger"); 853 assertNull(v); 854 v = (Integer ) BeanUtil.getProperty(dest, "fooint"); 855 assertEquals(0, v.intValue()); 856 vl = (Long ) BeanUtil.getProperty(dest, "fooLong"); 857 assertNull(vl); 858 vl = (Long ) BeanUtil.getProperty(dest, "foolong"); 859 assertEquals(0, vl.longValue()); 860 vb = (Byte ) BeanUtil.getProperty(dest, "fooByte"); 861 assertNull(vb); 862 vb = (Byte ) BeanUtil.getProperty(dest, "foobyte"); 863 assertEquals(0, vb.byteValue()); 864 c = (Character ) BeanUtil.getProperty(dest, "fooCharacter"); 865 assertNull(c); 866 c = (Character ) BeanUtil.getProperty(dest, "foochar"); 867 assertEquals(0, c.charValue()); 868 b = (Boolean ) BeanUtil.getProperty(dest, "fooBoolean"); 869 assertNull(b); 870 b = (Boolean ) BeanUtil.getProperty(dest, "fooboolean"); 871 assertFalse(b.booleanValue()); 872 f = (Float ) BeanUtil.getProperty(dest, "fooFloat"); 873 assertNull(f); 874 f = (Float ) BeanUtil.getProperty(dest, "foofloat"); 875 assertEquals(0, f.floatValue(), 0.005); 876 d = (Double ) BeanUtil.getProperty(dest, "fooDouble"); 877 assertNull(d); 878 d = (Double ) BeanUtil.getProperty(dest, "foodouble"); 879 assertEquals(0, d.doubleValue(), 0.005); 880 s = (String ) BeanUtil.getProperty(dest, "fooString"); 881 assertNull(s); 882 sa = (String []) BeanUtil.getProperty(dest, "fooStringA"); 883 assertNull(sa); 884 } 885 886 public void testNested() { 887 Cbean cbean = new Cbean(); 888 String value = "testnest"; 889 String value2 = "nesttest"; 890 assertTrue(BeanUtil.hasDeclaredProperty(cbean, "bbean.abean.fooProp")); 891 BeanUtil.setProperty(cbean, "bbean.abean.fooProp", value); 892 assertEquals(value, (String ) BeanUtil.getProperty(cbean, "bbean.abean.fooProp")); 893 Bbean bbean = (Bbean) BeanUtil.getProperty(cbean, "bbean"); 894 assertTrue(BeanUtil.hasDeclaredProperty(bbean, "abean.fooProp")); 895 assertEquals(value, (String ) BeanUtil.getProperty(bbean, "abean.fooProp")); 896 Abean abean = (Abean) BeanUtil.getProperty(bbean, "abean"); 897 assertEquals(value, (String ) BeanUtil.getProperty(abean, "fooProp")); 898 BeanUtil.setProperty(bbean, "abean.fooProp", value2); 899 assertEquals(value2, (String ) BeanUtil.getProperty(bbean, "abean.fooProp")); 900 } 901 902 public void testIster() { 903 Abean abean = new Abean(); 904 Boolean b = (Boolean ) BeanUtil.getProperty(abean, "something"); 905 assertTrue(b.booleanValue()); 906 try { 907 BeanUtil.getProperty(abean, "Something"); 908 fail(); 909 } catch (BeanException bex) { 910 } 912 913 } 914 915 916 public void testMap() { 917 Cbean cbean = new Cbean(); 918 Abean abean = cbean.getBbean().getAbean(); 919 BeanUtil.setProperty(abean, "mval", new Integer (173)); 920 BeanUtil.setProperty(abean, "mval2", new Integer (1)); 921 assertEquals(173, (abean.get("mval")).intValue()); 922 assertEquals(173, ((Integer ) BeanUtil.getProperty(abean, "mval")).intValue()); 923 assertEquals(1, ((Integer ) BeanUtil.getProperty(abean, "mval2")).intValue()); 924 assertTrue(BeanUtil.hasDeclaredProperty(cbean, "bbean.abean.mval")); 925 BeanUtil.setProperty(cbean, "bbean.abean.mval", new Integer (3)); 926 assertEquals(3, ((Integer ) BeanUtil.getProperty(abean, "mval")).intValue()); 927 assertEquals(3, ((Integer ) BeanUtil.getProperty(cbean, "bbean.abean.mval")).intValue()); 928 HashMap map = new HashMap (); 929 BeanUtil.setProperty(map, "val1", new Integer (173)); 930 assertEquals(173, ((Integer )map.get("val1")).intValue()); 931 Integer i = (Integer ) BeanUtil.getProperty(map, "val1"); 932 assertEquals(173, i.intValue()); 933 } 934 935 public void testMap3() { 936 Map m = new HashMap (); 937 BeanUtil.setProperty(m, "Foo", "John"); 938 assertEquals("John", (String ) m.get("Foo")); 939 assertNull(m.get("foo")); 940 assertFalse(BeanUtil.hasDeclaredProperty(m, "foo")); 941 BeanUtil.setProperty(m, "foo", new HashMap ()); 942 assertTrue(BeanUtil.hasDeclaredProperty(m, "foo")); 943 assertFalse(BeanUtil.hasDeclaredProperty(m, "foo.Name")); 944 BeanUtil.setProperty(m, "foo.Name", "Doe"); 945 assertEquals("John", (String ) m.get("Foo")); 946 assertEquals("Doe", ((HashMap ) m.get("foo")).get("Name")); 947 assertNull("Doe", ((HashMap ) m.get("foo")).get("name")); 948 assertEquals("John", (String ) BeanUtil.getProperty(m, "Foo")); 949 assertEquals("Doe", (String ) BeanUtil.getProperty(m, "foo.Name")); 950 try { 951 assertNull(BeanUtil.getProperty(m, "foo.name")); 952 fail(); 953 } catch (Exception e) { 954 } 955 } 956 957 public void testNotDeclared() { 958 FooBean3 fb = new FooBean3(); 959 960 try { 961 BeanUtil.setProperty(fb, "pprotected", new Integer (1)); 962 fail(); 963 } catch(Exception ex) {} 964 try { 965 BeanUtil.getProperty(fb, "pprotected"); 966 fail(); 967 } catch(Exception ex) {} 968 969 try { 970 BeanUtil.setProperty(fb, "ppackage", new Integer (2)); 971 fail(); 972 } catch(Exception ex) {} 973 try { 974 BeanUtil.getProperty(fb, "ppackage"); 975 fail(); 976 } catch(Exception ex) {} 977 978 try { 979 BeanUtil.setProperty(fb, "pprivate", new Integer (3)); 980 fail(); 981 } catch(Exception ex) {} 982 try { 983 BeanUtil.getProperty(fb, "pprivate"); 984 fail(); 985 } catch(Exception ex) {} 986 } 987 988 public void testDeclared() { 989 FooBean3 fb = new FooBean3(); 990 991 BeanUtil.setDeclaredProperty(fb, "pprotected", new Integer (1)); 992 Integer value = (Integer ) BeanUtil.getDeclaredProperty(fb, "pprotected"); 993 assertNotNull(value); 994 assertEquals(1, value.intValue()); 995 996 BeanUtil.setDeclaredProperty(fb, "ppackage", new Integer (2)); 997 value = (Integer ) BeanUtil.getDeclaredProperty(fb, "ppackage"); 998 assertNotNull(value); 999 assertEquals(2, value.intValue()); 1000 1001 BeanUtil.setDeclaredProperty(fb, "pprivate", new Integer (3)); 1002 value = (Integer ) BeanUtil.getDeclaredProperty(fb, "pprivate"); 1003 assertNotNull(value); 1004 assertEquals(3, value.intValue()); 1005 } 1006 1007 1008 static class Dummy { 1009 private FooBean4 fb = new FooBean4(); 1010 1011 public FooBean4 getFb() { 1012 return fb; 1013 } 1014 1015 public void setFb(FooBean4 fb) { 1016 this.fb = fb; 1017 } 1018 1019 1020 private Integer [] data = new Integer [] {Integer.valueOf("173"), Integer.valueOf("2")}; 1021 1022 public Integer [] getData() { 1023 return data; 1024 } 1025 } 1026 1027 public void testArrays() { 1028 FooBean4 fb4 = new FooBean4(); 1029 Dummy dummy = new Dummy(); 1030 assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[0].bbean.abean.fooProp")); 1031 assertEquals("xxx", BeanUtil.getProperty(fb4, "data[0].bbean.abean.fooProp")); 1032 assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[1].bbean.abean.fooProp")); 1033 assertEquals("yyy", BeanUtil.getProperty(fb4, "data[1].bbean.abean.fooProp")); 1034 1035 assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[2].bbean.abean.fooProp")); 1036 assertEquals("zzz", BeanUtil.getProperty(fb4, "data[2].bbean.abean.fooProp")); 1037 BeanUtil.setProperty(fb4, "data[2].bbean.abean.fooProp", "ZZZ"); 1038 assertEquals("ZZZ", BeanUtil.getProperty(fb4, "data[2].bbean.abean.fooProp")); 1039 1040 assertTrue(BeanUtil.hasDeclaredProperty(fb4, "list[0].bbean.abean.fooProp")); 1041 assertEquals("LLL", BeanUtil.getProperty(fb4, "list[0].bbean.abean.fooProp")); 1042 BeanUtil.setProperty(fb4, "list[0].bbean.abean.fooProp", "EEE"); 1043 assertEquals("EEE", BeanUtil.getProperty(fb4, "list[0].bbean.abean.fooProp")); 1044 assertEquals("lll", BeanUtil.getProperty(fb4, "list[1]")); 1045 BeanUtil.setProperty(fb4, "list[1]", "eee"); 1046 1047 assertFalse(BeanUtil.hasDeclaredProperty(fb4, "list[1].bbean.abean.fooProp")); 1048 assertTrue(BeanUtil.hasDeclaredProperty(fb4, "list[1]")); 1049 assertEquals("eee", BeanUtil.getProperty(fb4, "list[1]")); 1050 assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[0].bbean.abean.fooProp")); 1051 assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[1].bbean.abean.fooProp")); 1052 assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[2].bbean.abean.fooProp")); 1053 assertEquals("xxx", BeanUtil.getProperty(dummy, "fb.data[0].bbean.abean.fooProp")); 1054 assertEquals("yyy", BeanUtil.getProperty(dummy, "fb.data[1].bbean.abean.fooProp")); 1055 assertEquals("zzz", BeanUtil.getProperty(dummy, "fb.data[2].bbean.abean.fooProp")); 1056 1057 BeanUtil.setProperty(dummy, "fb.data[2].bbean.abean.fooProp", "ZZZ"); 1058 assertEquals("ZZZ", BeanUtil.getProperty(dummy, "fb.data[2].bbean.abean.fooProp")); 1059 assertEquals(new Integer (173), BeanUtil.getProperty(dummy, "data[0]")); 1060 1061 BeanUtil.setProperty(dummy, "data[0]", new Integer (-173)); 1062 assertEquals(new Integer (-173), BeanUtil.getProperty(dummy, "data[0]")); 1063 } 1064 1065 1066 public void testForced() { 1067 XBean x = new XBean(); 1068 assertTrue(BeanUtil.hasDeclaredProperty(x, "y")); 1069 assertFalse(BeanUtil.hasDeclaredProperty(x, "y.foo")); 1070 assertFalse(BeanUtil.hasDeclaredProperty(x, "y[23].foo")); 1071 try{ 1072 BeanUtil.setProperty(x, "y.foo", "yyy"); 1073 fail(); 1074 } catch(Exception ex) {} 1075 assertNull(x.getY()); 1076 1077 BeanUtil.setPropertyForced(x, "y.foo", "yyy"); 1078 assertTrue(BeanUtil.hasDeclaredProperty(x, "y.foo")); 1079 assertEquals("yyy", x.getY().getFoo()); 1080 1081 assertNotNull(x.getYy()); 1082 assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[2].foo")); 1083 try{ 1084 BeanUtil.setProperty(x, "yy[2].foo", "yyy"); 1085 fail(); 1086 } catch(Exception ex) { 1087 } 1088 assertNull(x.getYy()[2]); 1089 1090 BeanUtil.setPropertyForced(x, "yy[2].foo", "xxx"); 1091 assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[2].foo")); 1092 assertEquals("xxx", x.getYy()[2].getFoo()); 1093 1094 assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[20].foo")); 1095 BeanUtil.setPropertyForced(x, "yy[20].foo", "zzz"); 1096 assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[20].foo")); 1097 assertEquals("zzz", x.getYy()[20].getFoo()); 1098 } 1099 1100 1101 public void testSilent() { 1102 FooBean fb = new FooBean(); 1103 assertFalse(BeanUtil.hasDeclaredProperty(fb, "notexisting")); 1104 try { 1105 BeanUtil.setProperty(fb, "notexisting", null); 1106 fail(); 1107 } catch(Exception ex) {} 1108 assertFalse(BeanUtil.setPropertySilent(fb, "notexisting", null)); 1109 1110 } 1111} | Popular Tags |