| 1 16 17 package org.springframework.beans; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 33 import junit.framework.TestCase; 34 import org.hibernate.FlushMode; 35 36 import org.springframework.beans.support.DerivedFromProtectedBaseBean; 37 import org.springframework.util.StringUtils; 38 39 43 public class BeanWrapperTests extends TestCase { 44 45 public void testSetWrappedInstanceOfSameClass() throws Exception { 46 TestBean tb = new TestBean(); 47 BeanWrapper bw = new BeanWrapperImpl(tb); 48 assertTrue(bw.isReadableProperty("age")); 49 tb.setAge(11); 50 51 TestBean tb2 = new TestBean(); 52 bw.setWrappedInstance(tb2); 53 54 bw.setPropertyValue("age", new Integer (14)); 55 assertTrue("2nd changed", tb2.getAge() == 14); 56 assertTrue("1 didn't change", tb.getAge() == 11); 57 } 58 59 public void testIsReadablePropertyNotReadable() { 60 NoRead nr = new NoRead(); 61 BeanWrapper bw = new BeanWrapperImpl(nr); 62 assertFalse(bw.isReadableProperty("age")); 63 } 64 65 68 public void testIsReadablePropertyNoSuchProperty() { 69 NoRead nr = new NoRead(); 70 BeanWrapper bw = new BeanWrapperImpl(nr); 71 assertFalse(bw.isReadableProperty("xxxxx")); 72 } 73 74 public void testIsReadablePropertyNull() { 75 NoRead nr = new NoRead(); 76 BeanWrapper bw = new BeanWrapperImpl(nr); 77 try { 78 bw.isReadableProperty(null); 79 fail("Can't inquire into readability of null property"); 80 } 81 catch (IllegalArgumentException ex) { 82 } 84 } 85 86 public void testIsWritablePropertyNull() { 87 NoRead nr = new NoRead(); 88 BeanWrapper bw = new BeanWrapperImpl(nr); 89 try { 90 bw.isWritableProperty(null); 91 fail("Can't inquire into writability of null property"); 92 } 93 catch (IllegalArgumentException ex) { 94 } 96 } 97 98 public void testReadableAndWritableForIndexedProperties() { 99 BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class); 100 101 assertTrue(bw.isReadableProperty("array")); 102 assertTrue(bw.isReadableProperty("list")); 103 assertTrue(bw.isReadableProperty("set")); 104 assertTrue(bw.isReadableProperty("map")); 105 assertFalse(bw.isReadableProperty("xxx")); 106 107 assertTrue(bw.isWritableProperty("array")); 108 assertTrue(bw.isWritableProperty("list")); 109 assertTrue(bw.isWritableProperty("set")); 110 assertTrue(bw.isWritableProperty("map")); 111 assertFalse(bw.isWritableProperty("xxx")); 112 113 assertTrue(bw.isReadableProperty("array[0]")); 114 assertTrue(bw.isReadableProperty("array[0].name")); 115 assertTrue(bw.isReadableProperty("list[0]")); 116 assertTrue(bw.isReadableProperty("list[0].name")); 117 assertTrue(bw.isReadableProperty("set[0]")); 118 assertTrue(bw.isReadableProperty("set[0].name")); 119 assertTrue(bw.isReadableProperty("map[key1]")); 120 assertTrue(bw.isReadableProperty("map[key1].name")); 121 assertTrue(bw.isReadableProperty("map[key4][0]")); 122 assertTrue(bw.isReadableProperty("map[key4][0].name")); 123 assertTrue(bw.isReadableProperty("map[key4][1]")); 124 assertTrue(bw.isReadableProperty("map[key4][1].name")); 125 assertFalse(bw.isReadableProperty("array[key1]")); 126 127 assertTrue(bw.isWritableProperty("array[0]")); 128 assertTrue(bw.isWritableProperty("array[0].name")); 129 assertTrue(bw.isWritableProperty("list[0]")); 130 assertTrue(bw.isWritableProperty("list[0].name")); 131 assertTrue(bw.isWritableProperty("set[0]")); 132 assertTrue(bw.isWritableProperty("set[0].name")); 133 assertTrue(bw.isWritableProperty("map[key1]")); 134 assertTrue(bw.isWritableProperty("map[key1].name")); 135 assertTrue(bw.isWritableProperty("map[key4][0]")); 136 assertTrue(bw.isWritableProperty("map[key4][0].name")); 137 assertTrue(bw.isWritableProperty("map[key4][1]")); 138 assertTrue(bw.isWritableProperty("map[key4][1].name")); 139 assertFalse(bw.isWritableProperty("array[key1]")); 140 } 141 142 public void testSetWrappedInstanceOfDifferentClass() { 143 ThrowsException tex = new ThrowsException(); 144 BeanWrapper bw = new BeanWrapperImpl(tex); 145 146 TestBean tb2 = new TestBean(); 147 bw.setWrappedInstance(tb2); 148 149 bw.setPropertyValue("age", new Integer (14)); 150 assertTrue("2nd changed", tb2.getAge() == 14); 151 } 152 153 public void testGetterThrowsException() { 154 GetterBean gb = new GetterBean(); 155 BeanWrapper bw = new BeanWrapperImpl(gb); 156 bw.setPropertyValue("name", "tom"); 157 assertTrue("Set name to tom", gb.getName().equals("tom")); 158 } 159 160 public void testEmptyPropertyValuesSet() { 161 TestBean t = new TestBean(); 162 int age = 50; 163 String name = "Tony"; 164 t.setAge(age); 165 t.setName(name); 166 try { 167 BeanWrapper bw = new BeanWrapperImpl(t); 168 assertTrue("age is OK", t.getAge() == age); 169 assertTrue("name is OK", name.equals(t.getName())); 170 bw.setPropertyValues(new MutablePropertyValues()); 171 assertTrue("age is OK", t.getAge() == age); 173 assertTrue("name is OK", name.equals(t.getName())); 174 } 175 catch (BeansException ex) { 176 fail("Shouldn't throw exception when everything is valid"); 177 } 178 } 179 180 public void testAllValid() { 181 TestBean t = new TestBean(); 182 String newName = "tony"; 183 int newAge = 65; 184 String newTouchy = "valid"; 185 try { 186 BeanWrapper bw = new BeanWrapperImpl(t); 187 MutablePropertyValues pvs = new MutablePropertyValues(); 189 pvs.addPropertyValue(new PropertyValue("age", new Integer (newAge))); 190 pvs.addPropertyValue(new PropertyValue("name", newName)); 191 pvs.addPropertyValue(new PropertyValue("touchy", newTouchy)); 192 bw.setPropertyValues(pvs); 193 assertTrue("Validly set property must stick", t.getName().equals(newName)); 194 assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy)); 195 assertTrue("Validly set property must stick", t.getAge() == newAge); 196 } 197 catch (BeansException ex) { 198 fail("Shouldn't throw exception when everything is valid"); 199 } 200 } 201 202 public void testBeanWrapperUpdates() { 203 TestBean t = new TestBean(); 204 int newAge = 33; 205 try { 206 BeanWrapper bw = new BeanWrapperImpl(t); 207 t.setAge(newAge); 208 Object bwAge = bw.getPropertyValue("age"); 209 assertTrue("Age is an integer", bwAge instanceof Integer ); 210 int bwi = ((Integer ) bwAge).intValue(); 211 assertTrue("Bean wrapper must pick up changes", bwi == newAge); 212 } 213 catch (Exception ex) { 214 fail("Shouldn't throw exception when everything is valid"); 215 } 216 } 217 218 public void testValidNullUpdate() { 219 TestBean t = new TestBean(); 220 t.setName("Frank"); t.setSpouse(t); 222 BeanWrapper bw = new BeanWrapperImpl(t); 223 assertTrue("name is not null to start off", t.getName() != null); 224 bw.setPropertyValue("name", null); 225 assertTrue("name is now null", t.getName() == null); 226 assertTrue("spouse is not null to start off", t.getSpouse() != null); 228 bw.setPropertyValue("spouse", null); 229 assertTrue("spouse is now null", t.getSpouse() == null); 230 } 231 232 public void testIgnoringIndexedProperty() { 233 MutablePropertyValues values = new MutablePropertyValues(); 234 values.addPropertyValue("toBeIgnored[0]", new Integer (42)); 235 BeanWrapper wrapper = new BeanWrapperImpl(new Object ()); 236 wrapper.setPropertyValues(values, true); 237 } 238 239 public void testBooleanObject() { 240 BooleanTestBean tb = new BooleanTestBean(); 241 BeanWrapper bw = new BeanWrapperImpl(tb); 242 243 try { 244 bw.setPropertyValue("bool2", "true"); 245 } 246 catch (BeansException ex) { 247 fail("Should not throw BeansException: " + ex.getMessage()); 248 } 249 assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); 250 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 251 252 try { 253 bw.setPropertyValue("bool2", "false"); 254 } 255 catch (BeansException ex) { 256 fail("Should not throw BeansException: " + ex.getMessage()); 257 } 258 assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); 259 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 260 261 } 262 263 public void testNumberObjects() { 264 NumberTestBean tb = new NumberTestBean(); 265 BeanWrapper bw = new BeanWrapperImpl(tb); 266 267 try { 268 bw.setPropertyValue("short2", "2"); 269 bw.setPropertyValue("int2", "8"); 270 bw.setPropertyValue("long2", "6"); 271 bw.setPropertyValue("bigInteger", "3"); 272 bw.setPropertyValue("float2", "8.1"); 273 bw.setPropertyValue("double2", "6.1"); 274 bw.setPropertyValue("bigDecimal", "4.0"); 275 } 276 catch (BeansException ex) { 277 fail("Should not throw BeansException: " + ex.getMessage()); 278 } 279 280 assertTrue("Correct short2 value", new Short ("2").equals(bw.getPropertyValue("short2"))); 281 assertTrue("Correct short2 value", new Short ("2").equals(tb.getShort2())); 282 assertTrue("Correct int2 value", new Integer ("8").equals(bw.getPropertyValue("int2"))); 283 assertTrue("Correct int2 value", new Integer ("8").equals(tb.getInt2())); 284 assertTrue("Correct long2 value", new Long ("6").equals(bw.getPropertyValue("long2"))); 285 assertTrue("Correct long2 value", new Long ("6").equals(tb.getLong2())); 286 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(bw.getPropertyValue("bigInteger"))); 287 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(tb.getBigInteger())); 288 assertTrue("Correct float2 value", new Float ("8.1").equals(bw.getPropertyValue("float2"))); 289 assertTrue("Correct float2 value", new Float ("8.1").equals(tb.getFloat2())); 290 assertTrue("Correct double2 value", new Double ("6.1").equals(bw.getPropertyValue("double2"))); 291 assertTrue("Correct double2 value", new Double ("6.1").equals(tb.getDouble2())); 292 assertTrue("Correct bigDecimal value", new BigDecimal ("4.0").equals(bw.getPropertyValue("bigDecimal"))); 293 assertTrue("Correct bigDecimal value", new BigDecimal ("4.0").equals(tb.getBigDecimal())); 294 } 295 296 public void testEnum() { 297 EnumTest et = new EnumTest(); 298 BeanWrapper bw = new BeanWrapperImpl(et); 299 300 bw.setPropertyValue("flushMode", "NEVER"); 301 assertEquals(FlushMode.NEVER, et.getFlushMode()); 302 303 try { 304 bw.setPropertyValue("flushMode", "EVER"); 305 fail("Should have thrown TypeMismatchException"); 306 } 307 catch (TypeMismatchException ex) { 308 } 310 } 311 312 public void testPropertiesProperty() throws Exception { 313 PropsTest pt = new PropsTest(); 314 BeanWrapper bw = new BeanWrapperImpl(pt); 315 bw.setPropertyValue("name", "ptest"); 316 317 String ps = "peace=war\nfreedom=slavery"; 319 bw.setPropertyValue("properties", ps); 320 321 assertTrue("name was set", pt.name.equals("ptest")); 322 assertTrue("props non null", pt.props != null); 323 String freedomVal = pt.props.getProperty("freedom"); 324 String peaceVal = pt.props.getProperty("peace"); 325 assertTrue("peace==war", peaceVal.equals("war")); 326 assertTrue("Freedom==slavery", freedomVal.equals("slavery")); 327 } 328 329 public void testStringArrayProperty() throws Exception { 330 PropsTest pt = new PropsTest(); 331 BeanWrapper bw = new BeanWrapperImpl(pt); 332 333 bw.setPropertyValue("stringArray", new String []{"foo", "fi", "fi", "fum"}); 334 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 335 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 336 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 337 338 List list = new ArrayList (); 339 list.add("foo"); 340 list.add("fi"); 341 list.add("fi"); 342 list.add("fum"); 343 bw.setPropertyValue("stringArray", list); 344 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 345 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 346 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 347 348 Set set = new HashSet (); 349 set.add("foo"); 350 set.add("fi"); 351 set.add("fum"); 352 bw.setPropertyValue("stringArray", set); 353 assertTrue("stringArray length = 3", pt.stringArray.length == 3); 354 List result = Arrays.asList(pt.stringArray); 355 assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum")); 356 357 bw.setPropertyValue("stringArray", "one"); 358 assertTrue("stringArray length = 1", pt.stringArray.length == 1); 359 assertTrue("stringArray elt is ok", pt.stringArray[0].equals("one")); 360 361 bw.setPropertyValue("stringArray", "foo,fi,fi,fum"); 362 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 363 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 364 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 365 } 366 367 public void testStringArrayPropertyWithCustomStringEditor() throws Exception { 368 PropsTest pt = new PropsTest(); 369 BeanWrapper bw = new BeanWrapperImpl(pt); 370 bw.registerCustomEditor(String .class, "stringArray", new PropertyEditorSupport () { 371 public void setAsText(String text) { 372 setValue(text.substring(1)); 373 } 374 }); 375 376 bw.setPropertyValue("stringArray", new String [] {"4foo", "7fi", "6fi", "5fum"}); 377 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 378 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 379 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 380 381 List list = new ArrayList (); 382 list.add("4foo"); 383 list.add("7fi"); 384 list.add("6fi"); 385 list.add("5fum"); 386 bw.setPropertyValue("stringArray", list); 387 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 388 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 389 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 390 391 Set set = new HashSet (); 392 set.add("4foo"); 393 set.add("7fi"); 394 set.add("6fum"); 395 bw.setPropertyValue("stringArray", set); 396 assertTrue("stringArray length = 3", pt.stringArray.length == 3); 397 List result = Arrays.asList(pt.stringArray); 398 assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum")); 399 400 bw.setPropertyValue("stringArray", "8one"); 401 assertTrue("stringArray length = 1", pt.stringArray.length == 1); 402 assertTrue("correct values", pt.stringArray[0].equals("one")); 403 404 bw.setPropertyValue("stringArray", "1foo,3fi,2fi,1fum"); 405 assertTrue("stringArray length = 4", pt.stringArray.length == 4); 406 assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && 407 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); 408 } 409 410 public void testStringArrayPropertyWithCustomStringArrayEditor() throws Exception { 411 TestBean tb = new TestBean(); 412 BeanWrapper bw = new BeanWrapperImpl(tb); 413 bw.registerCustomEditor(String .class, "name", new PropertyEditorSupport () { 414 public void setValue(Object value) { 415 if (value instanceof String []) { 416 setValue(StringUtils.arrayToDelimitedString(((String []) value), "-")); 417 } 418 else { 419 super.setValue(value); 420 } 421 } 422 }); 423 bw.setPropertyValue("name", new String [] {"a1", "b2"}); 424 assertEquals("a1-b2", tb.getName()); 425 } 426 427 public void testIntArrayProperty() { 428 PropsTest pt = new PropsTest(); 429 BeanWrapper bw = new BeanWrapperImpl(pt); 430 431 bw.setPropertyValue("intArray", new int[]{4, 5, 2, 3}); 432 assertTrue("intArray length = 4", pt.intArray.length == 4); 433 assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && 434 pt.intArray[2] == 2 && pt.intArray[3] == 3); 435 436 bw.setPropertyValue("intArray", new String []{"4", "5", "2", "3"}); 437 assertTrue("intArray length = 4", pt.intArray.length == 4); 438 assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && 439 pt.intArray[2] == 2 && pt.intArray[3] == 3); 440 441 List list = new ArrayList (); 442 list.add(new Integer (4)); 443 list.add("5"); 444 list.add(new Integer (2)); 445 list.add("3"); 446 bw.setPropertyValue("intArray", list); 447 assertTrue("intArray length = 4", pt.intArray.length == 4); 448 assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && 449 pt.intArray[2] == 2 && pt.intArray[3] == 3); 450 451 Set set = new HashSet (); 452 set.add("4"); 453 set.add(new Integer (5)); 454 set.add("3"); 455 bw.setPropertyValue("intArray", set); 456 assertTrue("intArray length = 3", pt.intArray.length == 3); 457 List result = new ArrayList (); 458 result.add(new Integer (pt.intArray[0])); 459 result.add(new Integer (pt.intArray[1])); 460 result.add(new Integer (pt.intArray[2])); 461 assertTrue("correct values", result.contains(new Integer (4)) && result.contains(new Integer (5)) && 462 result.contains(new Integer (3))); 463 464 bw.setPropertyValue("intArray", new Integer []{new Integer (1)}); 465 assertTrue("intArray length = 4", pt.intArray.length == 1); 466 assertTrue("correct values", pt.intArray[0] == 1); 467 468 bw.setPropertyValue("intArray", new Integer (1)); 469 assertTrue("intArray length = 4", pt.intArray.length == 1); 470 assertTrue("correct values", pt.intArray[0] == 1); 471 472 bw.setPropertyValue("intArray", new String []{"1"}); 473 assertTrue("intArray length = 4", pt.intArray.length == 1); 474 assertTrue("correct values", pt.intArray[0] == 1); 475 476 bw.setPropertyValue("intArray", "1"); 477 assertTrue("intArray length = 4", pt.intArray.length == 1); 478 assertTrue("correct values", pt.intArray[0] == 1); 479 } 480 481 public void testIntArrayPropertyWithCustomEditor() { 482 PropsTest pt = new PropsTest(); 483 BeanWrapper bw = new BeanWrapperImpl(pt); 484 bw.registerCustomEditor(int.class, new PropertyEditorSupport () { 485 public void setAsText(String text) { 486 setValue(new Integer (Integer.parseInt(text) + 1)); 487 } 488 }); 489 490 bw.setPropertyValue("intArray", new int[]{4, 5, 2, 3}); 491 assertTrue("intArray length = 4", pt.intArray.length == 4); 492 assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && 493 pt.intArray[2] == 2 && pt.intArray[3] == 3); 494 495 bw.setPropertyValue("intArray", new String []{"3", "4", "1", "2"}); 496 assertTrue("intArray length = 4", pt.intArray.length == 4); 497 assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && 498 pt.intArray[2] == 2 && pt.intArray[3] == 3); 499 500 bw.setPropertyValue("intArray", new Integer (1)); 501 assertTrue("intArray length = 4", pt.intArray.length == 1); 502 assertTrue("correct values", pt.intArray[0] == 1); 503 504 bw.setPropertyValue("intArray", new String []{"0"}); 505 assertTrue("intArray length = 4", pt.intArray.length == 1); 506 assertTrue("correct values", pt.intArray[0] == 1); 507 508 bw.setPropertyValue("intArray", "0"); 509 assertTrue("intArray length = 4", pt.intArray.length == 1); 510 assertTrue("correct values", pt.intArray[0] == 1); 511 } 512 513 public void testIndividualAllValid() { 514 TestBean t = new TestBean(); 515 String newName = "tony"; 516 int newAge = 65; 517 String newTouchy = "valid"; 518 try { 519 BeanWrapper bw = new BeanWrapperImpl(t); 520 bw.setPropertyValue("age", new Integer (newAge)); 521 bw.setPropertyValue(new PropertyValue("name", newName)); 522 bw.setPropertyValue(new PropertyValue("touchy", newTouchy)); 523 assertTrue("Validly set property must stick", t.getName().equals(newName)); 524 assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy)); 525 assertTrue("Validly set property must stick", t.getAge() == newAge); 526 } 527 catch (BeansException ex) { 528 fail("Shouldn't throw exception when everything is valid"); 529 } 530 } 531 532 public void test2Invalid() { 533 TestBean t = new TestBean(); 534 String newName = "tony"; 535 String invalidTouchy = ".valid"; 536 try { 537 BeanWrapper bw = new BeanWrapperImpl(t); 538 MutablePropertyValues pvs = new MutablePropertyValues(); 540 pvs.addPropertyValue(new PropertyValue("age", "foobar")); 541 pvs.addPropertyValue(new PropertyValue("name", newName)); 542 pvs.addPropertyValue(new PropertyValue("touchy", invalidTouchy)); 543 bw.setPropertyValues(pvs); 544 fail("Should throw exception when everything is valid"); 545 } 546 catch (PropertyAccessExceptionsException ex) { 547 assertTrue("Must contain 2 exceptions", ex.getExceptionCount() == 2); 548 assertTrue("Validly set property must stick", t.getName().equals(newName)); 550 assertTrue("Invalidly set property must retain old value", t.getAge() == 0); 551 assertTrue("New value of dodgy setter must be available through exception", 552 ex.getPropertyAccessException("touchy").getPropertyChangeEvent().getNewValue().equals(invalidTouchy)); 553 } 554 catch (Exception ex) { 555 fail("Shouldn't throw exception other than pvee"); 556 } 557 } 558 559 public void testTypeMismatch() { 560 TestBean t = new TestBean(); 561 try { 562 BeanWrapper bw = new BeanWrapperImpl(t); 563 bw.setPropertyValue("age", "foobar"); 564 fail("Should throw exception on type mismatch"); 565 } 566 catch (TypeMismatchException ex) { 567 } 569 catch (Exception ex) { 570 fail("Shouldn't throw exception other than Type mismatch"); 571 } 572 } 573 574 public void testEmptyValueForPrimitiveProperty() { 575 TestBean t = new TestBean(); 576 try { 577 BeanWrapper bw = new BeanWrapperImpl(t); 578 bw.setPropertyValue("age", ""); 579 fail("Should throw exception on type mismatch"); 580 } 581 catch (TypeMismatchException ex) { 582 } 584 catch (Exception ex) { 585 fail("Shouldn't throw exception other than Type mismatch"); 586 } 587 } 588 589 public void testSetPropertyValuesIgnoresInvalidNestedOnRequest() { 590 ITestBean rod = new TestBean(); 591 MutablePropertyValues pvs = new MutablePropertyValues(); 592 pvs.addPropertyValue(new PropertyValue("name", "rod")); 593 pvs.addPropertyValue(new PropertyValue("graceful.rubbish", "tony")); 594 pvs.addPropertyValue(new PropertyValue("more.garbage", new Object ())); 595 BeanWrapper bw = new BeanWrapperImpl(rod); 596 bw.setPropertyValues(pvs, true); 597 assertTrue("Set valid and ignored invalid", rod.getName().equals("rod")); 598 try { 599 bw.setPropertyValues(pvs, false); 601 fail("Shouldn't have ignored invalid updates"); 602 } 603 catch (NotWritablePropertyException ex) { 604 } 606 } 607 608 public void testGetNestedProperty() { 609 ITestBean rod = new TestBean("rod", 31); 610 ITestBean kerry = new TestBean("kerry", 35); 611 rod.setSpouse(kerry); 612 kerry.setSpouse(rod); 613 BeanWrapper bw = new BeanWrapperImpl(rod); 614 Integer KA = (Integer ) bw.getPropertyValue("spouse.age"); 615 assertTrue("kerry is 35", KA.intValue() == 35); 616 Integer RA = (Integer ) bw.getPropertyValue("spouse.spouse.age"); 617 assertTrue("rod is 31, not" + RA, RA.intValue() == 31); 618 ITestBean spousesSpouse = (ITestBean) bw.getPropertyValue("spouse.spouse"); 619 assertTrue("spousesSpouse = initial point", rod == spousesSpouse); 620 } 621 622 public void testGetNestedPropertyNullValue() throws Exception { 623 ITestBean rod = new TestBean("rod", 31); 624 ITestBean kerry = new TestBean("kerry", 35); 625 rod.setSpouse(kerry); 626 627 BeanWrapper bw = new BeanWrapperImpl(rod); 628 try { 629 bw.getPropertyValue("spouse.spouse.age"); 630 fail("Shouldn't have succeded with null path"); 631 } 632 catch (NullValueInNestedPathException ex) { 633 |