View Javadoc

1   /*
2    * $Id: TestFormBeanConfig.java 421117 2006-07-12 04:35:47Z wsmoak $
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.config;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import java.lang.reflect.InvocationTargetException;
25  
26  /***
27   * Unit tests for the <code>org.apache.struts.config.FormBeanConfig</code>
28   * class.  Currently only contains code to test the methods that support
29   * configuration inheritance.
30   *
31   * @version $Rev: 421117 $ $Date: 2005-05-25 19:35:00 -0400 (Wed, 25 May 2005)
32   *          $
33   */
34  public class TestFormBeanConfig extends TestCase {
35      // ----------------------------------------------------- Instance Variables
36  
37      /***
38       * The ModuleConfig we'll use.
39       */
40      protected ModuleConfig config = null;
41  
42      /***
43       * The common base we'll use.
44       */
45      protected FormBeanConfig baseForm = null;
46  
47      // ----------------------------------------------------------- Constructors
48  
49      /***
50       * Construct a new instance of this test case.
51       *
52       * @param name Name of the test case
53       */
54      public TestFormBeanConfig(String name) {
55          super(name);
56      }
57  
58      // --------------------------------------------------------- Public Methods
59  
60      /***
61       * Set up instance variables required by this test case.
62       */
63      public void setUp() {
64          ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
65  
66          config = factoryObject.createModuleConfig("");
67  
68          // setup the base form
69          baseForm = new FormBeanConfig();
70          baseForm.setName("baseForm");
71          baseForm.setType("org.apache.struts.action.DynaActionForm");
72  
73          // set up id, name, and score
74          FormPropertyConfig property = new FormPropertyConfig();
75  
76          property.setName("id");
77          property.setType("java.lang.String");
78          baseForm.addFormPropertyConfig(property);
79  
80          property = new FormPropertyConfig();
81          property.setName("name");
82          property.setType("java.lang.String");
83          property.setProperty("count", "10");
84          baseForm.addFormPropertyConfig(property);
85  
86          property = new FormPropertyConfig();
87          property.setName("score");
88          property.setType("java.lang.String");
89          baseForm.addFormPropertyConfig(property);
90  
91          property = new FormPropertyConfig();
92          property.setName("message");
93          property.setType("java.lang.String");
94          baseForm.addFormPropertyConfig(property);
95  
96          // register it to our config
97          config.addFormBeanConfig(baseForm);
98      }
99  
100     /***
101      * Return the tests included in this test suite.
102      */
103     public static Test suite() {
104         return (new TestSuite(TestFormBeanConfig.class));
105     }
106 
107     /***
108      * Tear down instance variables required by this test case.
109      */
110     public void tearDown() {
111         config = null;
112         baseForm = null;
113     }
114 
115     // ------------------------------------------------------- Individual Tests
116 
117     /***
118      * Basic check that shouldn't detect an error.
119      */
120     public void testCheckCircularInheritance() {
121         FormBeanConfig child = new FormBeanConfig();
122 
123         child.setName("child");
124         child.setExtends("baseForm");
125 
126         FormBeanConfig grandChild = new FormBeanConfig();
127 
128         grandChild.setName("grandChild");
129         grandChild.setExtends("child");
130 
131         config.addFormBeanConfig(child);
132         config.addFormBeanConfig(grandChild);
133 
134         assertTrue("Circular inheritance shouldn't have been detected",
135             !grandChild.checkCircularInheritance(config));
136     }
137 
138     /***
139      * Basic check that SHOULD detect an error.
140      */
141     public void testCheckCircularInheritanceError() {
142         FormBeanConfig child = new FormBeanConfig();
143 
144         child.setName("child");
145         child.setExtends("baseForm");
146 
147         FormBeanConfig grandChild = new FormBeanConfig();
148 
149         grandChild.setName("grandChild");
150         grandChild.setExtends("child");
151 
152         // establish the circular relationship with base
153         baseForm.setExtends("grandChild");
154 
155         config.addFormBeanConfig(child);
156         config.addFormBeanConfig(grandChild);
157 
158         assertTrue("Circular inheritance should've been detected",
159             grandChild.checkCircularInheritance(config));
160     }
161 
162     /***
163      * Test that processExtends() makes sure that a base form's own extension
164      * has been processed.
165      */
166     public void testProcessExtendsBaseFormExtends()
167         throws Exception {
168         CustomFormBeanConfig first = new CustomFormBeanConfig();
169 
170         first.setName("first");
171 
172         CustomFormBeanConfig second = new CustomFormBeanConfig();
173 
174         second.setName("second");
175         second.setExtends("first");
176 
177         config.addFormBeanConfig(first);
178         config.addFormBeanConfig(second);
179 
180         // set baseForm to extend second
181         baseForm.setExtends("second");
182 
183         baseForm.processExtends(config);
184 
185         assertTrue("The first form's processExtends() wasn't called",
186             first.processExtendsCalled);
187         assertTrue("The second form's processExtends() wasn't called",
188             second.processExtendsCalled);
189     }
190 
191     /***
192      * Make sure that correct exception is thrown if a base form can't be
193      * found.
194      */
195     public void testProcessExtendsMissingBaseForm()
196         throws Exception {
197         baseForm.setExtends("someMissingForm");
198 
199         try {
200             baseForm.processExtends(config);
201             fail(
202                 "An exception should be thrown if a super form can't be found.");
203         } catch (NullPointerException e) {
204             // succeed
205         } catch (InstantiationException e) {
206             fail("Unrecognized exception thrown.");
207         }
208     }
209 
210     /***
211      * Test a typical form bean configuration extension where various
212      * properties should be inherited from a base form.  This method checks
213      * all the properties.
214      */
215     public void testInheritFrom()
216         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,
256             config.findFormBeanConfig("subForm"));
257 
258         // check our configured sub form
259         assertNotNull("Form bean type was not inherited", subForm.getType());
260         assertEquals("Wrong form bean name", subFormName, subForm.getName());
261         assertEquals("Wrong form bean type", baseForm.getType(),
262             subForm.getType());
263         assertEquals("Wrong restricted value", baseForm.isRestricted(),
264             subForm.isRestricted());
265 
266         FormPropertyConfig[] formPropertyConfigs =
267             subForm.findFormPropertyConfigs();
268 
269         assertEquals("Wrong prop count", 4, formPropertyConfigs.length);
270 
271         // we want to check that the form is EXACTLY as we want it, so
272         //  here are some fine grain checks
273         property = subForm.findFormPropertyConfig("name");
274 
275         FormPropertyConfig original = baseForm.findFormPropertyConfig("name");
276 
277         assertNotNull("'name' property was not inherited", property);
278         assertEquals("Wrong type for name", original.getType(),
279             property.getType());
280         assertEquals("Wrong initial value for name", original.getInitial(),
281             property.getInitial());
282         assertEquals("Wrong size value for name", original.getSize(),
283             property.getSize());
284 
285         property = subForm.findFormPropertyConfig("id");
286         original = baseForm.findFormPropertyConfig("id");
287         assertNotNull("'id' property was not found", property);
288         assertEquals("Wrong type for id", original.getType(), property.getType());
289         assertEquals("Wrong initial value for id", "unknown",
290             property.getInitial());
291         assertEquals("Wrong size value for id", original.getSize(),
292             property.getSize());
293 
294         property = subForm.findFormPropertyConfig("score");
295         original = baseForm.findFormPropertyConfig("score");
296         assertNotNull("'score' property was not found", property);
297         assertEquals("Wrong type for score", "java.lang.Integer",
298             property.getType());
299         assertEquals("Wrong initial value for score", original.getInitial(),
300             property.getInitial());
301         assertEquals("Wrong size value for score", original.getSize(),
302             property.getSize());
303 
304         property = subForm.findFormPropertyConfig("message");
305         original = baseForm.findFormPropertyConfig("message");
306         assertNotNull("'message' property was not found", property);
307         assertEquals("Wrong type for message", original.getType(),
308             property.getType());
309         assertEquals("Wrong initial value for message", original.getInitial(),
310             property.getInitial());
311         assertEquals("Wrong size value for message", 10, property.getSize());
312 
313         property = subForm.findFormPropertyConfig("name");
314         original = baseForm.findFormPropertyConfig("name");
315         assertEquals("Arbitrary property not found",
316             original.getProperty("count"), property.getProperty("count"));
317 
318         String count = subForm.getProperty("count");
319 
320         assertEquals("Arbitrary property was not inherited", baseFormCount,
321             count);
322     }
323 
324     /***
325      * Used to detect that FormBeanConfig is making the right calls.
326      */
327     public static class CustomFormBeanConfig extends FormBeanConfig {
328         boolean processExtendsCalled = false;
329 
330         public void processExtends(ModuleConfig moduleConfig)
331             throws ClassNotFoundException, IllegalAccessException,
332                 InstantiationException, InvocationTargetException {
333             super.processExtends(moduleConfig);
334             processExtendsCalled = true;
335         }
336     }
337 }