Clover Coverage Report - Commons BeanUtils 1.8.3-kuali-SNAPSHOT
Coverage timestamp: Thu Jun 9 2011 14:31:17 EDT
175   499   34   8.33
2   264   0.19   21
21     1.62  
1    
 
  LazyDynaBeanTestCase       Line # 33 175 0% 34 14 92.9% 0.9292929
 
  (16)
 
1    /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10    *
11    * Unless required by applicable law or agreed to in writing, software
12    * distributed under the License is distributed on an "AS IS" BASIS,
13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    * See the License for the specific language governing permissions and
15    * limitations under the License.
16    */
17    package org.apache.commons.beanutils;
18   
19    import java.util.HashMap;
20    import java.util.TreeMap;
21    import java.util.ArrayList;
22    import java.util.LinkedList;
23    import java.lang.reflect.InvocationTargetException;
24    import junit.framework.TestCase;
25    import junit.framework.Test;
26    import junit.framework.TestSuite;
27   
28    /**
29    * <p>Test Case for the <code>LazyDynaBean</code> implementation class.</p>
30    *
31    * @author Niall Pemberton
32    */
 
33    public class LazyDynaBeanTestCase extends TestCase {
34   
35    protected LazyDynaBean bean = null;
36    protected LazyDynaClass dynaClass = null;
37    protected String testProperty = "myProperty";
38    protected String testPropertyA = "myProperty-A";
39    protected String testPropertyB = "myProperty-B";
40    protected String testString1 = "myStringValue-1";
41    protected String testString2 = "myStringValue-2";
42    protected Integer testInteger1 = new Integer(30);
43    protected Integer testInteger2 = new Integer(40);
44    protected String testKey = "myKey";
45   
46    // ---------------------------------------------------------- Constructors
47   
48    /**
49    * Construct a new instance of this test case.
50    *
51    * @param name Name of the test case
52    */
 
53  16 toggle public LazyDynaBeanTestCase(String name) {
54  16 super(name);
55    }
56   
57    // -------------------------------------------------- Overall Test Methods
58   
59    /**
60    * Run thus Test
61    */
 
62  0 toggle public static void main(String[] args) {
63  0 junit.textui.TestRunner.run(suite());
64    }
65   
66    /**
67    * Return the tests included in this test suite.
68    */
 
69  1 toggle public static Test suite() {
70  1 return (new TestSuite(LazyDynaBeanTestCase.class));
71    }
72   
73    /**
74    * Set up instance variables required by this test case.
75    */
 
76  16 toggle public void setUp() throws Exception {
77  16 bean = new LazyDynaBean();
78  16 dynaClass = (LazyDynaClass)bean.getDynaClass();
79  16 dynaClass.setReturnNull(true);
80    }
81   
82    /**
83    * Tear down instance variables required by this test case.
84    */
 
85  16 toggle public void tearDown() {
86  16 bean = null;
87    }
88   
89    // ------------------------------------------------ Individual Test Methods
90   
91    /**
92    * Test Getting/Setting a Simple Property
93    */
 
94  1 toggle public void testSimpleProperty() {
95   
96    // Check the property & value doesn't exist
97  1 assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty));
98  1 assertNull("Check Value is null", bean.get(testProperty));
99   
100    // Set a new property - should add new property and set value
101  1 bean.set(testProperty, testInteger1);
102  1 assertEquals("Check First Value is correct", testInteger1, bean.get(testProperty));
103  1 assertEquals("Check Property type is correct", Integer.class, dynaClass.getDynaProperty(testProperty).getType());
104   
105    // Set the property again - should set the new value
106  1 bean.set(testProperty, testInteger2);
107  1 assertEquals("Check Second Value is correct", testInteger2, bean.get(testProperty));
108   
109    // Set the property again - with a different type, should fail
110  1 try {
111  1 bean.set(testProperty, testString1);
112  0 fail("expected ConversionException trying to set an Integer property to a String");
113    } catch (ConversionException expected) {
114    // expected result
115    }
116   
117    }
118   
119    /**
120    * Test Getting/Setting a 'null' Property
121    */
 
