View Javadoc

1   /*
2    * $Id: DynaBeanPropertyAccessorTest.java 560753 2007-07-29 16:18:28Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.s1;
22  
23  import junit.framework.*;
24  import java.io.*;
25  import java.util.*;
26  import org.apache.commons.beanutils.*;
27  
28  import ognl.*;
29  
30  /***  Description of the Class */
31  public class DynaBeanPropertyAccessorTest extends TestCase {
32  
33      protected DynaBean bean = null;
34      
35      public DynaBeanPropertyAccessorTest(String name) throws Exception {
36          super(name);
37      }
38  
39  
40      public static void main(String args[]) {
41          junit.textui.TestRunner.run(DynaBeanPropertyAccessorTest.class);
42      }
43  
44      /***
45       * Set up instance variables required by this test case.
46       */
47      public void setUp() throws Exception {
48  
49          // Instantiate a new DynaBean instance
50          DynaClass dynaClass = createDynaClass();
51          bean = dynaClass.newInstance();
52  
53          // Initialize the DynaBean's property values (like TestBean)
54          bean.set("booleanProperty", new Boolean(true));
55          bean.set("booleanSecond", new Boolean(true));
56          bean.set("doubleProperty", new Double(321.0));
57          bean.set("floatProperty", new Float((float) 123.0));
58          int intArray[] = { 0, 10, 20, 30, 40 };
59          bean.set("intArray", intArray);
60          int intIndexed[] = { 0, 10, 20, 30, 40 };
61          bean.set("intIndexed", intIndexed);
62          bean.set("intProperty", new Integer(123));
63          List listIndexed = new ArrayList();
64          listIndexed.add("String 0");
65          listIndexed.add("String 1");
66          listIndexed.add("String 2");
67          listIndexed.add("String 3");
68          listIndexed.add("String 4");
69          bean.set("listIndexed", listIndexed);
70          bean.set("longProperty", new Long((long) 321));
71          HashMap mappedProperty = new HashMap();
72          mappedProperty.put("First Key", "First Value");
73          mappedProperty.put("Second Key", "Second Value");
74          bean.set("mappedProperty", mappedProperty);
75          HashMap mappedIntProperty = new HashMap();
76          mappedIntProperty.put("One", new Integer(1));
77          mappedIntProperty.put("Two", new Integer(2));
78          bean.set("mappedIntProperty", mappedIntProperty);
79          // Property "nullProperty" is not initialized, so it should return null
80          bean.set("shortProperty", new Short((short) 987));
81          String stringArray[] =
82                  { "String 0", "String 1", "String 2", "String 3", "String 4" };
83          bean.set("stringArray", stringArray);
84          String stringIndexed[] =
85                  { "String 0", "String 1", "String 2", "String 3", "String 4" };
86          bean.set("stringIndexed", stringIndexed);
87          bean.set("stringProperty", "This is a string");
88  
89      }
90  
91  
92  
93  
94      public void testGetProperty() throws Exception {
95          
96          DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor();
97          assertTrue("This is a string".equals(trans.getProperty(null, bean, "stringProperty"))); 
98          assertTrue(trans.getProperty(null, bean, "listIndexed") instanceof List); 
99          
100     }
101 
102     public void testSetProperty() throws Exception {
103         
104         DynaBeanPropertyAccessor trans = new DynaBeanPropertyAccessor();
105         trans.setProperty(null, bean, "stringProperty", "bob");
106         assertTrue("bob".equals(trans.getProperty(null, bean, "stringProperty"))); 
107         
108     }
109 
110     public void testOGNL() throws Exception {
111         
112         OgnlRuntime.setPropertyAccessor(DynaBean.class, new DynaBeanPropertyAccessor());
113 
114         assertTrue("This is a string".equals(Ognl.getValue("stringProperty", bean)));
115 
116     }
117 
118 
119     /***
120      * Create and return a <code>DynaClass</code> instance for our test
121      * <code>DynaBean</code>.
122      */
123     protected DynaClass createDynaClass() {
124 
125         int intArray[] = new int[0];
126         String stringArray[] = new String[0];
127 
128         DynaClass dynaClass = new BasicDynaClass
129                 ("TestDynaClass", null,
130                         new DynaProperty[]{
131                             new DynaProperty("booleanProperty", Boolean.TYPE),
132                             new DynaProperty("booleanSecond", Boolean.TYPE),
133                             new DynaProperty("doubleProperty", Double.TYPE),
134                             new DynaProperty("floatProperty", Float.TYPE),
135                             new DynaProperty("intArray", intArray.getClass()),
136                             new DynaProperty("intIndexed", intArray.getClass()),
137                             new DynaProperty("intProperty", Integer.TYPE),
138                             new DynaProperty("listIndexed", List.class),
139                             new DynaProperty("longProperty", Long.TYPE),
140                             new DynaProperty("mappedProperty", Map.class),
141                             new DynaProperty("mappedIntProperty", Map.class),
142                             new DynaProperty("nullProperty", String.class),
143                             new DynaProperty("shortProperty", Short.TYPE),
144                             new DynaProperty("stringArray", stringArray.getClass()),
145                             new DynaProperty("stringIndexed", stringArray.getClass()),
146                             new DynaProperty("stringProperty", String.class),
147                         });
148         return (dynaClass);
149 
150     }
151 
152 
153 }
154