KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > BeanUtilTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.bean;
4
5 import java.math.BigDecimal JavaDoc;
6 import java.sql.Date JavaDoc;
7 import java.sql.Time JavaDoc;
8 import java.sql.Timestamp JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import jodd.bean.modifier.TrimStringsBeanModifier;
13 import jodd.bean.test.*;
14 import junit.framework.TestCase;
15
16 public class BeanUtilTest extends TestCase {
17
18     public void testSimpleProperty() {
19         FooBean fb = new FooBean();
20
21         // read non initialized property (null)
22
assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", false));
23         assertTrue(BeanUtil.hasProperty(fb, "fooInteger"));
24         // set property
25
BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer JavaDoc(173), false);
26         // read initialized property
27
assertEquals(new Integer JavaDoc(173), BeanUtil.getSimpleProperty(fb, "fooInteger", false));
28
29         // read non-initialized simple property (zero)
30
assertEquals(new Integer JavaDoc(0), BeanUtil.getSimpleProperty(fb, "fooint", false));
31         assertTrue(BeanUtil.hasProperty(fb, "fooint"));
32         assertFalse(BeanUtil.hasProperty(fb, "fooint-xxx"));
33
34         // read forced non-initialized property (not null)
35
assertTrue(BeanUtil.hasProperty(fb, "fooByte"));
36         assertEquals(new Byte JavaDoc((byte)0), BeanUtil.getSimplePropertyForced(fb, "fooByte", false));
37
38         Map JavaDoc m = new HashMap JavaDoc();
39         // set property in map
40
BeanUtil.setSimpleProperty(m, "foo", new Integer JavaDoc(173), false);
41         // read property from map
42
assertTrue(BeanUtil.hasProperty(m, "foo"));
43         assertEquals(new Integer JavaDoc(173), BeanUtil.getSimpleProperty(m, "foo", false));
44
45         // read non-initialized map property
46
assertTrue(BeanUtil.hasProperty(fb, "fooMap"));
47         assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", false));
48         // read forced non-initialized map property
49
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap", false));
50
51         // read non-initialized list property
52
assertTrue(BeanUtil.hasProperty(fb, "fooList"));
53         assertNull(BeanUtil.getSimpleProperty(fb, "fooList", false));
54         // read forced non-initialized list property
55
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList", false));
56
57         // read non-initialized array (null)
58
assertTrue(BeanUtil.hasProperty(fb, "fooStringA"));
59         assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false));
60         String JavaDoc[] tmp = new String JavaDoc[10];
61         tmp[2] = "foo";
62         // set array property
63
BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, false);
64         // read array property
65
tmp = (String JavaDoc[]) BeanUtil.getSimpleProperty(fb, "fooStringA", false);
66         assertEquals("foo", tmp[2]);
67
68         fb.setFooStringA(null);
69         // read non-initialized array property
70
assertTrue(BeanUtil.hasProperty(fb, "fooStringA"));
71         assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false));
72         // read forced non-initialized array property
73
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooStringA", false));
74     }
75
76     public void testSimplePropertySlimPrivate() {
77         FooBeanSlim fb = new FooBeanSlim();
78
79         // read non initialized property (null)
80
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooInteger"));
81         assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", true));
82         // set property
83
BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer JavaDoc(173), true);
84         // read initialized property
85
assertEquals(new Integer JavaDoc(173), BeanUtil.getSimpleProperty(fb, "fooInteger", true));
86
87         // read non-initialized simple property (zero)
88
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooint"));
89         assertEquals(new Integer JavaDoc(0), BeanUtil.getSimpleProperty(fb, "fooint", true));
90
91         // read forced non-initialized property (not null)
92
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooByte"));
93         assertEquals(new Byte JavaDoc((byte)0), BeanUtil.getSimplePropertyForced(fb, "fooByte", true));
94
95         Map JavaDoc m = new HashMap JavaDoc();
96         // set property in map
97
assertFalse(BeanUtil.hasDeclaredProperty(m, "foo"));
98         BeanUtil.setSimpleProperty(m, "foo", new Integer JavaDoc(173), true);
99         // read property from map
100
assertTrue(BeanUtil.hasDeclaredProperty(m, "foo"));
101         assertEquals(new Integer JavaDoc(173), BeanUtil.getSimpleProperty(m, "foo", true));
102
103         // read non-initialized map property
104
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap"));
105         assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", true));
106         // read forced non-initialized map property
107
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap", true));
108
109         // read non-initialized list property
110
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList"));
111         assertNull(BeanUtil.getSimpleProperty(fb, "fooList", true));
112         // read forced non-initialized list property
113
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList", true));
114
115         // read non-initialized array (null)
116
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA"));
117         assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true));
118         String JavaDoc[] tmp = new String JavaDoc[10];
119         tmp[2] = "foo";
120         // set array property
121
BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, true);
122         // read array property
123
tmp = (String JavaDoc[]) BeanUtil.getSimpleProperty(fb, "fooStringA", true);
124         assertEquals("foo", tmp[2]);
125
126         fb = new FooBeanSlim();
127         // read non-initialized array property
128
assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true));
129         // read forced non-initialized array property
130
assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooStringA", true));
131     }
132
133     public void testIndexProperty() {
134         FooBean fb = new FooBean();
135
136         // read forced non-initialized array property
137
assertNull(fb.getFooStringA());
138         assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[0]"));
139         try {
140             BeanUtil.getIndexProperty(fb, "fooStringA[0]", false, true);
141             fail();
142         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobex) {
143             // ignore
144
}
145         assertNotNull(fb.getFooStringA());
146         assertEquals(0, fb.getFooStringA().length);
147
148         // set array property (non-forced)
149
assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[7]"));
150         try {
151             BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx", false, false);
152             fail();
153         } catch(ArrayIndexOutOfBoundsException JavaDoc aioobex) {
154             // ignore
155
}
156
157         // set forced array property
158
BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", false, true);
159         assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[40]"));
160         assertEquals("zzz", fb.getFooStringA()[40]);
161         assertEquals(41, fb.getFooStringA().length);
162
163         // set null
164
assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]"));
165         BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, false, true);
166         assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]"));
167         assertNull(fb.getFooStringA()[43]);
168         assertEquals(44, fb.getFooStringA().length);
169
170         // get forced
171
assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[15]"));
172         assertNotNull(BeanUtil.getIndexProperty(fb, "fooStringA[15]", false, true));
173         assertNull(fb.getFooStringA()[0]);
174         assertNotNull(fb.getFooStringA()[15]);
175
176
177         // set uninitialized array property
178
fb.setFooStringA(null);
179         BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", false, true);
180         assertEquals("ccc", fb.getFooStringA()[7]);
181
182
183
184
185         
186         // read forced non-initialized list property
187
assertNull(fb.getFooList());
188         assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
189         try {
190             BeanUtil.getIndexProperty(fb, "fooList[1]", false, true);
191             fail();
192         } catch (IndexOutOfBoundsException JavaDoc ioobex) {
193             // ignore
194
}
195         assertNotNull(fb.getFooList());
196
197         // set list property (non-forced)
198
try {
199             BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false, false);
200             fail();
201         } catch (IndexOutOfBoundsException JavaDoc ioobex) {
202             // ignore
203
}
204
205         // set forced list property
206
assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[40]"));
207         BeanUtil.setIndexProperty(fb, "fooList[40]", "zzz", false, true);
208         assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[40]"));
209         assertEquals("zzz", fb.getFooList().get(40));
210         assertEquals(41, fb.getFooList().size());
211
212         // set forced unitialized list property
213
fb.setFooList(null);
214         assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
215         BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false, true);
216         assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
217         assertEquals("xxx", fb.getFooList().get(1));
218         assertEquals(2, fb.getFooList().size());
219
220
221         // read forced non-initialized map property
222
assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
223         assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", false, true));
224         assertNotNull(fb.getFooMap());
225         // set non-initialized map property
226
fb.setFooMap(null);
227         assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
228         BeanUtil.setIndexProperty(fb, "fooMap[foo]", "xxx", false, true);
229         assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
230         assertEquals("xxx", fb.getFooMap().get("foo"));
231         assertEquals(1, fb.getFooMap().size());
232     }
233
234     public void testIndexPropertySlimPrivate() {
235         FooBeanSlim fb = new FooBeanSlim();
236
237         // read forced non-initialized array property
238
assertNull(fb.getStringA());
239         try {
240             BeanUtil.getIndexProperty(fb, "fooStringA[0]", true, true);
241             fail();
242         } catch (ArrayIndexOutOfBoundsException JavaDoc aioobex) {
243             // ignore
244
}
245         assertNotNull(fb.getStringA());
246         assertEquals(0, fb.getStringA().length);
247
248         // set array property (non-forced)
249
try {
250             BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx", true, false);
251             fail();
252         } catch(ArrayIndexOutOfBoundsException JavaDoc aioobex) {
253             // ignore
254
}
255
256         // set forced array property
257
BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", true, true);
258         assertEquals("zzz", fb.getStringA()[40]);
259         assertEquals(41, fb.getStringA().length);
260
261         BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, true, true);
262         assertNull(fb.getStringA()[43]);
263         assertEquals(44, fb.getStringA().length);
264
265
266         // set uninitialized array property
267
fb = new FooBeanSlim();
268         assertNull(fb.getStringA());
269         BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", true, true);
270         assertNotNull(fb.getStringA());
271         assertEquals("ccc", fb.getStringA()[7]);
272
273
274         // read forced non-initialized list property
275
assertNull(fb.getList());
276         try {
277             BeanUtil.getIndexProperty(fb, "fooList[1]", true, true);
278             fail();
279         } catch(IndexOutOfBoundsException JavaDoc ioobex) {
280             // ignore
281
}
282         assertNotNull(fb.getList());
283
284         // set list property (non-forced)
285
try {
286             BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true, false);
287             fail();
288         } catch(IndexOutOfBoundsException JavaDoc ioobex) {
289             // ignore
290
}
291         
292         // set forced list property
293
BeanUtil.setIndexProperty(fb, "fooList[40]", "zzz", true, true);
294         assertEquals("zzz", fb.getList().get(40));
295         assertEquals(41, fb.getList().size());
296
297         // set forced unitialized list property
298
fb = new FooBeanSlim();
299         BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true, true);
300         assertEquals("xxx", fb.getList().get(1));
301
302         // read forced non-initialized map property
303
assertNull(fb.getMap());
304         assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", true, true));
305         assertNotNull(fb.getMap());
306
307         // set non-initialized map property
308
fb = new FooBeanSlim();
309         assertNull(fb.getMap());
310         BeanUtil.setIndexProperty(fb, "fooMap[foo]", "xxx", true, true);
311         assertNotNull(fb.getMap());
312         assertEquals("xxx", fb.getMap().get("foo"));
313     }
314
315
316     // ---------------------------------------------------------------- types
317

