001: /*
002: * $Id: TestFormBeanConfig.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.config;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import java.lang.reflect.InvocationTargetException;
028:
029: /**
030: * Unit tests for the <code>org.apache.struts.config.FormBeanConfig</code>
031: * class. Currently only contains code to test the methods that support
032: * configuration inheritance.
033: *
034: * @version $Rev: 471754 $ $Date: 2005-05-25 19:35:00 -0400 (Wed, 25 May 2005)
035: * $
036: */
037: public class TestFormBeanConfig extends TestCase {
038: // ----------------------------------------------------- Instance Variables
039:
040: /**
041: * The ModuleConfig we'll use.
042: */
043: protected ModuleConfig config = null;
044:
045: /**
046: * The common base we'll use.
047: */
048: protected FormBeanConfig baseForm = null;
049:
050: // ----------------------------------------------------------- Constructors
051:
052: /**
053: * Construct a new instance of this test case.
054: *
055: * @param name Name of the test case
056: */
057: public TestFormBeanConfig(String name) {
058: super (name);
059: }
060:
061: // --------------------------------------------------------- Public Methods
062:
063: /**
064: * Set up instance variables required by this test case.
065: */
066: public void setUp() {
067: ModuleConfigFactory factoryObject = ModuleConfigFactory
068: .createFactory();
069:
070: config = factoryObject.createModuleConfig("");
071:
072: // setup the base form
073: baseForm = new FormBeanConfig();
074: baseForm.setName("baseForm");
075: baseForm.setType("org.apache.struts.action.DynaActionForm");
076:
077: // set up id, name, and score
078: FormPropertyConfig property = new FormPropertyConfig();
079:
080: property.setName("id");
081: property.setType("java.lang.String");
082: baseForm.addFormPropertyConfig(property);
083:
084: property = new FormPropertyConfig();
085: property.setName("name");
086: property.setType("java.lang.String");
087: property.setProperty("count", "10");
088: baseForm.addFormPropertyConfig(property);
089:
090: property = new FormPropertyConfig();
091: property.setName("score");
092: property.setType("java.lang.String");
093: baseForm.addFormPropertyConfig(property);
094:
095: property = new FormPropertyConfig();
096: property.setName("message");
097: property.setType("java.lang.String");
098: baseForm.addFormPropertyConfig(property);
099:
100: // register it to our config
101: config.addFormBeanConfig(baseForm);
102: }
103:
104: /**
105: * Return the tests included in this test suite.
106: */
107: public static Test suite() {
108: return (new TestSuite(TestFormBeanConfig.class));
109: }
110:
111: /**
112: * Tear down instance variables required by this test case.
113: */
114: public void tearDown() {
115: config = null;
116: baseForm = null;
117: }
118:
119: // ------------------------------------------------------- Individual Tests
120:
121: /**
122: * Basic check that shouldn't detect an error.
123: */
124: public void testCheckCircularInheritance() {
125: FormBeanConfig child = new FormBeanConfig();
126:
127: child.setName("child");
128: child.setExtends("baseForm");
129:
130: FormBeanConfig grandChild = new FormBeanConfig();
131:
132: grandChild.setName("grandChild");
133: grandChild.setExtends("child");
134:
135: config.addFormBeanConfig(child);
136: config.addFormBeanConfig(grandChild);
137:
138: assertTrue("Circular inheritance shouldn't have been detected",
139: !grandChild.checkCircularInheritance(config));
140: }
141:
142: /**
143: * Basic check that SHOULD detect an error.
144: */
145: public void testCheckCircularInheritanceError() {
146: FormBeanConfig child = new FormBeanConfig();
147:
148: child.setName("child");
149: child.setExtends("baseForm");
150:
151: FormBeanConfig grandChild = new FormBeanConfig();
152:
153: grandChild.setName("grandChild");
154: grandChild.setExtends("child");
155:
156: // establish the circular relationship with base
157: baseForm.setExtends("grandChild");
158:
159: config.addFormBeanConfig(child);
160: config.addFormBeanConfig(grandChild);
161:
162: assertTrue("Circular inheritance should've been detected",
163: grandChild.checkCircularInheritance(config));
164: }
165:
166: /**
167: * Test that processExtends() makes sure that a base form's own extension
168: * has been processed.
169: */
170: public void testProcessExtendsBaseFormExtends() throws Exception {
171: CustomFormBeanConfig first = new CustomFormBeanConfig();
172:
173: first.setName("first");
174:
175: CustomFormBeanConfig second = new CustomFormBeanConfig();
176:
177: second.setName("second");
178: second.setExtends("first");
179:
180: config.addFormBeanConfig(first);
181: config.addFormBeanConfig(second);
182:
183: // set baseForm to extend second
184: baseForm.setExtends("second");
185:
186: baseForm.processExtends(config);
187:
188: assertTrue("The first form's processExtends() wasn't called",
189: first.processExtendsCalled);
190: assertTrue("The second form's processExtends() wasn't called",
191: second.processExtendsCalled);
192: }
193:
194: /**
195: * Make sure that correct exception is thrown if a base form can't be
196: * found.
197: */
198: public void testProcessExtendsMissingBaseForm() throws Exception {
199: baseForm.setExtends("someMissingForm");
200:
201: try {
202: baseForm.processExtends(config);
203: fail("An exception should be thrown if a super form can't be found.");
204: } catch (NullPointerException e) {
205: // succeed
206: } catch (InstantiationException e) {
207: fail("Unrecognized exception thrown.");
208: }
209: }
210:
211: /**
212: * Test a typical form bean configuration extension where various
213: * properties should be inherited from a base form. This method checks
214: * all the properties.
215: */
216: public void testInheritFrom() throws Exception {
217: // give baseForm some arbitrary parameters
218: String baseFormCount = "1";
219:
220: baseForm.setProperty("count", baseFormCount);
221:
222: // create a basic subform
223: FormBeanConfig subForm = new FormBeanConfig();
224: String subFormName = "subForm";
225:
226: subForm.setName(subFormName);
227: subForm.setExtends("baseForm");
228:
229: // override score
230: FormPropertyConfig property = new FormPropertyConfig();
231:
232: property.setName("score");
233: property.setType("java.lang.Integer");
234: subForm.addFormPropertyConfig(property);
235:
236: // ... and id
237: property = new FormPropertyConfig();
238: property.setName("id");
239: property.setType("java.lang.String");
240: property.setInitial("unknown");
241: subForm.addFormPropertyConfig(property);
242:
243: // ... and message
244: property = new FormPropertyConfig();
245: property.setName("message");
246: property.setType("java.lang.String");
247: property.setSize(10);
248: subForm.addFormPropertyConfig(property);
249:
250: config.addFormBeanConfig(subForm);
251:
252: subForm.inheritFrom(baseForm);
253:
254: // check that our subForm is still the one in the config
255: assertSame("subForm no longer in ModuleConfig", subForm, config
256: .findFormBeanConfig("subForm"));
257:
258: // check our configured sub form
259: assertNotNull("Form bean type was not inherited", subForm
260: .getType());
261: assertEquals("Wrong form bean name", subFormName, subForm
262: .getName());
263: assertEquals("Wrong form bean type", baseForm.getType(),
264: subForm.getType());
265: assertEquals("Wrong restricted value", baseForm.isRestricted(),
266: subForm.isRestricted());
267:
268: FormPropertyConfig[] formPropertyConfigs = subForm
269: .findFormPropertyConfigs();
270:
271: assertEquals("Wrong prop count", 4, formPropertyConfigs.length);
272:
273: // we want to check that the form is EXACTLY as we want it, so
274: // here are some fine grain checks
275: property = subForm.findFormPropertyConfig("name");
276:
277: FormPropertyConfig original = baseForm
278: .findFormPropertyConfig("name");
279:
280: assertNotNull("'name' property was not inherited", property);
281: assertEquals("Wrong type for name", original.getType(),
282: property.getType());
283: assertEquals("Wrong initial value for name", original
284: .getInitial(), property.getInitial());
285: assertEquals("Wrong size value for name", original.getSize(),
286: property.getSize());
287:
288: property = subForm.findFormPropertyConfig("id");
289: original = baseForm.findFormPropertyConfig("id");
290: assertNotNull("'id' property was not found", property);
291: assertEquals("Wrong type for id", original.getType(), property
292: .getType());
293: assertEquals("Wrong initial value for id", "unknown", property
294: .getInitial());
295: assertEquals("Wrong size value for id", original.getSize(),
296: property.getSize());
297:
298: property = subForm.findFormPropertyConfig("score");
299: original = baseForm.findFormPropertyConfig("score");
300: assertNotNull("'score' property was not found", property);
301: assertEquals("Wrong type for score", "java.lang.Integer",
302: property.getType());
303: assertEquals("Wrong initial value for score", original
304: .getInitial(), property.getInitial());
305: assertEquals("Wrong size value for score", original.getSize(),
306: property.getSize());
307:
308: property = subForm.findFormPropertyConfig("message");
309: original = baseForm.findFormPropertyConfig("message");
310: assertNotNull("'message' property was not found", property);
311: assertEquals("Wrong type for message", original.getType(),
312: property.getType());
313: assertEquals("Wrong initial value for message", original
314: .getInitial(), property.getInitial());
315: assertEquals("Wrong size value for message", 10, property
316: .getSize());
317:
318: property = subForm.findFormPropertyConfig("name");
319: original = baseForm.findFormPropertyConfig("name");
320: assertEquals("Arbitrary property not found", original
321: .getProperty("count"), property.getProperty("count"));
322:
323: String count = subForm.getProperty("count");
324:
325: assertEquals("Arbitrary property was not inherited",
326: baseFormCount, count);
327: }
328:
329: /**
330: * Used to detect that FormBeanConfig is making the right calls.
331: */
332: public static class CustomFormBeanConfig extends FormBeanConfig {
333: boolean processExtendsCalled = false;
334:
335: public void processExtends(ModuleConfig moduleConfig)
336: throws ClassNotFoundException, IllegalAccessException,
337: InstantiationException, InvocationTargetException {
338: super .processExtends(moduleConfig);
339: processExtendsCalled = true;
340: }
341: }
342: }
|