KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > CustomEditorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditor JavaDoc;
20 import java.beans.PropertyEditorSupport JavaDoc;
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25 import java.text.NumberFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
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 /**
45  * @author Juergen Hoeller
46  * @since 10.06.2003
47  */

48 public class CustomEditorTests extends TestCase {
49
50     public void testComplexObject() {
51         TestBean t = new TestBean();
52         String JavaDoc newName = "Rod";
53         String JavaDoc 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 JavaDoc(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 JavaDoc.class, "name", new PropertyEditorSupport JavaDoc() {
72             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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 JavaDoc.class, new PropertyEditorSupport JavaDoc() {
88             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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 JavaDoc.class, "spouse.name", new PropertyEditorSupport JavaDoc() {
105             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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 JavaDoc.class, new PropertyEditorSupport JavaDoc() {
122             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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             // expected
170
}
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             // expected
209
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 JavaDoc.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 JavaDoc("1").equals(bw.getPropertyValue("short1")));
268         assertTrue("Correct short1 value", tb.getShort1() == 1);
269         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(bw.getPropertyValue("short2")));
270         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(tb.getShort2()));
271         assertTrue("Correct int1 value", new Integer JavaDoc("7").equals(bw.getPropertyValue("int1")));
272         assertTrue("Correct int1 value", tb.getInt1() == 7);
273         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(bw.getPropertyValue("int2")));
274         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(tb.getInt2()));
275         assertTrue("Correct long1 value", new Long JavaDoc("5").equals(bw.getPropertyValue("long1")));
276         assertTrue("Correct long1 value", tb.getLong1() == 5);
277         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(bw.getPropertyValue("long2")));
278         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(tb.getLong2()));
279         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(bw.getPropertyValue("bigInteger")));
280         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(tb.getBigInteger()));
281         assertTrue("Correct float1 value", new Float JavaDoc("7.1").equals(bw.getPropertyValue("float1")));
282         assertTrue("Correct float1 value", new Float JavaDoc("7.1").equals(new Float JavaDoc(tb.getFloat1())));
283         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(bw.getPropertyValue("float2")));
284         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(tb.getFloat2()));
285         assertTrue("Correct double1 value", new Double JavaDoc("5.1").equals(bw.getPropertyValue("double1")));
286         assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
287         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(bw.getPropertyValue("double2")));
288         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(tb.getDouble2()));
289         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.5").equals(bw.getPropertyValue("bigDecimal")));
290         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.5").equals(tb.getBigDecimal()));
291     }
292
293     public void testCustomNumberEditorWithoutAllowEmpty() {
294         NumberFormat JavaDoc nf = NumberFormat.getNumberInstance(Locale.GERMAN);
295         NumberTestBean tb = new NumberTestBean();
296         BeanWrapper bw = new BeanWrapperImpl(tb);
297         bw.registerCustomEditor(short.class, new CustomNumberEditor(Short JavaDoc.class, nf, false));
298         bw.registerCustomEditor(Short JavaDoc.class, new CustomNumberEditor(Short JavaDoc.class, nf, false));
299         bw.registerCustomEditor(int.class, new CustomNumberEditor(Short JavaDoc.class, nf, false));
300         bw.registerCustomEditor(Integer JavaDoc.class, new CustomNumberEditor(Integer JavaDoc.class, nf, false));
301         bw.registerCustomEditor(long.class, new CustomNumberEditor(Long JavaDoc.class, nf, false));
302         bw.registerCustomEditor(Long JavaDoc.class, new CustomNumberEditor(Long JavaDoc.class, nf, false));
303         bw.registerCustomEditor(BigInteger JavaDoc.class, new CustomNumberEditor(BigInteger JavaDoc.class, nf, false));
304         bw.registerCustomEditor(float.class, new CustomNumberEditor(Float JavaDoc.class, nf, false));
305         bw.registerCustomEditor(Float JavaDoc.class, new CustomNumberEditor(Float JavaDoc.class, nf, false));
306         bw.registerCustomEditor(double.class, new CustomNumberEditor(Double JavaDoc.class, nf, false));
307         bw.registerCustomEditor(Double JavaDoc.class, new CustomNumberEditor(Double JavaDoc.class, nf, false));
308         bw.registerCustomEditor(BigDecimal JavaDoc.class, new CustomNumberEditor(BigDecimal JavaDoc.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 JavaDoc("1").equals(bw.getPropertyValue("short1")));
324         assertTrue("Correct short1 value", tb.getShort1() == 1);
325         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(bw.getPropertyValue("short2")));
326         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(tb.getShort2()));
327         assertTrue("Correct int1 value", new Integer JavaDoc("7").equals(bw.getPropertyValue("int1")));
328         assertTrue("Correct int1 value", tb.getInt1() == 7);
329         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(bw.getPropertyValue("int2")));
330         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(tb.getInt2()));
331         assertTrue("Correct long1 value", new Long JavaDoc("5").equals(bw.getPropertyValue("long1")));
332         assertTrue("Correct long1 value", tb.getLong1() == 5);
333         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(bw.getPropertyValue("long2")));
334         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(tb.getLong2()));
335         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(bw.getPropertyValue("bigInteger")));
336         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(tb.getBigInteger()));
337         assertTrue("Correct float1 value", new Float JavaDoc("7.1").equals(bw.getPropertyValue("float1")));
338         assertTrue("Correct float1 value", new Float JavaDoc("7.1").equals(new Float JavaDoc(tb.getFloat1())));
339         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(bw.getPropertyValue("float2")));
340         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(tb.getFloat2()));
341         assertTrue("Correct double1 value", new Double JavaDoc("5.1").equals(bw.getPropertyValue("double1")));
342         assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
343         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(bw.getPropertyValue("double2")));
344         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(tb.getDouble2()));
345         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.5").equals(bw.getPropertyValue("bigDecimal")));
346         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.5").equals(tb.getBigDecimal()));
347     }
348
349     public void testCustomNumberEditorWithAllowEmpty() {
350         NumberFormat JavaDoc nf = NumberFormat.getNumberInstance(Locale.GERMAN);
351         NumberTestBean tb = new NumberTestBean();
352         BeanWrapper bw = new BeanWrapperImpl(tb);
353         bw.registerCustomEditor(long.class, new CustomNumberEditor(Long JavaDoc.class, nf, true));
354         bw.registerCustomEditor(Long JavaDoc.class, new CustomNumberEditor(Long JavaDoc.class, nf, true));
355
356         bw.setPropertyValue("long1", "5");
357         bw.setPropertyValue("long2", "6");
358         assertTrue("Correct long1 value", new Long JavaDoc("5").equals(bw.getPropertyValue("long1")));
359         assertTrue("Correct long1 value", tb.getLong1() == 5);
360         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(bw.getPropertyValue("long2")));
361         assertTrue("Correct long2 value", new Long JavaDoc("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             // expected
373
assertTrue("Correct long1 value", new Long JavaDoc("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 JavaDoc(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 JavaDoc('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 JavaDoc 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 JavaDoc 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 JavaDoc fileEditor = new FileEditor();
412         fileEditor.setAsText("C:/test/myfile.txt");
413         assertEquals(new File JavaDoc("C:/test/myfile.txt"), fileEditor.getValue());
414         assertEquals((new File JavaDoc("C:/test/myfile.txt")).getAbsolutePath(), fileEditor.getAsText());
415     }
416
417     public void testLocaleEditor() {
418         PropertyEditor JavaDoc 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 JavaDoc.class, false);
466         editor.setAsText("5");
467         assertEquals(new Integer JavaDoc(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 JavaDoc.class, true);
476         editor.setAsText("5");
477         assertEquals(new Integer JavaDoc(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 JavaDoc.class, new PropertyEditorSupport JavaDoc() {
551             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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         bw.registerCustomEditor(String JavaDoc.class, "array.name", new PropertyEditorSupport JavaDoc() {
602             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
603                 setValue("array" + text);
604             }
605         });
606         bw.registerCustomEditor(String JavaDoc.class, "list.name", new PropertyEditorSupport JavaDoc() {
607             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
608                 setValue("list" + text);
609             }
610         });
611         bw.registerCustomEditor(String JavaDoc.class, "map.name", new PropertyEditorSupport JavaDoc() {
612             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
613                 setValue("map" + text);
614             }
615         });
616         bean.populate();
617
618         TestBean tb0 = bean.getArray()[0];
619         TestBean tb1 = bean.getArray()[1];
620         TestBean tb2 = ((TestBean) bean.getList().get(0));
621         TestBean tb3 = ((TestBean) bean.getList().get(1));
622         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
623         TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
624         assertEquals("name0", tb0.getName());
625         assertEquals("name1", tb1.getName());
626         assertEquals("name2", tb2.getName());
627         assertEquals("name3", tb3.getName());
628         assertEquals("name4", tb4.getName());
629         assertEquals("name5", tb5.getName());
630         assertEquals("name0", bw.getPropertyValue("array[0].name"));
631         assertEquals("name1", bw.getPropertyValue("array[1].name"));
632         assertEquals("name2", bw.getPropertyValue("list[0].name"));
633         assertEquals("name3", bw.getPropertyValue("list[1].name"));
634         assertEquals("name4", bw.getPropertyValue("map[key1].name"));
635         assertEquals("name5", bw.getPropertyValue("map[key2].name"));
636         assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
637         assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
638
639         MutablePropertyValues pvs = new MutablePropertyValues();
640         pvs.addPropertyValue("array[0].name", "name5");
641         pvs.addPropertyValue("array[1].name", "name4");
642         pvs.addPropertyValue("list[0].name", "name3");
643         pvs.addPropertyValue("list[1].name", "name2");
644         pvs.addPropertyValue("map[key1].name", "name1");
645         pvs.addPropertyValue("map['key2'].name", "name0");
646         bw.setPropertyValues(pvs);
647         assertEquals("arrayname5", tb0.getName());
648         assertEquals("arrayname4", tb1.getName());
649         assertEquals("listname3", tb2.getName());
650         assertEquals("listname2", tb3.getName());
651         assertEquals("mapname1", tb4.getName());
652         assertEquals("mapname0", tb5.getName());
653         assertEquals("arrayname5", bw.getPropertyValue("array[0].name"));
654         assertEquals("arrayname4", bw.getPropertyValue("array[1].name"));
655         assertEquals("listname3", bw.getPropertyValue("list[0].name"));
656         assertEquals("listname2", bw.getPropertyValue("list[1].name"));
657         assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name"));
658         assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
659     }
660
661     public void testIndexedPropertiesWithIndividualCustomEditorForProperty() {
662         IndexedTestBean bean = new IndexedTestBean(false);
663         BeanWrapper bw = new BeanWrapperImpl(bean);
664         bw.registerCustomEditor(String JavaDoc.class, "array[0].name", new PropertyEditorSupport JavaDoc() {
665             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
666                 setValue("array0" + text);
667             }
668         });
669         bw.registerCustomEditor(String JavaDoc.class, "array[1].name", new PropertyEditorSupport JavaDoc() {
670             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
671                 setValue("array1" + text);
672             }
673         });
674         bw.registerCustomEditor(String JavaDoc.class, "list[0].name", new PropertyEditorSupport JavaDoc() {
675             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
676                 setValue("list0" + text);
677             }
678         });
679         bw.registerCustomEditor(String JavaDoc.class, "list[1].name", new PropertyEditorSupport JavaDoc() {
680             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
681                 setValue("list1" + text);
682             }
683         });
684         bw.registerCustomEditor(String JavaDoc.class, "map[key1].name", new PropertyEditorSupport JavaDoc() {
685             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
686                 setValue("mapkey1" + text);
687             }
688         });
689         bw.registerCustomEditor(String JavaDoc.class, "map[key2].name", new PropertyEditorSupport JavaDoc() {
690             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
691                 setValue("mapkey2" + text);
692             }
693         });
694         bean.populate();
695
696         TestBean tb0 = bean.getArray()[0];
697         TestBean tb1 = bean.getArray()[1];
698         TestBean tb2 = ((TestBean) bean.getList().get(0));
699         TestBean tb3 = ((TestBean) bean.getList().get(1));
700         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
701         TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
702         assertEquals("name0", tb0.getName());
703         assertEquals("name1", tb1.getName());
704         assertEquals("name2", tb2.getName());
705         assertEquals("name3", tb3.getName());
706         assertEquals("name4", tb4.getName());
707         assertEquals("name5", tb5.getName());
708         assertEquals("name0", bw.getPropertyValue("array[0].name"));
709         assertEquals("name1", bw.getPropertyValue("array[1].name"));
710         assertEquals("name2", bw.getPropertyValue("list[0].name"));
711         assertEquals("name3", bw.getPropertyValue("list[1].name"));
712         assertEquals("name4", bw.getPropertyValue("map[key1].name"));
713         assertEquals("name5", bw.getPropertyValue("map[key2].name"));
714         assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
715         assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
716
717         MutablePropertyValues pvs = new MutablePropertyValues();
718         pvs.addPropertyValue("array[0].name", "name5");
719         pvs.addPropertyValue("array[1].name", "name4");
720         pvs.addPropertyValue("list[0].name", "name3");
721         pvs.addPropertyValue("list[1].name", "name2");
722         pvs.addPropertyValue("map[key1].name", "name1");
723         pvs.addPropertyValue("map['key2'].name", "name0");
724         bw.setPropertyValues(pvs);
725         assertEquals("array0name5", tb0.getName());
726         assertEquals("array1name4", tb1.getName());
727         assertEquals("list0name3", tb2.getName());
728         assertEquals("list1name2", tb3.getName());
729         assertEquals("mapkey1name1", tb4.getName());
730         assertEquals("mapkey2name0", tb5.getName());
731         assertEquals("array0name5", bw.getPropertyValue("array[0].name"));
732         assertEquals("array1name4", bw.getPropertyValue("array[1].name"));
733         assertEquals("list0name3", bw.getPropertyValue("list[0].name"));
734         assertEquals("list1name2", bw.getPropertyValue("list[1].name"));
735         assertEquals("mapkey1name1", bw.getPropertyValue("map[\"key1\"].name"));
736         assertEquals("mapkey2name0", bw.getPropertyValue("map['key2'].name"));
737     }
738
739     public void testNestedIndexedPropertiesWithCustomEditorForProperty() {
740         IndexedTestBean bean = new IndexedTestBean();
741         TestBean tb0 = bean.getArray()[0];
742         TestBean tb1 = bean.getArray()[1];
743         TestBean tb2 = ((TestBean) bean.getList().get(0));
744         TestBean tb3 = ((TestBean) bean.getList().get(1));
745         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
746         TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
747         tb0.setNestedIndexedBean(new IndexedTestBean());
748         tb1.setNestedIndexedBean(new IndexedTestBean());
749         tb2.setNestedIndexedBean(new IndexedTestBean());
750         tb3.setNestedIndexedBean(new IndexedTestBean());
751         tb4.setNestedIndexedBean(new IndexedTestBean());
752         tb5.setNestedIndexedBean(new IndexedTestBean());
753         BeanWrapper bw = new BeanWrapperImpl(bean);
754         bw.registerCustomEditor(String JavaDoc.class, "array.nestedIndexedBean.array.name", new PropertyEditorSupport JavaDoc() {
755             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
756                 setValue("array" + text);
757             }
758
759             public String JavaDoc getAsText() {
760                 return ((String JavaDoc) getValue()).substring(5);
761             }
762         });
763         bw.registerCustomEditor(String JavaDoc.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport JavaDoc() {
764             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
765                 setValue("list" + text);
766             }
767
768             public String JavaDoc getAsText() {
769                 return ((String JavaDoc) getValue()).substring(4);
770             }
771         });
772         bw.registerCustomEditor(String JavaDoc.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport JavaDoc() {
773             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
774                 setValue("map" + text);
775             }
776
777             public String JavaDoc getAsText() {
778                 return ((String JavaDoc) getValue()).substring(4);
779             }
780         });
781         assertEquals("name0", tb0.getName());
782         assertEquals("name1", tb1.getName());
783         assertEquals("name2", tb2.getName());
784         assertEquals("name3", tb3.getName());
785         assertEquals("name4", tb4.getName());
786         assertEquals("name5", tb5.getName());
787         assertEquals("name0", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
788         assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
789         assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
790         assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
791         assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name"));
792         assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name"));
793
794         MutablePropertyValues pvs = new MutablePropertyValues();
795         pvs.addPropertyValue("array[0].nestedIndexedBean.array[0].name", "name5");
796         pvs.addPropertyValue("array[1].nestedIndexedBean.array[1].name", "name4");
797         pvs.addPropertyValue("list[0].nestedIndexedBean.list[0].name", "name3");
798         pvs.addPropertyValue("list[1].nestedIndexedBean.list[1].name", "name2");
799         pvs.addPropertyValue("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
800         pvs.addPropertyValue("map['key2'].nestedIndexedBean.map[key2].name", "name0");
801         bw.setPropertyValues(pvs);
802         assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
803         assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName());
804         assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
805         assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
806         assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
807         assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
808         assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
809         assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
810         assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
811         assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
812         assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name"));
813         assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name"));
814     }
815
816     public void testNestedIndexedPropertiesWithIndexedCustomEditorForProperty() {
817         IndexedTestBean bean = new IndexedTestBean();
818         TestBean tb0 = bean.getArray()[0];
819         TestBean tb1 = bean.getArray()[1];
820         TestBean tb2 = ((TestBean) bean.getList().get(0));
821         TestBean tb3 = ((TestBean) bean.getList().get(1));
822         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
823         TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
824         tb0.setNestedIndexedBean(new IndexedTestBean());
825         tb1.setNestedIndexedBean(new IndexedTestBean());
826         tb2.setNestedIndexedBean(new IndexedTestBean());
827         tb3.setNestedIndexedBean(new IndexedTestBean());
828         tb4.setNestedIndexedBean(new IndexedTestBean());
829         tb5.setNestedIndexedBean(new IndexedTestBean());
830         BeanWrapper bw = new BeanWrapperImpl(bean);
831         bw.registerCustomEditor(String JavaDoc.class, "array[0].nestedIndexedBean.array[0].name", new PropertyEditorSupport JavaDoc() {
832             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
833                 setValue("array" + text);
834             }
835         });
836         bw.registerCustomEditor(String JavaDoc.class, "list.nestedIndexedBean.list[1].name", new PropertyEditorSupport JavaDoc() {
837             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
838                 setValue("list" + text);
839             }
840         });
841         bw.registerCustomEditor(String JavaDoc.class, "map[key1].nestedIndexedBean.map.name", new PropertyEditorSupport JavaDoc() {
842             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
843                 setValue("map" + text);
844             }
845         });
846
847         MutablePropertyValues pvs = new MutablePropertyValues();
848         pvs.addPropertyValue("array[0].nestedIndexedBean.array[0].name", "name5");
849         pvs.addPropertyValue("array[1].nestedIndexedBean.array[1].name", "name4");
850         pvs.addPropertyValue("list[0].nestedIndexedBean.list[0].name", "name3");
851         pvs.addPropertyValue("list[1].nestedIndexedBean.list[1].name", "name2");
852         pvs.addPropertyValue("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
853         pvs.addPropertyValue("map['key2'].nestedIndexedBean.map[key2].name", "name0");
854         bw.setPropertyValues(pvs);
855         assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
856         assertEquals("name4", tb1.getNestedIndexedBean().getArray()[1].getName());
857         assertEquals("name3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
858         assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
859         assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
860         assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
861     }
862
863     public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
864         IndexedTestBean bean = new IndexedTestBean();
865         BeanWrapper bw = new BeanWrapperImpl(bean);
866         bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport JavaDoc() {
867             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
868                 setValue(new TestBean("array" + text, 99));
869             }
870
871             public String JavaDoc getAsText() {
872                 return ((TestBean) getValue()).getName();
873             }
874         });
875         bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport JavaDoc() {
876             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
877                 setValue(new TestBean("list" + text, 99));
878             }
879
880             public String JavaDoc getAsText() {
881                 return ((TestBean) getValue()).getName();
882             }
883         });
884         bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport JavaDoc() {
885             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
886                 setValue(new TestBean("map" + text, 99));
887             }
888
889             public String JavaDoc getAsText() {
890                 return ((TestBean) getValue()).getName();
891             }
892         });
893
894         MutablePropertyValues pvs = new MutablePropertyValues();
895         pvs.addPropertyValue("array[0]", "a");
896         pvs.addPropertyValue("array[1]", "b");
897         pvs.addPropertyValue("list[0]", "c");
898         pvs.addPropertyValue("list[1]", "d");
899         pvs.addPropertyValue("map[key1]", "e");
900         pvs.addPropertyValue("map['key2']", "f");
901         bw.setPropertyValues(pvs);
902         assertEquals("arraya", bean.getArray()[0].getName());
903         assertEquals("arrayb", bean.getArray()[1].getName());
904         assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
905         assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
906         assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
907         assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
908     }
909
910     public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
911         IndexedTestBean bean = new IndexedTestBean();
912         BeanWrapper bw = new BeanWrapperImpl(bean);
913         bw.registerCustomEditor(TestBean.class, "array[0]", new PropertyEditorSupport JavaDoc() {
914             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
915                 setValue(new TestBean("array0" + text, 99));
916             }
917
918             public String JavaDoc getAsText() {
919                 return ((TestBean) getValue()).getName();
920             }
921         });
922         bw.registerCustomEditor(TestBean.class, "array[1]", new PropertyEditorSupport JavaDoc() {
923             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
924                 setValue(new TestBean("array1" + text, 99));
925             }
926
927             public String JavaDoc getAsText() {
928                 return ((TestBean) getValue()).getName();
929             }
930         });
931         bw.registerCustomEditor(TestBean.class, "list[0]", new PropertyEditorSupport JavaDoc() {
932             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
933                 setValue(new TestBean("list0" + text, 99));
934             }
935
936             public String JavaDoc getAsText() {
937                 return ((TestBean) getValue()).getName();
938             }
939         });
940         bw.registerCustomEditor(TestBean.class, "list[1]", new PropertyEditorSupport JavaDoc() {
941             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
942                 setValue(new TestBean("list1" + text, 99));
943             }
944
945             public String JavaDoc getAsText() {
946                 return ((TestBean) getValue()).getName();
947             }
948         });
949         bw.registerCustomEditor(TestBean.class, "map[key1]", new PropertyEditorSupport JavaDoc() {
950             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
951                 setValue(new TestBean("mapkey1" + text, 99));
952             }
953
954             public String JavaDoc getAsText() {
955                 return ((TestBean) getValue()).getName();
956             }
957         });
958         bw.registerCustomEditor(TestBean.class, "map[key2]", new PropertyEditorSupport JavaDoc() {
959             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
960                 setValue(new TestBean("mapkey2" + text, 99));
961             }
962
963             public String JavaDoc getAsText() {
964                 return ((TestBean) getValue()).getName();
965             }
966         });
967
968         MutablePropertyValues pvs = new MutablePropertyValues();
969         pvs.addPropertyValue("array[0]", "a");
970         pvs.addPropertyValue("array[1]", "b");
971         pvs.addPropertyValue("list[0]", "c");
972         pvs.addPropertyValue("list[1]", "d");
973         pvs.addPropertyValue("map[key1]", "e");
974         pvs.addPropertyValue("map['key2']", "f");
975         bw.setPropertyValues(pvs);
976         assertEquals("array0a", bean.getArray()[0].getName());
977         assertEquals("array1b", bean.getArray()[1].getName());
978         assertEquals("list0c", ((TestBean) bean.getList().get(0)).getName());
979         assertEquals("list1d", ((TestBean) bean.getList().get(1)).getName());
980         assertEquals("mapkey1e", ((TestBean) bean.getMap().get("key1")).getName());
981         assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName());
982     }
983
984     public void testIndexedPropertiesWithListPropertyEditor() {
985         IndexedTestBean bean = new IndexedTestBean();
986         BeanWrapper bw = new BeanWrapperImpl(bean);
987         bw.registerCustomEditor(List JavaDoc.class, "list", new PropertyEditorSupport JavaDoc() {
988             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
989                 List JavaDoc result = new ArrayList JavaDoc();
990                 result.add(new TestBean("list" + text, 99));
991                 setValue(result);
992             }
993         });
994         bw.setPropertyValue("list", "1");
995         assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
996         bw.setPropertyValue("list[0]", "test");
997         assertEquals("test", bean.getList().get(0));
998     }
999
1000    public void testUninitializedArrayPropertyWithCustomEditor() {
1001        IndexedTestBean bean = new IndexedTestBean(false);
1002        BeanWrapper bw = new BeanWrapperImpl(bean);
1003        PropertyEditor JavaDoc pe = new CustomNumberEditor(Integer JavaDoc.class, true);
1004        bw.registerCustomEditor(null, "list.age", pe);
1005        TestBean tb = new TestBean();
1006        bw.setPropertyValue("list", new ArrayList JavaDoc());
1007        bw.setPropertyValue("list[0]", tb);
1008        assertEquals(tb, bean.getList().get(0));
1009        assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
1010        assertEquals(pe, bw.findCustomEditor(null, "list.age"));
1011        assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
1012        assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
1013    }
1014
1015    public void testArrayToArrayConversion() throws PropertyVetoException JavaDoc {
1016        IndexedTestBean tb = new IndexedTestBean();
1017        BeanWrapper bw = new BeanWrapperImpl(tb);
1018        bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport JavaDoc() {
1019            public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
1020                setValue(new TestBean(text, 99));
1021            }
1022        });
1023        bw.setPropertyValue("array", new String JavaDoc[]{"a", "b"});
1024        assertEquals(2, tb.getArray().length);
1025        assertEquals("a", tb.getArray()[0].getName());
1026        assertEquals("b", tb.getArray()[1].getName());
1027    }
1028
1029    public void testArrayToStringConversion() throws PropertyVetoException JavaDoc {
1030        TestBean tb = new TestBean();
1031        BeanWrapper bw = new BeanWrapperImpl(tb);
1032        bw.registerCustomEditor(String JavaDoc.class, new PropertyEditorSupport JavaDoc() {
1033            public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
1034                setValue("-" + text + "-");
1035            }
1036        });
1037        bw.setPropertyValue("name", new String JavaDoc[]{"a", "b"});
1038        assertEquals("-a,b-", tb.getName());
1039    }
1040
1041
1042    private static class TestBeanEditor extends PropertyEditorSupport JavaDoc {
1043
1044        public void setAsText(String JavaDoc text) {
1045            TestBean tb = new TestBean();
1046            StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(text, "_");
1047            tb.setName(st.nextToken());
1048            tb.setAge(Integer.parseInt(st.nextToken()));
1049            setValue(tb);
1050        }
1051    }
1052
1053
1054    private static class ByteArrayBean {
1055
1056        private byte[] array;
1057
1058        public byte[] getArray() {
1059            return array;
1060        }
1061
1062        public void setArray(byte[] array) {
1063            this.array = array;
1064        }
1065    }
1066
1067
1068    private static class CharBean {
1069
1070        private char myChar;
1071
1072        public char getMyChar() {
1073            return myChar;
1074        }
1075
1076        public void setMyChar(char myChar) {
1077            this.myChar = myChar;
1078        }
1079    }
1080
1081}
1082
Popular Tags