318     public void testSetPropertyNumbers() {
319         FooBean fb = new FooBean();
320
321         // Integer
322
String JavaDoc propName = "fooInteger";
323         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(1));
324         assertEquals(1, fb.getFooInteger().intValue());
325         BeanUtil.setProperty(fb, propName, null);
326         assertNull(fb.getFooInteger());
327         BeanUtil.setProperty(fb, propName, "2"); // valid string
328
assertEquals(2, fb.getFooInteger().intValue());
329         try {
330             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
331
fail();
332         } catch(Exception JavaDoc ex) {}
333         assertEquals(2, fb.getFooInteger().intValue());
334
335         // int
336
propName = "fooint";
337         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(1));
338         assertEquals(1, fb.getFooint());
339
340         try {
341             BeanUtil.setProperty(fb, propName, null); // null is not an int
342
fail();
343         } catch(Exception JavaDoc ex) {}
344         assertEquals(1, fb.getFooint());
345
346         BeanUtil.setProperty(fb, propName, "2");
347         assertEquals(2, fb.getFooint());
348
349         try {
350             BeanUtil.setProperty(fb, propName, "w"); // invalid string
351
fail();
352         } catch (Exception JavaDoc ex) {}
353         assertEquals(2, fb.getFooint());
354
355
356         // Long
357
propName = "fooLong";
358         BeanUtil.setProperty(fb, propName, new Long JavaDoc(1));
359         assertEquals(1L, fb.getFooLong().longValue());
360         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
361         assertEquals(3L, fb.getFooLong().longValue());
362         BeanUtil.setProperty(fb, propName, null);
363         assertNull(fb.getFooLong());
364         BeanUtil.setProperty(fb, propName, "2"); // valid string
365
assertEquals(2L, fb.getFooLong().longValue());
366
367         try {
368             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
369
fail();
370         } catch (Exception JavaDoc ex) {}
371         assertEquals(2L, fb.getFooLong().longValue());
372
373         // long
374
propName = "foolong";
375         BeanUtil.setProperty(fb, propName, new Long JavaDoc(1));
376         assertEquals(1L, fb.getFoolong());
377         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
378         assertEquals(3L, fb.getFoolong());
379
380         try {
381             BeanUtil.setProperty(fb, propName, null); // null is not a long
382
fail();
383         } catch (Exception JavaDoc ex) {}
384
385         assertEquals(3L, fb.getFoolong());
386         BeanUtil.setProperty(fb, propName, "2"); // valid string
387
assertEquals(2L, fb.getFoolong());
388         try {
389             BeanUtil.setProperty(fb, propName, "w"); // invalid string
390
fail();
391         } catch (Exception JavaDoc ex) {}
392         assertEquals(2L, fb.getFoolong());
393
394         // Byte
395
propName = "fooByte";
396         BeanUtil.setProperty(fb, propName, new Byte JavaDoc((byte) 1));
397         assertEquals(1, fb.getFooByte().byteValue());
398         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
399         assertEquals(3, fb.getFooByte().byteValue());
400         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(257));
401         assertEquals(1, fb.getFooByte().byteValue()); // lower byte of 257
402
BeanUtil.setProperty(fb, propName, null);
403         assertNull(fb.getFooByte());
404         BeanUtil.setProperty(fb, propName, "2"); // valid string
405
assertEquals(2, fb.getFooByte().byteValue());
406
407         try {
408             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
409
fail();
410         } catch (Exception JavaDoc ex) {}
411         assertEquals(2, fb.getFooByte().byteValue());
412
413         // byte
414
propName = "foobyte";
415         BeanUtil.setProperty(fb, propName, new Byte JavaDoc((byte) 1));
416         assertEquals(1, fb.getFoobyte());
417         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
418         assertEquals(3, fb.getFoobyte());
419         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(257));
420         assertEquals(1, fb.getFoobyte());
421         try {
422             BeanUtil.setProperty(fb, propName, null); // null is not a byte
423
fail();
424         } catch (Exception JavaDoc ex) {}
425         assertEquals(1, fb.getFoobyte());
426         BeanUtil.setProperty(fb, propName, "2"); // valid string
427
assertEquals(2, fb.getFoobyte());
428         try {
429             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
430
fail();
431         } catch(Exception JavaDoc ex) {}
432         assertEquals(2, fb.getFoobyte());
433
434         // Boolean
435
propName = "fooBoolean";
436         BeanUtil.setProperty(fb, propName, Boolean.TRUE);
437         assertTrue(fb.getFooBoolean().booleanValue());
438         BeanUtil.setProperty(fb, propName, Boolean.FALSE);
439         assertFalse(fb.getFooBoolean().booleanValue());
440         BeanUtil.setProperty(fb, propName, null);
441         assertNull(fb.getFooBoolean());
442         BeanUtil.setProperty(fb, propName, "yes");
443         assertTrue(fb.getFooBoolean().booleanValue());
444         BeanUtil.setProperty(fb, propName, "y");
445         assertTrue(fb.getFooBoolean().booleanValue());
446         BeanUtil.setProperty(fb, propName, "true");
447         assertTrue(fb.getFooBoolean().booleanValue());
448         BeanUtil.setProperty(fb, propName, "on");
449         assertTrue(fb.getFooBoolean().booleanValue());
450         BeanUtil.setProperty(fb, propName, "1");
451         assertTrue(fb.getFooBoolean().booleanValue());
452         BeanUtil.setProperty(fb, propName, "no");
453         assertFalse(fb.getFooBoolean().booleanValue());
454         BeanUtil.setProperty(fb, propName, "n");
455         assertFalse(fb.getFooBoolean().booleanValue());
456         BeanUtil.setProperty(fb, propName, "false");
457         assertFalse(fb.getFooBoolean().booleanValue());
458         BeanUtil.setProperty(fb, propName, "off");
459         assertFalse(fb.getFooBoolean().booleanValue());
460         BeanUtil.setProperty(fb, propName, "0");
461         assertFalse(fb.getFooBoolean().booleanValue());
462
463         // boolean
464
propName = "fooboolean";
465         BeanUtil.setProperty(fb, propName, Boolean.TRUE);
466         assertTrue(fb.getFooboolean());
467         BeanUtil.setProperty(fb, propName, Boolean.FALSE);
468         assertFalse(fb.getFooboolean());
469
470         try {
471             BeanUtil.setProperty(fb, propName, null);
472             fail();
473         } catch(Exception JavaDoc ex) {}
474
475         assertFalse(fb.getFooboolean());
476         BeanUtil.setProperty(fb, propName, "yes");
477         assertTrue(fb.getFooboolean());
478         BeanUtil.setProperty(fb, propName, "y");
479         assertTrue(fb.getFooboolean());
480         BeanUtil.setProperty(fb, propName, "true");
481         assertTrue(fb.getFooboolean());
482         BeanUtil.setProperty(fb, propName, "on");
483         assertTrue(fb.getFooboolean());
484         BeanUtil.setProperty(fb, propName, "1");
485         assertTrue(fb.getFooboolean());
486         BeanUtil.setProperty(fb, propName, "no");
487         assertFalse(fb.getFooboolean());
488         BeanUtil.setProperty(fb, propName, "n");
489         assertFalse(fb.getFooboolean());
490         BeanUtil.setProperty(fb, propName, "false");
491         assertFalse(fb.getFooboolean());
492         BeanUtil.setProperty(fb, propName, "off");
493         assertFalse(fb.getFooboolean());
494         BeanUtil.setProperty(fb, propName, "0");
495         assertFalse(fb.getFooboolean());
496
497         // Float
498
propName = "fooFloat";
499         BeanUtil.setProperty(fb, propName, new Float JavaDoc(1.1));
500         assertEquals(1.1, fb.getFooFloat().floatValue(), 0.0005);
501         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
502         assertEquals(3.0, fb.getFooFloat().floatValue(), 0.0005);
503         BeanUtil.setProperty(fb, propName, null);
504         assertNull(fb.getFooFloat());
505         BeanUtil.setProperty(fb, propName, "2.2"); // valid string
506
assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005);
507         try {
508             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
509
fail();
510         } catch (Exception JavaDoc ex) {}
511         assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005);
512
513         // float
514
propName = "foofloat";
515         BeanUtil.setProperty(fb, propName, new Float JavaDoc(1.1));
516         assertEquals(1.1, fb.getFoofloat(), 0.0005);
517         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
518         assertEquals(3.0, fb.getFoofloat(), 0.0005);
519         try {
520             BeanUtil.setProperty(fb, propName, null); // null is not a long
521
fail();
522         } catch(Exception JavaDoc ex) {}
523         assertEquals(3.0, fb.getFoofloat(), 0.0005);
524         BeanUtil.setProperty(fb, propName, "2.2"); // valid string
525

