View Javadoc

1   /*
2    * $Id: TestDynaActionFormClass.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.action;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.beanutils.DynaProperty;
25  import org.apache.struts.config.FormBeanConfig;
26  import org.apache.struts.config.FormPropertyConfig;
27  
28  /***
29   * Suite of unit tests for the <code>org.apache.struts.action.DynaActionFormClass</code>
30   * class.
31   */
32  public class TestDynaActionFormClass extends TestCase {
33      /***
34       * The set of <code>FormPropertyConfig</code> objects to use when creating
35       * our <code>FormBeanConfig</code>.
36       */
37      protected static final FormPropertyConfig[] dynaProperties =
38          {
39              new FormPropertyConfig("booleanProperty", "boolean", "true", "true"),
40              new FormPropertyConfig("booleanSecond", "boolean", "true", "true"),
41              new FormPropertyConfig("doubleProperty", "double", "321.0", "GET"),
42              new FormPropertyConfig("floatProperty", "float", "123.0",
43                  "POST, HEAD"),
44              new FormPropertyConfig("intArray", "int[]",
45                  "{ 0, 10,20, \"30\" '40' }"),
46              new FormPropertyConfig("intIndexed", "int[]",
47                  " 0 100, 200, 300, 400 "),
48              new FormPropertyConfig("intProperty", "int", "123"),
49              new FormPropertyConfig("listIndexed", "java.util.List", null),
50              new FormPropertyConfig("longProperty", "long", "321"),
51              new FormPropertyConfig("mappedProperty", "java.util.Map", null),
52              new FormPropertyConfig("mappedIntProperty", "java.util.Map", null),
53  
54  
55              //        new FormPropertyConfig("nullProperty", "java.lang.String", null),
56              new FormPropertyConfig("shortProperty", "short", "987"),
57              new FormPropertyConfig("stringArray", "java.lang.String[]",
58                  "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
59              new FormPropertyConfig("stringIndexed", "java.lang.String[]",
60                  "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
61              new FormPropertyConfig("stringProperty", "java.lang.String",
62                  "This is a string"),
63          };
64  
65      // ----------------------------------------------------- Instance Variables
66  
67      /***
68       * The <code>FormBeanConfig</code> structure for the form bean we will be
69       * creating.
70       */
71      protected FormBeanConfig beanConfig = null;
72  
73      /***
74       * The <code>DynaActionFormClass</code> to use for testing.
75       */
76      protected DynaActionFormClass dynaClass = null;
77  
78      /***
79       * Defines the testcase name for JUnit.
80       *
81       * @param theName the testcase's name.
82       */
83      public TestDynaActionFormClass(String theName) {
84          super(theName);
85      }
86  
87      /***
88       * Start the tests.
89       *
90       * @param theArgs the arguments. Not used
91       */
92      public static void main(String[] theArgs) {
93          junit.awtui.TestRunner.main(new String[] {
94                  TestDynaActionFormClass.class.getName()
95              });
96      }
97  
98      /***
99       * @return a test suite (<code>TestSuite</code>) that includes all methods
100      *         starting with "test"
101      */
102     public static Test suite() {
103         // All methods starting with "test" will be executed in the test suite.
104         return new TestSuite(TestDynaActionFormClass.class);
105     }
106 
107     // ----------------------------------------------------- Setup and Teardown
108     public void setUp() {
109         // Construct a FormBeanConfig to be used
110         beanConfig = new FormBeanConfig();
111         beanConfig.setName("dynaForm");
112         beanConfig.setType("org.apache.struts.action.DynaActionForm");
113 
114         // Add relevant property definitions
115         for (int i = 0; i < dynaProperties.length; i++) {
116             beanConfig.addFormPropertyConfig(dynaProperties[i]);
117         }
118 
119         // Construct a corresponding DynaActionFormClass
120         dynaClass = new DynaActionFormClass(beanConfig);
121     }
122 
123     public void tearDown() {
124         dynaClass = null;
125         beanConfig = null;
126     }
127 
128     // -------------------------------------------------- Verify FormBeanConfig
129     // Check for ability to add a property before and after freezing
130     public void testConfigAdd() {
131         FormPropertyConfig prop = null;
132 
133         // Before freezing
134         prop = beanConfig.findFormPropertyConfig("fooProperty");
135         assertNull("fooProperty not found", prop);
136         beanConfig.addFormPropertyConfig(new FormPropertyConfig("fooProperty",
137                 "java.lang.String", ""));
138         prop = beanConfig.findFormPropertyConfig("fooProperty");
139         assertNotNull("fooProperty found", prop);
140 
141         // after freezing
142         beanConfig.freeze();
143         prop = beanConfig.findFormPropertyConfig("barProperty");
144         assertNull("barProperty not found", prop);
145 
146         try {
147             beanConfig.addFormPropertyConfig(new FormPropertyConfig(
148                     "barProperty", "java.lang.String", ""));
149             fail("barProperty add not prevented");
150         } catch (IllegalStateException e) {
151             ; // Expected result
152         }
153     }
154 
155     // Check basic FormBeanConfig properties
156     public void testConfigCreate() {
157         assertTrue("dynamic is correct", beanConfig.getDynamic());
158         assertEquals("name is correct", "dynaForm", beanConfig.getName());
159         assertEquals("type is correct",
160             "org.apache.struts.action.DynaActionForm", beanConfig.getType());
161     }
162 
163     // Check attempts to add a duplicate property name
164     public void testConfigDuplicate() {
165         FormPropertyConfig prop = null;
166 
167         assertNull("booleanProperty is found", prop);
168 
169         try {
170             beanConfig.addFormPropertyConfig(new FormPropertyConfig(
171                     "booleanProperty", "java.lang.String", ""));
172             fail("Adding duplicate property not prevented");
173         } catch (IllegalArgumentException e) {
174             ; // Expected result
175         }
176     }
177 
178     // Check the configured FormPropertyConfig element initial values
179     public void testConfigInitialValues() {
180         assertEquals("booleanProperty value", Boolean.TRUE,
181             beanConfig.findFormPropertyConfig("booleanProperty").initial());
182         assertEquals("booleanSecond value", Boolean.TRUE,
183             beanConfig.findFormPropertyConfig("booleanSecond").initial());
184         assertEquals("doubleProperty value", new Double(321.0),
185             beanConfig.findFormPropertyConfig("doubleProperty").initial());
186         assertEquals("floatProperty value", new Float((float) 123.0),
187             beanConfig.findFormPropertyConfig("floatProperty").initial());
188         assertEquals("intProperty value", new Integer(123),
189             beanConfig.findFormPropertyConfig("intProperty").initial());
190 
191         // FIXME - listIndexed
192         assertEquals("longProperty value", new Long(321),
193             beanConfig.findFormPropertyConfig("longProperty").initial());
194 
195         // FIXME - mappedProperty
196         // FIXME - mappedIntProperty
197         //        assertNull("nullProperty value",
198         //                   beanConfig.findFormPropertyConfig("nullProperty").initial());
199         assertEquals("shortProperty value", new Short((short) 987),
200             beanConfig.findFormPropertyConfig("shortProperty").initial());
201 
202         // FIXME - stringArray
203         // FIXME - stringIndexed
204         assertEquals("stringProperty value", "This is a string",
205             beanConfig.findFormPropertyConfig("stringProperty").initial());
206     }
207 
208     // Check the configured FormPropertyConfig element properties
209     public void testConfigProperties() {
210         for (int i = 0; i < dynaProperties.length; i++) {
211             FormPropertyConfig dynaProperty =
212                 beanConfig.findFormPropertyConfig(dynaProperties[i].getName());
213 
214             assertNotNull("Found dynaProperty " + dynaProperties[i].getName(),
215                 dynaProperty);
216             assertEquals("dynaProperty name for " + dynaProperties[i].getName(),
217                 dynaProperties[i].getName(), dynaProperty.getName());
218             assertEquals("dynaProperty type for " + dynaProperties[i].getName(),
219                 dynaProperties[i].getType(), dynaProperty.getType());
220             assertEquals("dynaProperty initial for "
221                 + dynaProperties[i].getName(), dynaProperties[i].getInitial(),
222                 dynaProperty.getInitial());
223         }
224     }
225 
226     // Check for ability to remove a property before and after freezing
227     public void testConfigRemove() {
228         FormPropertyConfig prop = null;
229 
230         // Before freezing
231         prop = beanConfig.findFormPropertyConfig("booleanProperty");
232         assertNotNull("booleanProperty found", prop);
233         beanConfig.removeFormPropertyConfig(prop);
234         prop = beanConfig.findFormPropertyConfig("booleanProperty");
235         assertNull("booleanProperty not deleted", prop);
236 
237         // after freezing
238         beanConfig.freeze();
239         prop = beanConfig.findFormPropertyConfig("booleanSecond");
240         assertNotNull("booleanSecond found", prop);
241 
242         try {
243             beanConfig.removeFormPropertyConfig(prop);
244             fail("booleanSecond remove not prevented");
245         } catch (IllegalStateException e) {
246             ; // Expected result
247         }
248     }
249 
250     // --------------------------------------------- Create DynaActionFormClass
251     // Test basic DynaActionFormClass name and properties
252     public void testClassCreate() {
253         assertEquals("name", "dynaForm", dynaClass.getName());
254 
255         for (int i = 0; i < dynaProperties.length; i++) {
256             DynaProperty prop =
257                 dynaClass.getDynaProperty(dynaProperties[i].getName());
258 
259             assertNotNull("Found property " + dynaProperties[i].getName());
260             assertEquals("Class for property " + dynaProperties[i].getName(),
261                 dynaProperties[i].getTypeClass(), prop.getType());
262         }
263     }
264 }