View Javadoc

1   package org.apache.struts2.s1;
2   
3   import junit.framework.*;
4   import java.io.*;
5   import java.util.*;
6   import org.apache.commons.beanutils.*;
7   
8   import ognl.*;
9   
10  /***  Description of the Class */
11  public class DynaBeanPropertyAccessorTest extends TestCase {
12  
13      protected DynaBean bean = null;
14      
15      public DynaBeanPropertyAccessorTest(String name) throws Exception {
16          super(name);
17      }
18  
19  
20      public static void main(String args[]) {
21          junit.textui.TestRunner.run(DynaBeanPropertyAccessorTest.class);
22      }
23  
24      /***
25       * Set up instance variables required by this test case.
26       */
27      public void setUp() throws Exception {
28  
29          // Instantiate a new DynaBean instance
30          DynaClass dynaClass = createDynaClass();
31          bean = dynaClass.newInstance();
32  
33          // Initialize the DynaBean's property values (like TestBean)
34          bean.set("booleanProperty", new Boolean(true));
35          bean.set("booleanSecond", new Boolean(true));
36          bean.set("doubleProperty", new Double(321.0));
37          bean.set("floatProperty", new Float((float) 123.0));
38          int intArray[] = { 0, 10, 20, 30, 40 };
39          bean.set("intArray", intArray);
40          int intIndexed[] = { 0, 10, 20, 30, 40 };
41          bean.set("intIndexed", intIndexed);
42          bean.set("intProperty", new Integer(123));
43          List listIndexed = new ArrayList();
44          listIndexed.add("String 0");
45          listIndexed.add("String 1");
46          listIndexed.add("String 2");
47          listIndexed.add("String 3");
48          listIndexed.add("String 4");
49          bean.set("listIndexed", listIndexed);
50          bean.set("longProperty", new Long((long) 321));
51          HashMap mappedProperty = new HashMap();
52          mappedProperty.put("First Key", "First Value");
53          mappedProperty.put("Second Key", "Second Value");
54          bean.set("mappedProperty", mappedProperty);
55          HashMap mappedIntProperty = new HashMap();
56          mappedIntProperty.put("One", new Integer(1));
57          mappedIntProperty.put("Two", new Integer(2));
58          bean.set("mappedIntProperty", mappedIntProperty);
59          // Property "nullProperty" is not initialized, so it should return null
60          bean.set("shortProperty", new Short((short) 987));
61          String stringArray[] =
62                  { "String 0", "String 1", "String 2", "String 3", "String 4" };
63          bean.set("stringArray", stringArray);
64          String stringIndexed[] =
65                  { "String 0", "String 1", "String 2", "String 3", "String 4" };
66          bean.set("stringIndexed", stringIndexed);
67          bean.set("stringProperty", "This is a string");
68  
69      }
70  
71  
72  
73  
74      public void testGetProperty() throws Exception {
75          
76          DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor();
77          assertTrue("This is a string".equals(trans.getProperty(null, bean, "stringProperty"))); 
78          assertTrue(trans.getProperty(null, bean, "listIndexed") instanceof List); 
79          
80      }
81  
82      public void testSetProperty() throws Exception {
83          
84          DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor();
85          trans.setProperty(null, bean, "stringProperty", "bob");
86          assertTrue("bob".equals(trans.getProperty(null, bean, "stringProperty"))); 
87          
88      }
89  
90      public void testOGNL() throws Exception {
91          
92          OgnlRuntime.setPropertyAccessor(DynaBean.class, new DynaBeanPropertyAccessor());
93  
94          assertTrue("This is a string".equals(Ognl.getValue("stringProperty", bean)));
95  
96      }
97  
98  
99      /***
100      * Create and return a <code>DynaClass</code> instance for our test
101      * <code>DynaBean</code>.
102      */
103     protected DynaClass createDynaClass() {
104 
105         int intArray[] = new int[0];
106         String stringArray[] = new String[0];
107 
108         DynaClass dynaClass = new BasicDynaClass
109                 ("TestDynaClass", null,
110                         new DynaProperty[]{
111                             new DynaProperty("booleanProperty", Boolean.TYPE),
112                             new DynaProperty("booleanSecond", Boolean.TYPE),
113                             new DynaProperty("doubleProperty", Double.TYPE),
114                             new DynaProperty("floatProperty", Float.TYPE),
115                             new DynaProperty("intArray", intArray.getClass()),
116                             new DynaProperty("intIndexed", intArray.getClass()),
117                             new DynaProperty("intProperty", Integer.TYPE),
118                             new DynaProperty("listIndexed", List.class),
119                             new DynaProperty("longProperty", Long.TYPE),
120                             new DynaProperty("mappedProperty", Map.class),
121                             new DynaProperty("mappedIntProperty", Map.class),
122                             new DynaProperty("nullProperty", String.class),
123                             new DynaProperty("shortProperty", Short.TYPE),
124                             new DynaProperty("stringArray", stringArray.getClass()),
125                             new DynaProperty("stringIndexed", stringArray.getClass()),
126                             new DynaProperty("stringProperty", String.class),
127                         });
128         return (dynaClass);
129 
130     }
131 
132 
133 }
134