526         assertEquals(2.2, fb.getFoofloat(), 0.0005);
527         try {
528             BeanUtil.setProperty(fb, propName, "w"); // invalid string
529
fail();
530         } catch(Exception JavaDoc ex) {}
531         assertEquals(2.2, fb.getFoofloat(), 0.0005);
532
533         // Double
534
propName = "fooDouble";
535         BeanUtil.setProperty(fb, propName, new Double JavaDoc(1.1));
536         assertEquals(1.1, fb.getFooDouble().doubleValue(), 0.0005);
537         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
538         assertEquals(3.0, fb.getFooDouble().doubleValue(), 0.0005);
539         BeanUtil.setProperty(fb, propName, null);
540         assertNull(fb.getFooDouble());
541         BeanUtil.setProperty(fb, propName, "2.2"); // valid string
542
assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005);
543         try {
544             BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
545
fail();
546         } catch (Exception JavaDoc ex) {}
547         assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005);
548
549         // double
550
propName = "foodouble";
551         BeanUtil.setProperty(fb, propName, new Double JavaDoc(1.1));
552         assertEquals(1.1, fb.getFoodouble(), 0.0005);
553         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(3));
554         assertEquals(3.0, fb.getFoodouble(), 0.0005);
555         try {
556             BeanUtil.setProperty(fb, propName, null); // null is not a long
557
fail();
558         } catch(Exception JavaDoc ex) {}
559         assertEquals(3.0, fb.getFoodouble(), 0.0005);
560         BeanUtil.setProperty(fb, propName, "2.2"); // valid string
561

