| 1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditor ; 20 import java.beans.PropertyEditorSupport ; 21 import java.beans.PropertyVetoException ; 22 import java.io.File ; 23 import java.math.BigDecimal ; 24 import java.math.BigInteger ; 25 import java.text.NumberFormat ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.Locale ; 29 import java.util.StringTokenizer ; 30 31 import junit.framework.TestCase; 32 33 import org.springframework.beans.BeanWrapper; 34 import org.springframework.beans.BeanWrapperImpl; 35 import org.springframework.beans.BeansException; 36 import org.springframework.beans.BooleanTestBean; 37 import org.springframework.beans.ITestBean; 38 import org.springframework.beans.IndexedTestBean; 39 import org.springframework.beans.MutablePropertyValues; 40 import org.springframework.beans.NumberTestBean; 41 import org.springframework.beans.PropertyValue; 42 import org.springframework.beans.TestBean; 43 44 48 public class CustomEditorTests extends TestCase { 49 50 public void testComplexObject() { 51 TestBean t = new TestBean(); 52 String newName = "Rod"; 53 String tbString = "Kerry_34"; 54 55 BeanWrapper bw = new BeanWrapperImpl(t); 56 bw.registerCustomEditor(ITestBean.class, new TestBeanEditor()); 57 MutablePropertyValues pvs = new MutablePropertyValues(); 58 pvs.addPropertyValue(new PropertyValue("age", new Integer (55))); 59 pvs.addPropertyValue(new PropertyValue("name", newName)); 60 pvs.addPropertyValue(new PropertyValue("touchy", "valid")); 61 pvs.addPropertyValue(new PropertyValue("spouse", tbString)); 62 bw.setPropertyValues(pvs); 63 assertTrue("spouse is non-null", t.getSpouse() != null); 64 assertTrue("spouse name is Kerry and age is 34", 65 t.getSpouse().getName().equals("Kerry") && t.getSpouse().getAge() == 34); 66 } 67 68 public void testCustomEditorForSingleProperty() { 69 TestBean tb = new TestBean(); 70 BeanWrapper bw = new BeanWrapperImpl(tb); 71 bw.registerCustomEditor(String .class, "name", new PropertyEditorSupport () { 72 public void setAsText(String text) throws IllegalArgumentException { 73 setValue("prefix" + text); 74 } 75 }); 76 bw.setPropertyValue("name", "value"); 77 bw.setPropertyValue("touchy", "value"); 78 assertEquals("prefixvalue", bw.getPropertyValue("name")); 79 assertEquals("prefixvalue", tb.getName()); 80 assertEquals("value", bw.getPropertyValue("touchy")); 81 assertEquals("value", tb.getTouchy()); 82 } 83 84 public void testCustomEditorForAllStringProperties() { 85 TestBean tb = new TestBean(); 86 BeanWrapper bw = new BeanWrapperImpl(tb); 87 bw.registerCustomEditor(String .class, new PropertyEditorSupport () { 88 public void setAsText(String text) throws IllegalArgumentException { 89 setValue("prefix" + text); 90 } 91 }); 92 bw.setPropertyValue("name", "value"); 93 bw.setPropertyValue("touchy", "value"); 94 assertEquals("prefixvalue", bw.getPropertyValue("name")); 95 assertEquals("prefixvalue", tb.getName()); 96 assertEquals("prefixvalue", bw.getPropertyValue("touchy")); 97 assertEquals("prefixvalue", tb.getTouchy()); 98 } 99 100 public void testCustomEditorForSingleNestedProperty() { 101 TestBean tb = new TestBean(); 102 tb.setSpouse(new TestBean()); 103 BeanWrapper bw = new BeanWrapperImpl(tb); 104 bw.registerCustomEditor(String .class, "spouse.name", new PropertyEditorSupport () { 105 public void setAsText(String text) throws IllegalArgumentException { 106 setValue("prefix" + text); 107 } 108 }); 109 bw.setPropertyValue("spouse.name", "value"); 110 bw.setPropertyValue("touchy", "value"); 111 assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); 112 assertEquals("prefixvalue", tb.getSpouse().getName()); 113 assertEquals("value", bw.getPropertyValue("touchy")); 114 assertEquals("value", tb.getTouchy()); 115 } 116 117 public void testCustomEditorForAllNestedStringProperties() { 118 TestBean tb = new TestBean(); 119 tb.setSpouse(new TestBean()); 120 BeanWrapper bw = new BeanWrapperImpl(tb); 121 bw.registerCustomEditor(String .class, new PropertyEditorSupport () { 122 public void setAsText(String text) throws IllegalArgumentException { 123 setValue("prefix" + text); 124 } 125 }); 126 bw.setPropertyValue("spouse.name", "value"); 127 bw.setPropertyValue("touchy", "value"); 128 assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); 129 assertEquals("prefixvalue", tb.getSpouse().getName()); 130 assertEquals("prefixvalue", bw.getPropertyValue("touchy")); 131 assertEquals("prefixvalue", tb.getTouchy()); 132 } 133 134 public void testDefaultBooleanEditorForPrimitiveType() { 135 BooleanTestBean tb = new BooleanTestBean(); 136 BeanWrapper bw = new BeanWrapperImpl(tb); 137 138 bw.setPropertyValue("bool1", "true"); 139 assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1"))); 140 assertTrue("Correct bool1 value", tb.isBool1()); 141 142 bw.setPropertyValue("bool1", "false"); 143 assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1"))); 144 assertTrue("Correct bool1 value", !tb.isBool1()); 145 146 bw.setPropertyValue("bool1", "on"); 147 assertTrue("Correct bool1 value", tb.isBool1()); 148 149 bw.setPropertyValue("bool1", "off"); 150 assertTrue("Correct bool1 value", !tb.isBool1()); 151 152 bw.setPropertyValue("bool1", "yes"); 153 assertTrue("Correct bool1 value", tb.isBool1()); 154 155 bw.setPropertyValue("bool1", "no"); 156 assertTrue("Correct bool1 value", !tb.isBool1()); 157 158 bw.setPropertyValue("bool1", "1"); 159 assertTrue("Correct bool1 value", tb.isBool1()); 160 161 bw.setPropertyValue("bool1", "0"); 162 assertTrue("Correct bool1 value", !tb.isBool1()); 163 164 try { 165 bw.setPropertyValue("bool1", "argh"); 166 fail("Should have thrown BeansException"); 167 } 168 catch (BeansException ex) { 169 } 171 } 172 173 public void testDefaultBooleanEditorForWrapperType() { 174 BooleanTestBean tb = new BooleanTestBean(); 175 BeanWrapper bw = new BeanWrapperImpl(tb); 176 177 bw.setPropertyValue("bool2", "true"); 178 assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); 179 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 180 181 bw.setPropertyValue("bool2", "false"); 182 assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); 183 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 184 185 bw.setPropertyValue("bool2", "on"); 186 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 187 188 bw.setPropertyValue("bool2", "off"); 189 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 190 191 bw.setPropertyValue("bool2", "yes"); 192 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 193 194 bw.setPropertyValue("bool2", "no"); 195 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 196 197 bw.setPropertyValue("bool2", "1"); 198 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 199 200 bw.setPropertyValue("bool2", "0"); 201 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 202 203 try { 204 bw.setPropertyValue("bool2", ""); 205 fail("Should have throw BeansException"); 206 } 207 catch (BeansException ex) { 208 assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") != null); 210 assertTrue("Correct bool2 value", tb.getBool2() != null); 211 } 212 } 213 214 public void testCustomBooleanEditorWithAllowEmpty() { 215 BooleanTestBean tb = new BooleanTestBean(); 216 BeanWrapper bw = new BeanWrapperImpl(tb); 217 bw.registerCustomEditor(Boolean .class, new CustomBooleanEditor(true)); 218 219 bw.setPropertyValue("bool2", "true"); 220 assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); 221 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 222 223 bw.setPropertyValue("bool2", "false"); 224 assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); 225 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 226 227 bw.setPropertyValue("bool2", "on"); 228 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 229 230 bw.setPropertyValue("bool2", "off"); 231 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 232 233 bw.setPropertyValue("bool2", "yes"); 234 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 235 236 bw.setPropertyValue("bool2", "no"); 237 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 238 239 bw.setPropertyValue("bool2", "1"); 240 assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); 241 242 bw.setPropertyValue("bool2", "0"); 243 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); 244 245 bw.setPropertyValue("bool2", ""); 246 assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null); 247 assertTrue("Correct bool2 value", tb.getBool2() == null); 248 } 249 250 public void testDefaultNumberEditor() { 251 NumberTestBean tb = new NumberTestBean(); 252 BeanWrapper bw = new BeanWrapperImpl(tb); 253 254 bw.setPropertyValue("short1", "1"); 255 bw.setPropertyValue("short2", "2"); 256 bw.setPropertyValue("int1", "7"); 257 bw.setPropertyValue("int2", "8"); 258 bw.setPropertyValue("long1", "5"); 259 bw.setPropertyValue("long2", "6"); 260 bw.setPropertyValue("bigInteger", "3"); 261 bw.setPropertyValue("float1", "7.1"); 262 bw.setPropertyValue("float2", "8.1"); 263 bw.setPropertyValue("double1", "5.1"); 264 bw.setPropertyValue("double2", "6.1"); 265 bw.setPropertyValue("bigDecimal", "4.5"); 266 267 assertTrue("Correct short1 value", new Short ("1").equals(bw.getPropertyValue("short1"))); 268 assertTrue("Correct short1 value", tb.getShort1() == 1); 269 assertTrue("Correct short2 value", new Short ("2").equals(bw.getPropertyValue("short2"))); 270 assertTrue("Correct short2 value", new Short ("2").equals(tb.getShort2())); 271 assertTrue("Correct int1 value", new Integer ("7").equals(bw.getPropertyValue("int1"))); 272 assertTrue("Correct int1 value", tb.getInt1() == 7); 273 assertTrue("Correct int2 value", new Integer ("8").equals(bw.getPropertyValue("int2"))); 274 assertTrue("Correct int2 value", new Integer ("8").equals(tb.getInt2())); 275 assertTrue("Correct long1 value", new Long ("5").equals(bw.getPropertyValue("long1"))); 276 assertTrue("Correct long1 value", tb.getLong1() == 5); 277 assertTrue("Correct long2 value", new Long ("6").equals(bw.getPropertyValue("long2"))); 278 assertTrue("Correct long2 value", new Long ("6").equals(tb.getLong2())); 279 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(bw.getPropertyValue("bigInteger"))); 280 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(tb.getBigInteger())); 281 assertTrue("Correct float1 value", new Float ("7.1").equals(bw.getPropertyValue("float1"))); 282 assertTrue("Correct float1 value", new Float ("7.1").equals(new Float (tb.getFloat1()))); 283 assertTrue("Correct float2 value", new Float ("8.1").equals(bw.getPropertyValue("float2"))); 284 assertTrue("Correct float2 value", new Float ("8.1").equals(tb.getFloat2())); 285 assertTrue("Correct double1 value", new Double ("5.1").equals(bw.getPropertyValue("double1"))); 286 assertTrue("Correct double1 value", tb.getDouble1() == 5.1); 287 assertTrue("Correct double2 value", new Double ("6.1").equals(bw.getPropertyValue("double2"))); 288 assertTrue("Correct double2 value", new Double ("6.1").equals(tb.getDouble2())); 289 assertTrue("Correct bigDecimal value", new BigDecimal ("4.5").equals(bw.getPropertyValue("bigDecimal"))); 290 assertTrue("Correct bigDecimal value", new BigDecimal ("4.5").equals(tb.getBigDecimal())); 291 } 292 293 public void testCustomNumberEditorWithoutAllowEmpty() { 294 NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); 295 NumberTestBean tb = new NumberTestBean(); 296 BeanWrapper bw = new BeanWrapperImpl(tb); 297 bw.registerCustomEditor(short.class, new CustomNumberEditor(Short .class, nf, false)); 298 bw.registerCustomEditor(Short .class, new CustomNumberEditor(Short .class, nf, false)); 299 bw.registerCustomEditor(int.class, new CustomNumberEditor(Short .class, nf, false)); 300 bw.registerCustomEditor(Integer .class, new CustomNumberEditor(Integer .class, nf, false)); 301 bw.registerCustomEditor(long.class, new CustomNumberEditor(Long .class, nf, false)); 302 bw.registerCustomEditor(Long .class, new CustomNumberEditor(Long .class, nf, false)); 303 bw.registerCustomEditor(BigInteger .class, new CustomNumberEditor(BigInteger .class, nf, false)); 304 bw.registerCustomEditor(float.class, new CustomNumberEditor(Float .class, nf, false)); 305 bw.registerCustomEditor(Float .class, new CustomNumberEditor(Float .class, nf, false)); 306 bw.registerCustomEditor(double.class, new CustomNumberEditor(Double .class, nf, false)); 307 bw.registerCustomEditor(Double .class, new CustomNumberEditor(Double .class, nf, false)); 308 bw.registerCustomEditor(BigDecimal .class, new CustomNumberEditor(BigDecimal .class, nf, false)); 309 310 bw.setPropertyValue("short1", "1"); 311 bw.setPropertyValue("short2", "2"); 312 bw.setPropertyValue("int1", "7"); 313 bw.setPropertyValue("int2", "8"); 314 bw.setPropertyValue("long1", "5"); 315 bw.setPropertyValue("long2", "6"); 316 bw.setPropertyValue("bigInteger", "3"); 317 bw.setPropertyValue("float1", "7,1"); 318 bw.setPropertyValue("float2", "8,1"); 319 bw.setPropertyValue("double1", "5,1"); 320 bw.setPropertyValue("double2", "6,1"); 321 bw.setPropertyValue("bigDecimal", "4,5"); 322 323 assertTrue("Correct short1 value", new Short ("1").equals(bw.getPropertyValue("short1"))); 324 assertTrue("Correct short1 value", tb.getShort1() == 1); 325 assertTrue("Correct short2 value", new Short ("2").equals(bw.getPropertyValue("short2"))); 326 assertTrue("Correct short2 value", new Short ("2").equals(tb.getShort2())); 327 assertTrue("Correct int1 value", new Integer ("7").equals(bw.getPropertyValue("int1"))); 328 assertTrue("Correct int1 value", tb.getInt1() == 7); 329 assertTrue("Correct int2 value", new Integer ("8").equals(bw.getPropertyValue("int2"))); 330 assertTrue("Correct int2 value", new Integer ("8").equals(tb.getInt2())); 331 assertTrue("Correct long1 value", new Long ("5").equals(bw.getPropertyValue("long1"))); 332 assertTrue("Correct long1 value", tb.getLong1() == 5); 333 assertTrue("Correct long2 value", new Long ("6").equals(bw.getPropertyValue("long2"))); 334 assertTrue("Correct long2 value", new Long ("6").equals(tb.getLong2())); 335 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(bw.getPropertyValue("bigInteger"))); 336 assertTrue("Correct bigInteger value", new BigInteger ("3").equals(tb.getBigInteger())); 337 assertTrue("Correct float1 value", new Float ("7.1").equals(bw.getPropertyValue("float1"))); 338 assertTrue("Correct float1 value", new Float ("7.1").equals(new Float (tb.getFloat1()))); 339 assertTrue("Correct float2 value", new Float ("8.1").equals(bw.getPropertyValue("float2"))); 340 assertTrue("Correct float2 value", new Float ("8.1").equals(tb.getFloat2())); 341 assertTrue("Correct double1 value", new Double ("5.1").equals(bw.getPropertyValue("double1"))); 342 assertTrue("Correct double1 value", tb.getDouble1() == 5.1); 343 assertTrue("Correct double2 value", new Double ("6.1").equals(bw.getPropertyValue("double2"))); 344 assertTrue("Correct double2 value", new Double ("6.1").equals(tb.getDouble2())); 345 assertTrue("Correct bigDecimal value", new BigDecimal ("4.5").equals(bw.getPropertyValue("bigDecimal"))); 346 assertTrue("Correct bigDecimal value", new BigDecimal ("4.5").equals(tb.getBigDecimal())); 347 } 348 349 public void testCustomNumberEditorWithAllowEmpty() { 350 NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); 351 NumberTestBean tb = new NumberTestBean(); 352 BeanWrapper bw = new BeanWrapperImpl(tb); 353 bw.registerCustomEditor(long.class, new CustomNumberEditor(Long .class, nf, true)); 354 bw.registerCustomEditor(Long .class, new CustomNumberEditor(Long .class, nf, true)); 355 356 bw.setPropertyValue("long1", "5"); 357 bw.setPropertyValue("long2", "6"); 358 assertTrue("Correct long1 value", new Long ("5").equals(bw.getPropertyValue("long1"))); 359 assertTrue("Correct long1 value", tb.getLong1() == 5); 360 assertTrue("Correct long2 value", new Long ("6").equals(bw.getPropertyValue("long2"))); 361 assertTrue("Correct long2 value", new Long ("6").equals(tb.getLong2())); 362 363 bw.setPropertyValue("long2", ""); 364 assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null); 365 assertTrue("Correct long2 value", tb.getLong2() == null); 366 367 try { 368 bw.setPropertyValue("long1", ""); 369 fail("Should have thrown BeansException"); 370 } 371 catch (BeansException ex) { 372 assertTrue("Correct long1 value", new Long ("5").equals(bw.getPropertyValue("long1"))); 374 assertTrue("Correct long1 value", tb.getLong1() == 5); 375 } 376 } 377 378 public void testByteArrayPropertyEditor() { 379 ByteArrayBean bean = new ByteArrayBean(); 380 BeanWrapper bw = new BeanWrapperImpl(bean); 381 bw.setPropertyValue("array", "myvalue"); 382 assertEquals("myvalue", new String (bean.getArray())); 383 } 384 385 public void testCharacterEditor() { 386 CharBean cb = new CharBean(); 387 BeanWrapper bw = new BeanWrapperImpl(cb); 388 389 bw.setPropertyValue("myChar", new Character ('c')); 390 assertEquals('c', cb.getMyChar()); 391 392 bw.setPropertyValue("myChar", "c"); 393 assertEquals('c', cb.getMyChar()); 394 } 395 396 public void testClassEditor() { 397 PropertyEditor classEditor = new ClassEditor(); 398 classEditor.setAsText("org.springframework.beans.TestBean"); 399 assertEquals(TestBean.class, classEditor.getValue()); 400 assertEquals("org.springframework.beans.TestBean", classEditor.getAsText()); 401 } 402 403 public void testClassEditorWithArray() { 404 PropertyEditor classEditor = new ClassEditor(); 405 classEditor.setAsText("org.springframework.beans.TestBean[]"); 406 assertEquals(TestBean[].class, classEditor.getValue()); 407 assertEquals("org.springframework.beans.TestBean[]", classEditor.getAsText()); 408 } 409 410 public void testFileEditor() { 411 PropertyEditor fileEditor = new FileEditor(); 412 fileEditor.setAsText("C:/test/myfile.txt"); 413 assertEquals(new File ("C:/test/myfile.txt"), fileEditor.getValue()); 414 assertEquals((new File ("C:/test/myfile.txt")).getAbsolutePath(), fileEditor.getAsText()); 415 } 416 417 public void testLocaleEditor() { 418 PropertyEditor localeEditor = new LocaleEditor(); 419 localeEditor.setAsText("en_CA"); 420 assertEquals(Locale.CANADA, localeEditor.getValue()); 421 assertEquals("en_CA", localeEditor.getAsText()); 422 } 423 424 public void testCustomBooleanEditor() { 425 CustomBooleanEditor editor = new CustomBooleanEditor(false); 426 editor.setAsText("true"); 427 assertEquals(Boolean.TRUE, editor.getValue()); 428 assertEquals("true", editor.getAsText()); 429 editor.setAsText("false"); 430 assertEquals(Boolean.FALSE, editor.getValue()); 431 assertEquals("false", editor.getAsText()); 432 editor.setValue(null); 433 assertEquals(null, editor.getValue()); 434 assertEquals("", editor.getAsText()); 435 } 436 437 public void testCustomBooleanEditorWithEmptyAsNull() { 438 CustomBooleanEditor editor = new CustomBooleanEditor(true); 439 editor.setAsText("true"); 440 assertEquals(Boolean.TRUE, editor.getValue()); 441 assertEquals("true", editor.getAsText()); 442 editor.setAsText("false"); 443 assertEquals(Boolean.FALSE, editor.getValue()); 444 assertEquals("false", editor.getAsText()); 445 editor.setValue(null); 446 assertEquals(null, editor.getValue()); 447 assertEquals("", editor.getAsText()); 448 } 449 450 public void testCustomDateEditor() { 451 CustomDateEditor editor = new CustomDateEditor(null, false); 452 editor.setValue(null); 453 assertEquals(null, editor.getValue()); 454 assertEquals("", editor.getAsText()); 455 } 456 457 public void testCustomDateEditorWithEmptyAsNull() { 458 CustomDateEditor editor = new CustomDateEditor(null, true); 459 editor.setValue(null); 460 assertEquals(null, editor.getValue()); 461 assertEquals("", editor.getAsText()); 462 } 463 464 public void testCustomNumberEditor() { 465 CustomNumberEditor editor = new CustomNumberEditor(Integer .class, false); 466 editor.setAsText("5"); 467 assertEquals(new Integer (5), editor.getValue()); 468 assertEquals("5", editor.getAsText()); 469 editor.setValue(null); 470 assertEquals(null, editor.getValue()); 471 assertEquals("", editor.getAsText()); 472 } 473 474 public void testCustomNumberEditorWithEmptyAsNull() { 475 CustomNumberEditor editor = new CustomNumberEditor(Integer .class, true); 476 editor.setAsText("5"); 477 assertEquals(new Integer (5), editor.getValue()); 478 assertEquals("5", editor.getAsText()); 479 editor.setAsText(""); 480 assertEquals(null, editor.getValue()); 481 assertEquals("", editor.getAsText()); 482 editor.setValue(null); 483 assertEquals(null, editor.getValue()); 484 assertEquals("", editor.getAsText()); 485 } 486 487 public void testStringTrimmerEditor() { 488 StringTrimmerEditor editor = new StringTrimmerEditor(false); 489 editor.setAsText("test"); 490 assertEquals("test", editor.getValue()); 491 assertEquals("test", editor.getAsText()); 492 editor.setAsText(" test "); 493 assertEquals("test", editor.getValue()); 494 assertEquals("test", editor.getAsText()); 495 editor.setAsText(""); 496 assertEquals("", editor.getValue()); 497 assertEquals("", editor.getAsText()); 498 editor.setValue(null); 499 assertEquals("", editor.getAsText()); 500 } 501 502 public void testStringTrimmerEditorWithEmptyAsNull() { 503 StringTrimmerEditor editor = new StringTrimmerEditor(true); 504 editor.setAsText("test"); 505 assertEquals("test", editor.getValue()); 506 assertEquals("test", editor.getAsText()); 507 editor.setAsText(" test "); 508 assertEquals("test", editor.getValue()); 509 assertEquals("test", editor.getAsText()); 510 editor.setAsText(" "); 511 assertEquals(null, editor.getValue()); 512 assertEquals("", editor.getAsText()); 513 editor.setValue(null); 514 assertEquals("", editor.getAsText()); 515 } 516 517 public void testStringTrimmerEditorWithCharsToDelete() { 518 StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false); 519 editor.setAsText("te\ns\ft"); 520 assertEquals("test", editor.getValue()); 521 assertEquals("test", editor.getAsText()); 522 editor.setAsText(" test "); 523 assertEquals("test", editor.getValue()); 524 assertEquals("test", editor.getAsText()); 525 editor.setAsText(""); 526 assertEquals("", editor.getValue()); 527 assertEquals("", editor.getAsText()); 528 editor.setValue(null); 529 assertEquals("", editor.getAsText()); 530 } 531 532 public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() { 533 StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true); 534 editor.setAsText("te\ns\ft"); 535 assertEquals("test", editor.getValue()); 536 assertEquals("test", editor.getAsText()); 537 editor.setAsText(" test "); 538 assertEquals("test", editor.getValue()); 539 assertEquals("test", editor.getAsText()); 540 editor.setAsText(" \n\f "); 541 assertEquals(null, editor.getValue()); 542 assertEquals("", editor.getAsText()); 543 editor.setValue(null); 544 assertEquals("", editor.getAsText()); 545 } 546 547 public void testIndexedPropertiesWithCustomEditorForType() { 548 IndexedTestBean bean = new IndexedTestBean(); 549 BeanWrapper bw = new BeanWrapperImpl(bean); 550 bw.registerCustomEditor(String .class, new PropertyEditorSupport () { 551 public void setAsText(String text) throws IllegalArgumentException { 552 setValue("prefix" + text); 553 } 554 }); 555 TestBean tb0 = bean.getArray()[0]; 556 TestBean tb1 = bean.getArray()[1]; 557 TestBean tb2 = ((TestBean) bean.getList().get(0)); 558 TestBean tb3 = ((TestBean) bean.getList().get(1)); 559 TestBean tb4 = ((TestBean) bean.getMap().get("key1")); 560 TestBean tb5 = ((TestBean) bean.getMap().get("key2")); 561 assertEquals("name0", tb0.getName()); 562 assertEquals("name1", tb1.getName()); 563 assertEquals("name2", tb2.getName()); 564 assertEquals("name3", tb3.getName()); 565 assertEquals("name4", tb4.getName()); 566 assertEquals("name5", tb5.getName()); 567 assertEquals("name0", bw.getPropertyValue("array[0].name")); 568 assertEquals("name1", bw.getPropertyValue("array[1].name")); 569 assertEquals("name2", bw.getPropertyValue("list[0].name")); 570 assertEquals("name3", bw.getPropertyValue("list[1].name")); 571 assertEquals("name4", bw.getPropertyValue("map[key1].name")); 572 assertEquals("name5", bw.getPropertyValue("map[key2].name")); 573 assertEquals("name4", bw.getPropertyValue("map['key1'].name")); 574 assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name")); 575 576 MutablePropertyValues pvs = new MutablePropertyValues(); 577 pvs.addPropertyValue("array[0].name", "name5"); 578 pvs.addPropertyValue("array[1].name", "name4"); 579 pvs.addPropertyValue("list[0].name", "name3"); 580 pvs.addPropertyValue("list[1].name", "name2"); 581 pvs.addPropertyValue("map[key1].name", "name1"); 582 pvs.addPropertyValue("map['key2'].name", "name0"); 583 bw.setPropertyValues(pvs); 584 assertEquals("prefixname5", tb0.getName()); 585 assertEquals("prefixname4", tb1.getName()); 586 assertEquals("prefixname3", tb2.getName()); 587 assertEquals("prefixname2", tb3.getName()); 588 assertEquals("prefixname1", tb4.getName()); 589 assertEquals("prefixname0", tb5.getName()); 590 assertEquals("prefixname5", bw.getPropertyValue("array[0].name")); 591 assertEquals("prefixname4", bw.getPropertyValue("array[1].name")); 592 assertEquals("prefixname3", bw.getPropertyValue("list[0].name")); 593 assertEquals("prefixname2", bw.getPropertyValue("list[1].name")); 594 assertEquals("prefixname1", bw.getPropertyValue("map[\"key1\"].name")); 595 assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name")); 596 } 597 598 public void testIndexedPropertiesWithCustomEditorForProperty() { 599 IndexedTestBean bean = new IndexedTestBean(false); 600 BeanWrapper bw = new BeanWrapperImpl(bean); 601 &n
|