View Javadoc

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