View Javadoc

1   /*
2    * $Id: TestForwardConfig.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  /***
25   * <p>Unit tests for ForwardConfig.  Currently contains tests for methods
26   * supporting configuration inheritance.</p>
27   *
28   * @version $Rev: 421117 $ $Date: 2005-05-21 19:06:53 -0400 (Sat, 21 May 2005)
29   *          $
30   */
31  public class TestForwardConfig extends TestCase {
32      // ----------------------------------------------------- Instance Variables
33  
34      /***
35       * The ModuleConfig we'll use.
36       */
37      protected ModuleConfig moduleConfig = null;
38  
39      /***
40       * The common base we'll use.
41       */
42      protected ForwardConfig baseConfig = null;
43  
44      /***
45       * The common subForward we'll use.
46       */
47      protected ForwardConfig subConfig = null;
48  
49      /***
50       * A ForwardConfig we'll use to test cases where a ForwardConfig declared
51       * for an action extends a ForwardConfig declared globally, with both
52       * ForwardConfigs using the same name.
53       */
54      protected ForwardConfig actionBaseConfig = null;
55  
56      /***
57       * An action mapping we'll use within tests.
58       */
59      protected ActionConfig actionConfig = null;
60  
61      // ----------------------------------------------------------- Constructors
62  
63      /***
64       * Construct a new instance of this test case.
65       *
66       * @param name Name of the test case
67       */
68      public TestForwardConfig(String name) {
69          super(name);
70      }
71  
72      // --------------------------------------------------------- Public Methods
73  
74      /***
75       * Set up instance variables required by this test case.
76       */
77      public void setUp() {
78          ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
79  
80          moduleConfig = factoryObject.createModuleConfig("");
81  
82          // Setup the base and sub forwards, with sub extending base
83          baseConfig = new ForwardConfig();
84          baseConfig.setName("baseConfig");
85          baseConfig.setPath("/somePage.jsp");
86  
87          subConfig = new ForwardConfig();
88          subConfig.setName("subConfig");
89          subConfig.setExtends("baseConfig");
90          subConfig.setRedirect(true);
91  
92          actionBaseConfig = new ForwardConfig();
93          actionBaseConfig.setName("baseConfig");
94          actionBaseConfig.setExtends("baseConfig");
95          actionBaseConfig.setModule("/other");
96  
97          // Setup the default action config
98          actionConfig = new ActionConfig();
99          actionConfig.setPath("/index");
100         moduleConfig.addActionConfig(actionConfig);
101 
102         // No forward configs are registered to either module or action configs.
103         // Each test will determine where it needs these configs, if at all.
104     }
105 
106     /***
107      * Return the tests included in this test suite.
108      */
109     public static Test suite() {
110         return (new TestSuite(TestForwardConfig.class));
111     }
112 
113     /***
114      * Tear down instance variables required by this test case.
115      */
116     public void tearDown() {
117         moduleConfig = null;
118         baseConfig = null;
119     }
120 
121     // ------------------------------------------------------- Individual Tests
122 
123     /***
124      * Make sure checkCircularInheritance() works as expected where there is
125      * no inheritance set up.
126      */
127     public void testCheckCircularInheritanceNoExtends() {
128         moduleConfig.addForwardConfig(baseConfig);
129 
130         boolean result =
131             baseConfig.checkCircularInheritance(moduleConfig, null);
132 
133         assertTrue("Incorrect result", !result);
134     }
135 
136     /***
137      * Test checkCircularInheritance() for when there is no circular
138      * inheritance.
139      */
140     public void testCheckCircularInheritanceNoConflicts() {
141         moduleConfig.addForwardConfig(baseConfig);
142         moduleConfig.addForwardConfig(subConfig);
143 
144         boolean result = subConfig.checkCircularInheritance(moduleConfig, null);
145 
146         assertTrue("Incorrect result", !result);
147     }
148 
149     /***
150      * Test checkCircularInheritance() for circular inheritance between global
151      * forwards.
152      */
153     public void testCheckCircularInheritanceBasicGlobal() {
154         moduleConfig.addForwardConfig(subConfig);
155         moduleConfig.addForwardConfig(baseConfig);
156 
157         // set the baseConfig to extend subConfig
158         baseConfig.setExtends("subConfig");
159 
160         boolean result = subConfig.checkCircularInheritance(moduleConfig, null);
161 
162         assertTrue("Circular inheritance not detected.", result);
163     }
164 
165     /***
166      * Test checkCircularInheritance() for circular inheritance between global
167      * forwards.
168      */
169     public void testCheckCircularInheritanceGlobal2Levels() {
170         moduleConfig.addForwardConfig(baseConfig);
171         moduleConfig.addForwardConfig(subConfig);
172 
173         ForwardConfig grand = new ForwardConfig();
174 
175         grand.setName("grandConfig");
176         grand.setExtends("subConfig");
177         moduleConfig.addForwardConfig(grand);
178 
179         // set the baseConfig to extend grandConfig
180         baseConfig.setExtends("grandConfig");
181 
182         boolean result = grand.checkCircularInheritance(moduleConfig, null);
183 
184         assertTrue("Circular inheritance not detected.", result);
185     }
186 
187     /***
188      * Test checkCircularInheritance() for circular inheritance between
189      * forwards in an action.
190      */
191     public void testCheckCircularInheritanceActionForwardsNoConflict() {
192         actionConfig.addForwardConfig(baseConfig);
193         actionConfig.addForwardConfig(subConfig);
194 
195         boolean result =
196             subConfig.checkCircularInheritance(moduleConfig, actionConfig);
197 
198         assertTrue("Incorrect result", !result);
199     }
200 
201     /***
202      * Test checkCircularInheritance() for circular inheritance between
203      * forwards in an action.
204      */
205     public void testCheckCircularInheritanceActionForwardsBasic() {
206         actionConfig.addForwardConfig(baseConfig);
207         actionConfig.addForwardConfig(subConfig);
208 
209         // set base to extend sub
210         baseConfig.setExtends("subConfig");
211 
212         boolean result =
213             subConfig.checkCircularInheritance(moduleConfig, actionConfig);
214 
215         assertTrue("Circular inheritance not detected.", result);
216     }
217 
218     /***
219      * Test checkCircularInheritance() for circular inheritance between a
220      * forward declared in an action and a global forward.
221      */
222     public void testCheckCircularInheritanceActionForwardExtendGlobal() {
223         actionConfig.addForwardConfig(subConfig);
224         moduleConfig.addForwardConfig(baseConfig);
225 
226         boolean result =
227             subConfig.checkCircularInheritance(moduleConfig, actionConfig);
228 
229         assertTrue("Incorrect result", !result);
230     }
231 
232     /***
233      * Test checkCircularInheritance() for circular inheritance between a
234      * forward declared in an action and a global forward and both forwards
235      * have the same name.
236      */
237     public void testCheckCircularInheritanceActionForwardExtendGlobalSameName() {
238         moduleConfig.addForwardConfig(baseConfig);
239         actionConfig.addForwardConfig(actionBaseConfig);
240 
241         boolean result =
242             actionBaseConfig.checkCircularInheritance(moduleConfig, actionConfig);
243 
244         assertTrue("Incorrect result", !result);
245     }
246 
247     /***
248      * Make sure processExtends() throws an error when the config is already
249      * configured.
250      */
251     public void testProcessExtendsConfigured()
252         throws Exception {
253         baseConfig.configured = true;
254         moduleConfig.addForwardConfig(baseConfig);
255 
256         try {
257             baseConfig.processExtends(moduleConfig, null);
258             fail(
259                 "processExtends should not succeed when object is already configured");
260         } catch (IllegalStateException e) {
261             // success
262         }
263     }
264 
265     /***
266      * Test processExtends() when nothing is extended.
267      */
268     public void testProcessExtendsNoExtension()
269         throws Exception {
270         String path = baseConfig.getPath();
271         String module = baseConfig.getModule();
272         String name = baseConfig.getName();
273         String inherit = baseConfig.getExtends();
274         boolean redirect = baseConfig.getRedirect();
275 
276         moduleConfig.addForwardConfig(baseConfig);
277         baseConfig.processExtends(moduleConfig, null);
278 
279         assertEquals("Path shouldn't have changed", path, baseConfig.getPath());
280         assertEquals("Module shouldn't have changed", module,
281             baseConfig.getModule());
282         assertEquals("Name shouldn't have changed", name, baseConfig.getName());
283         assertEquals("Extends shouldn't have changed", inherit,
284             baseConfig.getExtends());
285         assertEquals("Redirect shouldn't have changed", redirect,
286             baseConfig.getRedirect());
287     }
288 
289     /***
290      * Test processExtends() with a basic extension.
291      */
292     public void testProcessExtendsBasicExtension()
293         throws Exception {
294         String baseCount = "10";
295 
296         baseConfig.setProperty("count", baseCount);
297 
298         String baseLabel = "label a";
299 
300         baseConfig.setProperty("label", baseLabel);
301 
302         String path = baseConfig.getPath();
303         String module = baseConfig.getModule();
304 
305         String inherit = subConfig.getExtends();
306         String name = subConfig.getName();
307         boolean redirect = subConfig.getRedirect();
308 
309         String subLabel = "label b";
310 
311         subConfig.setProperty("label", subLabel);
312 
313         moduleConfig.addForwardConfig(baseConfig);
314         moduleConfig.addForwardConfig(subConfig);
315         subConfig.processExtends(moduleConfig, null);
316 
317         assertEquals("Path wasn't inherited", path, subConfig.getPath());
318         assertEquals("Module wasn't inherited", module, subConfig.getModule());
319         assertEquals("Name shouldn't have changed", name, subConfig.getName());
320         assertEquals("Extends shouldn't have changed", inherit,
321             subConfig.getExtends());
322         assertEquals("Redirect shouldn't have changed", redirect,
323             subConfig.getRedirect());
324         assertEquals("Arbitrary config property was not inherited", baseCount,
325             subConfig.getProperty("count"));
326         assertEquals("Overridden config property was not retained", subLabel,
327             subConfig.getProperty("label"));
328     }
329 
330     /***
331      * Test processExtends() with a basic extension between an action config
332      * and a global config.
333      */
334     public void testProcessExtendsBasicGlobalExtension()
335         throws Exception {
336         String path = baseConfig.getPath();
337         String module = baseConfig.getModule();
338 
339         boolean redirect = subConfig.getRedirect();
340         String inherit = subConfig.getExtends();
341         String name = subConfig.getName();
342 
343         moduleConfig.addForwardConfig(baseConfig);
344         actionConfig.addForwardConfig(subConfig);
345         subConfig.processExtends(moduleConfig, actionConfig);
346 
347         assertEquals("Path wasn't inherited", path, subConfig.getPath());
348         assertEquals("Module wasn't inherited", module, subConfig.getModule());
349         assertEquals("Name shouldn't have changed", name, subConfig.getName());
350         assertEquals("Extends shouldn't have changed", inherit,
351             subConfig.getExtends());
352         assertEquals("Redirect shouldn't have changed", redirect,
353             subConfig.getRedirect());
354     }
355 
356     /***
357      * Test processExtends() with an incorrect setup where a global config
358      * attempts to extend an action config.
359      */
360     public void testProcessExtendsGlobalExtendingAction()
361         throws Exception {
362         moduleConfig.addForwardConfig(subConfig);
363         actionConfig.addForwardConfig(baseConfig);
364 
365         try {
366             subConfig.processExtends(moduleConfig, actionConfig);
367             fail(
368                 "Should not find config from actionConfig when *this* is global");
369         } catch (NullPointerException npe) {
370             // succeed
371         }
372     }
373 
374     /***
375      * Test processExtends() with an action config that extends a global
376      * config with the same name.
377      */
378     public void testProcessExtendsSameNames()
379         throws Exception {
380         String path = baseConfig.getPath();
381         boolean redirect = baseConfig.getRedirect();
382 
383         String module = actionBaseConfig.getModule();
384         String inherit = actionBaseConfig.getExtends();
385         String name = actionBaseConfig.getName();
386 
387         moduleConfig.addForwardConfig(baseConfig);
388         actionConfig.addForwardConfig(actionBaseConfig);
389 
390         actionBaseConfig.processExtends(moduleConfig, actionConfig);
391 
392         assertEquals("Path wasn't inherited", path, actionBaseConfig.getPath());
393         assertEquals("Module shouldn't have changed", module,
394             actionBaseConfig.getModule());
395         assertEquals("Name shouldn't have changed", name,
396             actionBaseConfig.getName());
397         assertEquals("Extends shouldn't have changed", inherit,
398             actionBaseConfig.getExtends());
399         assertEquals("Redirect shouldn't have changed", redirect,
400             actionBaseConfig.getRedirect());
401     }
402 
403     /***
404      * Test processExtends() where an action ForwardConfig extends another
405      * ForwardConfig, which in turn extends a global ForwardConfig with the
406      * same name.
407      */
408     public void testProcessExtendsActionExtendsActionExtendsGlobalWithSameName()
409         throws Exception {
410         String path = baseConfig.getPath();
411 
412         String module = actionBaseConfig.getModule();
413 
414         boolean redirect = subConfig.getRedirect();
415         String inherit = subConfig.getExtends();
416         String name = subConfig.getName();
417 
418         moduleConfig.addForwardConfig(baseConfig);
419         actionConfig.addForwardConfig(actionBaseConfig);
420         actionConfig.addForwardConfig(subConfig);
421 
422         subConfig.processExtends(moduleConfig, actionConfig);
423 
424         assertTrue("baseConfig's processExtends() should've been called",
425             baseConfig.extensionProcessed);
426         assertTrue("actionBaseConfig's processExtends() should've been called",
427             actionBaseConfig.extensionProcessed);
428 
429         assertEquals("Path wasn't inherited", path, subConfig.getPath());
430         assertEquals("Module wasn't inherited", module, subConfig.getModule());
431         assertEquals("Name shouldn't have changed", name, subConfig.getName());
432         assertEquals("Extends shouldn't have changed", inherit,
433             subConfig.getExtends());
434         assertEquals("Redirect shouldn't have changed", redirect,
435             subConfig.getRedirect());
436     }
437 }