001: /*
002: * $Id: TestDynaActionFormClass.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.action;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.beanutils.DynaProperty;
028: import org.apache.struts.config.FormBeanConfig;
029: import org.apache.struts.config.FormPropertyConfig;
030:
031: /**
032: * Suite of unit tests for the <code>org.apache.struts.action.DynaActionFormClass</code>
033: * class.
034: */
035: public class TestDynaActionFormClass extends TestCase {
036: /**
037: * The set of <code>FormPropertyConfig</code> objects to use when creating
038: * our <code>FormBeanConfig</code>.
039: */
040: protected static final FormPropertyConfig[] dynaProperties = {
041: new FormPropertyConfig("booleanProperty", "boolean",
042: "true", "true"),
043: new FormPropertyConfig("booleanSecond", "boolean", "true",
044: "true"),
045: new FormPropertyConfig("doubleProperty", "double", "321.0",
046: "GET"),
047: new FormPropertyConfig("floatProperty", "float", "123.0",
048: "POST, HEAD"),
049: new FormPropertyConfig("intArray", "int[]",
050: "{ 0, 10,20, \"30\" '40' }"),
051: new FormPropertyConfig("intIndexed", "int[]",
052: " 0 100, 200, 300, 400 "),
053: new FormPropertyConfig("intProperty", "int", "123"),
054: new FormPropertyConfig("listIndexed", "java.util.List",
055: null),
056: new FormPropertyConfig("longProperty", "long", "321"),
057: new FormPropertyConfig("mappedProperty", "java.util.Map",
058: null),
059: new FormPropertyConfig("mappedIntProperty",
060: "java.util.Map", null),
061:
062: // new FormPropertyConfig("nullProperty", "java.lang.String", null),
063: new FormPropertyConfig("shortProperty", "short", "987"),
064: new FormPropertyConfig("stringArray", "java.lang.String[]",
065: "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
066: new FormPropertyConfig("stringIndexed",
067: "java.lang.String[]",
068: "{ 'String 0', 'String 1', 'String 2', 'String 3', 'String 4'}"),
069: new FormPropertyConfig("stringProperty",
070: "java.lang.String", "This is a string"), };
071:
072: // ----------------------------------------------------- Instance Variables
073:
074: /**
075: * The <code>FormBeanConfig</code> structure for the form bean we will be
076: * creating.
077: */
078: protected FormBeanConfig beanConfig = null;
079:
080: /**
081: * The <code>DynaActionFormClass</code> to use for testing.
082: */
083: protected DynaActionFormClass dynaClass = null;
084:
085: /**
086: * Defines the testcase name for JUnit.
087: *
088: * @param theName the testcase's name.
089: */
090: public TestDynaActionFormClass(String theName) {
091: super (theName);
092: }
093:
094: /**
095: * Start the tests.
096: *
097: * @param theArgs the arguments. Not used
098: */
099: public static void main(String[] theArgs) {
100: junit.awtui.TestRunner
101: .main(new String[] { TestDynaActionFormClass.class
102: .getName() });
103: }
104:
105: /**
106: * @return a test suite (<code>TestSuite</code>) that includes all methods
107: * starting with "test"
108: */
109: public static Test suite() {
110: // All methods starting with "test" will be executed in the test suite.
111: return new TestSuite(TestDynaActionFormClass.class);
112: }
113:
114: // ----------------------------------------------------- Setup and Teardown
115: public void setUp() {
116: // Construct a FormBeanConfig to be used
117: beanConfig = new FormBeanConfig();
118: beanConfig.setName("dynaForm");
119: beanConfig.setType("org.apache.struts.action.DynaActionForm");
120:
121: // Add relevant property definitions
122: for (int i = 0; i < dynaProperties.length; i++) {
123: beanConfig.addFormPropertyConfig(dynaProperties[i]);
124: }
125:
126: // Construct a corresponding DynaActionFormClass
127: dynaClass = new DynaActionFormClass(beanConfig);
128: }
129:
130: public void tearDown() {
131: dynaClass = null;
132: beanConfig = null;
133: }
134:
135: // -------------------------------------------------- Verify FormBeanConfig
136: // Check for ability to add a property before and after freezing
137: public void testConfigAdd() {
138: FormPropertyConfig prop = null;
139:
140: // Before freezing
141: prop = beanConfig.findFormPropertyConfig("fooProperty");
142: assertNull("fooProperty not found", prop);
143: beanConfig.addFormPropertyConfig(new FormPropertyConfig(
144: "fooProperty", "java.lang.String", ""));
145: prop = beanConfig.findFormPropertyConfig("fooProperty");
146: assertNotNull("fooProperty found", prop);
147:
148: // after freezing
149: beanConfig.freeze();
150: prop = beanConfig.findFormPropertyConfig("barProperty");
151: assertNull("barProperty not found", prop);
152:
153: try {
154: beanConfig.addFormPropertyConfig(new FormPropertyConfig(
155: "barProperty", "java.lang.String", ""));
156: fail("barProperty add not prevented");
157: } catch (IllegalStateException e) {
158: ; // Expected result
159: }
160: }
161:
162: // Check basic FormBeanConfig properties
163: public void testConfigCreate() {
164: assertTrue("dynamic is correct", beanConfig.getDynamic());
165: assertEquals("name is correct", "dynaForm", beanConfig
166: .getName());
167: assertEquals("type is correct",
168: "org.apache.struts.action.DynaActionForm", beanConfig
169: .getType());
170: }
171:
172: // Check attempts to add a duplicate property name
173: public void testConfigDuplicate() {
174: FormPropertyConfig prop = null;
175:
176: assertNull("booleanProperty is found", prop);
177:
178: try {
179: beanConfig.addFormPropertyConfig(new FormPropertyConfig(
180: "booleanProperty", "java.lang.String", ""));
181: fail("Adding duplicate property not prevented");
182: } catch (IllegalArgumentException e) {
183: ; // Expected result
184: }
185: }
186:
187: // Check the configured FormPropertyConfig element initial values
188: public void testConfigInitialValues() {
189: assertEquals("booleanProperty value", Boolean.TRUE, beanConfig
190: .findFormPropertyConfig("booleanProperty").initial());
191: assertEquals("booleanSecond value", Boolean.TRUE, beanConfig
192: .findFormPropertyConfig("booleanSecond").initial());
193: assertEquals("doubleProperty value", new Double(321.0),
194: beanConfig.findFormPropertyConfig("doubleProperty")
195: .initial());
196: assertEquals("floatProperty value", new Float((float) 123.0),
197: beanConfig.findFormPropertyConfig("floatProperty")
198: .initial());
199: assertEquals("intProperty value", new Integer(123), beanConfig
200: .findFormPropertyConfig("intProperty").initial());
201:
202: // FIXME - listIndexed
203: assertEquals("longProperty value", new Long(321), beanConfig
204: .findFormPropertyConfig("longProperty").initial());
205:
206: // FIXME - mappedProperty
207: // FIXME - mappedIntProperty
208: // assertNull("nullProperty value",
209: // beanConfig.findFormPropertyConfig("nullProperty").initial());
210: assertEquals("shortProperty value", new Short((short) 987),
211: beanConfig.findFormPropertyConfig("shortProperty")
212: .initial());
213:
214: // FIXME - stringArray
215: // FIXME - stringIndexed
216: assertEquals("stringProperty value", "This is a string",
217: beanConfig.findFormPropertyConfig("stringProperty")
218: .initial());
219: }
220:
221: // Check the configured FormPropertyConfig element properties
222: public void testConfigProperties() {
223: for (int i = 0; i < dynaProperties.length; i++) {
224: FormPropertyConfig dynaProperty = beanConfig
225: .findFormPropertyConfig(dynaProperties[i].getName());
226:
227: assertNotNull("Found dynaProperty "
228: + dynaProperties[i].getName(), dynaProperty);
229: assertEquals("dynaProperty name for "
230: + dynaProperties[i].getName(), dynaProperties[i]
231: .getName(), dynaProperty.getName());
232: assertEquals("dynaProperty type for "
233: + dynaProperties[i].getName(), dynaProperties[i]
234: .getType(), dynaProperty.getType());
235: assertEquals("dynaProperty initial for "
236: + dynaProperties[i].getName(), dynaProperties[i]
237: .getInitial(), dynaProperty.getInitial());
238: }
239: }
240:
241: // Check for ability to remove a property before and after freezing
242: public void testConfigRemove() {
243: FormPropertyConfig prop = null;
244:
245: // Before freezing
246: prop = beanConfig.findFormPropertyConfig("booleanProperty");
247: assertNotNull("booleanProperty found", prop);
248: beanConfig.removeFormPropertyConfig(prop);
249: prop = beanConfig.findFormPropertyConfig("booleanProperty");
250: assertNull("booleanProperty not deleted", prop);
251:
252: // after freezing
253: beanConfig.freeze();
254: prop = beanConfig.findFormPropertyConfig("booleanSecond");
255: assertNotNull("booleanSecond found", prop);
256:
257: try {
258: beanConfig.removeFormPropertyConfig(prop);
259: fail("booleanSecond remove not prevented");
260: } catch (IllegalStateException e) {
261: ; // Expected result
262: }
263: }
264:
265: // --------------------------------------------- Create DynaActionFormClass
266: // Test basic DynaActionFormClass name and properties
267: public void testClassCreate() {
268: assertEquals("name", "dynaForm", dynaClass.getName());
269:
270: for (int i = 0; i < dynaProperties.length; i++) {
271: DynaProperty prop = dynaClass
272: .getDynaProperty(dynaProperties[i].getName());
273:
274: assertNotNull("Found property "
275: + dynaProperties[i].getName());
276: assertEquals("Class for property "
277: + dynaProperties[i].getName(), dynaProperties[i]
278: .getTypeClass(), prop.getType());
279: }
280: }
281: }
|