001: /*
002: * $Id: TestActionConfig.java 480593 2006-11-29 15:17:52Z niallp $
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.ActionConfig</code>
031: * class. Currently only contains code to test the methods that support
032: * configuration inheritance.
033: *
034: * @version $Rev: 480593 $ $Date: 2005-05-25 19:35:00 -0400 (Wed, 25 May 2005)
035: * $
036: */
037: public class TestActionConfig 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 ActionConfig baseConfig = 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 TestActionConfig(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: baseConfig = new ActionConfig();
074: baseConfig.setPath("/base");
075: baseConfig.setType("org.apache.struts.actions.DummyAction");
076:
077: // set up success and failure forward
078: ForwardConfig forward = new ForwardConfig("success",
079: "/success.jsp", false);
080:
081: baseConfig.addForwardConfig(forward);
082:
083: forward = new ForwardConfig("failure", "/failure.jsp", false);
084: forward.setProperty("forwardCount", "10");
085: baseConfig.addForwardConfig(forward);
086:
087: // setup an exception handler
088: ExceptionConfig exceptionConfig = new ExceptionConfig();
089:
090: exceptionConfig.setType("java.sql.SQLException");
091: exceptionConfig.setKey("msg.exception.sql");
092: exceptionConfig.setProperty("exceptionCount", "10");
093: baseConfig.addExceptionConfig(exceptionConfig);
094:
095: // set some arbitrary properties
096: baseConfig.setProperty("label", "base");
097: baseConfig.setProperty("version", "1a");
098:
099: // register it to our config
100: config.addActionConfig(baseConfig);
101: }
102:
103: /**
104: * Return the tests included in this test suite.
105: */
106: public static Test suite() {
107: return (new TestSuite(TestActionConfig.class));
108: }
109:
110: /**
111: * Tear down instance variables required by this test case.
112: */
113: public void tearDown() {
114: config = null;
115: baseConfig = null;
116: }
117:
118: // ------------------------------------------------------- Individual Tests
119:
120: /**
121: * Basic check that shouldn't detect circular inheritance.
122: */
123: public void testCheckCircularInheritance() {
124: ActionConfig child = new ActionConfig();
125:
126: child.setPath("/child");
127: child.setExtends("/base");
128:
129: ActionConfig grandChild = new ActionConfig();
130:
131: grandChild.setPath("/grandChild");
132: grandChild.setExtends("/child");
133:
134: config.addActionConfig(child);
135: config.addActionConfig(grandChild);
136:
137: assertTrue("Circular inheritance shouldn't have been detected",
138: !grandChild.checkCircularInheritance(config));
139: }
140:
141: /**
142: * Basic check that should detect circular inheritance.
143: */
144: public void testCheckCircularInheritanceError() {
145: ActionConfig child = new ActionConfig();
146:
147: child.setPath("/child");
148: child.setExtends("/base");
149:
150: ActionConfig grandChild = new ActionConfig();
151:
152: grandChild.setPath("/grandChild");
153: grandChild.setExtends("/child");
154:
155: // establish the circular relationship with base
156: baseConfig.setExtends("/grandChild");
157:
158: config.addActionConfig(child);
159: config.addActionConfig(grandChild);
160:
161: assertTrue("Circular inheritance should've been detected",
162: grandChild.checkCircularInheritance(config));
163: }
164:
165: /**
166: * Test that processExtends() makes sure that a base action's own
167: * extension has been processed.
168: */
169: public void testProcessExtendsActionExtends() throws Exception {
170: CustomActionConfig first = new CustomActionConfig();
171:
172: first.setPath("/first");
173:
174: CustomActionConfig second = new CustomActionConfig();
175:
176: second.setPath("/second");
177: second.setExtends("/first");
178:
179: config.addActionConfig(first);
180: config.addActionConfig(second);
181:
182: // set baseConfig to extend second
183: baseConfig.setExtends("/second");
184:
185: baseConfig.processExtends(config);
186:
187: assertTrue("The first action's processExtends() wasn't called",
188: first.processExtendsCalled);
189: assertTrue(
190: "The second action's processExtends() wasn't called",
191: second.processExtendsCalled);
192: }
193:
194: /**
195: * Make sure that correct exception is thrown if a base action can't be
196: * found.
197: */
198: public void testProcessExtendsMissingAction() throws Exception {
199: baseConfig.setExtends("/someMissingAction");
200:
201: try {
202: baseConfig.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 forwards
213: * and exception handlers should be inherited from a base form. This
214: * method checks all the subelements.
215: */
216: public void testInheritFrom() throws Exception {
217: // create a basic subform
218: ActionConfig subConfig = new ActionConfig();
219: String subConfigPath = "subConfig";
220:
221: subConfig.setPath(subConfigPath);
222: subConfig.setExtends("/base");
223:
224: // override success
225: ForwardConfig forward = new ForwardConfig();
226:
227: forward.setName("success");
228: forward.setPath("/newSuccess.jsp");
229: forward.setRedirect(true);
230: subConfig.addForwardConfig(forward);
231:
232: // add an exception handler
233: ExceptionConfig handler = new ExceptionConfig();
234:
235: handler.setType("java.lang.NullPointerException");
236: handler.setKey("msg.exception.npe");
237: subConfig.addExceptionConfig(handler);
238:
239: // override arbitrary "label" property
240: subConfig.setProperty("label", "sub");
241:
242: config.addActionConfig(subConfig);
243:
244: subConfig.inheritFrom(baseConfig);
245:
246: // check that our subConfig is still the one in the config
247: assertSame("subConfig no longer in ModuleConfig", subConfig,
248: config.findActionConfig("subConfig"));
249:
250: // check our configured sub config
251: assertNotNull("Action type was not inherited", subConfig
252: .getType());
253: assertEquals("Wrong config path", subConfigPath, subConfig
254: .getPath());
255: assertEquals("Wrong config type", baseConfig.getType(),
256: subConfig.getType());
257:
258: // check our forwards
259: ForwardConfig[] forwards = subConfig.findForwardConfigs();
260:
261: assertEquals("Wrong forwards count", 2, forwards.length);
262:
263: forward = subConfig.findForwardConfig("success");
264: assertNotNull("'success' forward was not found", forward);
265: assertEquals("Wrong path for success", "/newSuccess.jsp",
266: forward.getPath());
267:
268: forward = subConfig.findForwardConfig("failure");
269:
270: ForwardConfig origForward = baseConfig
271: .findForwardConfig("failure");
272:
273: assertNotNull("'failure' forward was not inherited", forward);
274: assertEquals("Wrong type for 'failure'", origForward.getPath(),
275: forward.getPath());
276: assertEquals("Arbitrary property not copied", origForward
277: .getProperty("forwardCount"), forward
278: .getProperty("forwardCount"));
279:
280: // check our exceptions
281: ExceptionConfig[] handlers = subConfig.findExceptionConfigs();
282:
283: assertEquals("Wrong exception config count", 2, handlers.length);
284:
285: handler = subConfig
286: .findExceptionConfig("java.sql.SQLException");
287:
288: ExceptionConfig origHandler = baseConfig
289: .findExceptionConfig("java.sql.SQLException");
290:
291: assertNotNull("'SQLException' handler was not found", handler);
292: assertEquals("Wrong key for 'SQLException'", origHandler
293: .getKey(), handler.getKey());
294: assertEquals("Arbitrary property not copied", origHandler
295: .getProperty("exceptionCount"), handler
296: .getProperty("exceptionCount"));
297:
298: handler = subConfig
299: .findExceptionConfig("java.lang.NullPointerException");
300: assertNotNull("'NullPointerException' handler disappeared",
301: handler);
302:
303: // check the arbitrary properties
304: String version = subConfig.getProperty("version");
305:
306: assertEquals("Arbitrary property 'version' wasn't inherited",
307: "1a", version);
308:
309: String label = subConfig.getProperty("label");
310:
311: assertEquals(
312: "Arbitrary property 'label' shouldn't have changed",
313: "sub", label);
314: }
315:
316: /**
317: * Make sure that correct exception is thrown if a base action can't be
318: * found.
319: */
320: public void testInheritBoolean() throws Exception {
321:
322: ActionConfig parentConfig = new ActionConfig();
323: parentConfig.setPath("/parent");
324: ActionConfig childConfig = null;
325:
326: // Test if boolean is NOT set it IS inherited
327: parentConfig.setValidate(true);
328: parentConfig.setCancellable(true);
329: childConfig = new ActionConfig();
330: childConfig.inheritFrom(parentConfig);
331: assertEquals("default validate inherit true", true, childConfig
332: .getValidate());
333: assertEquals("default cancellable inherit true", true,
334: childConfig.getValidate());
335:
336: // Test if boolean is NOT set it IS inherited
337: parentConfig.setValidate(false);
338: parentConfig.setCancellable(false);
339: childConfig = new ActionConfig();
340: childConfig.inheritFrom(parentConfig);
341: assertEquals("default validate inherit false", false,
342: childConfig.getValidate());
343: assertEquals("default cancellable inherit false", false,
344: childConfig.getValidate());
345:
346: // Test if boolean IS set it is NOT inherited
347: parentConfig.setValidate(true);
348: parentConfig.setCancellable(true);
349: childConfig = new ActionConfig();
350: childConfig.setValidate(false);
351: childConfig.setCancellable(false);
352: childConfig.inheritFrom(parentConfig);
353: assertEquals("set validate (not inherit true)", false,
354: childConfig.getValidate());
355: assertEquals("set cancellable (not inherit false)", false,
356: childConfig.getValidate());
357:
358: // Test if boolean IS set it is NOT inherited
359: parentConfig.setValidate(false);
360: parentConfig.setCancellable(false);
361: childConfig = new ActionConfig();
362: childConfig.setValidate(true);
363: childConfig.setCancellable(true);
364: childConfig.inheritFrom(parentConfig);
365: assertEquals("set validate (not inherit false)", true,
366: childConfig.getValidate());
367: assertEquals("set cancellable (not inherit false)", true,
368: childConfig.getValidate());
369:
370: }
371:
372: /**
373: * Used to detect that ActionConfig is making the right calls.
374: */
375: public static class CustomActionConfig extends ActionConfig {
376: boolean processExtendsCalled = false;
377:
378: public void processExtends(ModuleConfig moduleConfig)
379: throws ClassNotFoundException, IllegalAccessException,
380: InstantiationException, InvocationTargetException {
381: super .processExtends(moduleConfig);
382: processExtendsCalled = true;
383: }
384: }
385: }
|