122  1 toggle public void testNullProperty() {
123   
124    // Check the property & value doesn't exist
125  1 assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty));
126  1 assertNull("Check Value is null", bean.get(testProperty));
127   
128    // Set a new property to null
129  1 bean.set(testProperty, null);
130  1 assertNull("Check Value is still null", bean.get(testProperty));
131   
132    }
133   
134    /**
135    * Test Setting a Simple Property when MutableDynaClass is set to restricted
136    */
 
137  1 toggle public void testSimplePropertyRestricted() {
138   
139    // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added
140  1 dynaClass.setRestricted(true);
141  1 assertTrue("Check MutableDynaClass is restricted", dynaClass.isRestricted());
142   
143    // Check the property & value doesn't exist
144  1 assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty));
145  1 assertNull("Check Value is null", bean.get(testProperty));
146   
147    // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted
148  1 try {
149  1 bean.set(testProperty, testString1);
150  0 fail("expected IllegalArgumentException trying to add new property to restricted DynaClass");
151    } catch (IllegalArgumentException expected) {
152    // expected result
153    }
154   
155    }
156   
157    /**
158    * Test Getting/Setting a 'Mapped' Property - default HashMap property
159    */
 
160  1 toggle public void testMappedPropertyDefault() {
161   
162    // Check the property & value doesn't exist
163  1 assertNull("Check Mapped Property doesn't exist", dynaClass.getDynaProperty(testProperty));
164  1 assertNull("Check Map is null", bean.get(testProperty));
165  1 assertNull("Check Mapped Value is null", bean.get(testProperty, testKey));
166   
167    // Set a new mapped property - should add new HashMap property and set the mapped value
168  1 bean.set(testProperty, testKey, testInteger1);
169  1 assertEquals("Check Mapped Property exists", HashMap.class, bean.get(testProperty).getClass());
170  1 assertEquals("Check First Mapped Value is correct(a)", testInteger1, bean.get(testProperty, testKey));
171  1 assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((HashMap)bean.get(testProperty)).get(testKey));
172   
173    // Set the property again - should set the new value
174  1 bean.set(testProperty, testKey, testInteger2);
175  1 assertEquals("Check Second Mapped Value is correct(a)", testInteger2, bean.get(testProperty, testKey));
176  1 assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((HashMap)bean.get(testProperty)).get(testKey));
177    }
178   
179    /**
180    * Test Getting/Setting a 'Mapped' Property - use TreeMap property
181    */
 
182  1 toggle public void testMappedPropertyTreeMap() {
183   
184    // Check the property & value doesn't exist
185  1 assertNull("Check Mapped Property doesn't exist", dynaClass.getDynaProperty(testProperty));
186   
187    // Add a 'TreeMap' property to the DynaClass
188  1 dynaClass.add(testProperty, TreeMap.class);
189  1 assertTrue("Check Property is mapped", dynaClass.getDynaProperty(testProperty).isMapped());
190  1 assertEquals("Check Property is correct type", TreeMap.class, dynaClass.getDynaProperty(testProperty).getType());
191  1 assertEquals("Check Mapped Property exists", TreeMap.class, bean.get(testProperty).getClass());
192    // assertNull("Check mapped property is null", bean.get(testProperty));
193   
194    // Set a new mapped property - should instatiate a new TreeMap property and set the mapped value
195  1 bean.set(testProperty, testKey, testInteger1);
196  1 assertEquals("Check Mapped Property exists", TreeMap.class, bean.get(testProperty).getClass());
197  1 assertEquals("Check First Mapped Value is correct(a)", testInteger1, bean.get(testProperty, testKey));
198  1 assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((TreeMap)bean.get(testProperty)).get(testKey));
199   
200    // Set the property again - should set the new value
201  1 bean.set(testProperty, testKey, testInteger2);
202  1 assertEquals("Check Second Mapped Value is correct(a)", testInteger2, bean.get(testProperty, testKey));
203  1 assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((TreeMap)bean.get(testProperty)).get(testKey));
204    }
205   
206    /**
207    * Test Setting a 'Mapped' Property using PropertyUtils
208    */
 
