KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > reflection > test > BeanUtilTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.reflection.test;
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24
25 import org.sape.carbon.core.util.reflection.BeanUtil;
26
27 import junit.extensions.ActiveTestSuite;
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 /**
33  * Tests the BeanUtil class.
34  *
35  * Copyright 2002 Sapient
36  * @since carbon 1.0
37  * @author Jordan Reed, August 2002
38  * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/05/05 21:21:23 $)
39  */

40 public class BeanUtilTest extends TestCase {
41    public BeanUtilTest(String JavaDoc name) {
42         super(name);
43     }
44
45     /**
46      * Sets a string property and tests that a simple call to
47      * <code>getObjectAttribute</code> gets back the same value.
48      */

49     public void testGetObjectAttribute()
50         throws
51             NoSuchMethodException JavaDoc,
52             InvocationTargetException JavaDoc,
53             IllegalAccessException JavaDoc {
54
55         String JavaDoc stringValue = "TEST VALUE";
56         SimpleTestBean simpleTestBean = new SimpleTestBean();
57         simpleTestBean.setSimpleString(stringValue);
58
59         String JavaDoc result = (String JavaDoc) BeanUtil.getObjectAttribute(
60                                                 simpleTestBean,
61                                                 "simpleString");
62
63         if(!stringValue.equals(result)) {
64             fail();
65         }
66     }
67
68     /**
69      * Creates a hierachy of java contained java beans, and then
70      * tests that a call to <code>getObjectAttribute</code> will
71      * traverse into the structure and get the embedded property.
72      */

73     public void testGetObjectAttributeEmbeddedBean()
74         throws
75             NoSuchMethodException JavaDoc,
76             InvocationTargetException JavaDoc,
77             IllegalAccessException JavaDoc {
78
79         String JavaDoc stringValue = "TEST VALUE";
80
81         SimpleTestBean simpleTestBean = new SimpleTestBean();
82         simpleTestBean.setSimpleString(stringValue);
83
84         ComplicatedTestBean innerComplicatedTestBean =
85             new ComplicatedTestBean();
86         innerComplicatedTestBean.setSimpleTestBean(simpleTestBean);
87
88         ComplicatedTestBean outerComplicatedTestBean =
89             new ComplicatedTestBean();
90         outerComplicatedTestBean.setComplicatedTestBean(innerComplicatedTestBean);
91
92         String JavaDoc result = (String JavaDoc) BeanUtil.getObjectAttribute(
93                                                 outerComplicatedTestBean,
94                                                 "complicatedTestBean.simpleTestBean.simpleString");
95
96         if(!stringValue.equals(result)) {
97             fail();
98         }
99     }
100
101     /**
102      * Sets a int property on a bean and tests that
103      * <code>getObjectAttribute</code> will retreive back the
104      * same value wrapped in an <code>Integer</code> object.
105      */

106     public void testGetObjectAttributePrimitiveInt()
107         throws
108             NoSuchMethodException JavaDoc,
109             InvocationTargetException JavaDoc,
110             IllegalAccessException JavaDoc {
111
112         int intValue = 10;
113         SimpleTestBean simpleTestBean = new SimpleTestBean();
114         simpleTestBean.setPrimitiveInt(intValue);
115
116         Integer JavaDoc result = (Integer JavaDoc) BeanUtil.getObjectAttribute(
117                                                 simpleTestBean,
118                                                 "primitiveInt");
119
120         if(intValue != result.intValue()) {
121             fail();
122         }
123     }
124
125     /**
126      * Tests that <code>getObjectAttribute</code> will retreive
127      * the value from a read only property.
128      */

129     public void testGetObjectAttributeReadOnly()
130         throws
131             NoSuchMethodException JavaDoc,
132             InvocationTargetException JavaDoc,
133             IllegalAccessException JavaDoc {
134
135         String JavaDoc stringValue = "TEST VALUE";
136         SimpleTestBean simpleTestBean = new SimpleTestBean(stringValue);
137
138         String JavaDoc result = (String JavaDoc) BeanUtil.getObjectAttribute(
139                                                 simpleTestBean,
140                                                 "readOnlyString");
141
142         if(!stringValue.equals(result)) {
143             fail();
144         }
145     }
146
147     /**
148      * Tests that <code>getObjectAttribute</code> will throw a
149      * <code>IllegalAccessException</code> when attempting to
150      * read from a write-only property.
151      */

152     public void testGetObjectAttributeFailWriteOnly()
153         throws
154             NoSuchMethodException JavaDoc,
155             InvocationTargetException JavaDoc,
156             IllegalAccessException JavaDoc {
157
158         String JavaDoc stringValue = "TEST VALUE";
159         SimpleTestBean simpleTestBean = new SimpleTestBean();
160         simpleTestBean.setWriteOnlyString(stringValue);
161
162         try {
163             String JavaDoc result = (String JavaDoc) BeanUtil.getObjectAttribute(
164                                                     simpleTestBean,
165                                                     "writeOnlyString");
166             fail();
167         }
168         catch (IllegalAccessException JavaDoc e) {
169             // Success
170
}
171
172     }
173
174     /**
175      * Tests that <code>getObjectAttribute</code> will successfully
176      * get back a boolean property with the signature of
177      * <code>isPropertyName</code>
178      */

179     public void testGetObjectAttributeSimpleBoolean()
180         throws
181             NoSuchMethodException JavaDoc,
182             InvocationTargetException JavaDoc,
183             IllegalAccessException JavaDoc {
184
185         boolean booleanValue = true;
186         SimpleTestBean simpleTestBean = new SimpleTestBean();
187         simpleTestBean.setSimpleBoolean(booleanValue);
188
189         Boolean JavaDoc result = (Boolean JavaDoc) BeanUtil.getObjectAttribute(
190                                                 simpleTestBean,
191                                                 "simpleBoolean");
192
193         if(booleanValue != result.booleanValue()) {
194             fail();
195         }
196     }
197
198     /**
199      * Tests that <code>getObjectAttribute</code> will successfully
200      * retreive a boolean property that has get&lt;PropertyName&gt;
201      * as its getter method signature.
202      */

203     public void testGetObjectAttributeIncorrectBoolean()
204         throws
205             NoSuchMethodException JavaDoc,
206             InvocationTargetException JavaDoc,
207             IllegalAccessException JavaDoc {
208
209         boolean booleanValue = true;
210         SimpleTestBean simpleTestBean = new SimpleTestBean();
211         simpleTestBean.setIncorrectBoolean(booleanValue);
212
213         Boolean JavaDoc result = (Boolean JavaDoc) BeanUtil.getObjectAttribute(
214                                                 simpleTestBean,
215                                                 "incorrectBoolean");
216
217         if(booleanValue != result.booleanValue()) {
218             fail();
219         }
220     }
221
222     /**
223      * Tests that <code>getObjectAttribute</code> will call the
224      * is&lt;PropertyName&gt; method on a boolean property
225      * if there is also a get&lt;PropertyName&gt; method.
226      */

227     public void testGetObjectAttributeOverlapReadBoolean()
228         throws
229             NoSuchMethodException JavaDoc,
230             InvocationTargetException JavaDoc,
231             IllegalAccessException JavaDoc {
232
233         boolean booleanValue = true;
234         SimpleTestBean simpleTestBean = new SimpleTestBean();
235         simpleTestBean.setOverlapReadBoolean(booleanValue);
236
237         Boolean JavaDoc result = (Boolean JavaDoc) BeanUtil.getObjectAttribute(
238                                                 simpleTestBean,
239                                                 "overlapReadBoolean");
240
241         if(booleanValue != result.booleanValue()) {
242             fail();
243         }
244     }
245
246
247
248     /**
249      * Tests that <code>getObjectAttribute</code> will retrieve
250      * a collection property that has an internal HashSet value.
251      */

252     public void testGetObjectAttributeCollection()
253         throws
254             NoSuchMethodException JavaDoc,
255             InvocationTargetException JavaDoc,
256             IllegalAccessException JavaDoc {
257
258         Collection JavaDoc collection = new HashSet JavaDoc();
259         collection.add("Test String");
260
261         SimpleTestBean simpleTestBean = new SimpleTestBean();
262         simpleTestBean.setCollection(collection);
263
264         Collection JavaDoc result = (Collection JavaDoc) BeanUtil.getObjectAttribute(
265                                                 simpleTestBean,
266                                                 "collection");
267         if(!collection.equals(result)) {
268             fail();
269         }
270     }
271
272     /**
273      * Tests that <code>getObjectAttribute</code> will retrieve
274      * a collection property that has an internal ArrayList value.
275      */

276     public void testGetObjectAttributeCollectionArrayList()
277         throws
278             NoSuchMethodException JavaDoc,
279             InvocationTargetException JavaDoc,
280             IllegalAccessException JavaDoc {
281
282         Collection JavaDoc collection = new ArrayList JavaDoc();
283         collection.add("Test String");
284
285         SimpleTestBean simpleTestBean = new SimpleTestBean();
286         simpleTestBean.setCollection(collection);
287
288         Collection JavaDoc result = (Collection JavaDoc) BeanUtil.getObjectAttribute(
289                                                 simpleTestBean,
290                                                 "collection");
291         if(!collection.equals(result)) {
292             fail();
293         }
294     }
295
296     /**
297      * Tests that <code>getObjectAttribute</code> will return null
298      * for a property with embedded properties, where one of
299      * the results in the embedded structure is null.
300      */

301     public void testGetObjectAttributeWithNull()
302         throws
303             NoSuchMethodException JavaDoc,
304             InvocationTargetException JavaDoc,
305             IllegalAccessException JavaDoc {
306
307         ComplicatedTestBean outerComplicatedTestBean =
308             new ComplicatedTestBean();
309
310         String JavaDoc result = (String JavaDoc) BeanUtil.getObjectAttribute(
311                                                 outerComplicatedTestBean,
312                                                 "complicatedTestBean.simpleTestBean.simpleString");
313
314         if(result != null) {
315             fail("Expected to get back a null reference but got a value.");
316         }
317     }
318
319     /**
320      * Sets a String property with <code>setObjectAttribute</code> and
321      * tests that the getter returns the result
322      */

323     public void testSetObjectAttribute()
324         throws
325             IllegalAccessException JavaDoc,
326             IllegalArgumentException JavaDoc,
327             InvocationTargetException JavaDoc,
328             NoSuchMethodException JavaDoc {
329
330         String JavaDoc stringValue = "TEST VALUE";
331         SimpleTestBean simpleTestBean = new SimpleTestBean();
332
333         BeanUtil.setObjectAttribute(
334             simpleTestBean, "simpleString", stringValue);
335
336         if(!stringValue.equals(simpleTestBean.getSimpleString())) {
337             fail();
338         }
339     }
340
341     /**
342      * Sets a property on a complex hierachy of beans with
343      * <code>setObjectAttribute</code> and validates the getter
344      * returns the result.
345      */

346     public void testSetObjectAttributeEmbeddedBean()
347         throws
348             IllegalAccessException JavaDoc,
349             IllegalArgumentException JavaDoc,
350             InvocationTargetException JavaDoc,
351             NoSuchMethodException JavaDoc {
352
353         String JavaDoc stringValue = "TEST VALUE";
354
355         SimpleTestBean simpleTestBean = new SimpleTestBean();
356
357         ComplicatedTestBean innerComplicatedTestBean =
358             new ComplicatedTestBean();
359         innerComplicatedTestBean.setSimpleTestBean(simpleTestBean);
360
361         ComplicatedTestBean outerComplicatedTestBean =
362             new ComplicatedTestBean();
363         outerComplicatedTestBean.setComplicatedTestBean(innerComplicatedTestBean);
364
365         BeanUtil.setObjectAttribute(
366             outerComplicatedTestBean, "complicatedTestBean.simpleTestBean.simpleString", stringValue);
367
368         if(!stringValue.equals(simpleTestBean.getSimpleString())) {
369             fail();
370         }
371     }
372
373     /**
374      * Tests that an int property can be set with
375      * <code>setObjectAttribute</code> by passing an <code>Integer</code>
376      * object.
377      */

378     public void testSetObjectAttributePrimitiveInt()
379         throws
380             IllegalAccessException JavaDoc,
381             IllegalArgumentException JavaDoc,
382             InvocationTargetException JavaDoc,
383             NoSuchMethodException JavaDoc {
384
385         int intValue = 10;
386         SimpleTestBean simpleTestBean = new SimpleTestBean();
387
388         BeanUtil.setObjectAttribute(
389             simpleTestBean, "primitiveInt", new Integer JavaDoc(intValue));
390
391         if(intValue != simpleTestBean.getPrimitiveInt()) {
392             fail();
393         }
394
395     }
396
397     /**
398      * Tests that calling <code>setObjectAttribute</code> on a read-only
399      * property will throw a <code>IllegalAccessException</code>.
400      */

401     public void testSetObjectAttributeFailReadOnly()
402         throws
403             NoSuchMethodException JavaDoc,
404             InvocationTargetException JavaDoc,
405             IllegalAccessException JavaDoc {
406
407         String JavaDoc stringValue = "TEST VALUE";
408         SimpleTestBean simpleTestBean = new SimpleTestBean();
409
410         try {
411             BeanUtil.setObjectAttribute(
412                 simpleTestBean, "readOnlyString", stringValue);
413
414             fail("Successfully set a read-only property.");
415         }
416         catch (IllegalAccessException JavaDoc e) {
417             // Expected result
418
}
419
420     }
421
422     /**
423      * Tests <code>setObjectAttribute</code> on a write-only
424      * property.
425      */

426     public void testSetObjectAttributeWriteOnly()
427         throws
428             NoSuchMethodException JavaDoc,
429             InvocationTargetException JavaDoc,
430             IllegalAccessException JavaDoc {
431
432         String JavaDoc stringValue = "TEST VALUE";
433         SimpleTestBean simpleTestBean = new SimpleTestBean();
434
435         BeanUtil.setObjectAttribute(
436             simpleTestBean, "writeOnlyString", stringValue);
437
438         // Since there is no getter method, check equality against
439
// the public attribute
440
if(!stringValue.equals(simpleTestBean.writeOnlyString)) {
441             fail();
442         }
443     }
444
445     /**
446      * Sets a simple boolean with <code>setObjectAttribute</code> and
447      * tests the result matches
448      */

449     public void testSetObjectAttributeSimpleBoolean()
450         throws
451             NoSuchMethodException JavaDoc,
452             InvocationTargetException JavaDoc,
453             IllegalAccessException JavaDoc {
454
455         boolean booleanValue = true;
456         SimpleTestBean simpleTestBean = new SimpleTestBean();
457
458         BeanUtil.setObjectAttribute(
459             simpleTestBean, "simpleBoolean", new Boolean JavaDoc(booleanValue));
460
461         if(booleanValue != simpleTestBean.isSimpleBoolean()) {
462             fail("Object booleanValue [" + booleanValue + "] was not "
463                 + "equals to simpleTestBean.isSimpleBoolean() ["
464                 + simpleTestBean.isSimpleBoolean() + "] after setter.");
465         }
466     }
467
468     /**
469      * Sets a boolean that has an incorrect getter method (get instead
470      * of is) with <code>setObjectAttribute</code> and tests the
471      * result matches.
472      */

473     public void testSetObjectAttributeIncorrectBoolean()
474         throws
475             NoSuchMethodException JavaDoc,
476             InvocationTargetException JavaDoc,
477             IllegalAccessException JavaDoc {
478
479         boolean booleanValue = true;
480         SimpleTestBean simpleTestBean = new SimpleTestBean();
481
482         BeanUtil.setObjectAttribute(
483             simpleTestBean, "incorrectBoolean", new Boolean JavaDoc(booleanValue));
484
485         if(booleanValue != simpleTestBean.getIncorrectBoolean()) {
486             fail("Object booleanValue [" + booleanValue + "] was not "
487                 + "equals to simpleTestBean.isSimpleBoolean() ["
488                 + simpleTestBean.isSimpleBoolean() + "] after setter.");
489         }
490     }
491
492     /**
493      * Tests setting as HashSet object onto a Collection
494      * property using <code>setObjectAttribute</code>.
495      */

496     public void testSetObjectAttributeCollection()
497         throws
498             NoSuchMethodException JavaDoc,
499             InvocationTargetException JavaDoc,
500             IllegalAccessException JavaDoc {
501
502         HashSet JavaDoc hashSet = new HashSet JavaDoc();
503         hashSet.add("Test String");
504         SimpleTestBean simpleTestBean = new SimpleTestBean();
505
506         BeanUtil.setObjectAttribute(
507             simpleTestBean, "collection", hashSet);
508
509         if(!hashSet.equals(simpleTestBean.getCollection())) {
510             fail("Object hashSet [" + hashSet + "] was not "
511                 + "equals to simpleTestBean.getCollection() ["
512                 + simpleTestBean.isSimpleBoolean() + "] after setter.");
513         }
514     }
515
516     /**
517      * Tests setting an ArrayList object onto a Collection
518      * property using <code>setObjectAttribute</code>.
519      */

520     public void testSetObjectAttributeCollectionArrayList()
521         throws
522             NoSuchMethodException JavaDoc,
523             InvocationTargetException JavaDoc,
524             IllegalAccessException JavaDoc {
525
526         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
527         arrayList.add("Test String");
528         SimpleTestBean simpleTestBean = new SimpleTestBean();
529
530         BeanUtil.setObjectAttribute(
531             simpleTestBean, "collection", arrayList);
532
533         if(!arrayList.equals(simpleTestBean.getCollection())) {
534             fail("Object collection [" + arrayList + "] was not "
535                 + "equals to simpleTestBean.getCollection() ["
536                 + simpleTestBean.isSimpleBoolean() + "] after setter.");
537         }
538
539     }
540
541     /**
542      * Tests that <code>setObjectAttribute</code> throw a
543      * <code>NullPointerException</code> for a property with embedded
544      * properties, where one of the results in the embedded structure
545      * is null.
546      */

547     public void testSetObjectAttributeWithNull()
548         throws
549             NoSuchMethodException JavaDoc,
550             InvocationTargetException JavaDoc,
551             IllegalAccessException JavaDoc {
552
553         String JavaDoc stringValue = "Test Value";
554         ComplicatedTestBean outerComplicatedTestBean =
555             new ComplicatedTestBean();
556
557         try {
558             BeanUtil.setObjectAttribute(
559                 outerComplicatedTestBean,
560                 "complicatedTestBean.simpleTestBean.simpleString",
561                 stringValue);
562
563             fail("Call failed to throw a NullPointerException.");
564         }
565         catch(NullPointerException JavaDoc e) {
566             // Expected result.
567
}
568
569     }
570
571     /**
572      * Method called by jUnit to get all the tests in this test case.
573      * @return Test the suite of tests in this test case
574      */

575     public static Test suite() {
576         TestSuite masterSuite = new TestSuite();
577         // add single threaded tests
578
Test singleThreadedTests = getSingleThreadedTests();
579         if (singleThreadedTests != null) {
580             masterSuite.addTest(singleThreadedTests);
581         }
582         // add multi threaded tests
583
Test multiThreadedTests = getMultiThreadedTests();
584         if (multiThreadedTests != null) {
585             masterSuite.addTest(multiThreadedTests);
586         }
587         return masterSuite;
588     }
589
590     /**
591      * This method is used within the suite method to get all of the single threaded tests.
592      * Add all your single threaded tests in this method with a line like:
593      * suite.addTest(new InternalNodeTest("testFunction1"));
594      * @return Test the suite of single threaded tests in this test case
595      */

596     private static Test getSingleThreadedTests() {
597         TestSuite suite = new TestSuite();
598
599         suite.addTest(new BeanUtilTest("testGetObjectAttribute"));
600         suite.addTest(new BeanUtilTest("testGetObjectAttributeEmbeddedBean"));
601         suite.addTest(new BeanUtilTest("testGetObjectAttributePrimitiveInt"));
602         suite.addTest(new BeanUtilTest("testGetObjectAttributeReadOnly"));
603         suite.addTest(new BeanUtilTest("testGetObjectAttributeFailWriteOnly"));
604         suite.addTest(new BeanUtilTest("testGetObjectAttributeSimpleBoolean"));
605         suite.addTest(new BeanUtilTest("testGetObjectAttributeCollection"));
606         suite.addTest(new BeanUtilTest("testGetObjectAttributeCollectionArrayList"));
607         suite.addTest(new BeanUtilTest("testGetObjectAttributeWithNull"));
608         suite.addTest(new BeanUtilTest("testGetObjectAttributeIncorrectBoolean"));
609         suite.addTest(new BeanUtilTest("testGetObjectAttributeOverlapReadBoolean"));
610
611         suite.addTest(new BeanUtilTest("testSetObjectAttribute"));
612         suite.addTest(new BeanUtilTest("testSetObjectAttributeEmbeddedBean"));
613         suite.addTest(new BeanUtilTest("testSetObjectAttributePrimitiveInt"));
614         suite.addTest(new BeanUtilTest("testSetObjectAttributeFailReadOnly"));
615         suite.addTest(new BeanUtilTest("testSetObjectAttributeWriteOnly"));
616         suite.addTest(new BeanUtilTest("testSetObjectAttributeSimpleBoolean"));
617         suite.addTest(new BeanUtilTest("testSetObjectAttributeCollection"));
618         suite.addTest(new BeanUtilTest("testSetObjectAttributeCollectionArrayList"));
619         suite.addTest(new BeanUtilTest("testSetObjectAttributeWithNull"));
620         suite.addTest(new BeanUtilTest("testSetObjectAttributeIncorrectBoolean"));
621
622         return suite;
623     }
624
625     /**
626      * This method is used within the suite method to get all of the multi threaded tests.
627      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
628      * @return Test the suite of multi-threaded tests in this test case
629      */

630     private static Test getMultiThreadedTests() {
631         TestSuite suite = new ActiveTestSuite();
632
633         /*
634          * add your tests here following these examples:
635          *
636          * addTest(suite, "testFunction1", 5);
637          * addTest(suite, "testFunction2", 10);
638          */

639
640         return suite;
641     }
642
643     /**
644      * This method will add the give test to the give suite the specified
645      * number of times. This is best used for multi-threaded tests where
646      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
647      * @param suite the suite to add the test to.
648      * @param testName the name of the test to add.
649      * @param number the number of times to add the test to the suite
650      */

651     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
652         for (int count = 0; count < number; count++) {
653             suite.addTest(new BeanUtilTest(testName));
654         }
655     }
656 }
657
Popular Tags