562         assertEquals(2.2, fb.getFoodouble(), 0.0005);
563         try {
564             BeanUtil.setProperty(fb, propName, "w"); // invalid string
565
fail();
566         } catch(Exception JavaDoc ex) {}
567         assertEquals(2.2, fb.getFoodouble(), 0.0005);
568     }
569
570
571     public void testSetPropertySql() {
572         FooBean2 fb = new FooBean2();
573
574         String JavaDoc propName = "fooTimestamp";
575         //noinspection deprecation
576
Timestamp JavaDoc ts = new Timestamp JavaDoc(101, 0, 17, 1, 2, 3, 4);
577         BeanUtil.setProperty(fb, propName, ts);
578         assertEquals("2001-01-17 01:02:03.000000004", fb.getFooTimestamp().toString());
579
580         propName = "fooTime";
581         //noinspection deprecation
582
Time JavaDoc t = new Time JavaDoc(17, 13, 15);
583         BeanUtil.setProperty(fb, propName, t);
584
585         assertEquals("17:13:15", fb.getFooTime().toString());
586
587         propName = "fooDate";
588         //noinspection deprecation
589
Date JavaDoc d = new Date JavaDoc(101, 1, 17);
590         BeanUtil.setProperty(fb, propName, d);
591         assertEquals("2001-02-17", fb.getFooDate().toString());
592     }
593
594
595     public void testSetPropertyMath() {
596         FooBean2 fb = new FooBean2();
597         String JavaDoc propName = "fooBigDecimal";
598         BeanUtil.setProperty(fb, propName, new BigDecimal JavaDoc(1.2));
599         assertEquals(1.2, fb.getFooBigDecimal().doubleValue(), 0.0005);
600     }
601
602     public void testSetPropertyString() {
603         FooBean fb = new FooBean();
604
605         // String
606
String JavaDoc propName = "fooString";
607         BeanUtil.setProperty(fb, propName, "string");
608         assertEquals("string", fb.getFooString());
609         BeanUtil.setProperty(fb, propName, null);
610         assertNull(fb.getFooString());
611
612         // String array
613
propName = "fooStringA";
614         String JavaDoc[] sa = new String JavaDoc[] {"one", "two", "three"};
615         BeanUtil.setProperty(fb, propName, sa);
616         assertEquals("one", fb.getFooStringA()[0]);
617         assertEquals("two", fb.getFooStringA()[1]);
618         assertEquals("three", fb.getFooStringA()[2]);
619         BeanUtil.setProperty(fb, propName, "just a string");
620         sa = (String JavaDoc[]) BeanUtil.getProperty(fb, propName);
621         assertEquals(1, sa.length);
622         assertEquals("just a string", sa[0]);
623
624         // Character
625
propName = "fooCharacter";
626         BeanUtil.setProperty(fb, propName, new Character JavaDoc('a'));
627         assertEquals('a', fb.getFooCharacter().charValue());
628         BeanUtil.setProperty(fb, propName, "123");
629         assertEquals('1', fb.getFooCharacter().charValue());
630         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(789));
631         assertEquals('7', fb.getFooCharacter().charValue());
632
633         // char
634
propName = "foochar";
635         BeanUtil.setProperty(fb, propName, new Character JavaDoc('a'));
636         assertEquals('a', fb.getFoochar());
637         BeanUtil.setProperty(fb, propName, "123");
638         assertEquals('1', fb.getFoochar());
639         BeanUtil.setProperty(fb, propName, new Integer JavaDoc(789));
640         assertEquals('7', fb.getFoochar());
641     }
642
643
644     public void testLoaders() {
645         HashMap JavaDoc map = new HashMap JavaDoc();
646
647         map.put("fooInteger", new Integer JavaDoc(1));
648         map.put("fooint", new Integer JavaDoc(2));
649         map.put("fooLong", new Long JavaDoc(3));
650         map.put("foolong", new Long JavaDoc(4));
651         map.put("fooByte", new Byte JavaDoc((byte)5));
652         map.put("foobyte", new Byte JavaDoc((byte)6));
653         map.put("fooCharacter", new Character JavaDoc('7'));
654         map.put("foochar", new Character JavaDoc('8'));
655         map.put("fooBoolean", Boolean.TRUE);
656         map.put("fooboolean", Boolean.FALSE);
657         map.put("fooFloat", new Float JavaDoc(9.0));
658         map.put("foofloat", new Float JavaDoc(10.0));
659         map.put("fooDouble", new Double JavaDoc(11.0));
660         map.put("foodouble", new Double JavaDoc(12.0));
661         map.put("fooString", "13");
662         map.put("fooStringA", new String JavaDoc[]{"14", "15"});
663
664         FooBean fb = new FooBean();
665         BeanLoaderManager.load(fb, map);
666
667         assertEquals(1, fb.getFooInteger().intValue());
668         assertEquals(2, fb.getFooint());
669         assertEquals(3, fb.getFooLong().longValue());
670         assertEquals(4, fb.getFoolong());
671         assertEquals(5, fb.getFooByte().byteValue());
672         assertEquals(6, fb.getFoobyte());
673         assertEquals('7', fb.getFooCharacter().charValue());
674         assertEquals('8', fb.getFoochar());
675         assertTrue(fb.getFooBoolean().booleanValue());
676         assertFalse(fb.getFooboolean());
677         assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005);
678         assertEquals(10.0, fb.getFoofloat(), 0.005);
679         assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005);
680         assertEquals(12.0, fb.getFoodouble(), 0.005);
681         assertEquals("13", fb.getFooString());
682         assertEquals("14", fb.getFooStringA()[0]);
683         assertEquals("15", fb.getFooStringA()[1]);
684
685         map.put("FooInteger", new Integer JavaDoc(1));
686         map.put("Fooint", new Integer JavaDoc(2));
687         map.put("FooLong", new Long JavaDoc(3));
688         map.put("Foolong", new Long JavaDoc(4));
689         map.put("FooByte", new Byte JavaDoc((byte)5));
690         map.put("Foobyte", new Byte JavaDoc((byte)6));
691         map.put("FooCharacter", new Character JavaDoc('7'));
692         map.put("Foochar", new Character JavaDoc('8'));
693         map.put("FooBoolean", Boolean.TRUE);
694         map.put("Fooboolean", Boolean.FALSE);
695         map.put("FooFloat", new Float JavaDoc(9.0));
696         map.put("Foofloat", new Float JavaDoc(10.0));
697         map.put("FooDouble", new Double JavaDoc(11.0));
698         map.put("Foodouble", new Double JavaDoc(12.0));
699         map.put("FooString", "13");
700         map.put("FooStringA", new String JavaDoc[]{"14", "15"});
701
702         fb = new FooBean();
703         BeanLoaderManager.load(fb, map);
704
705         assertEquals(1, fb.getFooInteger().intValue());
706         assertEquals(2, fb.getFooint());
707         assertEquals(3, fb.getFooLong().longValue());
708         assertEquals(4, fb.getFoolong());
709         assertEquals(5, fb.getFooByte().byteValue());
710         assertEquals(6, fb.getFoobyte());
711         assertEquals('7', fb.getFooCharacter().charValue());
712         assertEquals('8', fb.getFoochar());
713         assertTrue(fb.getFooBoolean().booleanValue());
714         assertFalse(fb.getFooboolean());
715         assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005);
716         assertEquals(10.0, fb.getFoofloat(), 0.005);
717         assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005);
718         assertEquals(12.0, fb.getFoodouble(), 0.005);
719         assertEquals("13", fb.getFooString());
720         assertEquals("14", fb.getFooStringA()[0]);
721         assertEquals("15", fb.getFooStringA()[1]);
722
723     }
724
725     public void testForEach() {
726         FooBean fb = new FooBean();
727         fb.setFooString(" xxx ");
728         fb.setFooStringA(new String JavaDoc[] {" xxx ", " yy ", " z "});
729         new TrimStringsBeanModifier().modify(fb);
730         assertEquals("xxx", fb.getFooString());
731         assertEquals("xxx", fb.getFooStringA()[0]);
732         assertEquals("yy", fb.getFooStringA()[1]);
733         assertEquals("z", fb.getFooStringA()[2]);
734     }
735
736
737     public void testGet() {
738         FooBean fb = new FooBean();
739         fb.setFooInteger(new Integer JavaDoc(101));
740         fb.setFooint(102);
741         fb.setFooLong(new Long JavaDoc(103));
742         fb.setFoolong(104);
743         fb.setFooByte(new Byte JavaDoc((byte) 105));
744         fb.setFoobyte((byte) 106);
745         fb.setFooCharacter(new Character JavaDoc('7'));
746         fb.setFoochar('8');
747         fb.setFooBoolean(Boolean.TRUE);
748         fb.setFooboolean(false);
749         fb.setFooFloat(new Float JavaDoc(109.0));
750         fb.setFoofloat((float)110.0);
751         fb.setFooDouble(new Double JavaDoc(111.0));
752         fb.setFoodouble(112.0);
753         fb.setFooString("113");
754         fb.setFooStringA(new String JavaDoc[] {"114", "115"} );
755
756         Integer JavaDoc v = (Integer JavaDoc) BeanUtil.getProperty(fb, "fooInteger");
757         assertEquals(101, v.intValue());
758         v = (Integer JavaDoc) BeanUtil.getProperty(fb, "fooint");
759         assertEquals(102, v.intValue());
760         Long JavaDoc vl = (Long JavaDoc) BeanUtil.getProperty(fb, "fooLong");
761         assertEquals(103, vl.longValue());
762         vl = (Long JavaDoc) BeanUtil.getProperty(fb, "foolong");
763         assertEquals(104, vl.longValue());
764         Byte JavaDoc vb = (Byte JavaDoc) BeanUtil.getProperty(fb, "fooByte");
765         assertEquals(105, vb.intValue());
766         vb = (Byte JavaDoc) BeanUtil.getProperty(fb, "foobyte");
767         assertEquals(106, vb.intValue());
768         Character JavaDoc c = (Character JavaDoc) BeanUtil.getProperty(fb, "fooCharacter");
769         assertEquals('7', c.charValue());
770         c = (Character JavaDoc) BeanUtil.getProperty(fb, "foochar");
771         assertEquals('8', c.charValue());
772         Boolean JavaDoc b = (Boolean JavaDoc) BeanUtil.getProperty(fb, "fooBoolean");
773         assertTrue(b.booleanValue());
774         b = (Boolean JavaDoc) BeanUtil.getProperty(fb, "fooboolean");
775         assertFalse(b.booleanValue());
776         Float JavaDoc f = (Float JavaDoc) BeanUtil.getProperty(fb, "fooFloat");
777         assertEquals(109.0, f.floatValue(), 0.005);
778         f = (Float JavaDoc) BeanUtil.getProperty(fb, "foofloat");
779         assertEquals(110.0, f.floatValue(), 0.005);
780         Double JavaDoc d = (Double JavaDoc) BeanUtil.getProperty(fb, "fooDouble");
781         assertEquals(111.0, d.doubleValue(), 0.005);
782         d = (Double JavaDoc) BeanUtil.getProperty(fb, "foodouble");
783         assertEquals(112.0, d.doubleValue(), 0.005);
784         String JavaDoc s = (String JavaDoc) BeanUtil.getProperty(fb, "fooString");
785         assertEquals("113", s);
786         String JavaDoc[] sa = (String JavaDoc[]) BeanUtil.getProperty(fb, "fooStringA");
787         assertEquals(2, sa.length);
788         assertEquals("114", sa[0]);
789         assertEquals("115", sa[1]);
790     }
791
792     public void testCopy() {
793         FooBean fb = new FooBean();
794         fb.setFooInteger(new Integer JavaDoc(201));
795         fb.setFooint(202);
796         fb.setFooLong(new Long JavaDoc(203));
797         fb.setFoolong(204);
798         fb.setFooByte(new Byte JavaDoc((byte) 95));
799         fb.setFoobyte((byte) 96);
800         fb.setFooCharacter(new Character JavaDoc('7'));
801         fb.setFoochar('8');
802         fb.setFooBoolean(Boolean.TRUE);
803         fb.setFooboolean(false);
804         fb.setFooFloat(new Float JavaDoc(209.0));
805         fb.setFoofloat((float)210.0);
806         fb.setFooDouble(new Double JavaDoc(211.0));
807         fb.setFoodouble(212.0);
808         fb.setFooString("213");
809         fb.setFooStringA(new String JavaDoc[] {"214", "215"} );
810
811         FooBean dest = new FooBean();
812         BeanCopy.copyProperties(fb, dest);
813
814         Integer JavaDoc v = (Integer JavaDoc) BeanUtil.getProperty(dest, "fooInteger");
815         assertEquals(201, v.intValue());
816         v = (Integer JavaDoc) BeanUtil.getProperty(dest, "fooint");
817         assertEquals(202, v.intValue());
818         Long JavaDoc vl = (Long JavaDoc) BeanUtil.getProperty(dest, "fooLong");
819         assertEquals(203, vl.longValue());
820         vl = (Long JavaDoc) BeanUtil.getProperty(dest, "foolong");
821         assertEquals(204, vl.longValue());
822         Byte JavaDoc vb = (Byte JavaDoc) BeanUtil.getProperty(dest, "fooByte");
823         assertEquals(95, vb.intValue());
824         vb = (Byte JavaDoc) BeanUtil.getProperty(dest, "foobyte");
825         assertEquals(96, vb.intValue());
826         Character JavaDoc c = (Character JavaDoc) BeanUtil.getProperty(dest, "fooCharacter");
827         assertEquals('7', c.charValue());
828         c = (Character JavaDoc) BeanUtil.getProperty(dest, "foochar");
829         assertEquals('8', c.charValue());
830         Boolean JavaDoc b = (Boolean JavaDoc) BeanUtil.getProperty(dest, "fooBoolean");
831         assertTrue(b.booleanValue());
832         b = (Boolean JavaDoc) BeanUtil.getProperty(dest, "fooboolean");
833         assertFalse(b.booleanValue());
834         Float JavaDoc f = (Float JavaDoc) BeanUtil.getProperty(dest, "fooFloat");
835         assertEquals(209.0, f.floatValue(), 0.005);
836         f = (Float JavaDoc) BeanUtil.getProperty(dest, "foofloat");
837         assertEquals(210.0, f.floatValue(), 0.005);
838         Double JavaDoc d = (Double JavaDoc) BeanUtil.getProperty(dest, "fooDouble");
839         assertEquals(211.0, d.doubleValue(), 0.005);
840         d = (Double JavaDoc) BeanUtil.getProperty(dest, "foodouble");
841         assertEquals(212.0, d.doubleValue(), 0.005);
842         String JavaDoc s = (String JavaDoc) BeanUtil.getProperty(dest, "fooString");
843         assertEquals("213", s);
844         String JavaDoc[] sa = (String JavaDoc[]) BeanUtil.getProperty(dest, "fooStringA");
845         assertEquals(2, sa.length);
846         assertEquals("214", sa[0]);
847         assertEquals("215", sa[1]);
848
849
850         FooBean empty = new FooBean();
851         BeanCopy.copyProperties(empty, dest);
852         v = (Integer JavaDoc) BeanUtil.getProperty(dest, "fooInteger");
853         assertNull(v);
854         v = (Integer JavaDoc) BeanUtil.getProperty(dest, "fooint");
855         assertEquals(0, v.intValue());
856         vl = (Long JavaDoc) BeanUtil.getProperty(dest, "fooLong");
857         assertNull(vl);
858         vl = (Long JavaDoc) BeanUtil.getProperty(dest, "foolong");
859         assertEquals(0, vl.longValue());
860         vb = (Byte JavaDoc) BeanUtil.getProperty(dest, "fooByte");
861         assertNull(vb);
862         vb = (Byte JavaDoc) BeanUtil.getProperty(dest, "foobyte");
863         assertEquals(0, vb.byteValue());
864         c = (Character JavaDoc) BeanUtil.getProperty(dest, "fooCharacter");
865         assertNull(c);
866         c = (Character JavaDoc) BeanUtil.getProperty(dest, "foochar");
867         assertEquals(0, c.charValue());
868         b = (Boolean JavaDoc) BeanUtil.getProperty(dest, "fooBoolean");
869         assertNull(b);
870         b = (Boolean JavaDoc) BeanUtil.getProperty(dest, "fooboolean");
871         assertFalse(b.booleanValue());
872         f = (Float JavaDoc) BeanUtil.getProperty(dest, "fooFloat");
873         assertNull(f);
874         f = (Float JavaDoc) BeanUtil.getProperty(dest, "foofloat");
875         assertEquals(0, f.floatValue(), 0.005);
876         d = (Double JavaDoc) BeanUtil.getProperty(dest, "fooDouble");
877         assertNull(d);
878         d = (Double JavaDoc) BeanUtil.getProperty(dest, "foodouble");
879         assertEquals(0, d.doubleValue(), 0.005);
880         s = (String JavaDoc) BeanUtil.getProperty(dest, "fooString");
881         assertNull(s);
882         sa = (String JavaDoc[]) BeanUtil.getProperty(dest, "fooStringA");
883         assertNull(sa);
884     }
885
886     public void testNested() {
887         Cbean cbean = new Cbean();
888         String JavaDoc value = "testnest";
889         String JavaDoc value2 = "nesttest";
890         assertTrue(BeanUtil.hasDeclaredProperty(cbean, "bbean.abean.fooProp"));
891         BeanUtil.setProperty(cbean, "bbean.abean.fooProp", value);
892         assertEquals(value, (String JavaDoc) BeanUtil.getProperty(cbean, "bbean.abean.fooProp"));
893         Bbean bbean = (Bbean) BeanUtil.getProperty(cbean, "bbean");
894         assertTrue(BeanUtil.hasDeclaredProperty(bbean, "abean.fooProp"));
895         assertEquals(value, (String JavaDoc) BeanUtil.getProperty(bbean, "abean.fooProp"));
896         Abean abean = (Abean) BeanUtil.getProperty(bbean, "abean");
897         assertEquals(value, (String JavaDoc) BeanUtil.getProperty(abean, "fooProp"));
898         BeanUtil.setProperty(bbean, "abean.fooProp", value2);
899         assertEquals(value2, (String JavaDoc) BeanUtil.getProperty(bbean, "abean.fooProp"));
900     }
901
902     public void testIster() {
903         Abean abean = new Abean();
904         Boolean JavaDoc b = (Boolean JavaDoc) BeanUtil.getProperty(abean, "something");
905         assertTrue(b.booleanValue());
906         try {
907             BeanUtil.getProperty(abean, "Something");
908             fail();
909         } catch (BeanException bex) {
910             // ignore
911
}
912
913     }
914
915
916     public void testMap() {
917         Cbean cbean = new Cbean();
918         Abean abean = cbean.getBbean().getAbean();
919         BeanUtil.setProperty(abean, "mval", new Integer JavaDoc(173));
920         BeanUtil.setProperty(abean, "mval2", new Integer JavaDoc(1));
921         assertEquals(173, (abean.get("mval")).intValue());
922         assertEquals(173, ((Integer JavaDoc) BeanUtil.getProperty(abean, "mval")).intValue());
923         assertEquals(1, ((Integer JavaDoc) BeanUtil.getProperty(abean, "mval2")).intValue());
924         assertTrue(BeanUtil.hasDeclaredProperty(cbean, "bbean.abean.mval"));
925         BeanUtil.setProperty(cbean, "bbean.abean.mval", new Integer JavaDoc(3));
926         assertEquals(3, ((Integer JavaDoc) BeanUtil.getProperty(abean, "mval")).intValue());
927         assertEquals(3, ((Integer JavaDoc) BeanUtil.getProperty(cbean, "bbean.abean.mval")).intValue());
928         HashMap JavaDoc map = new HashMap JavaDoc();
929         BeanUtil.setProperty(map, "val1", new Integer JavaDoc(173));
930         assertEquals(173, ((Integer JavaDoc)map.get("val1")).intValue());
931         Integer JavaDoc i = (Integer JavaDoc) BeanUtil.getProperty(map, "val1");
932         assertEquals(173, i.intValue());
933     }
934
935     public void testMap3() {
936         Map JavaDoc m = new HashMap JavaDoc();
937         BeanUtil.setProperty(m, "Foo", "John");
938         assertEquals("John", (String JavaDoc) m.get("Foo"));
939         assertNull(m.get("foo"));
940         assertFalse(BeanUtil.hasDeclaredProperty(m, "foo"));
941         BeanUtil.setProperty(m, "foo", new HashMap JavaDoc());
942         assertTrue(BeanUtil.hasDeclaredProperty(m, "foo"));
943         assertFalse(BeanUtil.hasDeclaredProperty(m, "foo.Name"));
944         BeanUtil.setProperty(m, "foo.Name", "Doe");
945         assertEquals("John", (String JavaDoc) m.get("Foo"));
946         assertEquals("Doe", ((HashMap JavaDoc) m.get("foo")).get("Name"));
947         assertNull("Doe", ((HashMap JavaDoc) m.get("foo")).get("name"));
948         assertEquals("John", (String JavaDoc) BeanUtil.getProperty(m, "Foo"));
949         assertEquals("Doe", (String JavaDoc) BeanUtil.getProperty(m, "foo.Name"));
950         try {
951             assertNull(BeanUtil.getProperty(m, "foo.name"));
952             fail();
953         } catch (Exception JavaDoc e) {
954         }
955     }
956
957     public void testNotDeclared() {
958         FooBean3 fb = new FooBean3();
959
960         try {
961             BeanUtil.setProperty(fb, "pprotected", new Integer JavaDoc(1));
962             fail();
963         } catch(Exception JavaDoc ex) {}
964         try {
965             BeanUtil.getProperty(fb, "pprotected");
966             fail();
967         } catch(Exception JavaDoc ex) {}
968
969         try {
970             BeanUtil.setProperty(fb, "ppackage", new Integer JavaDoc(2));
971             fail();
972         } catch(Exception JavaDoc ex) {}
973         try {
974             BeanUtil.getProperty(fb, "ppackage");
975             fail();
976         } catch(Exception JavaDoc ex) {}
977
978         try {
979             BeanUtil.setProperty(fb, "pprivate", new Integer JavaDoc(3));
980             fail();
981         } catch(Exception JavaDoc ex) {}
982         try {
983             BeanUtil.getProperty(fb, "pprivate");
984             fail();
985         } catch(Exception JavaDoc ex) {}
986     }
987
988     public void testDeclared() {
989         FooBean3 fb = new FooBean3();
990
991         BeanUtil.setDeclaredProperty(fb, "pprotected", new Integer JavaDoc(1));
992         Integer JavaDoc value = (Integer JavaDoc) BeanUtil.getDeclaredProperty(fb, "pprotected");
993         assertNotNull(value);
994         assertEquals(1, value.intValue());
995
996         BeanUtil.setDeclaredProperty(fb, "ppackage", new Integer JavaDoc(2));
997         value = (Integer JavaDoc) BeanUtil.getDeclaredProperty(fb, "ppackage");
998         assertNotNull(value);
999         assertEquals(2, value.intValue());
1000
1001        BeanUtil.setDeclaredProperty(fb, "pprivate", new Integer JavaDoc(3));
1002        value = (Integer JavaDoc) BeanUtil.getDeclaredProperty(fb, "pprivate");
1003        assertNotNull(value);
1004        assertEquals(3, value.intValue());
1005    }
1006
1007
1008    static class Dummy {
1009        private FooBean4 fb = new FooBean4();
1010
1011        public FooBean4 getFb() {
1012            return fb;
1013        }
1014
1015        public void setFb(FooBean4 fb) {
1016            this.fb = fb;
1017        }
1018
1019        /** @noinspection UnnecessaryBoxing*/
1020        private Integer JavaDoc[] data = new Integer JavaDoc[] {Integer.valueOf("173"), Integer.valueOf("2")};
1021
1022        public Integer JavaDoc[] getData() {
1023            return data;
1024        }
1025    }
1026
1027    public void testArrays() {
1028        FooBean4 fb4 = new FooBean4();
1029        Dummy dummy = new Dummy();
1030        assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[0].bbean.abean.fooProp"));
1031        assertEquals("xxx", BeanUtil.getProperty(fb4, "data[0].bbean.abean.fooProp"));
1032        assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[1].bbean.abean.fooProp"));
1033        assertEquals("yyy", BeanUtil.getProperty(fb4, "data[1].bbean.abean.fooProp"));
1034
1035        assertTrue(BeanUtil.hasDeclaredProperty(fb4, "data[2].bbean.abean.fooProp"));
1036        assertEquals("zzz", BeanUtil.getProperty(fb4, "data[2].bbean.abean.fooProp"));
1037        BeanUtil.setProperty(fb4, "data[2].bbean.abean.fooProp", "ZZZ");
1038        assertEquals("ZZZ", BeanUtil.getProperty(fb4, "data[2].bbean.abean.fooProp"));
1039
1040        assertTrue(BeanUtil.hasDeclaredProperty(fb4, "list[0].bbean.abean.fooProp"));
1041        assertEquals("LLL", BeanUtil.getProperty(fb4, "list[0].bbean.abean.fooProp"));
1042        BeanUtil.setProperty(fb4, "list[0].bbean.abean.fooProp", "EEE");
1043        assertEquals("EEE", BeanUtil.getProperty(fb4, "list[0].bbean.abean.fooProp"));
1044        assertEquals("lll", BeanUtil.getProperty(fb4, "list[1]"));
1045        BeanUtil.setProperty(fb4, "list[1]", "eee");
1046
1047        assertFalse(BeanUtil.hasDeclaredProperty(fb4, "list[1].bbean.abean.fooProp"));
1048        assertTrue(BeanUtil.hasDeclaredProperty(fb4, "list[1]"));
1049        assertEquals("eee", BeanUtil.getProperty(fb4, "list[1]"));
1050        assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[0].bbean.abean.fooProp"));
1051        assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[1].bbean.abean.fooProp"));
1052        assertTrue(BeanUtil.hasDeclaredProperty(dummy, "fb.data[2].bbean.abean.fooProp"));
1053        assertEquals("xxx", BeanUtil.getProperty(dummy, "fb.data[0].bbean.abean.fooProp"));
1054        assertEquals("yyy", BeanUtil.getProperty(dummy, "fb.data[1].bbean.abean.fooProp"));
1055        assertEquals("zzz", BeanUtil.getProperty(dummy, "fb.data[2].bbean.abean.fooProp"));
1056
1057        BeanUtil.setProperty(dummy, "fb.data[2].bbean.abean.fooProp", "ZZZ");
1058        assertEquals("ZZZ", BeanUtil.getProperty(dummy, "fb.data[2].bbean.abean.fooProp"));
1059        assertEquals(new Integer JavaDoc(173), BeanUtil.getProperty(dummy, "data[0]"));
1060
1061        BeanUtil.setProperty(dummy, "data[0]", new Integer JavaDoc(-173));
1062        assertEquals(new Integer JavaDoc(-173), BeanUtil.getProperty(dummy, "data[0]"));
1063    }
1064
1065
1066    public void testForced() {
1067        XBean x = new XBean();
1068        assertTrue(BeanUtil.hasDeclaredProperty(x, "y"));
1069        assertFalse(BeanUtil.hasDeclaredProperty(x, "y.foo"));
1070        assertFalse(BeanUtil.hasDeclaredProperty(x, "y[23].foo"));
1071        try{
1072            BeanUtil.setProperty(x, "y.foo", "yyy");
1073            fail();
1074        } catch(Exception JavaDoc ex) {}
1075        assertNull(x.getY());
1076
1077        BeanUtil.setPropertyForced(x, "y.foo", "yyy");
1078        assertTrue(BeanUtil.hasDeclaredProperty(x, "y.foo"));
1079        assertEquals("yyy", x.getY().getFoo());
1080
1081        assertNotNull(x.getYy());
1082        assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[2].foo"));
1083        try{
1084            BeanUtil.setProperty(x, "yy[2].foo", "yyy");
1085            fail();
1086        } catch(Exception JavaDoc ex) {
1087        }
1088        assertNull(x.getYy()[2]);
1089
1090        BeanUtil.setPropertyForced(x, "yy[2].foo", "xxx");
1091        assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[2].foo"));
1092        assertEquals("xxx", x.getYy()[2].getFoo());
1093
1094        assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[20].foo"));
1095        BeanUtil.setPropertyForced(x, "yy[20].foo", "zzz");
1096        assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[20].foo"));
1097        assertEquals("zzz", x.getYy()[20].getFoo());
1098    }
1099
1100
1101    public void testSilent() {
1102        FooBean fb = new FooBean();
1103        assertFalse(BeanUtil.hasDeclaredProperty(fb, "notexisting"));
1104        try {
1105            BeanUtil.setProperty(fb, "notexisting", null);
1106            fail();
1107        } catch(Exception JavaDoc ex) {}
1108        assertFalse(BeanUtil.setPropertySilent(fb, "notexisting", null));
1109        
1110    }
1111}
Popular Tags