KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > beans > test > JavaBeanTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.beans.test;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.List JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.inversoft.beans.BeanException;
17 import com.inversoft.beans.BeanProperty;
18 import com.inversoft.beans.IndexedBeanProperty;
19 import com.inversoft.beans.JavaBean;
20 import com.inversoft.beans.NestedBeanProperty;
21 import com.inversoft.util.typeconverter.TypeConversionException;
22
23
24 /**
25  * This class contains all the tests for the JavaBean class.
26  * This should not directly test any other class.
27  *
28  * @author Brian Pontarelli
29  */

30 public class JavaBeanTest extends TestCase {
31
32     /** Constructs a new test instance */
33     public JavaBeanTest(String JavaDoc name) {
34         super(name);
35     }
36
37
38     /**
39      * Tests the constructores
40      */

41     public void testConstructors() {
42
43         try {
44             JavaBean jb = new JavaBean(Bean1.class);
45             assertEquals("com.inversoft.beans.test.Bean1", jb.getClassName());
46             jb = new JavaBean("com.inversoft.beans.test.Bean1");
47             assertEquals("com.inversoft.beans.test.Bean1", jb.getClassName());
48         } catch (BeanException e) {
49             fail(e.toString());
50         }
51
52         try {
53             new JavaBean("bad.class");
54             fail("Should have failed");
55         } catch (BeanException e) {
56             // expected
57
}
58     }
59
60     /**
61      * Tests the adding of a local property to the JavaBean
62      */

63     public void testGetLocalProperty() {
64
65         try {
66             JavaBean jb = new JavaBean(Bean1.class);
67             jb.getBeanProperty("property1");
68             BeanProperty bp = jb.getBeanProperty("property1");
69             assertTrue("Should never be null", bp != null);
70         } catch (BeanException e) {
71             fail(e.toString());
72         }
73     }
74
75     /**
76      * Tests the adding of a deep property to the JavaBean
77      */

78     public void testGetDeepProperty() {
79
80         // Tests the adding of a deep property
81
try {
82             JavaBean jb = new JavaBean(Bean1.class);
83             jb.getBeanProperty("property1.property2.property3");
84         } catch (BeanException e) {
85             fail(e.toString());
86         }
87
88         try {
89             JavaBean jb = new JavaBean(Bean1.class);
90             //System.err.println("START");
91
jb.getBeanProperty("property1");
92             jb.getBeanProperty("property1.property2");
93             jb.getBeanProperty("property1.property2.property3");
94             //System.err.println("END");
95
} catch (BeanException e) {
96             fail(e.toString());
97         }
98     }
99
100     /**
101      * Tests that the JavaBean instances for children are correct
102      */

103     public void testChildJavaBeans() {
104
105         // Tests the adding of a deep property and then getting the child java bean
106
// instances out and tests them
107
try {
108             JavaBean jb = new JavaBean(Bean1.class);
109             jb.getBeanProperty("property1.property2.property3");
110             JavaBean c1 = jb.getChildJavaBean("property1");
111             assertTrue("children should never be null", c1 != null);
112             assertTrue("children should be of type Bean2", c1.getBeanClass() == Bean2.class);
113             JavaBean c2 = jb.getChildJavaBean("property1.property2");
114             assertTrue("children should never be null 2", c2 != null);
115             assertTrue("children should be of type Bean3", c2.getBeanClass() == Bean3.class);
116             JavaBean c3 = jb.getChildJavaBean("property1.property2.property3");
117             assertTrue("children should never be null 3", c3 != null);
118             assertTrue("children should be of type String", c3.getBeanClass() == String JavaDoc.class);
119         } catch (BeanException e) {
120             fail(e.toString());
121         }
122     }
123
124     /**
125      * Tests that the bean property instances for nested properties are correct
126      */

127     public void testChildBenProperties() {
128
129         // Tests the adding of a deep property and then getting the child bean property
130
// instances out and tests them
131
try {
132             JavaBean jb = new JavaBean(Bean1.class);
133             jb.getBeanProperty("property1.property2.property3");
134             BeanProperty bp = jb.getBeanProperty("property1");
135             assertTrue("Should never be null", bp != null);
136             assertTrue("Should be of type Bean2", bp.getPropertyType() == Bean2.class);
137             BeanProperty bp2 = jb.getBeanProperty("property1.property2");
138             assertTrue("Should never be null 2", bp2 != null);
139             assertTrue("Should be of type Bean3", bp2.getPropertyType() == Bean3.class);
140             BeanProperty bp3 = jb.getBeanProperty("property1.property2.property3");
141             assertTrue("Should never be null 3", bp3 != null);
142             assertTrue("Should be of type String", bp3.getPropertyType() == String JavaDoc.class);
143         } catch (BeanException e) {
144             fail(e.toString());
145         }
146     }
147
148     /**
149      * Tests the child beans that don't exist cause the correct errors
150      */

151     public void testChildJavaBeanErrors() {
152
153         try {
154             JavaBean jb = new JavaBean(Bean1.class);
155             jb.getBeanProperty("property2");
156             fail("Should have failed because Bean1 has no property2");
157         } catch (BeanException e) {
158             //System.err.println("Good! " + e.toString());
159
assertTrue("Should have a root cause of NoSuchMethodException",
160                 e.getCause() != null && e.getCause() instanceof NoSuchMethodException JavaDoc);
161         }
162
163         try {
164             JavaBean jb = new JavaBean(Bean1.class);
165             jb.getBeanProperty("property1.property3");
166             fail("Should have failed because Bean2 has no property3");
167         } catch (BeanException e) {
168             //System.err.println("Good! " + e.toString());
169
assertTrue("Should have a root cause of NoSuchMethodException",
170                 e.getCause() != null && e.getCause() instanceof NoSuchMethodException JavaDoc);
171             assertTrue("Should NOT have a target", e.getTarget() == null);
172         }
173     }
174
175     /**
176      * Tests getting of local bean properties
177      */

178     public void testLocalGetting() {
179
180         // Test local property null
181
try {
182             Bean1 bean = new Bean1();
183             JavaBean jb = new JavaBean(Bean1.class);
184             jb.getBeanProperty("property1");
185             assertTrue("Should be null", jb.getPropertyValue("property1", bean, true) == null);
186         } catch (BeanException e) {
187             fail(e.toString());
188         }
189
190         // Test local property get
191
try {
192             Bean1 bean = new Bean1();
193             Bean2 bean2 = new Bean2();
194             bean.setProperty1(bean2);
195             JavaBean jb = new JavaBean(Bean1.class);
196             jb.getBeanProperty("property1");
197             assertTrue("Should be bean2 using prepopulated JavaBean",
198                        jb.getPropertyValue("property1", bean) == bean2);
199         } catch (BeanException e) {
200             fail(e.toString());
201         }
202
203         // Test local property without add
204
try {
205             Bean1 bean = new Bean1();
206             Bean2 bean2 = new Bean2();
207             bean.setProperty1(bean2);
208             JavaBean jb = new JavaBean(Bean1.class);
209             assertTrue("Should be bean2 using prepopulated JavaBean again",
210                        jb.getPropertyValue("property1", bean) == bean2);
211         } catch (BeanException e) {
212             fail(e.toString());
213         }
214     }
215
216     /**
217      * Tests getting of nested bean properties
218      */

219     public void testNestedGetting() {
220
221         // Test a nested property failure
222
try {
223             Bean1 bean = new Bean1();
224             JavaBean jb = new JavaBean(Bean1.class);
225             jb.getBeanProperty("property1.property2.property3");
226             jb.getPropertyValue("property1.property2.property3", bean, true);
227             fail("Should have error out because property1 is null");
228         } catch (BeanException e) {
229            // System.err.println("Good! " + e.toString());
230
assertTrue("Should not have a root cause or target",
231                 e.getCause() == null && e.getTarget() == null);
232         }
233
234         // Test a nested property get using instance of operator
235
try {
236             Bean1 bean = new Bean1();
237             bean.setProperty1(new Bean2());
238             bean.getProperty1().setProperty2(new Bean3());
239             bean.getProperty1().getProperty2().setProperty3("foo");
240             JavaBean jb = new JavaBean(Bean1.class);
241             jb.getBeanProperty("property1.property2.property3");
242             Object JavaDoc obj = jb.getPropertyValue("property1.property2", bean);
243             assertTrue("Should be a Bean3 instance prepopulated JavaBean", obj instanceof Bean3);
244         } catch (BeanException e) {
245             //e.printStackTrace();
246
fail(e.toString());
247         }
248
249         // Test a nested property get
250
try {
251             Bean1 bean = new Bean1();
252             bean.setProperty1(new Bean2());
253             bean.getProperty1().setProperty2(new Bean3());
254             bean.getProperty1().getProperty2().setProperty3("foo");
255             JavaBean jb = new JavaBean(Bean1.class);
256             jb.getBeanProperty("property1.property2.property3");
257             String JavaDoc value = (String JavaDoc) jb.getPropertyValue("property1.property2.property3", bean);
258             assertTrue("Should be foo using prepopulated JavaBean", value.equals("foo"));
259         } catch (BeanException e) {
260             //e.printStackTrace();
261
fail(e.toString());
262         }
263
264         // Test a nested property get without add
265
try {
266             Bean1 bean = new Bean1();
267             bean.setProperty1(new Bean2());
268             bean.getProperty1().setProperty2(new Bean3());
269             bean.getProperty1().getProperty2().setProperty3("foo");
270
271             JavaBean jb = new JavaBean(Bean1.class);
272             String JavaDoc value = (String JavaDoc) jb.getPropertyValue("property1.property2.property3", bean);
273             assertTrue("Should be foo using prepopulated JavaBean", value.equals("foo"));
274         } catch (BeanException e) {
275             //e.printStackTrace();
276
fail(e.toString());
277         }
278     }
279
280     /**
281      * Tests the setting of nested bean properties
282      */

283     public void testLocalSetting() {
284
285         // Test local set null
286
try {
287             Bean1 bean = new Bean1();
288             JavaBean jb = new JavaBean(Bean1.class);
289             bean.setProperty1(new Bean2());
290             jb.getBeanProperty("property1");
291             jb.setPropertyValue("property1", bean, null, /*convert=*/false, true);
292             assertTrue("Should have been set to null", bean.getProperty1() == null);
293         } catch (BeanException e) {
294             fail(e.toString());
295         } catch (TypeConversionException tce) {
296             fail(tce.toString());
297         }
298
299         // Test local set success
300
try {
301             Bean1 bean = new Bean1();
302             Bean2 bean2 = new Bean2();
303             JavaBean jb = new JavaBean(Bean1.class);
304             jb.getBeanProperty("property1");
305             jb.setPropertyValue("property1", bean, bean2);
306             assertTrue("Should be set to same object", bean.getProperty1() == bean2);
307         } catch (BeanException e) {
308             fail(e.toString());
309         }
310
311         // Test local set without add
312
try {
313             Bean1 bean = new Bean1();
314             Bean2 bean2 = new Bean2();
315             JavaBean jb = new JavaBean(Bean1.class);
316             jb.setPropertyValue("property1", bean, bean2);
317             assertTrue("Should be set to same object", bean.getProperty1() == bean2);
318         } catch (BeanException e) {
319             fail(e.toString());
320         }
321     }
322
323     /**
324      * Tests the setting of nested bean properties
325      */

326     public void testNestedSetting() {
327
328         // Test nested set failure
329
try {
330             Bean1 bean = new Bean1();
331             JavaBean jb = new JavaBean(Bean1.class);
332             jb.getBeanProperty("property1.property2.property3");
333             jb.setPropertyValue("property1.property2.property3", bean, "foo", /*convert=*/false, true);
334             fail("Should have error out because property1 is null");
335         } catch (BeanException e) {
336             //System.err.println("Good! " + e.toString());
337
assertTrue("Should not have a root cause or target",
338                 e.getCause() == null && e.getTarget() == null);
339         } catch (TypeConversionException tce) {
340             fail(tce.toString());
341         }
342
343         // Test nested set success
344
try {
345             Bean1 bean = new Bean1();
346             JavaBean jb = new JavaBean(Bean1.class);
347             jb.getBeanProperty("property1.property2.property3");
348             jb.setPropertyValue("property1.property2.property3", bean, "foo", /*convert=*/false);
349             assertTrue("Should be set to foo", bean.getProperty1().getProperty2().getProperty3().equals("foo"));
350         } catch (BeanException e) {
351             fail(e.toString());
352         } catch (TypeConversionException tce) {
353             fail(tce.toString());
354         }
355
356         // Test nested set partials
357
try {
358             Bean1 bean = new Bean1();
359             JavaBean jb = new JavaBean(Bean1.class);
360             jb.getBeanProperty("property1.property2.property3");
361             jb.setPropertyValue("property1", bean, new Bean2(), /*convert=*/false);
362             assertTrue("Should have a bean2", bean.getProperty1() != null);
363             assertTrue("Should not have a bean3", bean.getProperty1().getProperty2() == null);
364         } catch (BeanException e) {
365             fail(e.toString());
366         } catch (TypeConversionException tce) {
367             fail(tce.toString());
368         }
369
370         // Test nested set without add
371
try {
372             Bean1 bean = new Bean1();
373             JavaBean jb = new JavaBean(Bean1.class);
374             jb.setPropertyValue("property1.property2.property3", bean, "foo", /*convert=*/false);
375             assertTrue("Should be set to foo", bean.getProperty1().getProperty2().getProperty3().equals("foo"));
376         } catch (BeanException e) {
377             fail(e.toString());
378         } catch (TypeConversionException tce) {
379             fail(tce.toString());
380         }
381     }
382
383     /**
384      * Tests the getting of indexed properties
385      */

386     public void testGettingIndexed() {
387
388         // Test a simple local property
389
try {
390             Bean1 bean = new Bean1();
391             Bean2 bean2 = new Bean2();
392             bean.setIndexed(0, bean2);
393             JavaBean jb = new JavaBean(Bean1.class);
394             jb.getBeanProperty("indexed");
395             assertTrue("Should be same object", jb.getPropertyValue("indexed[0]", bean) == bean2);
396         } catch (BeanException be) {
397             fail(be.toString());
398         }
399
400         // Test a indexed property
401
try {
402             Bean1 bean = new Bean1();
403             bean.setIndexed(0, new Bean2());
404             bean.getIndexed(0).setName("foo");
405             JavaBean jb = new JavaBean(Bean1.class);
406             jb.getBeanProperty("indexed.name");
407             assertTrue("Should be foo", jb.getPropertyValue("indexed[0].name", bean).equals("foo"));
408         } catch (BeanException be) {
409             fail(be.toString());
410         }
411
412         // Test a indexed property failure
413
try {
414             Bean1 bean = new Bean1();
415             bean.setIndexed(0, new Bean2());
416             bean.getIndexed(0).setName("foo");
417             JavaBean jb = new JavaBean(Bean1.class);
418             jb.getBeanProperty("indexed.name");
419             jb.getPropertyValue("indexed[1].name", bean, true);
420             fail("Should have errored out because indexed[1] is null");
421         } catch (BeanException be) {
422             //System.err.println("Good! " + be.toString());
423
assertTrue("Should not have a root cause or target",
424                 be.getCause() == null && be.getTarget() == null);
425         }
426
427         // Test a deep indexed property
428
try {
429             Bean1 bean = new Bean1();
430             bean.setIndexed(0, new Bean2());
431             bean.getIndexed(0).setProperty2(new Bean3());
432             bean.getIndexed(0).getProperty2().setProperty3("foo");
433             JavaBean jb = new JavaBean(Bean1.class);
434             jb.getBeanProperty("indexed.property2.property3");
435             assertTrue("Should have been foo",
436                        jb.getPropertyValue("indexed[0].property2.property3", bean, true).equals("foo"));
437         } catch (BeanException be) {
438             fail(be.toString());
439         }
440     }
441
442     /**
443      * Tests the getting of indexed properties with indices
444      */

445     public void testGettingIndexedIndices() {
446
447         // Test a simple local property
448
try {
449             Bean1 bean = new Bean1();
450             Bean2 bean2 = new Bean2();
451             bean.setIndexed(0, bean2);
452             JavaBean jb = new JavaBean(Bean1.class);
453             jb.getBeanProperty("indexed");
454             assertTrue("Should be same object", jb.getPropertyValue("indexed", bean, new int[][]{{0}}) == bean2);
455         } catch (BeanException be) {
456             fail(be.toString());
457         }
458
459         // Test a indexed property
460
try {
461             Bean1 bean = new Bean1();
462             bean.setIndexed(0, new Bean2());
463             bean.getIndexed(0).setName("foo");
464             JavaBean jb = new JavaBean(Bean1.class);
465             jb.getBeanProperty("indexed.name");
466             assertTrue("Should be foo", jb.getPropertyValue("indexed.name", bean, new int[][]{{0}}).equals("foo"));
467         } catch (BeanException be) {
468             fail(be.toString());
469         }
470
471         // Test a indexed property failure
472
try {
473             Bean1 bean = new Bean1();
474             bean.setIndexed(0, new Bean2());
475             bean.getIndexed(0).setName("foo");
476             JavaBean jb = new JavaBean(Bean1.class);
477             jb.getBeanProperty("indexed.name");
478             jb.getPropertyValue("indexed.name", bean, new int[][]{{1}}, true);
479             fail("Should have errored out because indexed[1] is null");
480         } catch (BeanException be) {
481             //System.err.println("Good! " + be.toString());
482
assertTrue("Should not have a root cause or target",
483                 be.getCause() == null && be.getTarget() == null);
484         }
485
486         // Test a deep indexed property
487
try {
488             Bean1 bean = new Bean1();
489             bean.setIndexed(0, new Bean2());
490             bean.getIndexed(0).setProperty2(new Bean3());
491             bean.getIndexed(0).getProperty2().setProperty3("foo");
492             JavaBean jb = new JavaBean(Bean1.class);
493             jb.getBeanProperty("indexed.property2.property3");
494             assertTrue("Should have been foo",
495                        jb.getPropertyValue("indexed.property2.property3", bean, new int[][]{{0}}, true).equals("foo"));
496         } catch (BeanException be) {
497             fail(be.toString());
498         }
499
500         // Test a double deep indexed property
501
try {
502             Bean1 bean = new Bean1();
503             bean.setIndexed(0, new Bean2());
504             bean.getIndexed(0).setIndexed2(0, new Bean3());
505             bean.getIndexed(0).getIndexed2(0).setProperty3("foo");
506             JavaBean jb = new JavaBean(Bean1.class);
507             jb.getBeanProperty("indexed.indexed2.property3");
508             assertTrue("Should have been foo",
509                        jb.getPropertyValue("indexed.indexed2.property3", bean, new int[][]{{0}, {0}}, true).equals("foo"));
510         } catch (BeanException be) {
511             fail(be.toString());
512         }
513
514         // Test a double deep indexed property with list
515
try {
516             List JavaDoc list = new ArrayList JavaDoc();
517             List JavaDoc list1 = new ArrayList JavaDoc();
518             List JavaDoc list2 = new ArrayList JavaDoc();
519             list.add(list1);
520             list.add(list2);
521             list1.add(new Integer JavaDoc(0));
522             list2.add(new Integer JavaDoc(0));
523             Bean1 bean = new Bean1();
524             bean.setIndexed(0, new Bean2());
525             bean.getIndexed(0).setIndexed2(0, new Bean3());
526             bean.getIndexed(0).getIndexed2(0).setProperty3("foo");
527             JavaBean jb = new JavaBean(Bean1.class);
528             jb.getBeanProperty("indexed.indexed2.property3");
529             assertTrue("Should have been foo",
530                        jb.getPropertyValue("indexed.indexed2.property3", bean, list, true).equals("foo"));
531         } catch (BeanException be) {
532             fail(be.toString());
533         }
534
535         // Test a double deep indexed property with list failure
536
try {
537             List JavaDoc list = new ArrayList JavaDoc();
538             List JavaDoc list1 = new ArrayList JavaDoc();
539             List JavaDoc list2 = new ArrayList JavaDoc();
540             list.add(list1);
541             list.add(list2);
542             list1.add(new Integer JavaDoc(0));
543             list2.add(new String JavaDoc("foo"));
544             Bean1 bean = new Bean1();
545             bean.setIndexed(0, new Bean2());
546             bean.getIndexed(0).setIndexed2(0, new Bean3());
547             bean.getIndexed(0).getIndexed2(0).setProperty3("foo");
548             JavaBean jb = new JavaBean(Bean1.class);
549             jb.getBeanProperty("indexed.indexed2.property3");
550             jb.getPropertyValue("indexed.indexed2.property3", bean, list, true);
551             fail("Should have thrown an exception because the indices list contains a String");
552         } catch (BeanException be) {
553             System.err.println(be.toString());
554             assertTrue("Should not have a root cause or target", be.getCause() == null && be.getTarget() == null);
555         }
556     }
557
558     /**
559      * Tests the setting of indexed properties
560      */

561     public void testSettingIndexed() {
562
563         // Test a local set
564
try {
565             Bean1 bean = new Bean1();
566             Bean2 bean2 = new Bean2();
567             JavaBean jb = new JavaBean(Bean1.class);
568             jb.getBeanProperty("indexed");
569             jb.setPropertyValue("indexed[0]", bean, bean2, false, true);
570             assertTrue("Should be same object", bean.getIndexed(0) == bean2);
571         } catch (BeanException be) {
572             fail(be.toString());
573         }
574
575         // Test a indexed set failure
576
try {
577             Bean1 bean = new Bean1();
578             JavaBean jb = new JavaBean(Bean1.class);
579             jb.getBeanProperty("indexed.name");
580             jb.setPropertyValue("indexed[0].name", bean, "foo", true, true);
581             fail("Should have thrown and exception because indexed[0] is null");
582         } catch (BeanException be) {
583             //System.out.println("Good! " + be.toString());
584
assertTrue("Should not have a root cause or target",
585                 be.getCause() == null && be.getTarget() == null);
586         }
587
588         // Test a indexed set success
589
try {
590             Bean1 bean = new Bean1();
591             JavaBean jb = new JavaBean(Bean1.class);
592             jb.getBeanProperty("indexed.name");
593             jb.setPropertyValue("indexed[0].name", bean, "foo", false);
594             assertTrue("Should be foo", bean.getIndexed(0).getName().equals("foo"));
595         } catch (BeanException be) {
596             fail(be.toString());
597         }
598
599         // Test conversions
600
try {
601             Bean1 bean = new Bean1();
602             JavaBean jb = new JavaBean(Bean1.class);
603             jb.getBeanProperty("indexed.property2.integer3");
604             jb.setPropertyValue("indexed[0].property2.integer3", bean, "16", true, false);
605             assertTrue("Should be 16", bean.getIndexed(0).getProperty2().getInteger3().equals(new Integer JavaDoc(16)));
606         } catch (BeanException be) {
607             fail(be.toString());
608         } catch (TypeConversionException tce) {
609             fail(tce.toString());
610         }
611     }
612
613     /**
614      * Tests the instantiate method
615      */

616     public void testInstantiate() {
617
618         try {
619             JavaBean jb = new JavaBean(Bean1.class);
620             Object JavaDoc bean1 = jb.instantiate();
621             assertTrue("Should be an instance of Bean1", bean1 instanceof Bean1);
622         } catch (BeanException be) {
623             fail(be.toString());
624         }
625     }
626
627     /**
628      * Tests the setting of indexed properties
629      */

630     public void testIndexedProperty() {
631
632         // Test using the getIndexedBeanProperty method
633
try {
634             Bean1 bean = new Bean1();
635             Bean2 bean2 = new Bean2();
636             JavaBean jb = new JavaBean(Bean1.class);
637             IndexedBeanProperty ibp = jb.getIndexedBeanProperty("indexed");
638             jb.setPropertyValue("indexed[0]", bean, bean2, true, true);
639             assertTrue("Should be same object", ibp.getPropertyValue(bean, 0) == bean2);
640         } catch (BeanException be) {
641             be.printStackTrace();
642             fail(be.toString());
643         }
644
645         // Test a using the isBeanPropertyIndexed method
646
try {
647             JavaBean jb = new JavaBean(Bean1.class);
648             assertTrue("Should be indexed", jb.isBeanPropertyIndexed("indexed"));
649         } catch (BeanException be) {
650             be.printStackTrace();
651             fail(be.toString());
652         }
653
654         // Test using the getIndexedBeanProperty method deep
655
try {
656             Bean1 bean = new Bean1();
657             Bean2 bean2 = new Bean2();
658             Bean3 bean3 = new Bean3();
659             JavaBean jb = new JavaBean(Bean1.class);
660             IndexedBeanProperty ibp = jb.getIndexedBeanProperty("property1.indexed2");
661
662             bean.setProperty1(bean2);
663             jb.setPropertyValue("property1.indexed2[0]", bean, bean3, true, true);
664             assertTrue("Should be same object", ibp.getPropertyValue(bean2, 0) == bean3);
665         } catch (BeanException be) {
666             be.printStackTrace();
667             fail(be.toString());
668         }
669
670         // Test using the isBeanPropertyIndexed method deep
671
try {
672             JavaBean jb = new JavaBean(Bean1.class);
673             assertTrue("Should be indexed", jb.isBeanPropertyIndexed("property1.indexed2"));
674         } catch (BeanException be) {
675             be.printStackTrace();
676             fail(be.toString());
677         }
678     }
679
680     /**
681      * Tests getting the NestedBeanProperty from the JavaBean
682      */

683     public void testNestedBeanProperty() {
684
685         // Test using the getNestedBeanProperty method
686
try {
687             Bean1 bean = new Bean1();
688             Bean2 bean2 = new Bean2();
689             JavaBean jb = new JavaBean(Bean1.class);
690             NestedBeanProperty nbp = jb.getNestedBeanProperty("indexed[0]");
691             nbp.setPropertyValue(bean, bean2);
692             assertEquals("Should be same object", nbp.getPropertyValue(bean), bean2);
693             assertEquals("Should contain root property", nbp.getRootProperty(),
694                 jb.getBeanProperty("indexed"));
695         } catch (BeanException be) {
696             be.printStackTrace();
697             fail(be.toString());
698         }
699
700         // Test using the getNestedBeanProperty method deep
701
try {
702             Bean1 bean1 = new Bean1();
703             Bean2 bean2 = new Bean2();
704             Bean3 bean3 = new Bean3();
705             JavaBean jb = new JavaBean(Bean1.class);
706             NestedBeanProperty nbp = jb.getNestedBeanProperty("property1.indexed2[0]");
707
708             bean1.setProperty1(bean2);
709             nbp.setPropertyValue(bean1, bean3);
710             assertEquals("Should be same object", nbp.getPropertyValue(bean1), bean3);
711             assertEquals("Should contain root property", nbp.getRootProperty(),
712                 jb.getBeanProperty("property1"));
713         } catch (BeanException be) {
714             be.printStackTrace();
715             fail(be.toString());
716         }
717
718         // Test using the getNestedBeanProperty method deep
719
try {
720             Bean1 bean1 = new Bean1();
721             Bean2 bean2 = new Bean2();
722             JavaBean jb = new JavaBean(Bean1.class);
723             NestedBeanProperty nbp = jb.getNestedBeanProperty("property1.name");
724
725             bean1.setProperty1(bean2);
726             nbp.setPropertyValue(bean1, "fred");
727             assertEquals("Should be same object", nbp.getPropertyValue(bean1), "fred");
728             assertEquals("Should contain root property", nbp.getRootProperty(),
729                 jb.getBeanProperty("property1"));
730             assertEquals("Should be right type", Bean1.class, nbp.getBeanClass());
731             assertEquals("Should have property name", "property1.name", nbp.getPropertyName());
732             assertEquals("Should have property name", "property1.name", nbp.getFullName());
733         } catch (BeanException be) {
734             be.printStackTrace();
735             fail(be.toString());
736         }
737     }
738
739     /**
740      * Tests single array retrieval locally and nested
741      */

742     public void testSingleArrayGet() {
743         try {
744             Bean1 bean1 = new Bean1();
745             Bean2 bean2 = new Bean2();
746             JavaBean jb = new JavaBean(Bean1.class);
747
748             bean1.getSingleArray()[0] = bean2;
749
750             assertEquals("Should have value bean2", jb.getPropertyValue("singleArray[0]", bean1), bean2);
751         } catch (BeanException be) {
752             fail(be.toString());
753         }
754
755         try {
756             Bean1 bean1 = new Bean1();
757             Bean2 bean2 = new Bean2();
758             JavaBean jb = new JavaBean(Bean1.class);
759
760             bean1.getSingleArray()[0] = bean2;
761             bean2.setName("fred");
762
763             assertEquals("Should have value fred", jb.getPropertyValue("singleArray[0].name", bean1), "fred");
764         } catch (BeanException be) {
765             fail(be.toString());
766         }
767
768         // Test failure
769
try {
770             Bean1 bean1 = new Bean1();
771             JavaBean jb = new JavaBean(Bean1.class);
772
773             jb.getPropertyValue("singleArray[0].name", bean1, true);
774             fail("Should have failed because of strictness");
775         } catch (BeanException be) {
776             // Smother
777
}
778
779         try {
780             Bean1 bean1 = new Bean1();
781             JavaBean jb = new JavaBean(Bean1.class);
782
783             assertNull("Should have value null", jb.getPropertyValue("singleArray[0].name", bean1));
784         } catch (BeanException be) {
785             fail(be.toString());
786         }
787
788         try {
789             Bean1 bean1 = new Bean1();
790             Bean2 bean2 = new Bean2();
791             JavaBean jb = new JavaBean(Bean1.class);
792
793             bean1.getSingleArray()[0] = bean2;
794
795             assertNull("Should be null in nesting", jb.getPropertyValue("singleArray[0].name", bean1));
796         } catch (BeanException be) {
797             fail(be.toString());
798         }
799     }
800
801     /**
802      * Tests single array setting locally and nested
803      */

804     public void testSingleArraySet() {
805         try {
806             Bean1 bean1 = new Bean1();
807             Bean2 bean2 = new Bean2();
808             JavaBean jb = new JavaBean(Bean1.class);
809
810             jb.setPropertyValue("singleArray[0]", bean1, bean2, true, true);
811             assertEquals("Should have value bean2", bean1.getSingleArray()[0], bean2);
812         } catch (BeanException be) {
813             fail(be.toString());
814         }
815
816         try {
817             Bean1 bean1 = new Bean1();
818             Bean2 bean2 = new Bean2();
819             JavaBean jb = new JavaBean(Bean1.class);
820
821             bean1.getSingleArray()[0] = bean2;
822
823             jb.setPropertyValue("singleArray[0].name", bean1, "fred", true, true);
824             assertEquals("Should have value fred", bean1.getSingleArray()[0].getName(), "fred");
825         } catch (BeanException be) {
826             fail(be.toString());
827         }
828
829         // Test failure
830
try {
831             Bean1 bean1 = new Bean1();
832             JavaBean jb = new JavaBean(Bean1.class);
833
834             jb.setPropertyValue("singleArray[0].name", bean1, "fred", true, true);
835             fail("Should have failed because of strictness");
836         } catch (BeanException be) {
837             // Smother
838
}
839
840         // Test creation of the array
841
try {
842             Bean1 bean1 = new Bean1();
843             JavaBean jb = new JavaBean(Bean1.class);
844
845             bean1.setSingleArray(null);
846
847             jb.setPropertyValue("singleArray[2].name", bean1, "fred", true, false);
848             assertEquals("Should have an array of length 3", bean1.getSingleArray().length, 3);
849             assertNotNull("Should have a Bean2 at index 2", bean1.getSingleArray()[2]);
850             assertEquals("Should be fred", bean1.getSingleArray()[2].getName(), "fred");
851         } catch (BeanException be) {
852             fail(be.toString());
853         }
854
855         // Test creation of the array using Object
856
try {
857             Bean1 bean1 = new Bean1();
858             Bean2 bean2 = new Bean2();
859             JavaBean jb = new JavaBean(Bean1.class);
860
861             bean1.setSingleArrayObject(null);
862
863             jb.setPropertyValue("singleArrayObject[2]", bean1, bean2, true, false);
864             assertEquals("Should have an array of length 3", bean1.getSingleArrayObject().length, 3);
865             assertNotNull("Should have a Bean2 at index 2", bean1.getSingleArrayObject()[2]);
866             assertTrue("Should have a Bean2 at index 2", bean1.getSingleArrayObject()[2] instanceof Bean2);
867             assertEquals("Should be fred", bean1.getSingleArrayObject()[2], bean2);
868         } catch (BeanException be) {
869             fail(be.toString());
870         }
871
872         // Test Object failure
873
try {
874             Bean1 bean1 = new Bean1();
875             Bean2 bean2 = new Bean2();
876             JavaBean jb = new JavaBean(Bean1.class);
877
878             bean1.setSingleArrayObject(null);
879
880             jb.setPropertyValue("singleArrayObject[2].name", bean1, bean2, true, false);
881             fail("Should have failed because name is not on Object");
882         } catch (BeanException be) {
883             System.out.println(be.toString());
884         }
885     }
886
887     /**
888      * Tests multi array retrieval locally and nested
889      */

890     public void testMultiArrayGet() {
891         try {
892             Bean1 bean1 = new Bean1();
893             Bean2 bean2 = new Bean2();
894             JavaBean jb = new JavaBean(Bean1.class);
895
896             bean1.setMultiArray(new Bean2[1][2][3]);
897             bean1.getMultiArray()[0][1][2] = bean2;
898
899             assertEquals("Should have value bean2", jb.getPropertyValue("multiArray[0][1][2]", bean1), bean2);
900         } catch (BeanException be) {
901             fail(be.toString());
902         }
903
904         try {
905             Bean1 bean1 = new Bean1();
906             Bean2 bean2 = new Bean2();
907             JavaBean jb = new JavaBean(Bean1.class);
908
909             bean1.setMultiArray(new Bean2[1][2][5]);
910             bean1.getMultiArray()[0][1][4] = bean2;
911             bean2.setName("fred");
912
913             assertEquals("Should have value fred", jb.getPropertyValue("multiArray[0][1][4].name", bean1), "fred");
914         } catch (BeanException be) {
915             fail(be.toString());
916         }
917
918         // Test failure
919
try {
920             Bean1 bean1 = new Bean1();
921             JavaBean jb = new JavaBean(Bean1.class);
922
923             jb.getPropertyValue("multiArray[0].name", bean1, true);
924             fail("Should have failed because of strictness");
925         } catch (BeanException be) {
926             // Smother
927
}
928
929         try {
930             Bean1 bean1 = new Bean1();
931             JavaBean jb = new JavaBean(Bean1.class);
932
933             assertNull("Should have value null", jb.getPropertyValue("multiArray[0][1][1].name", bean1));
934         } catch (BeanException be) {
935             fail(be.toString());
936         }
937
938         try {
939             Bean1 bean1 = new Bean1();
940             Bean2 bean2 = new Bean2();
941             JavaBean jb = new JavaBean(Bean1.class);
942
943             bean1.setMultiArray(new Bean2[3][1][6]);
944             bean1.getMultiArray()[2][0][5] = bean2;
945
946             assertNull("Should be null in nesting", jb.getPropertyValue("multiArray[2][0][5].name", bean1));
947         } catch (BeanException be) {
948             fail(be.toString());
949         }
950     }
951
952     /**
953      * Tests single array setting locally and nested
954      */

955     public void testMultiArraySet() {
956         try {
957             Bean1 bean1 = new Bean1();
958             Bean2 bean2 = new Bean2();
959             JavaBean jb = new JavaBean(Bean1.class);
960
961             bean1.setMultiArray(new Bean2[1][4][2]);
962             jb.setPropertyValue("multiArray[0][3][1]", bean1, bean2, true, true);
963             assertEquals("Should have value bean2", bean1.getMultiArray()[0][3][1], bean2);
964         } catch (BeanException be) {
965             fail(be.toString());
966         }
967
968         try {
969             Bean1 bean1 = new Bean1();
970             Bean2 bean2 = new Bean2();
971             JavaBean jb = new JavaBean(Bean1.class);
972
973             bean1.setMultiArray(new Bean2[1][3][5]);
974             bean1.getMultiArray()[0][2][4] = bean2;
975
976             jb.setPropertyValue("multiArray[0][2][4].name", bean1, "fred", true, true);
977             assertEquals("Should have value fred", bean1.getMultiArray()[0][2][4].getName(), "fred");
978         } catch (BeanException be) {
979             fail(be.toString());
980         }
981
982         // Test failure
983
try {
984             Bean1 bean1 = new Bean1();
985             JavaBean jb = new JavaBean(Bean1.class);
986
987             bean1.setMultiArray(new Bean2[1][4][2]);
988             jb.setPropertyValue("multiArray[0][3][1].name", bean1, "fred", true, true);
989             fail("Should have failed because of strictness");
990         } catch (BeanException be) {
991             // Smother
992
}
993
994         // Test creation of the array
995
try {
996             Bean1 bean1 = new Bean1();
997             JavaBean jb = new JavaBean(Bean1.class);
998
999             bean1.setMultiArray(null);
1000
1001            jb.setPropertyValue("multiArray[2][0][3].name", bean1, "fred", true, false);
1002            assertEquals("Should have an array of length 3", bean1.getMultiArray().length, 3);
1003            assertNotNull("Should have a Bean2 at index 2,0,3", bean1.getMultiArray()[2][0][3]);
1004            assertEquals("Should be fred", bean1.getMultiArray()[2][0][3].getName(), "fred");
1005        } catch (BeanException be) {
1006            fail(be.toString());
1007        }
1008
1009        // Test Object multi array
1010
try {
1011            Bean1 bean1 = new Bean1();
1012            Bean2 bean2 = new Bean2();
1013            JavaBean jb = new JavaBean(Bean1.class);
1014
1015            bean1.setMultiArrayObject(null);
1016
1017            jb.setPropertyValue("multiArrayObject[2][3][1]", bean1, bean2, true, false);
1018            assertEquals("Should have an array of length 3", bean1.getMultiArrayObject().length, 3);
1019            assertNotNull("Should have a Bean2 at index 2", bean1.getMultiArrayObject()[2][3][1]);
1020            assertTrue("Should have a Bean2 at index 2", bean1.getMultiArrayObject()[2][3][1] instanceof Bean2);
1021            assertEquals("Should be bean2", bean1.getMultiArrayObject()[2][3][1], bean2);
1022        } catch (BeanException be) {
1023            fail(be.toString());
1024        }
1025
1026        // Test Object multi array
1027
try {
1028            Bean1 bean1 = new Bean1();
1029            Bean2 bean2 = new Bean2();
1030            JavaBean jb = new JavaBean(Bean1.class);
1031
1032            bean1.setMultiArrayObject(null);
1033
1034            jb.setPropertyValue("multiArrayObject[2][3]", bean1, bean2, true, false);
1035            fail("should have failed because not correct type");
1036        } catch (BeanException be) {
1037            fail("Should be a type conversion exception");
1038        } catch (TypeConversionException tce) {
1039            // Expected
1040
}
1041    }
1042
1043    /**
1044     * Tests Collection usage
1045     */

1046    public void testCollection() {
1047        try {
1048            Bean3 bean3 = new Bean3();
1049            Bean2 bean2 = new Bean2();
1050            JavaBean jb = new JavaBean(Bean3.class);
1051
1052            bean3.getCollection().add(bean2);
1053            bean2.setName("fred");
1054            assertEquals("Should have value fred", "fred", jb.getPropertyValue("collection[0].name", bean3));
1055
1056            List JavaDoc newList = new ArrayList JavaDoc();
1057            jb.setPropertyValue("collection", bean3, newList, true, true);
1058            assertSame("Should have list", bean3.getCollection(), newList);
1059            assertSame("Should have list", jb.getPropertyValue("collection", bean3), newList);
1060
1061            bean3.setCollection(null);
1062            assertNull("Should be null", jb.getPropertyValue("collection[0].foo.bar", bean3));
1063        } catch (BeanException be) {
1064            fail(be.toString());
1065        }
1066
1067        // Test set failure because you can't set a Collection
1068
try {
1069            Bean3 bean3 = new Bean3();
1070            JavaBean jb = new JavaBean(Bean3.class);
1071
1072            bean3.setCollection(new HashSet JavaDoc());
1073            jb.setPropertyValue("collection[0]", bean3, "fred", true, true);
1074            fail("Should fail because you can't set a collection");
1075        } catch (BeanException be) {
1076            System.out.println(be.toString());
1077        }
1078
1079        // Test strict failure
1080
try {
1081            Bean3 bean3 = new Bean3();
1082            JavaBean jb = new JavaBean(Bean3.class);
1083
1084            // Test failure even unstrict
1085
jb.setPropertyValue("collection[0].property1.name", bean3, "fred", true, true);
1086            fail("Should fail for strictness");
1087        } catch (BeanException be) {
1088            System.out.println(be.toString());
1089        }
1090
1091        // Test failure even unstrict
1092
try {
1093            Bean3 bean3 = new Bean3();
1094            JavaBean jb = new JavaBean(Bean3.class);
1095
1096            jb.setPropertyValue("collection[0].property1.name", bean3, "fred", true, false);
1097            fail("Should fail to Collection");
1098        } catch (BeanException be) {
1099            System.out.println(be.toString());
1100        }
1101
1102        // Wierd cases
1103
try {
1104            Bean3 bean3 = new Bean3();
1105            JavaBean jb = new JavaBean(Bean3.class);
1106
1107            bean3.setCollection(new ArrayList JavaDoc());
1108            bean3.getCollection().add(new Bean2[1][1]);
1109            jb.setPropertyValue("collection[0][0][0].name", bean3, "fred", true, false);
1110            assertEquals("fred", ((Bean2[][]) ((ArrayList JavaDoc) bean3.getCollection()).get(0))[0][0].getName());
1111
1112            bean3.setCollection(new ArrayList JavaDoc());
1113            Object JavaDoc[] array = new Object JavaDoc[] {new ArrayList JavaDoc(), new ArrayList JavaDoc(), new ArrayList JavaDoc()};
1114            bean3.getCollection().add(array);
1115            ((ArrayList JavaDoc) array[1]).add(new Bean2());
1116            jb.setPropertyValue("collection[0][1][0].name", bean3, "fred", true, false);
1117            assertEquals("fred",
1118                ((Bean2) ((ArrayList JavaDoc) ((Object JavaDoc[]) ((ArrayList JavaDoc) bean3.getCollection()).get(0))[1]).get(0)).getName());
1119        } catch (BeanException be) {
1120            // Smother
1121
}
1122    }
1123
1124    /**
1125     * Tests Map usage
1126     */

1127    public void testMap() {
1128        try {
1129            Bean3 bean3 = new Bean3();
1130            Bean2 bean2 = new Bean2();
1131            JavaBean jb = new JavaBean(Bean3.class);
1132
1133            bean3.getMap().put("b2", bean2);
1134            bean2.setName("fred");
1135            assertNotNull("Should have Bean2", jb.getPropertyValue("map[b2]", bean3));
1136            assertSame("Should have Bean2", bean2, jb.getPropertyValue("map[b2]", bean3));
1137            assertEquals("Should have value fred", "fred", jb.getPropertyValue("map[b2].name", bean3));
1138
1139            List JavaDoc newList = new ArrayList JavaDoc();
1140            newList.add(bean2);
1141            newList.add(bean2);
1142            jb.setPropertyValue("map[l1]", bean3, newList, true, true);
1143            assertSame("Should have list", newList, bean3.getMap().get("l1"));
1144            assertSame("Should have list", newList, jb.getPropertyValue("map[l1]", bean3));
1145            assertSame("Should have bean2", "fred", jb.getPropertyValue("map[l1][1].name", bean3));
1146            assertSame("Should have fred", "fred", jb.getPropertyValue("map[l1][1].name", bean3));
1147
1148            bean3.setMap(null);
1149            assertNull("Should be null", jb.getPropertyValue("map[0].foo.bar", bean3));
1150        } catch (BeanException be) {
1151            fail(be.toString());
1152        }
1153    }
1154}
Popular Tags