KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > action > TestDynaActionFormClass


1 /*
2  * $Id: TestDynaActionFormClass.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 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 import org.apache.struts.config.impl.ModuleConfigImpl;
28
29
30 /**
31  * Suite of unit tests for the
32  * <code>org.apache.struts.action.DynaActionFormClass</code> class.
33  */

34 public class TestDynaActionFormClass extends TestCase
35 {
36     /**
37      * Defines the testcase name for JUnit.
38      *
39      * @param theName the testcase's name.
40      */

41     public TestDynaActionFormClass(String JavaDoc theName)
42     {
43         super(theName);
44     }
45
46     /**
47      * Start the tests.
48      *
49      * @param theArgs the arguments. Not used
50      */

51     public static void main(String JavaDoc[] theArgs)
52     {
53         junit.awtui.TestRunner.main
54             (new String JavaDoc[] {TestDynaActionFormClass.class.getName()});
55     }
56
57     /**
58      * @return a test suite (<code>TestSuite</code>) that includes all methods
59      * starting with "test"
60      */

61     public static Test suite()
62     {
63         // All methods starting with "test" will be executed in the test suite.
64
return new TestSuite(TestDynaActionFormClass.class);
65     }
66
67
68     // ----------------------------------------------------- Instance Variables
69

70
71     /**
72      * The <code>FormBeanConfig</code> structure for the form bean we will
73      * be creating.
74      */

75     protected FormBeanConfig beanConfig = null;
76
77
78     /**
79      * The <code>DynaActionFormClass</code> to use for testing.
80      */

81     protected DynaActionFormClass dynaClass = null;
82
83
84     /**
85      * The set of <code>FormPropertyConfig</code> objects to use when
86      * creating our <code>FormBeanConfig</code>.
87      */

88     protected static final FormPropertyConfig[] dynaProperties = {
89         new FormPropertyConfig("booleanProperty", "boolean", "true"),
90         new FormPropertyConfig("booleanSecond", "boolean", "true"),
91         new FormPropertyConfig("doubleProperty", "double", "321.0"),
92         new FormPropertyConfig("floatProperty", "float", "123.0"),
93         new FormPropertyConfig("intArray", "int[]",
94                                "{ 0, 10,20, \"30\" '40' }"),
95         new FormPropertyConfig("intIndexed", "int[]",
96                                " 0 100, 200, 300, 400 "),
97         new FormPropertyConfig("intProperty", "int", "123"),
98         new FormPropertyConfig("listIndexed", "java.util.List", null),
99         new FormPropertyConfig("longProperty", "long", "321"),
100         new FormPropertyConfig("mappedProperty", "java.util.Map", null),
101         new FormPropertyConfig("mappedIntProperty", "java.util.Map", null),
102         // new FormPropertyConfig("nullProperty", "java.lang.String", null),
103
new FormPropertyConfig("shortProperty", "short", "987"),
104         new FormPropertyConfig("stringArray", "java.lang.String[]",
105                                "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
106         new FormPropertyConfig("stringIndexed", "java.lang.String[]",
107                                "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
108         new FormPropertyConfig("stringProperty", "java.lang.String",
109                                "This is a string"),
110    };
111
112
113     // ----------------------------------------------------- Setup and Teardown
114

115
116     public void setUp() {
117
118         // Construct a FormBeanConfig to be used
119
beanConfig = new FormBeanConfig();
120         beanConfig.setName("dynaForm");
121         beanConfig.setType("org.apache.struts.action.DynaActionForm");
122
123         // Add relevant property definitions
124
for (int i = 0; i < dynaProperties.length; i++) {
125             beanConfig.addFormPropertyConfig(dynaProperties[i]);
126         }
127         
128         // Create a temporary ModuleConfig
129
ModuleConfigImpl moduleConfig = new ModuleConfigImpl("");
130
131         // Construct a corresponding DynaActionFormClass
132
dynaClass = new DynaActionFormClass(beanConfig);
133
134     }
135
136
137     public void tearDown() {
138
139         DynaActionFormClass.clear();
140         dynaClass = null;
141         beanConfig = null;
142         DynaActionFormClass.clear();
143
144     }
145
146
147     // -------------------------------------------------- Verify FormBeanConfig
148

149
150     // Check for ability to add a property before and after freezing
151
public void testConfigAdd() {
152
153         FormPropertyConfig prop = null;
154
155         // Before freezing
156
prop = beanConfig.findFormPropertyConfig("fooProperty");
157         assertNull("fooProperty not found", prop);
158         beanConfig.addFormPropertyConfig
159             (new FormPropertyConfig("fooProperty", "java.lang.String", ""));
160         prop = beanConfig.findFormPropertyConfig("fooProperty");
161         assertNotNull("fooProperty found", prop);
162
163         // after freezing
164
beanConfig.freeze();
165         prop = beanConfig.findFormPropertyConfig("barProperty");
166         assertNull("barProperty not found", prop);
167         try {
168             beanConfig.addFormPropertyConfig
169                 (new FormPropertyConfig("barProperty", "java.lang.String", ""));
170             fail("barProperty add not prevented");
171         } catch (IllegalStateException JavaDoc e) {
172             ; // Expected result
173
}
174
175     }
176
177
178     // Check basic FormBeanConfig properties
179
public void testConfigCreate() {
180
181         assertTrue("dynamic is correct", beanConfig.getDynamic());
182         assertEquals("name is correct", "dynaForm", beanConfig.getName());
183         assertEquals("type is correct",
184                      "org.apache.struts.action.DynaActionForm",
185                      beanConfig.getType());
186
187     }
188
189
190     // Check attempts to add a duplicate property name
191
public void testConfigDuplicate() {
192
193         FormPropertyConfig prop = null;
194         assertNull("booleanProperty is found", prop);
195         try {
196             beanConfig.addFormPropertyConfig
197                 (new FormPropertyConfig("booleanProperty", "java.lang.String",
198                                         ""));
199             fail("Adding duplicate property not prevented");
200         } catch (IllegalArgumentException JavaDoc e) {
201             ; // Expected result
202
}
203
204     }
205
206
207     // Check the configured FormPropertyConfig element initial values
208
public void testConfigInitialValues() {
209
210         assertEquals("booleanProperty value",
211                      Boolean.TRUE,
212                      beanConfig.findFormPropertyConfig("booleanProperty").initial());
213         assertEquals("booleanSecond value",
214                      Boolean.TRUE,
215                      beanConfig.findFormPropertyConfig("booleanSecond").initial());
216         assertEquals("doubleProperty value",
217                      new Double JavaDoc(321.0),
218                      beanConfig.findFormPropertyConfig("doubleProperty").initial());
219         assertEquals("floatProperty value",
220                      new Float JavaDoc((float) 123.0),
221                      beanConfig.findFormPropertyConfig("floatProperty").initial());
222         assertEquals("intProperty value",
223                      new Integer JavaDoc(123),
224                      beanConfig.findFormPropertyConfig("intProperty").initial());
225         // FIXME - listIndexed
226
assertEquals("longProperty value",
227                      new Long JavaDoc(321),
228                      beanConfig.findFormPropertyConfig("longProperty").initial());
229         // FIXME - mappedProperty
230
// FIXME - mappedIntProperty
231
// assertNull("nullProperty value",
232
// beanConfig.findFormPropertyConfig("nullProperty").initial());
233
assertEquals("shortProperty value",
234                      new Short JavaDoc((short) 987),
235                      beanConfig.findFormPropertyConfig("shortProperty").initial());
236         // FIXME - stringArray
237
// FIXME - stringIndexed
238
assertEquals("stringProperty value",
239                      "This is a string",
240                      beanConfig.findFormPropertyConfig("stringProperty").initial());
241
242     }
243
244
245     // Check the configured FormPropertyConfig element properties
246
public void testConfigProperties() {
247
248         for (int i = 0; i < dynaProperties.length; i++) {
249             FormPropertyConfig dynaProperty =
250                 beanConfig.findFormPropertyConfig(dynaProperties[i].getName());
251             assertNotNull("Found dynaProperty " +
252                           dynaProperties[i].getName(), dynaProperty);
253             assertEquals("dynaProperty name for " +
254                          dynaProperties[i].getName(),
255                          dynaProperties[i].getName(),
256                          dynaProperty.getName());
257             assertEquals("dynaProperty type for " +
258                          dynaProperties[i].getName(),
259                          dynaProperties[i].getType(),
260                          dynaProperty.getType());
261             assertEquals("dynaProperty initial for " +
262                          dynaProperties[i].getName(),
263                          dynaProperties[i].getInitial(),
264                          dynaProperty.getInitial());
265         }
266
267     }
268
269
270     // Check for ability to remove a property before and after freezing
271
public void testConfigRemove() {
272
273         FormPropertyConfig prop = null;
274
275         // Before freezing
276
prop = beanConfig.findFormPropertyConfig("booleanProperty");
277         assertNotNull("booleanProperty found", prop);
278         beanConfig.removeFormPropertyConfig(prop);
279         prop = beanConfig.findFormPropertyConfig("booleanProperty");
280         assertNull("booleanProperty not deleted", prop);
281
282         // after freezing
283
beanConfig.freeze();
284         prop = beanConfig.findFormPropertyConfig("booleanSecond");
285         assertNotNull("booleanSecond found", prop);
286         try {
287             beanConfig.removeFormPropertyConfig(prop);
288             fail("booleanSecond remove not prevented");
289         } catch (IllegalStateException JavaDoc e) {
290             ; // Expected result
291
}
292
293     }
294
295
296     // --------------------------------------------- Create DynaActionFormClass
297

298
299     // Test basic DynaActionFormClass name and properties
300
public void testClassCreate() {
301
302         assertEquals("name", "dynaForm", dynaClass.getName());
303         for (int i = 0; i < dynaProperties.length; i++) {
304             DynaProperty prop =
305                 dynaClass.getDynaProperty(dynaProperties[i].getName());
306             assertNotNull("Found property " +
307                           dynaProperties[i].getName());
308             assertEquals("Class for property " +
309                          dynaProperties[i].getName(),
310                          dynaProperties[i].getTypeClass(),
311                          prop.getType());
312         }
313
314     }
315
316
317
318 }
319
Popular Tags