001: /*
002: * $Id: TestForwardConfig.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: /**
028: * <p>Unit tests for ForwardConfig. Currently contains tests for methods
029: * supporting configuration inheritance.</p>
030: *
031: * @version $Rev: 471754 $ $Date: 2005-05-21 19:06:53 -0400 (Sat, 21 May 2005)
032: * $
033: */
034: public class TestForwardConfig extends TestCase {
035: // ----------------------------------------------------- Instance Variables
036:
037: /**
038: * The ModuleConfig we'll use.
039: */
040: protected ModuleConfig moduleConfig = null;
041:
042: /**
043: * The common base we'll use.
044: */
045: protected ForwardConfig baseConfig = null;
046:
047: /**
048: * The common subForward we'll use.
049: */
050: protected ForwardConfig subConfig = null;
051:
052: /**
053: * A ForwardConfig we'll use to test cases where a ForwardConfig declared
054: * for an action extends a ForwardConfig declared globally, with both
055: * ForwardConfigs using the same name.
056: */
057: protected ForwardConfig actionBaseConfig = null;
058:
059: /**
060: * An action mapping we'll use within tests.
061: */
062: protected ActionConfig actionConfig = null;
063:
064: // ----------------------------------------------------------- Constructors
065:
066: /**
067: * Construct a new instance of this test case.
068: *
069: * @param name Name of the test case
070: */
071: public TestForwardConfig(String name) {
072: super (name);
073: }
074:
075: // --------------------------------------------------------- Public Methods
076:
077: /**
078: * Set up instance variables required by this test case.
079: */
080: public void setUp() {
081: ModuleConfigFactory factoryObject = ModuleConfigFactory
082: .createFactory();
083:
084: moduleConfig = factoryObject.createModuleConfig("");
085:
086: // Setup the base and sub forwards, with sub extending base
087: baseConfig = new ForwardConfig();
088: baseConfig.setName("baseConfig");
089: baseConfig.setPath("/somePage.jsp");
090:
091: subConfig = new ForwardConfig();
092: subConfig.setName("subConfig");
093: subConfig.setExtends("baseConfig");
094: subConfig.setRedirect(true);
095:
096: actionBaseConfig = new ForwardConfig();
097: actionBaseConfig.setName("baseConfig");
098: actionBaseConfig.setExtends("baseConfig");
099: actionBaseConfig.setModule("/other");
100:
101: // Setup the default action config
102: actionConfig = new ActionConfig();
103: actionConfig.setPath("/index");
104: moduleConfig.addActionConfig(actionConfig);
105:
106: // No forward configs are registered to either module or action configs.
107: // Each test will determine where it needs these configs, if at all.
108: }
109:
110: /**
111: * Return the tests included in this test suite.
112: */
113: public static Test suite() {
114: return (new TestSuite(TestForwardConfig.class));
115: }
116:
117: /**
118: * Tear down instance variables required by this test case.
119: */
120: public void tearDown() {
121: moduleConfig = null;
122: baseConfig = null;
123: }
124:
125: // ------------------------------------------------------- Individual Tests
126:
127: /**
128: * Make sure checkCircularInheritance() works as expected where there is
129: * no inheritance set up.
130: */
131: public void testCheckCircularInheritanceNoExtends() {
132: moduleConfig.addForwardConfig(baseConfig);
133:
134: boolean result = baseConfig.checkCircularInheritance(
135: moduleConfig, null);
136:
137: assertTrue("Incorrect result", !result);
138: }
139:
140: /**
141: * Test checkCircularInheritance() for when there is no circular
142: * inheritance.
143: */
144: public void testCheckCircularInheritanceNoConflicts() {
145: moduleConfig.addForwardConfig(baseConfig);
146: moduleConfig.addForwardConfig(subConfig);
147:
148: boolean result = subConfig.checkCircularInheritance(
149: moduleConfig, null);
150:
151: assertTrue("Incorrect result", !result);
152: }
153:
154: /**
155: * Test checkCircularInheritance() for circular inheritance between global
156: * forwards.
157: */
158: public void testCheckCircularInheritanceBasicGlobal() {
159: moduleConfig.addForwardConfig(subConfig);
160: moduleConfig.addForwardConfig(baseConfig);
161:
162: // set the baseConfig to extend subConfig
163: baseConfig.setExtends("subConfig");
164:
165: boolean result = subConfig.checkCircularInheritance(
166: moduleConfig, null);
167:
168: assertTrue("Circular inheritance not detected.", result);
169: }
170:
171: /**
172: * Test checkCircularInheritance() for circular inheritance between global
173: * forwards.
174: */
175: public void testCheckCircularInheritanceGlobal2Levels() {
176: moduleConfig.addForwardConfig(baseConfig);
177: moduleConfig.addForwardConfig(subConfig);
178:
179: ForwardConfig grand = new ForwardConfig();
180:
181: grand.setName("grandConfig");
182: grand.setExtends("subConfig");
183: moduleConfig.addForwardConfig(grand);
184:
185: // set the baseConfig to extend grandConfig
186: baseConfig.setExtends("grandConfig");
187:
188: boolean result = grand.checkCircularInheritance(moduleConfig,
189: null);
190:
191: assertTrue("Circular inheritance not detected.", result);
192: }
193:
194: /**
195: * Test checkCircularInheritance() for circular inheritance between
196: * forwards in an action.
197: */
198: public void testCheckCircularInheritanceActionForwardsNoConflict() {
199: actionConfig.addForwardConfig(baseConfig);
200: actionConfig.addForwardConfig(subConfig);
201:
202: boolean result = subConfig.checkCircularInheritance(
203: moduleConfig, actionConfig);
204:
205: assertTrue("Incorrect result", !result);
206: }
207:
208: /**
209: * Test checkCircularInheritance() for circular inheritance between
210: * forwards in an action.
211: */
212: public void testCheckCircularInheritanceActionForwardsBasic() {
213: actionConfig.addForwardConfig(baseConfig);
214: actionConfig.addForwardConfig(subConfig);
215:
216: // set base to extend sub
217: baseConfig.setExtends("subConfig");
218:
219: boolean result = subConfig.checkCircularInheritance(
220: moduleConfig, actionConfig);
221:
222: assertTrue("Circular inheritance not detected.", result);
223: }
224:
225: /**
226: * Test checkCircularInheritance() for circular inheritance between a
227: * forward declared in an action and a global forward.
228: */
229: public void testCheckCircularInheritanceActionForwardExtendGlobal() {
230: actionConfig.addForwardConfig(subConfig);
231: moduleConfig.addForwardConfig(baseConfig);
232:
233: boolean result = subConfig.checkCircularInheritance(
234: moduleConfig, actionConfig);
235:
236: assertTrue("Incorrect result", !result);
237: }
238:
239: /**
240: * Test checkCircularInheritance() for circular inheritance between a
241: * forward declared in an action and a global forward and both forwards
242: * have the same name.
243: */
244: public void testCheckCircularInheritanceActionForwardExtendGlobalSameName() {
245: moduleConfig.addForwardConfig(baseConfig);
246: actionConfig.addForwardConfig(actionBaseConfig);
247:
248: boolean result = actionBaseConfig.checkCircularInheritance(
249: moduleConfig, actionConfig);
250:
251: assertTrue("Incorrect result", !result);
252: }
253:
254: /**
255: * Make sure processExtends() throws an error when the config is already
256: * configured.
257: */
258: public void testProcessExtendsConfigured() throws Exception {
259: baseConfig.configured = true;
260: moduleConfig.addForwardConfig(baseConfig);
261:
262: try {
263: baseConfig.processExtends(moduleConfig, null);
264: fail("processExtends should not succeed when object is already configured");
265: } catch (IllegalStateException e) {
266: // success
267: }
268: }
269:
270: /**
271: * Test processExtends() when nothing is extended.
272: */
273: public void testProcessExtendsNoExtension() throws Exception {
274: String path = baseConfig.getPath();
275: String module = baseConfig.getModule();
276: String name = baseConfig.getName();
277: String inherit = baseConfig.getExtends();
278: boolean redirect = baseConfig.getRedirect();
279:
280: moduleConfig.addForwardConfig(baseConfig);
281: baseConfig.processExtends(moduleConfig, null);
282:
283: assertEquals("Path shouldn't have changed", path, baseConfig
284: .getPath());
285: assertEquals("Module shouldn't have changed", module,
286: baseConfig.getModule());
287: assertEquals("Name shouldn't have changed", name, baseConfig
288: .getName());
289: assertEquals("Extends shouldn't have changed", inherit,
290: baseConfig.getExtends());
291: assertEquals("Redirect shouldn't have changed", redirect,
292: baseConfig.getRedirect());
293: }
294:
295: /**
296: * Test processExtends() with a basic extension.
297: */
298: public void testProcessExtendsBasicExtension() throws Exception {
299: String baseCount = "10";
300:
301: baseConfig.setProperty("count", baseCount);
302:
303: String baseLabel = "label a";
304:
305: baseConfig.setProperty("label", baseLabel);
306:
307: String path = baseConfig.getPath();
308: String module = baseConfig.getModule();
309:
310: String inherit = subConfig.getExtends();
311: String name = subConfig.getName();
312: boolean redirect = subConfig.getRedirect();
313:
314: String subLabel = "label b";
315:
316: subConfig.setProperty("label", subLabel);
317:
318: moduleConfig.addForwardConfig(baseConfig);
319: moduleConfig.addForwardConfig(subConfig);
320: subConfig.processExtends(moduleConfig, null);
321:
322: assertEquals("Path wasn't inherited", path, subConfig.getPath());
323: assertEquals("Module wasn't inherited", module, subConfig
324: .getModule());
325: assertEquals("Name shouldn't have changed", name, subConfig
326: .getName());
327: assertEquals("Extends shouldn't have changed", inherit,
328: subConfig.getExtends());
329: assertEquals("Redirect shouldn't have changed", redirect,
330: subConfig.getRedirect());
331: assertEquals("Arbitrary config property was not inherited",
332: baseCount, subConfig.getProperty("count"));
333: assertEquals("Overridden config property was not retained",
334: subLabel, subConfig.getProperty("label"));
335: }
336:
337: /**
338: * Test processExtends() with a basic extension between an action config
339: * and a global config.
340: */
341: public void testProcessExtendsBasicGlobalExtension()
342: throws Exception {
343: String path = baseConfig.getPath();
344: String module = baseConfig.getModule();
345:
346: boolean redirect = subConfig.getRedirect();
347: String inherit = subConfig.getExtends();
348: String name = subConfig.getName();
349:
350: moduleConfig.addForwardConfig(baseConfig);
351: actionConfig.addForwardConfig(subConfig);
352: subConfig.processExtends(moduleConfig, actionConfig);
353:
354: assertEquals("Path wasn't inherited", path, subConfig.getPath());
355: assertEquals("Module wasn't inherited", module, subConfig
356: .getModule());
357: assertEquals("Name shouldn't have changed", name, subConfig
358: .getName());
359: assertEquals("Extends shouldn't have changed", inherit,
360: subConfig.getExtends());
361: assertEquals("Redirect shouldn't have changed", redirect,
362: subConfig.getRedirect());
363: }
364:
365: /**
366: * Test processExtends() with an incorrect setup where a global config
367: * attempts to extend an action config.
368: */
369: public void testProcessExtendsGlobalExtendingAction()
370: throws Exception {
371: moduleConfig.addForwardConfig(subConfig);
372: actionConfig.addForwardConfig(baseConfig);
373:
374: try {
375: subConfig.processExtends(moduleConfig, actionConfig);
376: fail("Should not find config from actionConfig when *this* is global");
377: } catch (NullPointerException npe) {
378: // succeed
379: }
380: }
381:
382: /**
383: * Test processExtends() with an action config that extends a global
384: * config with the same name.
385: */
386: public void testProcessExtendsSameNames() throws Exception {
387: String path = baseConfig.getPath();
388: boolean redirect = baseConfig.getRedirect();
389:
390: String module = actionBaseConfig.getModule();
391: String inherit = actionBaseConfig.getExtends();
392: String name = actionBaseConfig.getName();
393:
394: moduleConfig.addForwardConfig(baseConfig);
395: actionConfig.addForwardConfig(actionBaseConfig);
396:
397: actionBaseConfig.processExtends(moduleConfig, actionConfig);
398:
399: assertEquals("Path wasn't inherited", path, actionBaseConfig
400: .getPath());
401: assertEquals("Module shouldn't have changed", module,
402: actionBaseConfig.getModule());
403: assertEquals("Name shouldn't have changed", name,
404: actionBaseConfig.getName());
405: assertEquals("Extends shouldn't have changed", inherit,
406: actionBaseConfig.getExtends());
407: assertEquals("Redirect shouldn't have changed", redirect,
408: actionBaseConfig.getRedirect());
409: }
410:
411: /**
412: * Test processExtends() where an action ForwardConfig extends another
413: * ForwardConfig, which in turn extends a global ForwardConfig with the
414: * same name.
415: */
416: public void testProcessExtendsActionExtendsActionExtendsGlobalWithSameName()
417: throws Exception {
418: String path = baseConfig.getPath();
419:
420: String module = actionBaseConfig.getModule();
421:
422: boolean redirect = subConfig.getRedirect();
423: String inherit = subConfig.getExtends();
424: String name = subConfig.getName();
425:
426: moduleConfig.addForwardConfig(baseConfig);
427: actionConfig.addForwardConfig(actionBaseConfig);
428: actionConfig.addForwardConfig(subConfig);
429:
430: subConfig.processExtends(moduleConfig, actionConfig);
431:
432: assertTrue(
433: "baseConfig's processExtends() should've been called",
434: baseConfig.extensionProcessed);
435: assertTrue(
436: "actionBaseConfig's processExtends() should've been called",
437: actionBaseConfig.extensionProcessed);
438:
439: assertEquals("Path wasn't inherited", path, subConfig.getPath());
440: assertEquals("Module wasn't inherited", module, subConfig
441: .getModule());
442: assertEquals("Name shouldn't have changed", name, subConfig
443: .getName());
444: assertEquals("Extends shouldn't have changed", inherit,
445: subConfig.getExtends());
446: assertEquals("Redirect shouldn't have changed", redirect,
447: subConfig.getRedirect());
448: }
449: }
|