1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.nio.charset.Charset;
26 import java.text.NumberFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.StringTokenizer;
36 import java.util.Vector;
37 import java.util.regex.Pattern;
38
39 import org.junit.Test;
40
41 import org.springframework.beans.BeanWrapper;
42 import org.springframework.beans.BeanWrapperImpl;
43 import org.springframework.beans.BeansException;
44 import org.springframework.beans.MutablePropertyValues;
45 import org.springframework.beans.PropertyValue;
46 import org.springframework.tests.sample.beans.BooleanTestBean;
47 import org.springframework.tests.sample.beans.ITestBean;
48 import org.springframework.tests.sample.beans.IndexedTestBean;
49 import org.springframework.tests.sample.beans.NumberTestBean;
50 import org.springframework.tests.sample.beans.TestBean;
51
52 import static org.junit.Assert.*;
53
54
55
56
57
58
59
60
61
62
63
64
65 public class CustomEditorTests {
66
67 @Test
68 public void testComplexObject() {
69 TestBean tb = new TestBean();
70 String newName = "Rod";
71 String tbString = "Kerry_34";
72
73 BeanWrapper bw = new BeanWrapperImpl(tb);
74 bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
75 MutablePropertyValues pvs = new MutablePropertyValues();
76 pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
77 pvs.addPropertyValue(new PropertyValue("name", newName));
78 pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
79 pvs.addPropertyValue(new PropertyValue("spouse", tbString));
80 bw.setPropertyValues(pvs);
81 assertTrue("spouse is non-null", tb.getSpouse() != null);
82 assertTrue("spouse name is Kerry and age is 34",
83 tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
84 }
85
86 @Test
87 public void testComplexObjectWithOldValueAccess() {
88 TestBean tb = new TestBean();
89 String newName = "Rod";
90 String tbString = "Kerry_34";
91
92 BeanWrapper bw = new BeanWrapperImpl(tb);
93 bw.setExtractOldValueForEditor(true);
94 bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
95 MutablePropertyValues pvs = new MutablePropertyValues();
96 pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
97 pvs.addPropertyValue(new PropertyValue("name", newName));
98 pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
99 pvs.addPropertyValue(new PropertyValue("spouse", tbString));
100
101 bw.setPropertyValues(pvs);
102 assertTrue("spouse is non-null", tb.getSpouse() != null);
103 assertTrue("spouse name is Kerry and age is 34",
104 tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
105 ITestBean spouse = tb.getSpouse();
106
107 bw.setPropertyValues(pvs);
108 assertSame("Should have remained same object", spouse, tb.getSpouse());
109 }
110
111 @Test
112 public void testCustomEditorForSingleProperty() {
113 TestBean tb = new TestBean();
114 BeanWrapper bw = new BeanWrapperImpl(tb);
115 bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
116 @Override
117 public void setAsText(String text) throws IllegalArgumentException {
118 setValue("prefix" + text);
119 }
120 });
121 bw.setPropertyValue("name", "value");
122 bw.setPropertyValue("touchy", "value");
123 assertEquals("prefixvalue", bw.getPropertyValue("name"));
124 assertEquals("prefixvalue", tb.getName());
125 assertEquals("value", bw.getPropertyValue("touchy"));
126 assertEquals("value", tb.getTouchy());
127 }
128
129 @Test
130 public void testCustomEditorForAllStringProperties() {
131 TestBean tb = new TestBean();
132 BeanWrapper bw = new BeanWrapperImpl(tb);
133 bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
134 @Override
135 public void setAsText(String text) throws IllegalArgumentException {
136 setValue("prefix" + text);
137 }
138 });
139 bw.setPropertyValue("name", "value");
140 bw.setPropertyValue("touchy", "value");
141 assertEquals("prefixvalue", bw.getPropertyValue("name"));
142 assertEquals("prefixvalue", tb.getName());
143 assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
144 assertEquals("prefixvalue", tb.getTouchy());
145 }
146
147 @Test
148 public void testCustomEditorForSingleNestedProperty() {
149 TestBean tb = new TestBean();
150 tb.setSpouse(new TestBean());
151 BeanWrapper bw = new BeanWrapperImpl(tb);
152 bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
153 @Override
154 public void setAsText(String text) throws IllegalArgumentException {
155 setValue("prefix" + text);
156 }
157 });
158 bw.setPropertyValue("spouse.name", "value");
159 bw.setPropertyValue("touchy", "value");
160 assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
161 assertEquals("prefixvalue", tb.getSpouse().getName());
162 assertEquals("value", bw.getPropertyValue("touchy"));
163 assertEquals("value", tb.getTouchy());
164 }
165
166 @Test
167 public void testCustomEditorForAllNestedStringProperties() {
168 TestBean tb = new TestBean();
169 tb.setSpouse(new TestBean());
170 BeanWrapper bw = new BeanWrapperImpl(tb);
171 bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
172 @Override
173 public void setAsText(String text) throws IllegalArgumentException {
174 setValue("prefix" + text);
175 }
176 });
177 bw.setPropertyValue("spouse.name", "value");
178 bw.setPropertyValue("touchy", "value");
179 assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
180 assertEquals("prefixvalue", tb.getSpouse().getName());
181 assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
182 assertEquals("prefixvalue", tb.getTouchy());
183 }
184
185 @Test
186 public void testDefaultBooleanEditorForPrimitiveType() {
187 BooleanTestBean tb = new BooleanTestBean();
188 BeanWrapper bw = new BeanWrapperImpl(tb);
189
190 bw.setPropertyValue("bool1", "true");
191 assertTrue("Correct bool1 value", Boolean.TRUE.equals(bw.getPropertyValue("bool1")));
192 assertTrue("Correct bool1 value", tb.isBool1());
193
194 bw.setPropertyValue("bool1", "false");
195 assertTrue("Correct bool1 value", Boolean.FALSE.equals(bw.getPropertyValue("bool1")));
196 assertTrue("Correct bool1 value", !tb.isBool1());
197
198 bw.setPropertyValue("bool1", " true ");
199 assertTrue("Correct bool1 value", tb.isBool1());
200
201 bw.setPropertyValue("bool1", " false ");
202 assertTrue("Correct bool1 value", !tb.isBool1());
203
204 bw.setPropertyValue("bool1", "on");
205 assertTrue("Correct bool1 value", tb.isBool1());
206
207 bw.setPropertyValue("bool1", "off");
208 assertTrue("Correct bool1 value", !tb.isBool1());
209
210 bw.setPropertyValue("bool1", "yes");
211 assertTrue("Correct bool1 value", tb.isBool1());
212
213 bw.setPropertyValue("bool1", "no");
214 assertTrue("Correct bool1 value", !tb.isBool1());
215
216 bw.setPropertyValue("bool1", "1");
217 assertTrue("Correct bool1 value", tb.isBool1());
218
219 bw.setPropertyValue("bool1", "0");
220 assertTrue("Correct bool1 value", !tb.isBool1());
221
222 try {
223 bw.setPropertyValue("bool1", "argh");
224 fail("Should have thrown BeansException");
225 }
226 catch (BeansException ex) {
227
228 }
229 }
230
231 @Test
232 public void testDefaultBooleanEditorForWrapperType() {
233 BooleanTestBean tb = new BooleanTestBean();
234 BeanWrapper bw = new BeanWrapperImpl(tb);
235
236 bw.setPropertyValue("bool2", "true");
237 assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
238 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
239
240 bw.setPropertyValue("bool2", "false");
241 assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
242 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
243
244 bw.setPropertyValue("bool2", "on");
245 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
246
247 bw.setPropertyValue("bool2", "off");
248 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
249
250 bw.setPropertyValue("bool2", "yes");
251 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
252
253 bw.setPropertyValue("bool2", "no");
254 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
255
256 bw.setPropertyValue("bool2", "1");
257 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
258
259 bw.setPropertyValue("bool2", "0");
260 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
261
262 bw.setPropertyValue("bool2", "");
263 assertNull("Correct bool2 value", tb.getBool2());
264 }
265
266 @Test
267 public void testCustomBooleanEditorWithAllowEmpty() {
268 BooleanTestBean tb = new BooleanTestBean();
269 BeanWrapper bw = new BeanWrapperImpl(tb);
270 bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
271
272 bw.setPropertyValue("bool2", "true");
273 assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
274 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
275
276 bw.setPropertyValue("bool2", "false");
277 assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
278 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
279
280 bw.setPropertyValue("bool2", "on");
281 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
282
283 bw.setPropertyValue("bool2", "off");
284 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
285
286 bw.setPropertyValue("bool2", "yes");
287 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
288
289 bw.setPropertyValue("bool2", "no");
290 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
291
292 bw.setPropertyValue("bool2", "1");
293 assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
294
295 bw.setPropertyValue("bool2", "0");
296 assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
297
298 bw.setPropertyValue("bool2", "");
299 assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null);
300 assertTrue("Correct bool2 value", tb.getBool2() == null);
301 }
302
303 @Test
304 public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Exception {
305 final String trueString = "pechorin";
306 final String falseString = "nash";
307
308 CustomBooleanEditor editor = new CustomBooleanEditor(trueString, falseString, false);
309
310 editor.setAsText(trueString);
311 assertTrue(((Boolean) editor.getValue()).booleanValue());
312 assertEquals(trueString, editor.getAsText());
313 editor.setAsText(falseString);
314 assertFalse(((Boolean) editor.getValue()).booleanValue());
315 assertEquals(falseString, editor.getAsText());
316
317 editor.setAsText(trueString.toUpperCase());
318 assertTrue(((Boolean) editor.getValue()).booleanValue());
319 assertEquals(trueString, editor.getAsText());
320 editor.setAsText(falseString.toUpperCase());
321 assertFalse(((Boolean) editor.getValue()).booleanValue());
322 assertEquals(falseString, editor.getAsText());
323 }
324
325 @Test
326 public void testDefaultNumberEditor() {
327 NumberTestBean tb = new NumberTestBean();
328 BeanWrapper bw = new BeanWrapperImpl(tb);
329
330 bw.setPropertyValue("short1", "1");
331 bw.setPropertyValue("short2", "2");
332 bw.setPropertyValue("int1", "7");
333 bw.setPropertyValue("int2", "8");
334 bw.setPropertyValue("long1", "5");
335 bw.setPropertyValue("long2", "6");
336 bw.setPropertyValue("bigInteger", "3");
337 bw.setPropertyValue("float1", "7.1");
338 bw.setPropertyValue("float2", "8.1");
339 bw.setPropertyValue("double1", "5.1");
340 bw.setPropertyValue("double2", "6.1");
341 bw.setPropertyValue("bigDecimal", "4.5");
342
343 assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1")));
344 assertTrue("Correct short1 value", tb.getShort1() == 1);
345 assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
346 assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
347 assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1")));
348 assertTrue("Correct int1 value", tb.getInt1() == 7);
349 assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
350 assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
351 assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
352 assertTrue("Correct long1 value", tb.getLong1() == 5);
353 assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
354 assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
355 assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
356 assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
357 assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1")));
358 assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1())));
359 assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
360 assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
361 assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1")));
362 assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
363 assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
364 assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
365 assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal")));
366 assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
367 }
368
369 @Test
370 public void testCustomNumberEditorWithoutAllowEmpty() {
371 NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
372 NumberTestBean tb = new NumberTestBean();
373 BeanWrapper bw = new BeanWrapperImpl(tb);
374 bw.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, nf, false));
375 bw.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, nf, false));
376 bw.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, nf, false));
377 bw.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, nf, false));
378 bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, false));
379 bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, false));
380 bw.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, nf, false));
381 bw.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, nf, false));
382 bw.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false));
383 bw.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, nf, false));
384 bw.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, nf, false));
385 bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, false));
386
387 bw.setPropertyValue("short1", "1");
388 bw.setPropertyValue("short2", "2");
389 bw.setPropertyValue("int1", "7");
390 bw.setPropertyValue("int2", "8");
391 bw.setPropertyValue("long1", "5");
392 bw.setPropertyValue("long2", "6");
393 bw.setPropertyValue("bigInteger", "3");
394 bw.setPropertyValue("float1", "7,1");
395 bw.setPropertyValue("float2", "8,1");
396 bw.setPropertyValue("double1", "5,1");
397 bw.setPropertyValue("double2", "6,1");
398 bw.setPropertyValue("bigDecimal", "4,5");
399
400 assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1")));
401 assertTrue("Correct short1 value", tb.getShort1() == 1);
402 assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
403 assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
404 assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1")));
405 assertTrue("Correct int1 value", tb.getInt1() == 7);
406 assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
407 assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
408 assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
409 assertTrue("Correct long1 value", tb.getLong1() == 5);
410 assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
411 assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
412 assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
413 assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
414 assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1")));
415 assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1())));
416 assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
417 assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
418 assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1")));
419 assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
420 assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
421 assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
422 assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal")));
423 assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
424 }
425
426 @Test(expected=IllegalArgumentException.class)
427 public void testCustomNumberEditorCtorWithNullNumberType() throws Exception {
428 new CustomNumberEditor(null, true);
429 }
430
431 @Test
432 public void testCustomNumberEditorWithAllowEmpty() {
433 NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
434 NumberTestBean tb = new NumberTestBean();
435 BeanWrapper bw = new BeanWrapperImpl(tb);
436 bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
437 bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
438
439 bw.setPropertyValue("long1", "5");
440 bw.setPropertyValue("long2", "6");
441 assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
442 assertTrue("Correct long1 value", tb.getLong1() == 5);
443 assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
444 assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
445
446 bw.setPropertyValue("long2", "");
447 assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
448 assertTrue("Correct long2 value", tb.getLong2() == null);
449
450 try {
451 bw.setPropertyValue("long1", "");
452 fail("Should have thrown BeansException");
453 }
454 catch (BeansException ex) {
455
456 assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
457 assertTrue("Correct long1 value", tb.getLong1() == 5);
458 }
459 }
460
461 @Test
462 public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
463 NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
464 NumberTestBean tb = new NumberTestBean();
465 BeanWrapper bw = new BeanWrapperImpl(tb);
466 bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
467 bw.setPropertyValue("bigDecimal", "1000");
468 assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f);
469 bw.setPropertyValue("bigDecimal", "1000,5");
470 assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
471 bw.setPropertyValue("bigDecimal", "1 000,5");
472 assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
473 }
474
475 @Test
476 public void testParseShortGreaterThanMaxValueWithoutNumberFormat() {
477 try {
478 CustomNumberEditor editor = new CustomNumberEditor(Short.class, true);
479 editor.setAsText(String.valueOf(Short.MAX_VALUE + 1));
480 fail(Short.MAX_VALUE + 1 + " is greater than max value");
481 } catch (NumberFormatException ex) {
482
483 }
484 }
485
486 @Test
487 public void testByteArrayPropertyEditor() {
488 PrimitiveArrayBean bean = new PrimitiveArrayBean();
489 BeanWrapper bw = new BeanWrapperImpl(bean);
490 bw.setPropertyValue("byteArray", "myvalue");
491 assertEquals("myvalue", new String(bean.getByteArray()));
492 }
493
494 @Test
495 public void testCharArrayPropertyEditor() {
496 PrimitiveArrayBean bean = new PrimitiveArrayBean();
497 BeanWrapper bw = new BeanWrapperImpl(bean);
498 bw.setPropertyValue("charArray", "myvalue");
499 assertEquals("myvalue", new String(bean.getCharArray()));
500 }
501
502 @Test
503 public void testCharacterEditor() {
504 CharBean cb = new CharBean();
505 BeanWrapper bw = new BeanWrapperImpl(cb);
506
507 bw.setPropertyValue("myChar", new Character('c'));
508 assertEquals('c', cb.getMyChar());
509
510 bw.setPropertyValue("myChar", "c");
511 assertEquals('c', cb.getMyChar());
512
513 bw.setPropertyValue("myChar", "\u0041");
514 assertEquals('A', cb.getMyChar());
515
516 bw.setPropertyValue("myChar", "\\u0022");
517 assertEquals('"', cb.getMyChar());
518
519 CharacterEditor editor = new CharacterEditor(false);
520 editor.setAsText("M");
521 assertEquals("M", editor.getAsText());
522 }
523
524 @Test
525 public void testCharacterEditorWithAllowEmpty() {
526 CharBean cb = new CharBean();
527 BeanWrapper bw = new BeanWrapperImpl(cb);
528 bw.registerCustomEditor(Character.class, new CharacterEditor(true));
529
530 bw.setPropertyValue("myCharacter", new Character('c'));
531 assertEquals(new Character('c'), cb.getMyCharacter());
532
533 bw.setPropertyValue("myCharacter", "c");
534 assertEquals(new Character('c'), cb.getMyCharacter());
535
536 bw.setPropertyValue("myCharacter", "\u0041");
537 assertEquals(new Character('A'), cb.getMyCharacter());
538
539 bw.setPropertyValue("myCharacter", " ");
540 assertEquals(new Character(' '), cb.getMyCharacter());
541
542 bw.setPropertyValue("myCharacter", "");
543 assertNull(cb.getMyCharacter());
544 }
545
546 @Test(expected=IllegalArgumentException.class)
547 public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception {
548 PropertyEditor charEditor = new CharacterEditor(false);
549 charEditor.setAsText("ColdWaterCanyon");
550 }
551
552 @Test
553 public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception {
554 PropertyEditor charEditor = new CharacterEditor(false);
555 assertEquals("", charEditor.getAsText());
556 charEditor = new CharacterEditor(true);
557 charEditor.setAsText(null);
558 assertEquals("", charEditor.getAsText());
559 charEditor.setAsText("");
560 assertEquals("", charEditor.getAsText());
561 charEditor.setAsText(" ");
562 assertEquals(" ", charEditor.getAsText());
563 }
564
565 @Test(expected=IllegalArgumentException.class)
566 public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception {
567 PropertyEditor charEditor = new CharacterEditor(false);
568 charEditor.setAsText(null);
569 }
570
571 @Test
572 public void testClassEditor() {
573 PropertyEditor classEditor = new ClassEditor();
574 classEditor.setAsText(TestBean.class.getName());
575 assertEquals(TestBean.class, classEditor.getValue());
576 assertEquals(TestBean.class.getName(), classEditor.getAsText());
577
578 classEditor.setAsText(null);
579 assertEquals("", classEditor.getAsText());
580 classEditor.setAsText("");
581 assertEquals("", classEditor.getAsText());
582 classEditor.setAsText("\t ");
583 assertEquals("", classEditor.getAsText());
584 }
585
586 @Test(expected=IllegalArgumentException.class)
587 public void testClassEditorWithNonExistentClass() throws Exception {
588 PropertyEditor classEditor = new ClassEditor();
589 classEditor.setAsText("hairdresser.on.Fire");
590 }
591
592 @Test
593 public void testClassEditorWithArray() {
594 PropertyEditor classEditor = new ClassEditor();
595 classEditor.setAsText("org.springframework.tests.sample.beans.TestBean[]");
596 assertEquals(TestBean[].class, classEditor.getValue());
597 assertEquals("org.springframework.tests.sample.beans.TestBean[]", classEditor.getAsText());
598 }
599
600
601
602
603 @Test
604 public void testGetAsTextWithTwoDimensionalArray() throws Exception {
605 String[][] chessboard = new String[8][8];
606 ClassEditor editor = new ClassEditor();
607 editor.setValue(chessboard.getClass());
608 assertEquals("java.lang.String[][]", editor.getAsText());
609 }
610
611
612
613
614 @Test
615 public void testGetAsTextWithRidiculousMultiDimensionalArray() throws Exception {
616 String[][][][][] ridiculousChessboard = new String[8][4][0][1][3];
617 ClassEditor editor = new ClassEditor();
618 editor.setValue(ridiculousChessboard.getClass());
619 assertEquals("java.lang.String[][][][][]", editor.getAsText());
620 }
621
622 @Test
623 public void testFileEditor() {
624 PropertyEditor fileEditor = new FileEditor();
625 fileEditor.setAsText("file:myfile.txt");
626 assertEquals(new File("myfile.txt"), fileEditor.getValue());
627 assertEquals((new File("myfile.txt")).getPath(), fileEditor.getAsText());
628 }
629
630 @Test
631 public void testFileEditorWithRelativePath() {
632 PropertyEditor fileEditor = new FileEditor();
633 try {
634 fileEditor.setAsText("myfile.txt");
635 }
636 catch (IllegalArgumentException ex) {
637
638
639 }
640 }
641
642 @Test
643 public void testFileEditorWithAbsolutePath() {
644 PropertyEditor fileEditor = new FileEditor();
645
646 if (new File("C:/myfile.txt").isAbsolute()) {
647 fileEditor.setAsText("C:/myfile.txt");
648 assertEquals(new File("C:/myfile.txt"), fileEditor.getValue());
649 }
650
651 if (new File("/myfile.txt").isAbsolute()) {
652 fileEditor.setAsText("/myfile.txt");
653 assertEquals(new File("/myfile.txt"), fileEditor.getValue());
654 }
655 }
656
657 @Test
658 public void testLocaleEditor() {
659 PropertyEditor localeEditor = new LocaleEditor();
660 localeEditor.setAsText("en_CA");
661 assertEquals(Locale.CANADA, localeEditor.getValue());
662 assertEquals("en_CA", localeEditor.getAsText());
663
664 localeEditor = new LocaleEditor();
665 assertEquals("", localeEditor.getAsText());
666 }
667
668 @Test
669 public void testPatternEditor() {
670 final String REGEX = "a.*";
671
672 PropertyEditor patternEditor = new PatternEditor();
673 patternEditor.setAsText(REGEX);
674 assertEquals(Pattern.compile(REGEX).pattern(), ((Pattern) patternEditor.getValue()).pattern());
675 assertEquals(REGEX, patternEditor.getAsText());
676
677 patternEditor = new PatternEditor();
678 assertEquals("", patternEditor.getAsText());
679
680 patternEditor = new PatternEditor();
681 patternEditor.setAsText(null);
682 assertEquals("", patternEditor.getAsText());
683 }
684
685 @Test
686 public void testCustomBooleanEditor() {
687 CustomBooleanEditor editor = new CustomBooleanEditor(false);
688 editor.setAsText("true");
689 assertEquals(Boolean.TRUE, editor.getValue());
690 assertEquals("true", editor.getAsText());
691 editor.setAsText("false");
692 assertEquals(Boolean.FALSE, editor.getValue());
693 assertEquals("false", editor.getAsText());
694 editor.setValue(null);
695 assertEquals(null, editor.getValue());
696 assertEquals("", editor.getAsText());
697 }
698
699 @Test
700 public void testCustomBooleanEditorWithEmptyAsNull() {
701 CustomBooleanEditor editor = new CustomBooleanEditor(true);
702 editor.setAsText("true");
703 assertEquals(Boolean.TRUE, editor.getValue());
704 assertEquals("true", editor.getAsText());
705 editor.setAsText("false");
706 assertEquals(Boolean.FALSE, editor.getValue());
707 assertEquals("false", editor.getAsText());
708 editor.setValue(null);
709 assertEquals(null, editor.getValue());
710 assertEquals("", editor.getAsText());
711 }
712
713 @Test
714 public void testCustomDateEditor() {
715 CustomDateEditor editor = new CustomDateEditor(null, false);
716 editor.setValue(null);
717 assertEquals(null, editor.getValue());
718 assertEquals("", editor.getAsText());
719 }
720
721 @Test
722 public void testCustomDateEditorWithEmptyAsNull() {
723 CustomDateEditor editor = new CustomDateEditor(null, true);
724 editor.setValue(null);
725 assertEquals(null, editor.getValue());
726 assertEquals("", editor.getAsText());
727 }
728
729 @Test
730 public void testCustomDateEditorWithExactDateLength() {
731 int maxLength = 10;
732 String validDate = "01/01/2005";
733 String invalidDate = "01/01/05";
734
735 assertTrue(validDate.length() == maxLength);
736 assertFalse(invalidDate.length() == maxLength);
737
738 CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true, maxLength);
739
740 try {
741 editor.setAsText(validDate);
742 }
743 catch (IllegalArgumentException ex) {
744 fail("Exception shouldn't be thrown because this is a valid date");
745 }
746
747 try {
748 editor.setAsText(invalidDate);
749 fail("Exception should be thrown because this is an invalid date");
750 }
751 catch (IllegalArgumentException ex) {
752
753 assertTrue(ex.getMessage().indexOf("10") != -1);
754 }
755 }
756
757 @Test
758 public void testCustomNumberEditor() {
759 CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false);
760 editor.setAsText("5");
761 assertEquals(new Integer(5), editor.getValue());
762 assertEquals("5", editor.getAsText());
763 editor.setValue(null);
764 assertEquals(null, editor.getValue());
765 assertEquals("", editor.getAsText());
766 }
767
768 @Test
769 public void testCustomNumberEditorWithHex() {
770 CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false);
771 editor.setAsText("0x" + Integer.toHexString(64));
772 assertEquals(new Integer(64), editor.getValue());
773 }
774
775 @Test
776 public void testCustomNumberEditorWithEmptyAsNull() {
777 CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true);
778 editor.setAsText("5");
779 assertEquals(new Integer(5), editor.getValue());
780 assertEquals("5", editor.getAsText());
781 editor.setAsText("");
782 assertEquals(null, editor.getValue());
783 assertEquals("", editor.getAsText());
784 editor.setValue(null);
785 assertEquals(null, editor.getValue());
786 assertEquals("", editor.getAsText());
787 }
788
789 @Test
790 public void testStringTrimmerEditor() {
791 StringTrimmerEditor editor = new StringTrimmerEditor(false);
792 editor.setAsText("test");
793 assertEquals("test", editor.getValue());
794 assertEquals("test", editor.getAsText());
795 editor.setAsText(" test ");
796 assertEquals("test", editor.getValue());
797 assertEquals("test", editor.getAsText());
798 editor.setAsText("");
799 assertEquals("", editor.getValue());
800 assertEquals("", editor.getAsText());
801 editor.setValue(null);
802 assertEquals("", editor.getAsText());
803 editor.setAsText(null);
804 assertEquals("", editor.getAsText());
805 }
806
807 @Test
808 public void testStringTrimmerEditorWithEmptyAsNull() {
809 StringTrimmerEditor editor = new StringTrimmerEditor(true);
810 editor.setAsText("test");
811 assertEquals("test", editor.getValue());
812 assertEquals("test", editor.getAsText());
813 editor.setAsText(" test ");
814 assertEquals("test", editor.getValue());
815 assertEquals("test", editor.getAsText());
816 editor.setAsText(" ");
817 assertEquals(null, editor.getValue());
818 assertEquals("", editor.getAsText());
819 editor.setValue(null);
820 assertEquals("", editor.getAsText());
821 }
822
823 @Test
824 public void testStringTrimmerEditorWithCharsToDelete() {
825 StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", false);
826 editor.setAsText("te\ns\ft");
827 assertEquals("test", editor.getValue());
828 assertEquals("test", editor.getAsText());
829 editor.setAsText(" test ");
830 assertEquals("test", editor.getValue());
831 assertEquals("test", editor.getAsText());
832 editor.setAsText("");
833 assertEquals("", editor.getValue());
834 assertEquals("", editor.getAsText());
835 editor.setValue(null);
836 assertEquals("", editor.getAsText());
837 }
838
839 @Test
840 public void testStringTrimmerEditorWithCharsToDeleteAndEmptyAsNull() {
841 StringTrimmerEditor editor = new StringTrimmerEditor("\r\n\f", true);
842 editor.setAsText("te\ns\ft");
843 assertEquals("test", editor.getValue());
844 assertEquals("test", editor.getAsText());
845 editor.setAsText(" test ");
846 assertEquals("test", editor.getValue());
847 assertEquals("test", editor.getAsText());
848 editor.setAsText(" \n\f ");
849 assertEquals(null, editor.getValue());
850 assertEquals("", editor.getAsText());
851 editor.setValue(null);
852 assertEquals("", editor.getAsText());
853 }
854
855 @Test
856 public void testIndexedPropertiesWithCustomEditorForType() {
857 IndexedTestBean bean = new IndexedTestBean();
858 BeanWrapper bw = new BeanWrapperImpl(bean);
859 bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
860 @Override
861 public void setAsText(String text) throws IllegalArgumentException {
862 setValue("prefix" + text);
863 }
864 });
865 TestBean tb0 = bean.getArray()[0];
866 TestBean tb1 = bean.getArray()[1];
867 TestBean tb2 = ((TestBean) bean.getList().get(0));
868 TestBean tb3 = ((TestBean) bean.getList().get(1));
869 TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
870 TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
871 assertEquals("name0", tb0.getName());
872 assertEquals("name1", tb1.getName());
873 assertEquals("name2", tb2.getName());
874 assertEquals("name3", tb3.getName());
875 assertEquals("name4", tb4.getName());
876 assertEquals("name5", tb5.getName());
877 assertEquals("name0", bw.getPropertyValue("array[0].name"));
878 assertEquals("name1", bw.getPropertyValue("array[1].name"));
879 assertEquals("name2", bw.getPropertyValue("list[0].name"));
880 assertEquals("name3", bw.getPropertyValue("list[1].name"));
881 assertEquals("name4", bw.getPropertyValue("map[key1].name"));
882 assertEquals("name5", bw.getPropertyValue("map[key2].name"));
883 assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
884 assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
885
886 MutablePropertyValues pvs = new MutablePropertyValues();
887 pvs.add("array[0].name", "name5");
888 pvs.add("array[1].name", "name4");
889 pvs.add("list[0].name", "name3");
890 pvs.add("list[1].name", "name2");
891 pvs.add("map[key1].name", "name1");
892 pvs.add("map['key2'].name", "name0");
893 bw.setPropertyValues(pvs);
894 assertEquals("prefixname5", tb0.getName());
895 assertEquals("prefixname4", tb1.getName());
896 assertEquals("prefixname3", tb2.getName());
897 assertEquals("prefixname2", tb3.getName());
898 assertEquals("prefixname1", tb4.getName());
899 assertEquals("prefixname0", tb5.getName());
900 assertEquals("prefixname5", bw.getPropertyValue("array[0].name"));
901 assertEquals("prefixname4", bw.getPropertyValue("array[1].name"));
902 assertEquals("prefixname3", bw.getPropertyValue("list[0].name"));
903 assertEquals("prefixname2", bw.getPropertyValue("list[1].name"));
904 assertEquals("prefixname1", bw.getPropertyValue("map[\"key1\"].name"));
905 assertEquals("prefixname0", bw.getPropertyValue("map['key2'].name"));
906 }
907
908 @Test
909 public void testIndexedPropertiesWithCustomEditorForProperty() {
910 IndexedTestBean bean = new IndexedTestBean(false);
911 BeanWrapper bw = new BeanWrapperImpl(bean);
912 bw.registerCustomEditor(String.class, "array.name", new PropertyEditorSupport() {
913 @Override
914 public void setAsText(String text) throws IllegalArgumentException {
915 setValue("array" + text);
916 }
917 });
918 bw.registerCustomEditor(String.class, "list.name", new PropertyEditorSupport() {
919 @Override
920 public void setAsText(String text) throws IllegalArgumentException {
921 setValue("list" + text);
922 }
923 });
924 bw.registerCustomEditor(String.class, "map.name", new PropertyEditorSupport() {
925 @Override
926 public void setAsText(String text) throws IllegalArgumentException {
927 setValue("map" + text);
928 }
929 });
930 bean.populate();
931
932 TestBean tb0 = bean.getArray()[0];
933 TestBean tb1 = bean.getArray()[1];
934 TestBean tb2 = ((TestBean) bean.getList().get(0));
935 TestBean tb3 = ((TestBean) bean.getList().get(1));
936 TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
937 TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
938 assertEquals("name0", tb0.getName());
939 assertEquals("name1", tb1.getName());
940 assertEquals("name2", tb2.getName());
941 assertEquals("name3", tb3.getName());
942 assertEquals("name4", tb4.getName());
943 assertEquals("name5", tb5.getName());
944 assertEquals("name0", bw.getPropertyValue("array[0].name"));
945 assertEquals("name1", bw.getPropertyValue("array[1].name"));
946 assertEquals("name2", bw.getPropertyValue("list[0].name"));
947 assertEquals("name3", bw.getPropertyValue("list[1].name"));
948 assertEquals("name4", bw.getPropertyValue("map[key1].name"));
949 assertEquals("name5", bw.getPropertyValue("map[key2].name"));
950 assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
951 assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
952
953 MutablePropertyValues pvs = new MutablePropertyValues();
954 pvs.add("array[0].name", "name5");
955 pvs.add("array[1].name", "name4");
956 pvs.add("list[0].name", "name3");
957 pvs.add("list[1].name", "name2");
958 pvs.add("map[key1].name", "name1");
959 pvs.add("map['key2'].name", "name0");
960 bw.setPropertyValues(pvs);
961 assertEquals("arrayname5", tb0.getName());
962 assertEquals("arrayname4", tb1.getName());
963 assertEquals("listname3", tb2.getName());
964 assertEquals("listname2", tb3.getName());
965 assertEquals("mapname1", tb4.getName());
966 assertEquals("mapname0", tb5.getName());
967 assertEquals("arrayname5", bw.getPropertyValue("array[0].name"));
968 assertEquals("arrayname4", bw.getPropertyValue("array[1].name"));
969 assertEquals("listname3", bw.getPropertyValue("list[0].name"));
970 assertEquals("listname2", bw.getPropertyValue("list[1].name"));
971 assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name"));
972 assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
973 }
974
975 @Test
976 public void testIndexedPropertiesWithIndividualCustomEditorForProperty() {
977 IndexedTestBean bean = new IndexedTestBean(false);
978 BeanWrapper bw = new BeanWrapperImpl(bean);
979 bw.registerCustomEditor(String.class, "array[0].name", new PropertyEditorSupport() {
980 @Override
981 public void setAsText(String text) throws IllegalArgumentException {
982 setValue("array0" + text);
983 }
984 });
985 bw.registerCustomEditor(String.class, "array[1].name", new PropertyEditorSupport() {
986 @Override
987 public void setAsText(String text) throws IllegalArgumentException {
988 setValue("array1" + text);
989 }
990 });
991 bw.registerCustomEditor(String.class, "list[0].name", new PropertyEditorSupport() {
992 @Override
993 public void setAsText(String text) throws IllegalArgumentException {
994 setValue("list0" + text);
995 }
996 });
997 bw.registerCustomEditor(String.class, "list[1].name", new PropertyEditorSupport() {
998 @Override
999 public void setAsText(String text) throws IllegalArgumentException {
1000 setValue("list1" + text);
1001 }
1002 });
1003 bw.registerCustomEditor(String.class, "map[key1].name", new PropertyEditorSupport() {
1004 @Override
1005 public void setAsText(String text) throws IllegalArgumentException {
1006 setValue("mapkey1" + text);
1007 }
1008 });
1009 bw.registerCustomEditor(String.class, "map[key2].name", new PropertyEditorSupport() {
1010 @Override
1011 public void setAsText(String text) throws IllegalArgumentException {
1012 setValue("mapkey2" + text);
1013 }
1014 });
1015 bean.populate();
1016
1017 TestBean tb0 = bean.getArray()[0];
1018 TestBean tb1 = bean.getArray()[1];
1019 TestBean tb2 = ((TestBean) bean.getList().get(0));
1020 TestBean tb3 = ((TestBean) bean.getList().get(1));
1021 TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
1022 TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
1023 assertEquals("name0", tb0.getName());
1024 assertEquals("name1", tb1.getName());
1025 assertEquals("name2", tb2.getName());
1026 assertEquals("name3", tb3.getName());
1027 assertEquals("name4", tb4.getName());
1028 assertEquals("name5", tb5.getName());
1029 assertEquals("name0", bw.getPropertyValue("array[0].name"));
1030 assertEquals("name1", bw.getPropertyValue("array[1].name"));
1031 assertEquals("name2", bw.getPropertyValue("list[0].name"));
1032 assertEquals("name3", bw.getPropertyValue("list[1].name"));
1033 assertEquals("name4", bw.getPropertyValue("map[key1].name"));
1034 assertEquals("name5", bw.getPropertyValue("map[key2].name"));
1035 assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
1036 assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
1037
1038 MutablePropertyValues pvs = new MutablePropertyValues();
1039 pvs.add("array[0].name", "name5");
1040 pvs.add("array[1].name", "name4");
1041 pvs.add("list[0].name", "name3");
1042 pvs.add("list[1].name", "name2");
1043 pvs.add("map[key1].name", "name1");
1044 pvs.add("map['key2'].name", "name0");
1045 bw.setPropertyValues(pvs);
1046 assertEquals("array0name5", tb0.getName());
1047 assertEquals("array1name4", tb1.getName());
1048 assertEquals("list0name3", tb2.getName());
1049 assertEquals("list1name2", tb3.getName());
1050 assertEquals("mapkey1name1", tb4.getName());
1051 assertEquals("mapkey2name0", tb5.getName());
1052 assertEquals("array0name5", bw.getPropertyValue("array[0].name"));
1053 assertEquals("array1name4", bw.getPropertyValue("array[1].name"));
1054 assertEquals("list0name3", bw.getPropertyValue("list[0].name"));
1055 assertEquals("list1name2", bw.getPropertyValue("list[1].name"));
1056 assertEquals("mapkey1name1", bw.getPropertyValue("map[\"key1\"].name"));
1057 assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name"));
1058 }
1059
1060 @Test
1061 public void testNestedIndexedPropertiesWithCustomEditorForProperty() {
1062 IndexedTestBean bean = new IndexedTestBean();
1063 TestBean tb0 = bean.getArray()[0];
1064 TestBean tb1 = bean.getArray()[1];
1065 TestBean tb2 = ((TestBean) bean.getList().get(0));
1066 TestBean tb3 = ((TestBean) bean.getList().get(1));
1067 TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
1068 TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
1069 tb0.setNestedIndexedBean(new IndexedTestBean());
1070 tb1.setNestedIndexedBean(new IndexedTestBean());
1071 tb2.setNestedIndexedBean(new IndexedTestBean());
1072 tb3.setNestedIndexedBean(new IndexedTestBean());
1073 tb4.setNestedIndexedBean(new IndexedTestBean());
1074 tb5.setNestedIndexedBean(new IndexedTestBean());
1075 BeanWrapper bw = new BeanWrapperImpl(bean);
1076 bw.registerCustomEditor(String.class, "array.nestedIndexedBean.array.name", new PropertyEditorSupport() {
1077 @Override
1078 public void setAsText(String text) throws IllegalArgumentException {
1079 setValue("array" + text);
1080 }
1081
1082 @Override
1083 public String getAsText() {
1084 return ((String) getValue()).substring(5);
1085 }
1086 });
1087 bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport() {
1088 @Override
1089 public void setAsText(String text) throws IllegalArgumentException {
1090 setValue("list" + text);
1091 }
1092
1093 @Override
1094 public String getAsText() {
1095 return ((String) getValue()).substring(4);
1096 }
1097 });
1098 bw.registerCustomEditor(String.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport() {
1099 @Override
1100 public void setAsText(String text) throws IllegalArgumentException {
1101 setValue("map" + text);
1102 }
1103
1104 @Override
1105 public String getAsText() {
1106 return ((String) getValue()).substring(4);
1107 }
1108 });
1109 assertEquals("name0", tb0.getName());
1110 assertEquals("name1", tb1.getName());
1111 assertEquals("name2", tb2.getName());
1112 assertEquals("name3", tb3.getName());
1113 assertEquals("name4", tb4.getName());
1114 assertEquals("name5", tb5.getName());
1115 assertEquals("name0", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
1116 assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
1117 assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
1118 assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
1119 assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name"));
1120 assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name"));
1121
1122 MutablePropertyValues pvs = new MutablePropertyValues();
1123 pvs.add("array[0].nestedIndexedBean.array[0].name", "name5");
1124 pvs.add("array[1].nestedIndexedBean.array[1].name", "name4");
1125 pvs.add("list[0].nestedIndexedBean.list[0].name", "name3");
1126 pvs.add("list[1].nestedIndexedBean.list[1].name", "name2");
1127 pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
1128 pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0");
1129 bw.setPropertyValues(pvs);
1130 assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
1131 assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName());
1132 assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
1133 assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
1134 assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
1135 assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
1136 assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
1137 assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
1138 assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
1139 assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
1140 assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name"));
1141 assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name"));
1142 }
1143
1144 @Test
1145 public void testNestedIndexedPropertiesWithIndexedCustomEditorForProperty() {
1146 IndexedTestBean bean = new IndexedTestBean();
1147 TestBean tb0 = bean.getArray()[0];
1148 TestBean tb1 = bean.getArray()[1];
1149 TestBean tb2 = ((TestBean) bean.getList().get(0));
1150 TestBean tb3 = ((TestBean) bean.getList().get(1));
1151 TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
1152 TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
1153 tb0.setNestedIndexedBean(new IndexedTestBean());
1154 tb1.setNestedIndexedBean(new IndexedTestBean());
1155 tb2.setNestedIndexedBean(new IndexedTestBean());
1156 tb3.setNestedIndexedBean(new IndexedTestBean());
1157 tb4.setNestedIndexedBean(new IndexedTestBean());
1158 tb5.setNestedIndexedBean(new IndexedTestBean());
1159 BeanWrapper bw = new BeanWrapperImpl(bean);
1160 bw.registerCustomEditor(String.class, "array[0].nestedIndexedBean.array[0].name", new PropertyEditorSupport() {
1161 @Override
1162 public void setAsText(String text) throws IllegalArgumentException {
1163 setValue("array" + text);
1164 }
1165 });
1166 bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list[1].name", new PropertyEditorSupport() {
1167 @Override
1168 public void setAsText(String text) throws IllegalArgumentException {
1169 setValue("list" + text);
1170 }
1171 });
1172 bw.registerCustomEditor(String.class, "map[key1].nestedIndexedBean.map.name", new PropertyEditorSupport() {
1173 @Override
1174 public void setAsText(String text) throws IllegalArgumentException {
1175 setValue("map" + text);
1176 }
1177 });
1178
1179 MutablePropertyValues pvs = new MutablePropertyValues();
1180 pvs.add("array[0].nestedIndexedBean.array[0].name", "name5");
1181 pvs.add("array[1].nestedIndexedBean.array[1].name", "name4");
1182 pvs.add("list[0].nestedIndexedBean.list[0].name", "name3");
1183 pvs.add("list[1].nestedIndexedBean.list[1].name", "name2");
1184 pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
1185 pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0");
1186 bw.setPropertyValues(pvs);
1187 assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
1188 assertEquals("name4", tb1.getNestedIndexedBean().getArray()[1].getName());
1189 assertEquals("name3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
1190 assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
1191 assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
1192 assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
1193 }
1194
1195 @Test
1196 public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
1197 IndexedTestBean bean = new IndexedTestBean();
1198 BeanWrapper bw = new BeanWrapperImpl(bean);
1199 bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
1200 @Override
1201 public void setAsText(String text) throws IllegalArgumentException {
1202 setValue(new TestBean("array" + text, 99));
1203 }
1204
1205 @Override
1206 public String getAsText() {
1207 return ((TestBean) getValue()).getName();
1208 }
1209 });
1210 bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport() {
1211 @Override
1212 public void setAsText(String text) throws IllegalArgumentException {
1213 setValue(new TestBean("list" + text, 99));
1214 }
1215
1216 @Override
1217 public String getAsText() {
1218 return ((TestBean) getValue()).getName();
1219 }
1220 });
1221 bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
1222 @Override
1223 public void setAsText(String text) throws IllegalArgumentException {
1224 setValue(new TestBean("map" + text, 99));
1225 }
1226
1227 @Override
1228 public String getAsText() {
1229 return ((TestBean) getValue()).getName();
1230 }
1231 });
1232
1233 MutablePropertyValues pvs = new MutablePropertyValues();
1234 pvs.add("array[0]", "a");
1235 pvs.add("array[1]", "b");
1236 pvs.add("list[0]", "c");
1237 pvs.add("list[1]", "d");
1238 pvs.add("map[key1]", "e");
1239 pvs.add("map['key2']", "f");
1240 bw.setPropertyValues(pvs);
1241 assertEquals("arraya", bean.getArray()[0].getName());
1242 assertEquals("arrayb", bean.getArray()[1].getName());
1243 assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
1244 assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
1245 assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
1246 assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
1247 }
1248
1249 @Test
1250 public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
1251 IndexedTestBean bean = new IndexedTestBean();
1252 BeanWrapper bw = new BeanWrapperImpl(bean);
1253 bw.registerCustomEditor(TestBean.class, "array[0]", new PropertyEditorSupport() {
1254 @Override
1255 public void setAsText(String text) throws IllegalArgumentException {
1256 setValue(new TestBean("array0" + text, 99));
1257 }
1258
1259 @Override
1260 public String getAsText() {
1261 return ((TestBean) getValue()).getName();
1262 }
1263 });
1264 bw.registerCustomEditor(TestBean.class, "array[1]", new PropertyEditorSupport() {
1265 @Override
1266 public void setAsText(String text) throws IllegalArgumentException {
1267 setValue(new TestBean("array1" + text, 99));
1268 }
1269
1270 @Override
1271 public String getAsText() {
1272 return ((TestBean) getValue()).getName();
1273 }
1274 });
1275 bw.registerCustomEditor(TestBean.class, "list[0]", new PropertyEditorSupport() {
1276 @Override
1277 public void setAsText(String text) throws IllegalArgumentException {
1278 setValue(new TestBean("list0" + text, 99));
1279 }
1280
1281 @Override
1282 public String getAsText() {
1283 return ((TestBean) getValue()).getName();
1284 }
1285 });
1286 bw.registerCustomEditor(TestBean.class, "list[1]", new PropertyEditorSupport() {
1287 @Override
1288 public void setAsText(String text) throws IllegalArgumentException {
1289 setValue(new TestBean("list1" + text, 99));
1290 }
1291
1292 @Override
1293 public String getAsText() {
1294 return ((TestBean) getValue()).getName();
1295 }
1296 });
1297 bw.registerCustomEditor(TestBean.class, "map[key1]", new PropertyEditorSupport() {
1298 @Override
1299 public void setAsText(String text) throws IllegalArgumentException {
1300 setValue(new TestBean("mapkey1" + text, 99));
1301 }
1302
1303 @Override
1304 public String getAsText() {
1305 return ((TestBean) getValue()).getName();
1306 }
1307 });
1308 bw.registerCustomEditor(TestBean.class, "map[key2]", new PropertyEditorSupport() {
1309 @Override
1310 public void setAsText(String text) throws IllegalArgumentException {
1311 setValue(new TestBean("mapkey2" + text, 99));
1312 }
1313
1314 @Override
1315 public String getAsText() {
1316 return ((TestBean) getValue()).getName();
1317 }
1318 });
1319
1320 MutablePropertyValues pvs = new MutablePropertyValues();
1321 pvs.add("array[0]", "a");
1322 pvs.add("array[1]", "b");
1323 pvs.add("list[0]", "c");
1324 pvs.add("list[1]", "d");
1325 pvs.add("map[key1]", "e");
1326 pvs.add("map['key2']", "f");
1327 bw.setPropertyValues(pvs);
1328 assertEquals("array0a", bean.getArray()[0].getName());
1329 assertEquals("array1b", bean.getArray()[1].getName());
1330 assertEquals("list0c", ((TestBean) bean.getList().get(0)).getName());
1331 assertEquals("list1d", ((TestBean) bean.getList().get(1)).getName());
1332 assertEquals("mapkey1e", ((TestBean) bean.getMap().get("key1")).getName());
1333 assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName());
1334 }
1335
1336 @Test
1337 public void testIndexedPropertiesWithListPropertyEditor() {
1338 IndexedTestBean bean = new IndexedTestBean();
1339 BeanWrapper bw = new BeanWrapperImpl(bean);
1340 bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
1341 @Override
1342 public void setAsText(String text) throws IllegalArgumentException {
1343 List<TestBean> result = new ArrayList<TestBean>();
1344 result.add(new TestBean("list" + text, 99));
1345 setValue(result);
1346 }
1347 });
1348 bw.setPropertyValue("list", "1");
1349 assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
1350 bw.setPropertyValue("list[0]", "test");
1351 assertEquals("test", bean.getList().get(0));
1352 }
1353
1354 @Test
1355 public void testConversionToOldCollections() throws PropertyVetoException {
1356 OldCollectionsBean tb = new OldCollectionsBean();
1357 BeanWrapper bw = new BeanWrapperImpl(tb);
1358 bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class));
1359 bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class));
1360
1361 bw.setPropertyValue("vector", new String[] {"a", "b"});
1362 assertEquals(2, tb.getVector().size());
1363 assertEquals("a", tb.getVector().get(0));
1364 assertEquals("b", tb.getVector().get(1));
1365
1366 bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar"));
1367 assertEquals(1, tb.getHashtable().size());
1368 assertEquals("bar", tb.getHashtable().get("foo"));
1369 }
1370
1371 @Test
1372 public void testUninitializedArrayPropertyWithCustomEditor() {
1373 IndexedTestBean bean = new IndexedTestBean(false);
1374 BeanWrapper bw = new BeanWrapperImpl(bean);
1375 PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
1376 bw.registerCustomEditor(null, "list.age", pe);
1377 TestBean tb = new TestBean();
1378 bw.setPropertyValue("list", new ArrayList<Object>());
1379 bw.setPropertyValue("list[0]", tb);
1380 assertEquals(tb, bean.getList().get(0));
1381 assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
1382 assertEquals(pe, bw.findCustomEditor(null, "list.age"));
1383 assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
1384 assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
1385 }
1386
1387 @Test
1388 public void testArrayToArrayConversion() throws PropertyVetoException {
1389 IndexedTestBean tb = new IndexedTestBean();
1390 BeanWrapper bw = new BeanWrapperImpl(tb);
1391 bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
1392 @Override
1393 public void setAsText(String text) throws IllegalArgumentException {
1394 setValue(new TestBean(text, 99));
1395 }
1396 });
1397 bw.setPropertyValue("array", new String[] {"a", "b"});
1398 assertEquals(2, tb.getArray().length);
1399 assertEquals("a", tb.getArray()[0].getName());
1400 assertEquals("b", tb.getArray()[1].getName());
1401 }
1402
1403 @Test
1404 public void testArrayToStringConversion() throws PropertyVetoException {
1405 TestBean tb = new TestBean();
1406 BeanWrapper bw = new BeanWrapperImpl(tb);
1407 bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
1408 @Override
1409 public void setAsText(String text) throws IllegalArgumentException {
1410 setValue("-" + text + "-");
1411 }
1412 });
1413 bw.setPropertyValue("name", new String[] {"a", "b"});
1414 assertEquals("-a,b-", tb.getName());
1415 }
1416
1417 @Test
1418 public void testClassArrayEditorSunnyDay() throws Exception {
1419 ClassArrayEditor classArrayEditor = new ClassArrayEditor();
1420 classArrayEditor.setAsText("java.lang.String,java.util.HashMap");
1421 Class<?>[] classes = (Class<?>[]) classArrayEditor.getValue();
1422 assertEquals(2, classes.length);
1423 assertEquals(String.class, classes[0]);
1424 assertEquals(HashMap.class, classes[1]);
1425 assertEquals("java.lang.String,java.util.HashMap", classArrayEditor.getAsText());
1426
1427 classArrayEditor.setAsText(classArrayEditor.getAsText());
1428 }
1429
1430 @Test
1431 public void testClassArrayEditorSunnyDayWithArrayTypes() throws Exception {
1432 ClassArrayEditor classArrayEditor = new ClassArrayEditor();
1433 classArrayEditor.setAsText("java.lang.String[],java.util.Map[],int[],float[][][]");
1434 Class<?>[] classes = (Class<?>[]) classArrayEditor.getValue();
1435 assertEquals(4, classes.length);
1436 assertEquals(String[].class, classes[0]);
1437 assertEquals(Map[].class, classes[1]);
1438 assertEquals(int[].class, classes[2]);
1439 assertEquals(float[][][].class, classes[3]);
1440 assertEquals("java.lang.String[],java.util.Map[],int[],float[][][]", classArrayEditor.getAsText());
1441
1442 classArrayEditor.setAsText(classArrayEditor.getAsText());
1443 }
1444
1445 @Test
1446 public void testClassArrayEditorSetAsTextWithNull() throws Exception {
1447 ClassArrayEditor editor = new ClassArrayEditor();
1448 editor.setAsText(null);
1449 assertNull(editor.getValue());
1450 assertEquals("", editor.getAsText());
1451 }
1452
1453 @Test
1454 public void testClassArrayEditorSetAsTextWithEmptyString() throws Exception {
1455 ClassArrayEditor editor = new ClassArrayEditor();
1456 editor.setAsText("");
1457 assertNull(editor.getValue());
1458 assertEquals("", editor.getAsText());
1459 }
1460
1461 @Test
1462 public void testClassArrayEditorSetAsTextWithWhitespaceString() throws Exception {
1463 ClassArrayEditor editor = new ClassArrayEditor();
1464 editor.setAsText("\n");
1465 assertNull(editor.getValue());
1466 assertEquals("", editor.getAsText());
1467 }
1468
1469 @Test
1470 public void testCharsetEditor() throws Exception {
1471 CharsetEditor editor = new CharsetEditor();
1472 String name = "UTF-8";
1473 editor.setAsText(name);
1474 Charset charset = Charset.forName(name);
1475 assertEquals("Invalid Charset conversion", charset, editor.getValue());
1476 editor.setValue(charset);
1477 assertEquals("Invalid Charset conversion", name, editor.getAsText());
1478 }
1479
1480
1481 private static class TestBeanEditor extends PropertyEditorSupport {
1482
1483 @Override
1484 public void setAsText(String text) {
1485 TestBean tb = new TestBean();
1486 StringTokenizer st = new StringTokenizer(text, "_");
1487 tb.setName(st.nextToken());
1488 tb.setAge(Integer.parseInt(st.nextToken()));
1489 setValue(tb);
1490 }
1491 }
1492
1493
1494 private static class OldValueAccessingTestBeanEditor extends PropertyEditorSupport {
1495
1496 @Override
1497 public void setAsText(String text) {
1498 TestBean tb = new TestBean();
1499 StringTokenizer st = new StringTokenizer(text, "_");
1500 tb.setName(st.nextToken());
1501 tb.setAge(Integer.parseInt(st.nextToken()));
1502 if (!tb.equals(getValue())) {
1503 setValue(tb);
1504 }
1505 }
1506 }
1507
1508
1509 @SuppressWarnings("unused")
1510 private static class PrimitiveArrayBean {
1511
1512 private byte[] byteArray;
1513
1514 private char[] charArray;
1515
1516 public byte[] getByteArray() {
1517 return byteArray;
1518 }
1519
1520 public void setByteArray(byte[] byteArray) {
1521 this.byteArray = byteArray;
1522 }
1523
1524 public char[] getCharArray() {
1525 return charArray;
1526 }
1527
1528 public void setCharArray(char[] charArray) {
1529 this.charArray = charArray;
1530 }
1531 }
1532
1533
1534 @SuppressWarnings("unused")
1535 private static class CharBean {
1536
1537 private char myChar;
1538
1539 private Character myCharacter;
1540
1541 public char getMyChar() {
1542 return myChar;
1543 }
1544
1545 public void setMyChar(char myChar) {
1546 this.myChar = myChar;
1547 }
1548
1549 public Character getMyCharacter() {
1550 return myCharacter;
1551 }
1552
1553 public void setMyCharacter(Character myCharacter) {
1554 this.myCharacter = myCharacter;
1555 }
1556 }
1557
1558
1559 @SuppressWarnings("unused")
1560 private static class OldCollectionsBean {
1561
1562 private Vector<?> vector;
1563
1564 private Hashtable<?, ?> hashtable;
1565
1566 public Vector<?> getVector() {
1567 return vector;
1568 }
1569
1570 public void setVector(Vector<?> vector) {
1571 this.vector = vector;
1572 }
1573
1574 public Hashtable<?, ?> getHashtable() {
1575 return hashtable;
1576 }
1577
1578 public void setHashtable(Hashtable<?, ?> hashtable) {
1579 this.hashtable = hashtable;
1580 }
1581 }
1582
1583 }