209  1 toggle public void testMappedPropertyUtils() {
210   
211  1 dynaClass.setReturnNull(false);
212   
213    // Check the property & value doesn't exist
214  1 assertFalse("Check Mapped Property doesn't exist", dynaClass.isDynaProperty(testProperty));
215  1 assertNull("Check Map is null", bean.get(testProperty));
216  1 assertNull("Check Mapped Value is null", bean.get(testProperty, testKey));
217   
218    // Set the mapped property using PropertyUtils
219  1 try {
220  1 PropertyUtils.setProperty(bean, testProperty+"("+testKey+")", testString1);
221    }
222    catch (NoSuchMethodException ex) {
223  0 fail("testIndexedPropertyUtils threw "+ex);
224    }
225    catch (InvocationTargetException ex) {
226  0 fail("testIndexedPropertyUtils threw "+ex);
227    }
228    catch (IllegalAccessException ex) {
229  0 fail("testIndexedPropertyUtils threw "+ex);
230    }
231   
232    // Check property value correctly set
233  1 assertEquals("Check Mapped Bean Value is correct", testString1, bean.get(testProperty, testKey));
234   
235    }
236   
237    /**
238    * Test Setting a Mapped Property when MutableDynaClass is set to restricted
239    */
 
240  1 toggle public void testMappedPropertyRestricted() {
241   
242    // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added
243  1 dynaClass.setRestricted(true);
244  1 assertTrue("Check MutableDynaClass is restricted", dynaClass.isRestricted());
245   
246    // Check the property & value doesn't exist
247  1 assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty));
248  1 assertNull("Check Value is null", bean.get(testProperty));
249   
250    // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted
251  1 try {
252  1 bean.set(testProperty, testKey, testInteger1);
253  0 fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass");
254    } catch (IllegalArgumentException expected) {
255    // expected result
256    }
257   
258    }
259   
260    /**
261    * Test setting mapped property for type which is not Map
262    */
 
263  1 toggle public void testMappedInvalidType() {
264  1 dynaClass.add(testProperty, String.class);
265  1 assertFalse("Check Property is not mapped", dynaClass.getDynaProperty(testProperty).isMapped());
266  1 try {
267  1 bean.set(testProperty, testKey, testInteger1);
268  0 fail("set(property, key, value) should have thrown IllegalArgumentException");
269    } catch (IllegalArgumentException expected) {
270    // expected result
271    }
272    }
273   
274    /**
275    * Test Getting/Setting an 'Indexed' Property - default ArrayList property
276    */
 
277  1 toggle public void testIndexedPropertyDefault() {
278   
279  1 int index = 3;
280   
281    // Check the property & value doesn't exist
282  1 assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty));
283  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
284  1 assertNull("Check Indexed value is null", bean.get(testProperty, index));
285   
286    // Set the property, should create new ArrayList and set appropriate indexed value
287  1 bean.set(testProperty, index, testInteger1);
288  1 assertNotNull("Check Indexed Property is not null", bean.get(testProperty));
289  1 assertEquals("Check Indexed Property is correct type", ArrayList.class, bean.get(testProperty).getClass());
290  1 assertEquals("Check First Indexed Value is correct", testInteger1, bean.get(testProperty, index));
291  1 assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((ArrayList)bean.get(testProperty)).size()));
292   
293    // Set a second indexed value, should automatically grow the ArrayList and set appropriate indexed value
294  1 index = index + 2;
295  1 bean.set(testProperty, index, testString1);
296  1 assertEquals("Check Second Indexed Value is correct", testString1, bean.get(testProperty, index));
297  1 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((ArrayList)bean.get(testProperty)).size()));
298    }
299   
300    /**
301    * Test Getting/Setting a List 'Indexed' Property - use alternative List (LinkedList)
302    */
 
