KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > BeanWrapperTests


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;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import junit.framework.TestCase;
34 import org.hibernate.FlushMode;
35
36 import org.springframework.beans.support.DerivedFromProtectedBaseBean;
37 import org.springframework.util.StringUtils;
38
39 /**
40  * @author Rod Johnson
41  * @author Juergen Hoeller
42  */

43 public class BeanWrapperTests extends TestCase {
44
45     public void testSetWrappedInstanceOfSameClass() throws Exception JavaDoc {
46         TestBean tb = new TestBean();
47         BeanWrapper bw = new BeanWrapperImpl(tb);
48         assertTrue(bw.isReadableProperty("age"));
49         tb.setAge(11);
50
51         TestBean tb2 = new TestBean();
52         bw.setWrappedInstance(tb2);
53
54         bw.setPropertyValue("age", new Integer JavaDoc(14));
55         assertTrue("2nd changed", tb2.getAge() == 14);
56         assertTrue("1 didn't change", tb.getAge() == 11);
57     }
58
59     public void testIsReadablePropertyNotReadable() {
60         NoRead nr = new NoRead();
61         BeanWrapper bw = new BeanWrapperImpl(nr);
62         assertFalse(bw.isReadableProperty("age"));
63     }
64
65     /**
66      * Shouldn't throw an exception: should just return false
67      */

68     public void testIsReadablePropertyNoSuchProperty() {
69         NoRead nr = new NoRead();
70         BeanWrapper bw = new BeanWrapperImpl(nr);
71         assertFalse(bw.isReadableProperty("xxxxx"));
72     }
73
74     public void testIsReadablePropertyNull() {
75         NoRead nr = new NoRead();
76         BeanWrapper bw = new BeanWrapperImpl(nr);
77         try {
78             bw.isReadableProperty(null);
79             fail("Can't inquire into readability of null property");
80         }
81         catch (IllegalArgumentException JavaDoc ex) {
82             // expected
83
}
84     }
85
86     public void testIsWritablePropertyNull() {
87         NoRead nr = new NoRead();
88         BeanWrapper bw = new BeanWrapperImpl(nr);
89         try {
90             bw.isWritableProperty(null);
91             fail("Can't inquire into writability of null property");
92         }
93         catch (IllegalArgumentException JavaDoc ex) {
94             // expected
95
}
96     }
97
98     public void testReadableAndWritableForIndexedProperties() {
99         BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class);
100
101         assertTrue(bw.isReadableProperty("array"));
102         assertTrue(bw.isReadableProperty("list"));
103         assertTrue(bw.isReadableProperty("set"));
104         assertTrue(bw.isReadableProperty("map"));
105         assertFalse(bw.isReadableProperty("xxx"));
106
107         assertTrue(bw.isWritableProperty("array"));
108         assertTrue(bw.isWritableProperty("list"));
109         assertTrue(bw.isWritableProperty("set"));
110         assertTrue(bw.isWritableProperty("map"));
111         assertFalse(bw.isWritableProperty("xxx"));
112
113         assertTrue(bw.isReadableProperty("array[0]"));
114         assertTrue(bw.isReadableProperty("array[0].name"));
115         assertTrue(bw.isReadableProperty("list[0]"));
116         assertTrue(bw.isReadableProperty("list[0].name"));
117         assertTrue(bw.isReadableProperty("set[0]"));
118         assertTrue(bw.isReadableProperty("set[0].name"));
119         assertTrue(bw.isReadableProperty("map[key1]"));
120         assertTrue(bw.isReadableProperty("map[key1].name"));
121         assertTrue(bw.isReadableProperty("map[key4][0]"));
122         assertTrue(bw.isReadableProperty("map[key4][0].name"));
123         assertTrue(bw.isReadableProperty("map[key4][1]"));
124         assertTrue(bw.isReadableProperty("map[key4][1].name"));
125         assertFalse(bw.isReadableProperty("array[key1]"));
126
127         assertTrue(bw.isWritableProperty("array[0]"));
128         assertTrue(bw.isWritableProperty("array[0].name"));
129         assertTrue(bw.isWritableProperty("list[0]"));
130         assertTrue(bw.isWritableProperty("list[0].name"));
131         assertTrue(bw.isWritableProperty("set[0]"));
132         assertTrue(bw.isWritableProperty("set[0].name"));
133         assertTrue(bw.isWritableProperty("map[key1]"));
134         assertTrue(bw.isWritableProperty("map[key1].name"));
135         assertTrue(bw.isWritableProperty("map[key4][0]"));
136         assertTrue(bw.isWritableProperty("map[key4][0].name"));
137         assertTrue(bw.isWritableProperty("map[key4][1]"));
138         assertTrue(bw.isWritableProperty("map[key4][1].name"));
139         assertFalse(bw.isWritableProperty("array[key1]"));
140     }
141
142     public void testSetWrappedInstanceOfDifferentClass() {
143         ThrowsException tex = new ThrowsException();
144         BeanWrapper bw = new BeanWrapperImpl(tex);
145
146         TestBean tb2 = new TestBean();
147         bw.setWrappedInstance(tb2);
148
149         bw.setPropertyValue("age", new Integer JavaDoc(14));
150         assertTrue("2nd changed", tb2.getAge() == 14);
151     }
152
153     public void testGetterThrowsException() {
154         GetterBean gb = new GetterBean();
155         BeanWrapper bw = new BeanWrapperImpl(gb);
156         bw.setPropertyValue("name", "tom");
157         assertTrue("Set name to tom", gb.getName().equals("tom"));
158     }
159
160     public void testEmptyPropertyValuesSet() {
161         TestBean t = new TestBean();
162         int age = 50;
163         String JavaDoc name = "Tony";
164         t.setAge(age);
165         t.setName(name);
166         try {
167             BeanWrapper bw = new BeanWrapperImpl(t);
168             assertTrue("age is OK", t.getAge() == age);
169             assertTrue("name is OK", name.equals(t.getName()));
170             bw.setPropertyValues(new MutablePropertyValues());
171             // Check its unchanged
172
assertTrue("age is OK", t.getAge() == age);
173             assertTrue("name is OK", name.equals(t.getName()));
174         }
175         catch (BeansException ex) {
176             fail("Shouldn't throw exception when everything is valid");
177         }
178     }
179
180     public void testAllValid() {
181         TestBean t = new TestBean();
182         String JavaDoc newName = "tony";
183         int newAge = 65;
184         String JavaDoc newTouchy = "valid";
185         try {
186             BeanWrapper bw = new BeanWrapperImpl(t);
187             //System.out.println(bw);
188
MutablePropertyValues pvs = new MutablePropertyValues();
189             pvs.addPropertyValue(new PropertyValue("age", new Integer JavaDoc(newAge)));
190             pvs.addPropertyValue(new PropertyValue("name", newName));
191             pvs.addPropertyValue(new PropertyValue("touchy", newTouchy));
192             bw.setPropertyValues(pvs);
193             assertTrue("Validly set property must stick", t.getName().equals(newName));
194             assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy));
195             assertTrue("Validly set property must stick", t.getAge() == newAge);
196         }
197         catch (BeansException ex) {
198             fail("Shouldn't throw exception when everything is valid");
199         }
200     }
201
202     public void testBeanWrapperUpdates() {
203         TestBean t = new TestBean();
204         int newAge = 33;
205         try {
206             BeanWrapper bw = new BeanWrapperImpl(t);
207             t.setAge(newAge);
208             Object JavaDoc bwAge = bw.getPropertyValue("age");
209             assertTrue("Age is an integer", bwAge instanceof Integer JavaDoc);
210             int bwi = ((Integer JavaDoc) bwAge).intValue();
211             assertTrue("Bean wrapper must pick up changes", bwi == newAge);
212         }
213         catch (Exception JavaDoc ex) {
214             fail("Shouldn't throw exception when everything is valid");
215         }
216     }
217
218     public void testValidNullUpdate() {
219         TestBean t = new TestBean();
220         t.setName("Frank"); // we need to change it back
221
t.setSpouse(t);
222         BeanWrapper bw = new BeanWrapperImpl(t);
223         assertTrue("name is not null to start off", t.getName() != null);
224         bw.setPropertyValue("name", null);
225         assertTrue("name is now null", t.getName() == null);
226         // now test with non-string
227
assertTrue("spouse is not null to start off", t.getSpouse() != null);
228         bw.setPropertyValue("spouse", null);
229         assertTrue("spouse is now null", t.getSpouse() == null);
230     }
231
232     public void testIgnoringIndexedProperty() {
233         MutablePropertyValues values = new MutablePropertyValues();
234         values.addPropertyValue("toBeIgnored[0]", new Integer JavaDoc(42));
235         BeanWrapper wrapper = new BeanWrapperImpl(new Object JavaDoc());
236         wrapper.setPropertyValues(values, true);
237     }
238
239     public void testBooleanObject() {
240         BooleanTestBean tb = new BooleanTestBean();
241         BeanWrapper bw = new BeanWrapperImpl(tb);
242
243         try {
244             bw.setPropertyValue("bool2", "true");
245         }
246         catch (BeansException ex) {
247             fail("Should not throw BeansException: " + ex.getMessage());
248         }
249         assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
250         assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
251
252         try {
253             bw.setPropertyValue("bool2", "false");
254         }
255         catch (BeansException ex) {
256             fail("Should not throw BeansException: " + ex.getMessage());
257         }
258         assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
259         assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
260
261     }
262
263     public void testNumberObjects() {
264         NumberTestBean tb = new NumberTestBean();
265         BeanWrapper bw = new BeanWrapperImpl(tb);
266
267         try {
268             bw.setPropertyValue("short2", "2");
269             bw.setPropertyValue("int2", "8");
270             bw.setPropertyValue("long2", "6");
271             bw.setPropertyValue("bigInteger", "3");
272             bw.setPropertyValue("float2", "8.1");
273             bw.setPropertyValue("double2", "6.1");
274             bw.setPropertyValue("bigDecimal", "4.0");
275         }
276         catch (BeansException ex) {
277             fail("Should not throw BeansException: " + ex.getMessage());
278         }
279
280         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(bw.getPropertyValue("short2")));
281         assertTrue("Correct short2 value", new Short JavaDoc("2").equals(tb.getShort2()));
282         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(bw.getPropertyValue("int2")));
283         assertTrue("Correct int2 value", new Integer JavaDoc("8").equals(tb.getInt2()));
284         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(bw.getPropertyValue("long2")));
285         assertTrue("Correct long2 value", new Long JavaDoc("6").equals(tb.getLong2()));
286         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(bw.getPropertyValue("bigInteger")));
287         assertTrue("Correct bigInteger value", new BigInteger JavaDoc("3").equals(tb.getBigInteger()));
288         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(bw.getPropertyValue("float2")));
289         assertTrue("Correct float2 value", new Float JavaDoc("8.1").equals(tb.getFloat2()));
290         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(bw.getPropertyValue("double2")));
291         assertTrue("Correct double2 value", new Double JavaDoc("6.1").equals(tb.getDouble2()));
292         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.0").equals(bw.getPropertyValue("bigDecimal")));
293         assertTrue("Correct bigDecimal value", new BigDecimal JavaDoc("4.0").equals(tb.getBigDecimal()));
294     }
295
296     public void testEnum() {
297         EnumTest et = new EnumTest();
298         BeanWrapper bw = new BeanWrapperImpl(et);
299
300         bw.setPropertyValue("flushMode", "NEVER");
301         assertEquals(FlushMode.NEVER, et.getFlushMode());
302
303         try {
304             bw.setPropertyValue("flushMode", "EVER");
305             fail("Should have thrown TypeMismatchException");
306         }
307         catch (TypeMismatchException ex) {
308             // expected
309
}
310     }
311
312     public void testPropertiesProperty() throws Exception JavaDoc {
313         PropsTest pt = new PropsTest();
314         BeanWrapper bw = new BeanWrapperImpl(pt);
315         bw.setPropertyValue("name", "ptest");
316
317         // Note format...
318
String JavaDoc ps = "peace=war\nfreedom=slavery";
319         bw.setPropertyValue("properties", ps);
320
321         assertTrue("name was set", pt.name.equals("ptest"));
322         assertTrue("props non null", pt.props != null);
323         String JavaDoc freedomVal = pt.props.getProperty("freedom");
324         String JavaDoc peaceVal = pt.props.getProperty("peace");
325         assertTrue("peace==war", peaceVal.equals("war"));
326         assertTrue("Freedom==slavery", freedomVal.equals("slavery"));
327     }
328
329     public void testStringArrayProperty() throws Exception JavaDoc {
330         PropsTest pt = new PropsTest();
331         BeanWrapper bw = new BeanWrapperImpl(pt);
332
333         bw.setPropertyValue("stringArray", new String JavaDoc[]{"foo", "fi", "fi", "fum"});
334         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
335         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
336                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
337
338         List JavaDoc list = new ArrayList JavaDoc();
339         list.add("foo");
340         list.add("fi");
341         list.add("fi");
342         list.add("fum");
343         bw.setPropertyValue("stringArray", list);
344         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
345         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
346                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
347
348         Set JavaDoc set = new HashSet JavaDoc();
349         set.add("foo");
350         set.add("fi");
351         set.add("fum");
352         bw.setPropertyValue("stringArray", set);
353         assertTrue("stringArray length = 3", pt.stringArray.length == 3);
354         List JavaDoc result = Arrays.asList(pt.stringArray);
355         assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));
356
357         bw.setPropertyValue("stringArray", "one");
358         assertTrue("stringArray length = 1", pt.stringArray.length == 1);
359         assertTrue("stringArray elt is ok", pt.stringArray[0].equals("one"));
360
361         bw.setPropertyValue("stringArray", "foo,fi,fi,fum");
362         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
363         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
364                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
365     }
366
367     public void testStringArrayPropertyWithCustomStringEditor() throws Exception JavaDoc {
368         PropsTest pt = new PropsTest();
369         BeanWrapper bw = new BeanWrapperImpl(pt);
370         bw.registerCustomEditor(String JavaDoc.class, "stringArray", new PropertyEditorSupport JavaDoc() {
371             public void setAsText(String JavaDoc text) {
372                 setValue(text.substring(1));
373             }
374         });
375
376         bw.setPropertyValue("stringArray", new String JavaDoc[] {"4foo", "7fi", "6fi", "5fum"});
377         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
378         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
379                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
380
381         List JavaDoc list = new ArrayList JavaDoc();
382         list.add("4foo");
383         list.add("7fi");
384         list.add("6fi");
385         list.add("5fum");
386         bw.setPropertyValue("stringArray", list);
387         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
388         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
389                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
390
391         Set JavaDoc set = new HashSet JavaDoc();
392         set.add("4foo");
393         set.add("7fi");
394         set.add("6fum");
395         bw.setPropertyValue("stringArray", set);
396         assertTrue("stringArray length = 3", pt.stringArray.length == 3);
397         List JavaDoc result = Arrays.asList(pt.stringArray);
398         assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));
399
400         bw.setPropertyValue("stringArray", "8one");
401         assertTrue("stringArray length = 1", pt.stringArray.length == 1);
402         assertTrue("correct values", pt.stringArray[0].equals("one"));
403
404         bw.setPropertyValue("stringArray", "1foo,3fi,2fi,1fum");
405         assertTrue("stringArray length = 4", pt.stringArray.length == 4);
406         assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") &&
407                 pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));
408     }
409
410     public void testStringArrayPropertyWithCustomStringArrayEditor() throws Exception JavaDoc {
411         TestBean tb = new TestBean();
412         BeanWrapper bw = new BeanWrapperImpl(tb);
413         bw.registerCustomEditor(String JavaDoc.class, "name", new PropertyEditorSupport JavaDoc() {
414             public void setValue(Object JavaDoc value) {
415                 if (value instanceof String JavaDoc[]) {
416                     setValue(StringUtils.arrayToDelimitedString(((String JavaDoc[]) value), "-"));
417                 }
418                 else {
419                     super.setValue(value);
420                 }
421             }
422         });
423         bw.setPropertyValue("name", new String JavaDoc[] {"a1", "b2"});
424         assertEquals("a1-b2", tb.getName());
425     }
426
427     public void testIntArrayProperty() {
428         PropsTest pt = new PropsTest();
429         BeanWrapper bw = new BeanWrapperImpl(pt);
430
431         bw.setPropertyValue("intArray", new int[]{4, 5, 2, 3});
432         assertTrue("intArray length = 4", pt.intArray.length == 4);
433         assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 &&
434                 pt.intArray[2] == 2 && pt.intArray[3] == 3);
435
436         bw.setPropertyValue("intArray", new String JavaDoc[]{"4", "5", "2", "3"});
437         assertTrue("intArray length = 4", pt.intArray.length == 4);
438         assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 &&
439                 pt.intArray[2] == 2 && pt.intArray[3] == 3);
440
441         List JavaDoc list = new ArrayList JavaDoc();
442         list.add(new Integer JavaDoc(4));
443         list.add("5");
444         list.add(new Integer JavaDoc(2));
445         list.add("3");
446         bw.setPropertyValue("intArray", list);
447         assertTrue("intArray length = 4", pt.intArray.length == 4);
448         assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 &&
449                 pt.intArray[2] == 2 && pt.intArray[3] == 3);
450
451         Set JavaDoc set = new HashSet JavaDoc();
452         set.add("4");
453         set.add(new Integer JavaDoc(5));
454         set.add("3");
455         bw.setPropertyValue("intArray", set);
456         assertTrue("intArray length = 3", pt.intArray.length == 3);
457         List JavaDoc result = new ArrayList JavaDoc();
458         result.add(new Integer JavaDoc(pt.intArray[0]));
459         result.add(new Integer JavaDoc(pt.intArray[1]));
460         result.add(new Integer JavaDoc(pt.intArray[2]));
461         assertTrue("correct values", result.contains(new Integer JavaDoc(4)) && result.contains(new Integer JavaDoc(5)) &&
462                 result.contains(new Integer JavaDoc(3)));
463
464         bw.setPropertyValue("intArray", new Integer JavaDoc[]{new Integer JavaDoc(1)});
465         assertTrue("intArray length = 4", pt.intArray.length == 1);
466         assertTrue("correct values", pt.intArray[0] == 1);
467
468         bw.setPropertyValue("intArray", new Integer JavaDoc(1));
469         assertTrue("intArray length = 4", pt.intArray.length == 1);
470         assertTrue("correct values", pt.intArray[0] == 1);
471
472         bw.setPropertyValue("intArray", new String JavaDoc[]{"1"});
473         assertTrue("intArray length = 4", pt.intArray.length == 1);
474         assertTrue("correct values", pt.intArray[0] == 1);
475
476         bw.setPropertyValue("intArray", "1");
477         assertTrue("intArray length = 4", pt.intArray.length == 1);
478         assertTrue("correct values", pt.intArray[0] == 1);
479     }
480
481     public void testIntArrayPropertyWithCustomEditor() {
482         PropsTest pt = new PropsTest();
483         BeanWrapper bw = new BeanWrapperImpl(pt);
484         bw.registerCustomEditor(int.class, new PropertyEditorSupport JavaDoc() {
485             public void setAsText(String JavaDoc text) {
486                 setValue(new Integer JavaDoc(Integer.parseInt(text) + 1));
487             }
488         });
489
490         bw.setPropertyValue("intArray", new int[]{4, 5, 2, 3});
491         assertTrue("intArray length = 4", pt.intArray.length == 4);
492         assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 &&
493                 pt.intArray[2] == 2 && pt.intArray[3] == 3);
494
495         bw.setPropertyValue("intArray", new String JavaDoc[]{"3", "4", "1", "2"});
496         assertTrue("intArray length = 4", pt.intArray.length == 4);
497         assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 &&
498                 pt.intArray[2] == 2 && pt.intArray[3] == 3);
499
500         bw.setPropertyValue("intArray", new Integer JavaDoc(1));
501         assertTrue("intArray length = 4", pt.intArray.length == 1);
502         assertTrue("correct values", pt.intArray[0] == 1);
503
504         bw.setPropertyValue("intArray", new String JavaDoc[]{"0"});
505         assertTrue("intArray length = 4", pt.intArray.length == 1);
506         assertTrue("correct values", pt.intArray[0] == 1);
507
508         bw.setPropertyValue("intArray", "0");
509         assertTrue("intArray length = 4", pt.intArray.length == 1);
510         assertTrue("correct values", pt.intArray[0] == 1);
511     }
512
513     public void testIndividualAllValid() {
514         TestBean t = new TestBean();
515         String JavaDoc newName = "tony";
516         int newAge = 65;
517         String JavaDoc newTouchy = "valid";
518         try {
519             BeanWrapper bw = new BeanWrapperImpl(t);
520             bw.setPropertyValue("age", new Integer JavaDoc(newAge));
521             bw.setPropertyValue(new PropertyValue("name", newName));
522             bw.setPropertyValue(new PropertyValue("touchy", newTouchy));
523             assertTrue("Validly set property must stick", t.getName().equals(newName));
524             assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy));
525             assertTrue("Validly set property must stick", t.getAge() == newAge);
526         }
527         catch (BeansException ex) {
528             fail("Shouldn't throw exception when everything is valid");
529         }
530     }
531
532     public void test2Invalid() {
533         TestBean t = new TestBean();
534         String JavaDoc newName = "tony";
535         String JavaDoc invalidTouchy = ".valid";
536         try {
537             BeanWrapper bw = new BeanWrapperImpl(t);
538             //System.out.println(bw);
539
MutablePropertyValues pvs = new MutablePropertyValues();
540             pvs.addPropertyValue(new PropertyValue("age", "foobar"));
541             pvs.addPropertyValue(new PropertyValue("name", newName));
542             pvs.addPropertyValue(new PropertyValue("touchy", invalidTouchy));
543             bw.setPropertyValues(pvs);
544             fail("Should throw exception when everything is valid");
545         }
546         catch (PropertyAccessExceptionsException ex) {
547             assertTrue("Must contain 2 exceptions", ex.getExceptionCount() == 2);
548             // Test validly set property matches
549
assertTrue("Validly set property must stick", t.getName().equals(newName));
550             assertTrue("Invalidly set property must retain old value", t.getAge() == 0);
551             assertTrue("New value of dodgy setter must be available through exception",
552                     ex.getPropertyAccessException("touchy").getPropertyChangeEvent().getNewValue().equals(invalidTouchy));
553         }
554         catch (Exception JavaDoc ex) {
555             fail("Shouldn't throw exception other than pvee");
556         }
557     }
558
559     public void testTypeMismatch() {
560         TestBean t = new TestBean();
561         try {
562             BeanWrapper bw = new BeanWrapperImpl(t);
563             bw.setPropertyValue("age", "foobar");
564             fail("Should throw exception on type mismatch");
565         }
566         catch (TypeMismatchException ex) {
567             // expected
568
}
569         catch (Exception JavaDoc ex) {
570             fail("Shouldn't throw exception other than Type mismatch");
571         }
572     }
573
574     public void testEmptyValueForPrimitiveProperty() {
575         TestBean t = new TestBean();
576         try {
577             BeanWrapper bw = new BeanWrapperImpl(t);
578             bw.setPropertyValue("age", "");
579             fail("Should throw exception on type mismatch");
580         }
581         catch (TypeMismatchException ex) {
582             // expected
583
}
584         catch (Exception JavaDoc ex) {
585             fail("Shouldn't throw exception other than Type mismatch");
586         }
587     }
588
589     public void testSetPropertyValuesIgnoresInvalidNestedOnRequest() {
590         ITestBean rod = new TestBean();
591         MutablePropertyValues pvs = new MutablePropertyValues();
592         pvs.addPropertyValue(new PropertyValue("name", "rod"));
593         pvs.addPropertyValue(new PropertyValue("graceful.rubbish", "tony"));
594         pvs.addPropertyValue(new PropertyValue("more.garbage", new Object JavaDoc()));
595         BeanWrapper bw = new BeanWrapperImpl(rod);
596         bw.setPropertyValues(pvs, true);
597         assertTrue("Set valid and ignored invalid", rod.getName().equals("rod"));
598         try {
599             // Don't ignore: should fail
600
bw.setPropertyValues(pvs, false);
601             fail("Shouldn't have ignored invalid updates");
602         }
603         catch (NotWritablePropertyException ex) {
604             // OK: but which exception??
605
}
606     }
607
608     public void testGetNestedProperty() {
609         ITestBean rod = new TestBean("rod", 31);
610         ITestBean kerry = new TestBean("kerry", 35);
611         rod.setSpouse(kerry);
612         kerry.setSpouse(rod);
613         BeanWrapper bw = new BeanWrapperImpl(rod);
614         Integer JavaDoc KA = (Integer JavaDoc) bw.getPropertyValue("spouse.age");
615         assertTrue("kerry is 35", KA.intValue() == 35);
616         Integer JavaDoc RA = (Integer JavaDoc) bw.getPropertyValue("spouse.spouse.age");
617         assertTrue("rod is 31, not" + RA, RA.intValue() == 31);
618         ITestBean spousesSpouse = (ITestBean) bw.getPropertyValue("spouse.spouse");
619         assertTrue("spousesSpouse = initial point", rod == spousesSpouse);
620     }
621
622     public void testGetNestedPropertyNullValue() throws Exception JavaDoc {
623         ITestBean rod = new TestBean("rod", 31);
624         ITestBean kerry = new TestBean("kerry", 35);
625         rod.setSpouse(kerry);
626
627         BeanWrapper bw = new BeanWrapperImpl(rod);
628         try {
629             bw.getPropertyValue("spouse.spouse.age");
630             fail("Shouldn't have succeded with null path");
631         }
632         catch (NullValueInNestedPathException ex) {
633             // ok
634
assertTrue("it was the spouse.spouse property that was null, not " + ex.getPropertyName(),
635                     ex.getPropertyName().equals("spouse.spouse"));
636         }
637     }
638
639     public void testSetNestedProperty() throws Exception JavaDoc {
640         ITestBean rod = new TestBean("rod", 31);
641         ITestBean kerry = new TestBean("kerry", 0);
642
643         BeanWrapper bw = new BeanWrapperImpl(rod);
644         bw.setPropertyValue("spouse", kerry);
645
646         assertTrue("nested set worked", rod.getSpouse() == kerry);
647         assertTrue("no back relation", kerry.getSpouse() == null);
648         bw.setPropertyValue(new PropertyValue("spouse.spouse", rod));
649         assertTrue("nested set worked", kerry.getSpouse() == rod);
650         assertTrue("kerry age not set", kerry.getAge() == 0);
651         bw.setPropertyValue(new PropertyValue("spouse.age", new Integer JavaDoc(35)));
652         assertTrue("Set primitive on spouse", kerry.getAge() == 35);
653     }
654
655     public void testSetNestedPropertyNullValue() throws Exception JavaDoc {
656         ITestBean rod = new TestBean("rod", 31);
657         BeanWrapper bw = new BeanWrapperImpl(rod);
658         try {
659             bw.setPropertyValue("spouse.age", new Integer JavaDoc(31));
660             fail("Shouldn't have succeeded with null path");
661         }
662         catch (NullValueInNestedPathException ex) {
663             // expected
664
assertTrue("it was the spouse property that was null, not " + ex.getPropertyName(),
665                     ex.getPropertyName().equals("spouse"));
666         }
667     }
668
669     public void testSetNestedPropertyPolymorphic() throws Exception JavaDoc {
670         ITestBean rod = new TestBean("rod", 31);
671         ITestBean kerry = new Employee();
672
673         BeanWrapper bw = new BeanWrapperImpl(rod);
674         bw.setPropertyValue("spouse", kerry);
675         bw.setPropertyValue("spouse.age", new Integer JavaDoc(35));
676         bw.setPropertyValue("spouse.name", "Kerry");
677         bw.setPropertyValue("spouse.company", "Lewisham");
678         assertTrue("kerry name is Kerry", kerry.getName().equals("Kerry"));
679
680         assertTrue("nested set worked", rod.getSpouse() == kerry);
681         assertTrue("no back relation", kerry.getSpouse() == null);
682         bw.setPropertyValue(new PropertyValue("spouse.spouse", rod));
683         assertTrue("nested set worked", kerry.getSpouse() == rod);
684
685         BeanWrapper kbw = new BeanWrapperImpl(kerry);
686         assertTrue("spouse.spouse.spouse.spouse.company=Lewisham",
687                 "Lewisham".equals(kbw.getPropertyValue("spouse.spouse.spouse.spouse.company")));
688     }
689
690     public void testNewWrappedInstancePropertyValuesGet() {
691         BeanWrapper bw = new BeanWrapperImpl();
692
693         TestBean t = new TestBean("Tony", 50);
694         bw.setWrappedInstance(t);
695         assertEquals("Bean wrapper returns wrong property value",
696                 new Integer JavaDoc(t.getAge()), bw.getPropertyValue("age"));
697
698         TestBean u = new TestBean("Udo", 30);
699         bw.setWrappedInstance(u);
700         assertEquals("Bean wrapper returns cached property value",
701                 new Integer JavaDoc(u.getAge()), bw.getPropertyValue("age"));
702     }
703
704     public void testNewWrappedInstanceNestedPropertyValuesGet() {
705         BeanWrapper bw = new BeanWrapperImpl();
706
707         TestBean t = new TestBean("Tony", 50);
708         t.setSpouse(new TestBean("Sue", 40));
709         bw.setWrappedInstance(t);
710         assertEquals("Bean wrapper returns wrong nested property value",
711                 new Integer JavaDoc(t.getSpouse().getAge()), bw.getPropertyValue("spouse.age"));
712
713         TestBean u = new TestBean("Udo", 30);
714         u.setSpouse(new TestBean("Vera", 20));
715         bw.setWrappedInstance(u);
716         assertEquals("Bean wrapper returns cached nested property value",
717                 new Integer JavaDoc(u.getSpouse().getAge()), bw.getPropertyValue("spouse.age"));
718     }
719
720     public void testNullObject() {
721         try {
722             BeanWrapper bw = new BeanWrapperImpl((Object JavaDoc) null);
723             fail("Must throw an exception when constructed with null object");
724         }
725         catch (IllegalArgumentException JavaDoc ex) {
726             // expected
727
}
728     }
729
730     public void testNestedProperties() {
731         String JavaDoc doctorCompany = "";
732         String JavaDoc lawyerCompany = "Dr. Sueem";
733         TestBean tb = new TestBean();
734         BeanWrapper bw = new BeanWrapperImpl(tb);
735         bw.setPropertyValue("doctor.company", doctorCompany);
736         bw.setPropertyValue("lawyer.company", lawyerCompany);
737         assertEquals(doctorCompany, tb.getDoctor().getCompany());
738         assertEquals(lawyerCompany, tb.getLawyer().getCompany());
739     }
740
741     public void testIndexedProperties() {
742         IndexedTestBean bean = new IndexedTestBean();
743         BeanWrapper bw = new BeanWrapperImpl(bean);
744         TestBean tb0 = bean.getArray()[0];
745         TestBean tb1 = bean.getArray()[1];
746         TestBean tb2 = ((TestBean) bean.getList().get(0));
747         TestBean tb3 = ((TestBean) bean.getList().get(1));
748         TestBean tb6 = ((TestBean) bean.getSet().toArray()[0]);
749         TestBean tb7 = ((TestBean) bean.getSet().toArray()[1]);
750         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
751         TestBean tb5 = ((TestBean) bean.getMap().get("key.3"));
752         assertEquals("name0", tb0.getName());
753         assertEquals("name1", tb1.getName());
754         assertEquals("name2", tb2.getName());
755         assertEquals("name3", tb3.getName());
756         assertEquals("name6", tb6.getName());
757         assertEquals("name7", tb7.getName());
758         assertEquals("name4", tb4.getName());
759         assertEquals("name5", tb5.getName());
760         assertEquals("name0", bw.getPropertyValue("array[0].name"));
761         assertEquals("name1", bw.getPropertyValue("array[1].name"));
762         assertEquals("name2", bw.getPropertyValue("list[0].name"));
763         assertEquals("name3", bw.getPropertyValue("list[1].name"));
764         assertEquals("name6", bw.getPropertyValue("set[0].name"));
765         assertEquals("name7", bw.getPropertyValue("set[1].name"));
766         assertEquals("name4", bw.getPropertyValue("map[key1].name"));
767         assertEquals("name5", bw.getPropertyValue("map[key.3].name"));
768         assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
769         assertEquals("name5", bw.getPropertyValue("map[\"key.3\"].name"));
770         assertEquals("nameX", bw.getPropertyValue("map[key4][0].name"));
771         assertEquals("nameY", bw.getPropertyValue("map[key4][1].name"));
772
773         MutablePropertyValues pvs = new MutablePropertyValues();
774         pvs.addPropertyValue("array[0].name", "name5");
775         pvs.addPropertyValue("array[1].name", "name4");
776         pvs.addPropertyValue("list[0].name", "name3");
777         pvs.addPropertyValue("list[1].name", "name2");
778         pvs.addPropertyValue("set[0].name", "name8");
779         pvs.addPropertyValue("set[1].name", "name9");
780         pvs.addPropertyValue("map[key1].name", "name1");
781         pvs.addPropertyValue("map['key.3'].name", "name0");
782         pvs.addPropertyValue("map[key4][0].name", "nameA");
783         pvs.addPropertyValue("map[key4][1].name", "nameB");
784         bw.setPropertyValues(pvs);
785         assertEquals("name5", tb0.getName());
786         assertEquals("name4", tb1.getName());
787         assertEquals("name3", tb2.getName());
788         assertEquals("name2", tb3.getName());
789         assertEquals("name1", tb4.getName());
790         assertEquals("name0", tb5.getName());
791         assertEquals("name5", bw.getPropertyValue("array[0].name"));
792         assertEquals("name4", bw.getPropertyValue("array[1].name"));
793         assertEquals("name3", bw.getPropertyValue("list[0].name"));
794         assertEquals("name2", bw.getPropertyValue("list[1].name"));
795         assertEquals("name8", bw.getPropertyValue("set[0].name"));
796         assertEquals("name9", bw.getPropertyValue("set[1].name"));
797         assertEquals("name1", bw.getPropertyValue("map[\"key1\"].name"));
798         assertEquals("name0", bw.getPropertyValue("map['key.3'].name"));
799         assertEquals("nameA", bw.getPropertyValue("map[key4][0].name"));
800         assertEquals("nameB", bw.getPropertyValue("map[key4][1].name"));
801     }
802
803     public void testIndexedPropertiesWithDirectAccess() {
804         IndexedTestBean bean = new IndexedTestBean();
805         BeanWrapper bw = new BeanWrapperImpl(bean);
806         TestBean tb0 = bean.getArray()[0];
807         TestBean tb1 = bean.getArray()[1];
808         TestBean tb2 = ((TestBean) bean.getList().get(0));
809         TestBean tb3 = ((TestBean) bean.getList().get(1));
810         TestBean tb6 = ((TestBean) bean.getSet().toArray()[0]);
811         TestBean tb7 = ((TestBean) bean.getSet().toArray()[1]);
812         TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
813         TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
814         assertEquals(tb0, bw.getPropertyValue("array[0]"));
815         assertEquals(tb1, bw.getPropertyValue("array[1]"));
816         assertEquals(tb2, bw.getPropertyValue("list[0]"));
817         assertEquals(tb3, bw.getPropertyValue("list[1]"));
818         assertEquals(tb6, bw.getPropertyValue("set[0]"));
819         assertEquals(tb7, bw.getPropertyValue("set[1]"));
820         assertEquals(tb4, bw.getPropertyValue("map[key1]"));
821         assertEquals(tb5, bw.getPropertyValue("map[key2]"));
822         assertEquals(tb4, bw.getPropertyValue("map['key1']"));
823         assertEquals(tb5, bw.getPropertyValue("map[\"key2\"]"));
824
825         MutablePropertyValues pvs = new MutablePropertyValues();
826         pvs.addPropertyValue("array[0]", tb5);
827         pvs.addPropertyValue("array[1]", tb4);
828         pvs.addPropertyValue("list[0]", tb3);
829         pvs.addPropertyValue("list[1]", tb2);
830         pvs.addPropertyValue("list[2]", tb0);
831         pvs.addPropertyValue("list[4]", tb1);
832         pvs.addPropertyValue("map[key1]", tb1);
833         pvs.addPropertyValue("map['key2']", tb0);
834         pvs.addPropertyValue("map[key5]", tb4);
835         pvs.addPropertyValue("map['key9']", tb5);
836         bw.setPropertyValues(pvs);
837         assertEquals(tb5, bean.getArray()[0]);
838         assertEquals(tb4, bean.getArray()[1]);
839         assertEquals(tb3, ((TestBean) bean.getList().get(0)));
840         assertEquals(tb2, ((TestBean) bean.getList().get(1)));
841         assertEquals(tb0, ((TestBean) bean.getList().get(2)));
842         assertEquals(null, ((TestBean) bean.getList().get(3)));
843         assertEquals(tb1, ((TestBean) bean.getList().get(4)));
844         assertEquals(tb1, ((TestBean) bean.getMap().get("key1")));
845         assertEquals(tb0, ((TestBean) bean.getMap().get("key2")));
846         assertEquals(tb4, ((TestBean) bean.getMap().get("key5")));
847         assertEquals(tb5, ((TestBean) bean.getMap().get("key9")));
848         assertEquals(tb5, bw.getPropertyValue("array[0]"));
849         assertEquals(tb4, bw.getPropertyValue("array[1]"));
850         assertEquals(tb3, bw.getPropertyValue("list[0]"));
851         assertEquals(tb2, bw.getPropertyValue("list[1]"));
852         assertEquals(tb0, bw.getPropertyValue("list[2]"));
853         assertEquals(null, bw.getPropertyValue("list[3]"));
854         assertEquals(tb1, bw.getPropertyValue("list[4]"));
855         assertEquals(tb1, bw.getPropertyValue("map[\"key1\"]"));
856         assertEquals(tb0, bw.getPropertyValue("map['key2']"));
857         assertEquals(tb4, bw.getPropertyValue("map[\"key5\"]"));
858         assertEquals(tb5, bw.getPropertyValue("map['key9']"));
859     }
860
861     public void testPrimitiveArray() {
862         PrimitiveArrayBean tb = new PrimitiveArrayBean();
863         BeanWrapper bw = new BeanWrapperImpl(tb);
864         bw.setPropertyValue("array", new String JavaDoc[]{"1", "2"});
865         assertEquals(2, tb.getArray().length);
866         assertEquals(1, tb.getArray()[0]);
867         assertEquals(2, tb.getArray()[1]);
868     }
869
870     public void testPropertiesInProtectedBaseBean() {
871         DerivedFromProtectedBaseBean bean = new DerivedFromProtectedBaseBean();
872         BeanWrapper bw = new BeanWrapperImpl(bean);
873         bw.setPropertyValue("someProperty", "someValue");
874         assertEquals("someValue", bw.getPropertyValue("someProperty"));
875         assertEquals("someValue", bean.getSomeProperty());
876     }
877
878     public void testErrorMessageOfNestedProperty() {
879         ITestBean parent = new TestBean();
880         ITestBean child = new DifferentTestBean();
881         child.setName("test");
882         parent.setSpouse(child);
883         BeanWrapper bw = new BeanWrapperImpl(parent);
884         try {
885             bw.getPropertyValue("spouse.bla");
886         }
887         catch (NotReadablePropertyException ex) {
888             assertTrue(ex.getMessage().indexOf(TestBean.class.getName()) != -1);
889         }
890     }
891
892     public void testMatchingCollections() {
893         IndexedTestBean tb = new IndexedTestBean();
894         BeanWrapper bw = new BeanWrapperImpl(tb);
895         Collection JavaDoc coll = new HashSet JavaDoc();
896         coll.add("coll1");
897         bw.setPropertyValue("collection", coll);
898         Set JavaDoc set = new HashSet JavaDoc();
899         set.add("set1");
900         bw.setPropertyValue("set", set);
901         SortedSet JavaDoc sortedSet = new TreeSet JavaDoc();
902         sortedSet.add("sortedSet1");
903         bw.setPropertyValue("sortedSet", sortedSet);
904         List JavaDoc list = new LinkedList JavaDoc();
905         list.add("list1");
906         bw.setPropertyValue("list", list);
907         assertEquals(coll, tb.getCollection());
908         assertEquals(set, tb.getSet());
909         assertEquals(sortedSet, tb.getSortedSet());
910         assertEquals(list, tb.getList());
911     }
912
913     public void testNonMatchingCollections() {
914         IndexedTestBean tb = new IndexedTestBean();
915         BeanWrapper bw = new BeanWrapperImpl(tb);
916         Collection JavaDoc coll = new ArrayList JavaDoc();
917         coll.add("coll1");
918         bw.setPropertyValue("collection", coll);
919         List JavaDoc set = new LinkedList JavaDoc();
920         set.add("set1");
921         bw.setPropertyValue("set", set);
922         List JavaDoc sortedSet = new ArrayList JavaDoc();
923         sortedSet.add("sortedSet1");
924         bw.setPropertyValue("sortedSet", sortedSet);
925         Set JavaDoc list = new HashSet JavaDoc();
926         list.add("list1");
927         bw.setPropertyValue("list", list);
928         assertEquals(1, tb.getCollection().size());
929         assertTrue(tb.getCollection().containsAll(coll));
930         assertEquals(1, tb.getSet().size());
931         assertTrue(tb.getSet().containsAll(set));
932         assertEquals(1, tb.getSortedSet().size());
933         assertTrue(tb.getSortedSet().containsAll(sortedSet));
934         assertEquals(1, tb.getList().size());
935         assertTrue(tb.getList().containsAll(list));
936     }
937
938     public void testCollectionsWithArrayValues() {
939         IndexedTestBean tb = new IndexedTestBean();
940         BeanWrapper bw = new BeanWrapperImpl(tb);
941         Collection JavaDoc coll = new HashSet JavaDoc();
942         coll.add("coll1");
943         bw.setPropertyValue("collection", coll.toArray());
944         List JavaDoc set = new LinkedList JavaDoc();
945         set.add("set1");
946         bw.setPropertyValue("set", set.toArray());
947         List JavaDoc sortedSet = new ArrayList JavaDoc();
948         sortedSet.add("sortedSet1");
949         bw.setPropertyValue("sortedSet", sortedSet.toArray());
950         Set JavaDoc list = new HashSet JavaDoc();
951         list.add("list1");
952         bw.setPropertyValue("list", list.toArray());
953         assertEquals(1, tb.getCollection().size());
954         assertTrue(tb.getCollection().containsAll(coll));
955         assertEquals(1, tb.getSet().size());
956         assertTrue(tb.getSet().containsAll(set));
957         assertEquals(1, tb.getSortedSet().size());
958         assertTrue(tb.getSortedSet().containsAll(sortedSet));
959         assertEquals(1, tb.getList().size());
960         assertTrue(tb.getList().containsAll(list));
961     }
962
963     public void testCollectionsWithIntArrayValues() {
964         IndexedTestBean tb = new IndexedTestBean();
965         BeanWrapper bw = new BeanWrapperImpl(tb);
966         Collection JavaDoc coll = new HashSet JavaDoc();
967         coll.add(new Integer JavaDoc(0));
968         bw.setPropertyValue("collection", new int[] {0});
969         List JavaDoc set = new LinkedList JavaDoc();
970         set.add(new Integer JavaDoc(1));
971         bw.setPropertyValue("set", new int[] {1});
972         List JavaDoc sortedSet = new ArrayList JavaDoc();
973         sortedSet.add(new Integer JavaDoc(2));
974         bw.setPropertyValue("sortedSet", new int[] {2});
975         Set JavaDoc list = new HashSet JavaDoc();
976         list.add(new Integer JavaDoc(3));
977         bw.setPropertyValue("list", new int[] {3});
978         assertEquals(1, tb.getCollection().size());
979         assertTrue(tb.getCollection().containsAll(coll));
980         assertEquals(1, tb.getSet().size());
981         assertTrue(tb.getSet().containsAll(set));
982         assertEquals(1, tb.getSortedSet().size());
983         assertTrue(tb.getSortedSet().containsAll(sortedSet));
984         assertEquals(1, tb.getList().size());
985         assertTrue(tb.getList().containsAll(list));
986     }
987
988     public void testCollectionsWithIntegerValues() {
989         IndexedTestBean tb = new IndexedTestBean();
990         BeanWrapper bw = new BeanWrapperImpl(tb);
991         Collection JavaDoc coll = new HashSet JavaDoc();
992         coll.add(new Integer JavaDoc(0));
993         bw.setPropertyValue("collection", new Integer JavaDoc(0));
994         List JavaDoc set = new LinkedList JavaDoc();
995         set.add(new Integer JavaDoc(1));
996         bw.setPropertyValue("set", new Integer JavaDoc(1));
997         List JavaDoc sortedSet = new ArrayList JavaDoc();
998         sortedSet.add(new Integer JavaDoc(2));
999         bw.setPropertyValue("sortedSet", new Integer JavaDoc(2));
1000        Set JavaDoc list = new HashSet JavaDoc();
1001        list.add(new Integer JavaDoc(3));
1002        bw.setPropertyValue("list", new Integer JavaDoc(3));
1003        assertEquals(1, tb.getCollection().size());
1004        assertTrue(tb.getCollection().containsAll(coll));
1005        assertEquals(1, tb.getSet().size());
1006        assertTrue(tb.getSet().containsAll(set));
1007        assertEquals(1, tb.getSortedSet().size());
1008        assertTrue(tb.getSortedSet().containsAll(sortedSet));
1009        assertEquals(1, tb.getList().size());
1010        assertTrue(tb.getList().containsAll(list));
1011    }
1012
1013    public void testCollectionsWithStringValues() {
1014        IndexedTestBean tb = new IndexedTestBean();
1015        BeanWrapper bw = new BeanWrapperImpl(tb);
1016        List JavaDoc set = new LinkedList JavaDoc();
1017        set.add("set1");
1018        bw.setPropertyValue("set", "set1");
1019        List JavaDoc sortedSet = new ArrayList JavaDoc();
1020        sortedSet.add("sortedSet1");
1021        bw.setPropertyValue("sortedSet", "sortedSet1");
1022        Set JavaDoc list = new HashSet JavaDoc();
1023        list.add("list1");
1024        bw.setPropertyValue("list", "list1");
1025        assertEquals(1, tb.getSet().size());
1026        assertTrue(tb.getSet().containsAll(set));
1027        assertEquals(1, tb.getSortedSet().size());
1028        assertTrue(tb.getSortedSet().containsAll(sortedSet));
1029        assertEquals(1, tb.getList().size());
1030        assertTrue(tb.getList().containsAll(list));
1031    }
1032
1033    public void testSetNumberProperties() {
1034    NumberPropertyBean bean = new NumberPropertyBean();
1035        BeanWrapper bw = new BeanWrapperImpl(bean);
1036
1037        String JavaDoc byteValue = " " + Byte.MAX_VALUE + " ";
1038        String JavaDoc shortValue = " " + Short.MAX_VALUE + " ";
1039        String JavaDoc intValue = " " + Integer.MAX_VALUE + " ";
1040        String JavaDoc longValue = " " + Long.MAX_VALUE + " ";
1041        String JavaDoc floatValue = " " + Float.MAX_VALUE + " ";
1042        String JavaDoc doubleValue = " " + Double.MAX_VALUE + " ";
1043
1044        bw.setPropertyValue("myPrimitiveByte", byteValue);
1045        bw.setPropertyValue("myByte", byteValue);
1046
1047        bw.setPropertyValue("myPrimitiveShort", shortValue);
1048        bw.setPropertyValue("myShort", shortValue);
1049
1050        bw.setPropertyValue("myPrimitiveInt", intValue);
1051        bw.setPropertyValue("myInteger", intValue);
1052
1053        bw.setPropertyValue("myPrimitiveLong", longValue);
1054        bw.setPropertyValue("myLong", longValue);
1055
1056        bw.setPropertyValue("myPrimitiveFloat", floatValue);
1057        bw.setPropertyValue("myFloat", floatValue);
1058
1059        bw.setPropertyValue("myPrimitiveDouble", doubleValue);
1060        bw.setPropertyValue("myDouble", doubleValue);
1061
1062        assertEquals(Byte.MAX_VALUE, bean.getMyPrimitiveByte());
1063        assertEquals(Byte.MAX_VALUE, bean.getMyByte().byteValue());
1064
1065        assertEquals(Short.MAX_VALUE, bean.getMyPrimitiveShort());
1066        assertEquals(Short.MAX_VALUE, bean.getMyShort().shortValue());
1067
1068        assertEquals(Integer.MAX_VALUE, bean.getMyPrimitiveInt());
1069        assertEquals(Integer.MAX_VALUE, bean.getMyInteger().intValue());
1070
1071        assertEquals(Long.MAX_VALUE, bean.getMyPrimitiveLong());
1072        assertEquals(Long.MAX_VALUE, bean.getMyLong().longValue());
1073
1074        assertEquals(Float.MAX_VALUE, bean.getMyPrimitiveFloat(), 0.001);
1075        assertEquals(Float.MAX_VALUE, bean.getMyFloat().floatValue(), 0.001);
1076
1077        assertEquals(Double.MAX_VALUE, bean.getMyPrimitiveDouble(), 0.001);
1078        assertEquals(Double.MAX_VALUE, bean.getMyDouble().doubleValue(), 0.001);
1079
1080    }
1081
1082
1083    private static class DifferentTestBean extends TestBean {
1084        // class to test naming of beans in a BeanWrapper error message
1085
}
1086
1087
1088    private static class NoRead {
1089
1090        public void setAge(int age) {
1091        }
1092    }
1093
1094
1095    private static class EnumTest {
1096
1097        private FlushMode flushMode;
1098
1099        public void setFlushMode(FlushMode flushMode) {
1100            this.flushMode = flushMode;
1101        }
1102
1103        public FlushMode getFlushMode() {
1104            return flushMode;
1105        }
1106    }
1107
1108
1109    private static class PropsTest {
1110
1111        private Properties JavaDoc props;
1112
1113        private String JavaDoc name;
1114
1115        private String JavaDoc[] stringArray;
1116
1117        private int[] intArray;
1118
1119        public void setProperties(Properties JavaDoc p) {
1120            props = p;
1121        }
1122
1123        public void setName(String JavaDoc name) {
1124            this.name = name;
1125        }
1126
1127        public void setStringArray(String JavaDoc[] sa) {
1128            this.stringArray = sa;
1129        }
1130
1131        public void setIntArray(int[] intArray) {
1132            this.intArray = intArray;
1133        }
1134    }
1135
1136
1137    private static class GetterBean {
1138
1139        private String JavaDoc name;
1140
1141        public void setName(String JavaDoc name) {
1142            this.name = name;
1143        }
1144
1145        public String JavaDoc getName() {
1146            if (this.name == null) {
1147                throw new RuntimeException JavaDoc("name property must be set");
1148            }
1149            return name;
1150        }
1151    }
1152
1153
1154    private static class ThrowsException {
1155
1156        public void doSomething(Throwable JavaDoc t) throws Throwable JavaDoc {
1157            throw t;
1158        }
1159    }
1160
1161
1162    private static class PrimitiveArrayBean {
1163
1164        private int[] array;
1165
1166        public int[] getArray() {
1167            return array;
1168        }
1169
1170        public void setArray(int[] array) {
1171            this.array = array;
1172        }
1173    }
1174
1175
1176    private static class NumberPropertyBean {
1177
1178        private byte myPrimitiveByte;
1179        private Byte JavaDoc myByte;
1180
1181        private short myPrimitiveShort;
1182        private Short JavaDoc myShort;
1183
1184        private int myPrimitiveInt;
1185        private Integer JavaDoc myInteger;
1186
1187        private long myPrimitiveLong;
1188        private Long JavaDoc myLong;
1189
1190        private float myPrimitiveFloat;
1191        private Float JavaDoc myFloat;
1192
1193        private double myPrimitiveDouble;
1194        private Double JavaDoc myDouble;
1195
1196        public byte getMyPrimitiveByte() {
1197            return myPrimitiveByte;
1198        }
1199
1200        public void setMyPrimitiveByte(byte myPrimitiveByte) {
1201            this.myPrimitiveByte = myPrimitiveByte;
1202        }
1203
1204        public Byte JavaDoc getMyByte() {
1205            return myByte;
1206        }
1207
1208        public void setMyByte(Byte JavaDoc myByte) {
1209            this.myByte = myByte;
1210        }
1211
1212        public short getMyPrimitiveShort() {
1213            return myPrimitiveShort;
1214        }
1215
1216        public void setMyPrimitiveShort(short myPrimitiveShort) {
1217            this.myPrimitiveShort = myPrimitiveShort;
1218        }
1219
1220        public Short JavaDoc getMyShort() {
1221            return myShort;
1222        }
1223
1224        public void setMyShort(Short JavaDoc myShort) {
1225            this.myShort = myShort;
1226        }
1227
1228        public int getMyPrimitiveInt() {
1229            return myPrimitiveInt;
1230        }
1231
1232        public void setMyPrimitiveInt(int myPrimitiveInt) {
1233            this.myPrimitiveInt = myPrimitiveInt;
1234        }
1235
1236        public Integer JavaDoc getMyInteger() {
1237            return myInteger;
1238        }
1239
1240        public void setMyInteger(Integer JavaDoc myInteger) {
1241            this.myInteger = myInteger;
1242        }
1243
1244        public long getMyPrimitiveLong() {
1245            return myPrimitiveLong;
1246        }
1247
1248        public void setMyPrimitiveLong(long myPrimitiveLong) {
1249            this.myPrimitiveLong = myPrimitiveLong;
1250        }
1251
1252        public Long JavaDoc getMyLong() {
1253            return myLong;
1254        }
1255
1256        public void setMyLong(Long JavaDoc myLong) {
1257            this.myLong = myLong;
1258        }
1259
1260        public float getMyPrimitiveFloat() {
1261            return myPrimitiveFloat;
1262        }
1263
1264        public void setMyPrimitiveFloat(float myPrimitiveFloat) {
1265            this.myPrimitiveFloat = myPrimitiveFloat;
1266        }
1267
1268        public Float JavaDoc getMyFloat() {
1269            return myFloat;
1270        }
1271
1272        public void setMyFloat(Float JavaDoc myFloat) {
1273            this.myFloat = myFloat;
1274        }
1275
1276        public double getMyPrimitiveDouble() {
1277            return myPrimitiveDouble;
1278        }
1279
1280        public void setMyPrimitiveDouble(double myPrimitiveDouble) {
1281            this.myPrimitiveDouble = myPrimitiveDouble;
1282        }
1283
1284        public Double JavaDoc getMyDouble() {
1285            return myDouble;
1286        }
1287
1288        public void setMyDouble(Double JavaDoc myDouble) {
1289            this.myDouble = myDouble;
1290        }
1291    }
1292}
1293
Popular Tags