303  1 toggle public void testIndexedLinkedList() {
304   
305  1 int index = 3;
306   
307    // Check the property & value doesn't exist
308  1 assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty));
309  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
310   
311    // Add a 'LinkedList' property to the DynaClass
312  1 dynaClass.add(testProperty, LinkedList.class);
313  1 assertTrue("Check Property is indexed", dynaClass.getDynaProperty(testProperty).isIndexed());
314  1 assertEquals("Check Property is correct type", LinkedList.class, dynaClass.getDynaProperty(testProperty).getType());
315  1 assertEquals("Check Property type is correct", LinkedList.class, bean.get(testProperty).getClass());
316   
317    // Set the property, should instantiate a new LinkedList and set appropriate indexed value
318  1 bean.set(testProperty, index, testString1);
319  1 assertEquals("Check Property type is correct", LinkedList.class, bean.get(testProperty).getClass());
320  1 assertEquals("Check First Indexed Value is correct", testString1, bean.get(testProperty, index));
321  1 assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((LinkedList)bean.get(testProperty)).size()));
322   
323    // Set a second indexed value, should automatically grow the LinkedList and set appropriate indexed value
324  1 index = index + 2;
325  1 bean.set(testProperty, index, testInteger1);
326  1 assertEquals("Check Second Indexed Value is correct", testInteger1, bean.get(testProperty, index));
327  1 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((LinkedList)bean.get(testProperty)).size()));
328    }
329   
330    /**
331    * Test Getting/Setting a primitive array 'Indexed' Property - use int[]
332    */
 
333  1 toggle public void testIndexedPrimitiveArray() {
334   
335  1 int index = 3;
336  1 int[] primitiveArray = new int[0];
337   
338    // Check the property & value doesn't exist
339  1 assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty));
340  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
341   
342    // Add a DynaProperty of type int[]
343  1 dynaClass.add(testProperty, primitiveArray.getClass());
344  1 assertEquals("Check Indexed Property exists", primitiveArray.getClass(), dynaClass.getDynaProperty(testProperty).getType());
345  1 assertEquals("Check Indexed Property is correct type", primitiveArray.getClass(), bean.get(testProperty).getClass());
346   
347    // Set an indexed value
348  1 bean.set(testProperty, index, testInteger1);
349  1 assertNotNull("Check Indexed Property is not null", bean.get(testProperty));
350  1 assertEquals("Check Indexed Property is correct type", primitiveArray.getClass(), bean.get(testProperty).getClass());
351  1 assertEquals("Check First Indexed Value is correct(a)", testInteger1, bean.get(testProperty, index));
352  1 assertEquals("Check First Indexed Value is correct(b)", testInteger1, new Integer(((int[])bean.get(testProperty))[index]));
353  1 assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((int[])bean.get(testProperty)).length));
354   
355    // Set a second indexed value, should automatically grow the int[] and set appropriate indexed value
356  1 index = index + 2;
357  1 bean.set(testProperty, index, testInteger2);
358  1 assertEquals("Check Second Indexed Value is correct(a)", testInteger2, bean.get(testProperty, index));
359  1 assertEquals("Check Second Indexed Value is correct(b)", testInteger2, new Integer(((int[])bean.get(testProperty))[index]));
360  1 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((int[])bean.get(testProperty)).length));
361   
362    }
363   
364    /**
365    * Test Getting/Setting an Object array 'Indexed' Property - use String[]
366    */
 
367  1 toggle public void testIndexedObjectArray() {
368   
369  1 int index = 3;
370  1 Object objectArray = new String[0];
371   
372    // Check the property & value doesn't exist
373  1 assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty));
374  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
375   
376    // Add a DynaProperty of type String[]
377  1 dynaClass.add(testProperty, objectArray.getClass());
378  1 assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType());
379  1 assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass());
380   
381    // Set an indexed value
382  1 bean.set(testProperty, index, testString1);
383  1 assertNotNull("Check Indexed Property is not null", bean.get(testProperty));
384  1 assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass());
385  1 assertEquals("Check First Indexed Value is correct(a)", testString1, bean.get(testProperty, index));
386  1 assertEquals("Check First Indexed Value is correct(b)", testString1, ((String[])bean.get(testProperty))[index]);
387  1 assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((String[])bean.get(testProperty)).length));
388   
389    // Set a second indexed value, should automatically grow the String[] and set appropriate indexed value
390  1 index = index + 2;
391  1 bean.set(testProperty, index, testString2);
392  1 assertEquals("Check Second Indexed Value is correct(a)", testString2, bean.get(testProperty, index));
393  1 assertEquals("Check Second Indexed Value is correct(b)", testString2, ((String[])bean.get(testProperty))[index]);
394  1 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((String[])bean.get(testProperty)).length));
395    }
396   
397    /**
398    * Test Getting/Setting an DynaBean[] array
399    */
 
400  1 toggle public void testIndexedDynaBeanArray() {
401   
402  1 int index = 3;
403  1 Object objectArray = new LazyDynaMap[0];
404   
405    // Check the property & value doesn't exist
406  1 assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty));
407  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
408   
409    // Add a DynaProperty of type String[]
410  1 dynaClass.add(testProperty, objectArray.getClass());
411  1 assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType());
412  1 assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass());
413   
414    // Retrieving from Array should initialize DynaBean
415  5 for (int i = index; i >= 0; i--) {
416  4 assertEquals("Check Array Components initialized", LazyDynaMap.class, bean.get(testProperty, index).getClass());
417    }
418   
419  1 dynaClass.add(testPropertyB, objectArray.getClass());
420  1 LazyDynaMap newMap = new LazyDynaMap();
421  1 newMap.set(testPropertyB, testString2);
422  1 bean.set(testPropertyA, index, newMap);
423  1 assertEquals("Check Indexed Value is correct(a)", testString2, ((DynaBean)bean.get(testPropertyA, index)).get(testPropertyB));
424   
425    }
426   
427    /**
428    * Test Setting an 'Indexed' Property using PropertyUtils
429    */
 
430  1 toggle public void testIndexedPropertyUtils() {
431   
432  1 int index = 3;
433  1 dynaClass.setReturnNull(false);
434   
435    // Check the property & value doesn't exist
436  1 assertFalse("Check Indexed Property doesn't exist", dynaClass.isDynaProperty(testProperty));
437  1 assertNull("Check Indexed Property is null", bean.get(testProperty));
438  1 assertNull("Check Indexed value is null", bean.get(testProperty, index));
439   
440    // Use PropertyUtils to set the indexed value
441  1 try {
442  1 PropertyUtils.setProperty(bean, testProperty+"["+index+"]", testString1);
443    }
444    catch (NoSuchMethodException ex) {
445  0 fail("testIndexedPropertyUtils threw "+ex);
446    }
447    catch (InvocationTargetException ex) {
448  0 fail("testIndexedPropertyUtils threw "+ex);
449    }
450    catch (IllegalAccessException ex) {
451  0 fail("testIndexedPropertyUtils threw "+ex);
452    }
453   
454    // Check property value correctly set
455  1 assertEquals("Check Indexed Bean Value is correct", testString1, bean.get(testProperty, index));
456   
457    }
458   
459    /**
460    * Test Setting an Indexed Property when MutableDynaClass is set to restricted
461    */
 
462  1 toggle public void testIndexedPropertyRestricted() {
463   
464  1 int index = 3;
465   
466    // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added
467  1 dynaClass.setRestricted(true);
468  1 assertTrue("Check MutableDynaClass is restricted", dynaClass.isRestricted());
469   
470    // Check the property & value doesn't exist
471  1 assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty));
472  1 assertNull("Check Value is null", bean.get(testProperty));
473   
474    // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted
475  1 try {
476  1 bean.set(testProperty, index, testInteger1);
477  0 fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass");
478    } catch (IllegalArgumentException expected) {
479    // expected result
480    }
481   
482    }
483   
484    /**
485    * Test setting indexed property for type which is not List or Array
486    */
 
487  1 toggle public void testIndexedInvalidType() {
488  1 int index = 3;
489  1 dynaClass.add(testProperty, String.class);
490  1 assertFalse("Check Property is not indexed", dynaClass.getDynaProperty(testProperty).isIndexed());
491  1 try {
492  1 bean.set(testProperty, index, testString1);
493  0 fail("set(property, index, value) should have thrown IllegalArgumentException");
494    } catch (IllegalArgumentException expected) {
495    // expected result
496    }
497    }
498   
499    }