0001: /*
0002: * $Id: TestActionServlet.java 471754 2006-11-06 14:55:09Z husted $
0003: *
0004: * Licensed to the Apache Software Foundation (ASF) under one
0005: * or more contributor license agreements. See the NOTICE file
0006: * distributed with this work for additional information
0007: * regarding copyright ownership. The ASF licenses this file
0008: * to you under the Apache License, Version 2.0 (the
0009: * "License"); you may not use this file except in compliance
0010: * with the License. You may obtain a copy of the License at
0011: *
0012: * http://www.apache.org/licenses/LICENSE-2.0
0013: *
0014: * Unless required by applicable law or agreed to in writing,
0015: * software distributed under the License is distributed on an
0016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0017: * KIND, either express or implied. See the License for the
0018: * specific language governing permissions and limitations
0019: * under the License.
0020: */
0021: package org.apache.struts.action;
0022:
0023: import junit.framework.Test;
0024: import junit.framework.TestCase;
0025: import junit.framework.TestSuite;
0026:
0027: import org.apache.struts.config.ActionConfig;
0028: import org.apache.struts.config.ExceptionConfig;
0029: import org.apache.struts.config.FormBeanConfig;
0030: import org.apache.struts.config.FormPropertyConfig;
0031: import org.apache.struts.config.ForwardConfig;
0032: import org.apache.struts.config.ModuleConfig;
0033: import org.apache.struts.config.ModuleConfigFactory;
0034: import org.apache.struts.util.MessageResources;
0035:
0036: import javax.servlet.ServletException;
0037: import javax.servlet.UnavailableException;
0038:
0039: import java.util.List;
0040:
0041: /**
0042: * Suite of unit tests for the <code>org.apache.struts.action.ActionServlet</code>
0043: * class.
0044: */
0045: public class TestActionServlet extends TestCase {
0046: // ----------------------------------------------------- Instance Variables
0047:
0048: /**
0049: * The ModuleConfig we'll use.
0050: */
0051: protected ModuleConfig moduleConfig = null;
0052:
0053: /**
0054: * The common form bean we'll use.
0055: */
0056: protected FormBeanConfig baseFormBean = null;
0057:
0058: /**
0059: * The common exception config we'll use.
0060: */
0061: protected ExceptionConfig baseException = null;
0062:
0063: /**
0064: * The common action config we'll use.
0065: */
0066: protected ActionMapping baseAction = null;
0067:
0068: /**
0069: * The common action forward we'll use.
0070: */
0071: protected ActionForward baseForward = null;
0072:
0073: /**
0074: * The ActionServlet we'll test.
0075: */
0076: protected ActionServlet actionServlet = null;
0077:
0078: // ------------------------------------------ Constructors, suite, and main
0079:
0080: /**
0081: * Defines the testcase name for JUnit.
0082: *
0083: * @param theName the testcase's name.
0084: */
0085: public TestActionServlet(String theName) {
0086: super (theName);
0087: }
0088:
0089: /**
0090: * Start the tests.
0091: *
0092: * @param theArgs the arguments. Not used
0093: */
0094: public static void main(String[] theArgs) {
0095: junit.awtui.TestRunner
0096: .main(new String[] { TestActionServlet.class.getName() });
0097: }
0098:
0099: /**
0100: * @return a test suite (<code>TestSuite</code>) that includes all methods
0101: * starting with "test"
0102: */
0103: public static Test suite() {
0104: // All methods starting with "test" will be executed in the test suite.
0105: return new TestSuite(TestActionServlet.class);
0106: }
0107:
0108: // ------------------------------------------------- setUp() and tearDown()
0109:
0110: /**
0111: * Set up instance variables required by this test case.
0112: */
0113: public void setUp() throws Exception {
0114: actionServlet = new ActionServlet();
0115: actionServlet.initInternal();
0116:
0117: ModuleConfigFactory factoryObject = ModuleConfigFactory
0118: .createFactory();
0119:
0120: moduleConfig = factoryObject.createModuleConfig("");
0121:
0122: // Setup the base form
0123: baseFormBean = new FormBeanConfig();
0124: baseFormBean.setName("baseForm");
0125: baseFormBean.setType("org.apache.struts.action.DynaActionForm");
0126:
0127: // Set up id, name, and score
0128: FormPropertyConfig property = new FormPropertyConfig();
0129:
0130: property.setName("id");
0131: property.setType("java.lang.String");
0132: baseFormBean.addFormPropertyConfig(property);
0133:
0134: property = new FormPropertyConfig();
0135: property.setName("name");
0136: property.setType("java.lang.String");
0137: baseFormBean.addFormPropertyConfig(property);
0138:
0139: property = new FormPropertyConfig();
0140: property.setName("score");
0141: property.setType("java.lang.String");
0142: baseFormBean.addFormPropertyConfig(property);
0143:
0144: // Setup the exception handler
0145: baseException = new ExceptionConfig();
0146: baseException.setType("java.lang.NullPointerException");
0147: baseException.setKey("msg.exception.npe");
0148:
0149: // Setup the forward config
0150: baseForward = new ActionForward("success", "/succes.jsp", false);
0151:
0152: // Setup the action config
0153: baseAction = new ActionMapping();
0154: baseAction.setPath("/index");
0155: baseAction.setType("org.apache.struts.actions.DummyAction");
0156: baseAction.setName("someForm");
0157: baseAction.setInput("/input.jsp");
0158: baseAction.addForwardConfig(new ActionForward("next",
0159: "/next.jsp", false));
0160: baseAction.addForwardConfig(new ActionForward("prev",
0161: "/prev.jsp", false));
0162:
0163: ExceptionConfig exceptionConfig = new ExceptionConfig();
0164:
0165: exceptionConfig.setType("java.sql.SQLException");
0166: exceptionConfig.setKey("msg.exception.sql");
0167: baseAction.addExceptionConfig(exceptionConfig);
0168:
0169: // Nothing is registered to our module config until they are needed
0170: }
0171:
0172: /**
0173: * Tear down instance variables required by this test case.
0174: */
0175: public void tearDown() {
0176: moduleConfig = null;
0177: }
0178:
0179: // ----------------------------- initInternal() and destroyInternal() tests
0180:
0181: /**
0182: * Verify that we can initialize and destroy our internal message
0183: * resources object.
0184: */
0185: public void testInitDestroyInternal() {
0186: ActionServlet servlet = new ActionServlet();
0187:
0188: try {
0189: servlet.initInternal();
0190: } catch (ServletException e) {
0191: fail("initInternal() threw exception: " + e);
0192: }
0193:
0194: assertTrue("internal was initialized",
0195: servlet.getInternal() != null);
0196: assertTrue("internal of correct type",
0197: servlet.getInternal() instanceof MessageResources);
0198: servlet.destroyInternal();
0199: assertTrue("internal was destroyed",
0200: servlet.getInternal() == null);
0201: }
0202:
0203: /**
0204: * Test class loader resolution and splitting.
0205: */
0206: public void notestSplitAndResolvePaths() throws Exception {
0207: ActionServlet servlet = new ActionServlet();
0208: List list = servlet
0209: .splitAndResolvePaths("org/apache/struts/config/struts-config.xml");
0210:
0211: assertNotNull(list);
0212: assertTrue("List size should be 1", list.size() == 1);
0213:
0214: list = servlet
0215: .splitAndResolvePaths("org/apache/struts/config/struts-config.xml, "
0216: + "org/apache/struts/config/struts-config-1.1.xml");
0217: assertNotNull(list);
0218: assertTrue("List size should be 2, was " + list.size(), list
0219: .size() == 2);
0220:
0221: list = servlet.splitAndResolvePaths("META-INF/MANIFEST.MF");
0222: assertNotNull(list);
0223: assertTrue("Number of manifests should be more than 5, was "
0224: + list.size(), list.size() > 5);
0225:
0226: // test invalid path
0227: try {
0228: list = servlet
0229: .splitAndResolvePaths("org/apache/struts/config/struts-asdfasdfconfig.xml");
0230: fail("Should have thrown an exception on bad path");
0231: } catch (NullPointerException ex) {
0232: // correct behavior since internal error resources aren't loaded
0233: }
0234: }
0235:
0236: //----- Test initApplication() method --------------------------------------
0237:
0238: /**
0239: * Verify that nothing happens if no "application" property is defined in
0240: * the servlet configuration.
0241: */
0242:
0243: /*
0244: public void testInitApplicationNull() throws ServletException
0245: {
0246: ActionServlet servlet = new ActionServlet();
0247: servlet.init(config);
0248:
0249: // Test the initApplication() method
0250: servlet.initApplication();
0251:
0252: // As no "application" object is found in the servlet config, no
0253: // attribute should be set in the context
0254: assertTrue(config.getServletContext().getAttribute(Action.MESSAGES_KEY) == null);
0255: }
0256: */
0257:
0258: /**
0259: * Verify that eveything is fine when only a "application" parameter is
0260: * defined in the servlet configuration.
0261: */
0262:
0263: /*
0264: public void testInitApplicationOk1() throws ServletException
0265: {
0266: // initialize config
0267: config.setInitParameter("application", "org.apache.struts.webapp.example.ApplicationResources");
0268:
0269: ActionServlet servlet = new ActionServlet();
0270: servlet.init(config);
0271:
0272: // Test the initApplication() method
0273: servlet.initApplication();
0274:
0275: assertTrue(servlet.application != null);
0276: assertTrue(servlet.application.getReturnNull() == true);
0277:
0278: assertTrue(config.getServletContext().getAttribute(Action.MESSAGES_KEY) != null);
0279: assertEquals(servlet.application, config.getServletContext().getAttribute(Action.MESSAGES_KEY));
0280:
0281: }
0282: */
0283:
0284: // --------------------------------------------------- FormBeanConfig Tests
0285: /**
0286: * Test that nothing fails if there are no extensions.
0287: */
0288: public void testInitModuleFormBeansNoExtends()
0289: throws ServletException {
0290: moduleConfig.addFormBeanConfig(baseFormBean);
0291:
0292: try {
0293: actionServlet.initModuleExceptionConfigs(moduleConfig);
0294: } catch (Exception e) {
0295: fail("Unexpected exception caught.");
0296: }
0297: }
0298:
0299: /**
0300: * Test that initModuleFormBeans throws an exception when a form with a
0301: * null type is present.
0302: */
0303: public void testInitModuleFormBeansNullFormType()
0304: throws ServletException {
0305: FormBeanConfig formBean = new FormBeanConfig();
0306:
0307: formBean.setName("noTypeForm");
0308: moduleConfig.addFormBeanConfig(formBean);
0309:
0310: try {
0311: actionServlet.initModuleFormBeans(moduleConfig);
0312: fail("An exception should've been thrown here.");
0313: } catch (UnavailableException e) {
0314: // success
0315: } catch (Exception e) {
0316: fail("Unrecognized exception thrown: " + e);
0317: }
0318: }
0319:
0320: /**
0321: * Test that initModuleFormBeans throws an exception when a form whose
0322: * prop type is null is present.
0323: */
0324: public void testInitModuleFormBeansNullPropType()
0325: throws ServletException {
0326: moduleConfig.addFormBeanConfig(baseFormBean);
0327: baseFormBean.findFormPropertyConfig("name").setType(null);
0328:
0329: try {
0330: actionServlet.initModuleFormBeans(moduleConfig);
0331: fail("An exception should've been thrown here.");
0332: } catch (UnavailableException e) {
0333: // success
0334: } catch (Exception e) {
0335: fail("Unrecognized exception thrown: " + e);
0336: }
0337: }
0338:
0339: /**
0340: * Test that processFormBeanExtension() calls processExtends()
0341: */
0342: public void testProcessFormBeanExtension() throws ServletException {
0343: CustomFormBeanConfig form = new CustomFormBeanConfig();
0344:
0345: actionServlet.processFormBeanExtension(form, moduleConfig);
0346:
0347: assertTrue("processExtends() was not called",
0348: form.processExtendsCalled);
0349: }
0350:
0351: /**
0352: * Make sure processFormBeanConfigClass() returns an instance of the
0353: * correct class if the base config is using a custom class.
0354: */
0355: public void testProcessFormBeanConfigClass() throws Exception {
0356: CustomFormBeanConfig customBase = new CustomFormBeanConfig();
0357:
0358: customBase.setName("customBase");
0359: moduleConfig.addFormBeanConfig(customBase);
0360:
0361: FormBeanConfig customSub = new FormBeanConfig();
0362:
0363: customSub.setName("customSub");
0364: customSub.setExtends("customBase");
0365: customSub.setType("org.apache.struts.action.DynaActionForm");
0366: moduleConfig.addFormBeanConfig(customSub);
0367:
0368: FormBeanConfig result = actionServlet
0369: .processFormBeanConfigClass(customSub, moduleConfig);
0370:
0371: assertTrue("Incorrect class of form bean config",
0372: result instanceof CustomFormBeanConfig);
0373: assertEquals("Incorrect name", customSub.getName(), result
0374: .getName());
0375: assertEquals("Incorrect type", customSub.getType(), result
0376: .getType());
0377: assertEquals("Incorrect extends", customSub.getExtends(),
0378: result.getExtends());
0379: assertEquals("Incorrect 'restricted' value", customSub
0380: .isRestricted(), result.isRestricted());
0381:
0382: assertSame("Result was not registered in the module config",
0383: result, moduleConfig.findFormBeanConfig("customSub"));
0384: }
0385:
0386: /**
0387: * Make sure processFormBeanConfigClass() returns what it was given if the
0388: * form passed to it doesn't extend anything.
0389: */
0390: public void testProcessFormBeanConfigClassNoExtends()
0391: throws Exception {
0392: moduleConfig.addFormBeanConfig(baseFormBean);
0393:
0394: FormBeanConfig result = null;
0395:
0396: try {
0397: result = actionServlet.processFormBeanConfigClass(
0398: baseFormBean, moduleConfig);
0399: } catch (UnavailableException e) {
0400: fail("An exception should not be thrown when there's nothing to do");
0401: }
0402:
0403: assertSame("Result should be the same as the input.",
0404: baseFormBean, result);
0405: }
0406:
0407: /**
0408: * Make sure processFormBeanConfigClass() returns the same class instance
0409: * if the base config isn't using a custom class.
0410: */
0411: public void testProcessFormBeanConfigClassSubFormCustomClass()
0412: throws Exception {
0413: moduleConfig.addFormBeanConfig(baseFormBean);
0414:
0415: FormBeanConfig customSub = new FormBeanConfig();
0416:
0417: customSub.setName("customSub");
0418: customSub.setExtends("baseForm");
0419: moduleConfig.addFormBeanConfig(customSub);
0420:
0421: FormBeanConfig result = actionServlet
0422: .processFormBeanConfigClass(customSub, moduleConfig);
0423:
0424: assertSame(
0425: "The instance returned should be the param given it.",
0426: customSub, result);
0427: }
0428:
0429: /**
0430: * Make sure the code throws the correct exception when it can't create an
0431: * instance of the base config's custom class.
0432: */
0433: public void notestProcessFormBeanConfigClassError()
0434: throws Exception {
0435: CustomFormBeanConfigArg customBase = new CustomFormBeanConfigArg(
0436: "customBase");
0437:
0438: moduleConfig.addFormBeanConfig(customBase);
0439:
0440: FormBeanConfig customSub = new FormBeanConfig();
0441:
0442: customSub.setName("customSub");
0443: customSub.setExtends("customBase");
0444: moduleConfig.addFormBeanConfig(customSub);
0445:
0446: try {
0447: actionServlet.processFormBeanConfigClass(customSub,
0448: moduleConfig);
0449: fail("Exception should be thrown");
0450: } catch (UnavailableException e) {
0451: // success
0452: } catch (Exception e) {
0453: fail("Unexpected exception thrown.");
0454: }
0455: }
0456:
0457: /**
0458: * Test the case where the subform has already specified its own form bean
0459: * config class. If the code still attempts to create a new instance, an
0460: * error will be thrown.
0461: */
0462: public void testProcessFormBeanConfigClassOverriddenSubFormClass()
0463: throws Exception {
0464: CustomFormBeanConfigArg customBase = new CustomFormBeanConfigArg(
0465: "customBase");
0466:
0467: moduleConfig.addFormBeanConfig(customBase);
0468:
0469: FormBeanConfig customSub = new CustomFormBeanConfigArg(
0470: "customSub");
0471:
0472: customSub.setExtends("customBase");
0473: moduleConfig.addFormBeanConfig(customSub);
0474:
0475: try {
0476: actionServlet.processFormBeanConfigClass(customSub,
0477: moduleConfig);
0478: } catch (Exception e) {
0479: fail("Exception should not be thrown");
0480: }
0481: }
0482:
0483: // -------------------------------------------------- ExceptionConfig Tests
0484:
0485: /**
0486: * Test that nothing fails if there are no extensions.
0487: */
0488: public void testInitModuleExceptionConfigsNoExtends()
0489: throws ServletException {
0490: moduleConfig.addExceptionConfig(baseException);
0491:
0492: try {
0493: actionServlet.initModuleExceptionConfigs(moduleConfig);
0494: } catch (Exception e) {
0495: fail("Unexpected exception caught.");
0496: }
0497: }
0498:
0499: /**
0500: * Test that initModuleExceptionConfigs throws an exception when a handler
0501: * with a null key is present.
0502: */
0503: public void testInitModuleExceptionConfigsNullFormType()
0504: throws ServletException {
0505: ExceptionConfig handler = new ExceptionConfig();
0506:
0507: handler.setType("java.lang.NullPointerException");
0508: moduleConfig.addExceptionConfig(handler);
0509:
0510: try {
0511: actionServlet.initModuleExceptionConfigs(moduleConfig);
0512: fail("An exception should've been thrown here.");
0513: } catch (UnavailableException e) {
0514: // success
0515: } catch (Exception e) {
0516: fail("Unrecognized exception thrown: " + e);
0517: }
0518: }
0519:
0520: /**
0521: * Test that processExceptionExtension() calls processExtends()
0522: */
0523: public void testProcessExceptionExtension() throws ServletException {
0524: CustomExceptionConfig handler = new CustomExceptionConfig();
0525:
0526: handler.setType("java.lang.NullPointerException");
0527: moduleConfig.addExceptionConfig(handler);
0528: actionServlet.processExceptionExtension(handler, moduleConfig,
0529: null);
0530:
0531: assertTrue("processExtends() was not called",
0532: handler.processExtendsCalled);
0533: }
0534:
0535: /**
0536: * Make sure processExceptionConfigClass() returns an instance of the
0537: * correct class if the base config is using a custom class.
0538: */
0539: public void testProcessExceptionConfigClass() throws Exception {
0540: CustomExceptionConfig customBase = new CustomExceptionConfig();
0541:
0542: customBase.setType("java.lang.NullPointerException");
0543: customBase.setKey("msg.exception.npe");
0544: moduleConfig.addExceptionConfig(customBase);
0545:
0546: ExceptionConfig customSub = new ExceptionConfig();
0547:
0548: customSub.setType("java.lang.IllegalStateException");
0549: customSub.setExtends("java.lang.NullPointerException");
0550: moduleConfig.addExceptionConfig(customSub);
0551:
0552: ExceptionConfig result = actionServlet
0553: .processExceptionConfigClass(customSub, moduleConfig,
0554: null);
0555:
0556: assertTrue("Incorrect class of exception config",
0557: result instanceof CustomExceptionConfig);
0558: assertEquals("Incorrect type", customSub.getType(), result
0559: .getType());
0560: assertEquals("Incorrect key", customSub.getKey(), result
0561: .getKey());
0562: assertEquals("Incorrect extends", customSub.getExtends(),
0563: result.getExtends());
0564:
0565: assertSame(
0566: "Result was not registered in the module config",
0567: result,
0568: moduleConfig
0569: .findExceptionConfig("java.lang.IllegalStateException"));
0570: }
0571:
0572: /**
0573: * Make sure processExceptionConfigClass() returns what it was given if
0574: * the handler passed to it doesn't extend anything.
0575: */
0576: public void testProcessExceptionConfigClassNoExtends()
0577: throws Exception {
0578: moduleConfig.addExceptionConfig(baseException);
0579:
0580: ExceptionConfig result = null;
0581:
0582: try {
0583: result = actionServlet.processExceptionConfigClass(
0584: baseException, moduleConfig, null);
0585: } catch (UnavailableException e) {
0586: fail("An exception should not be thrown when there's nothing to do");
0587: }
0588:
0589: assertSame("Result should be the same as the input.",
0590: baseException, result);
0591: }
0592:
0593: /**
0594: * Make sure processExceptionConfigClass() returns the same class instance
0595: * if the base config isn't using a custom class.
0596: */
0597: public void testProcessExceptionConfigClassSubConfigCustomClass()
0598: throws Exception {
0599: moduleConfig.addExceptionConfig(baseException);
0600:
0601: ExceptionConfig customSub = new ExceptionConfig();
0602:
0603: customSub.setType("java.lang.IllegalStateException");
0604: customSub.setExtends("java.lang.NullPointerException");
0605: moduleConfig.addExceptionConfig(customSub);
0606:
0607: ExceptionConfig result = actionServlet
0608: .processExceptionConfigClass(customSub, moduleConfig,
0609: null);
0610:
0611: assertSame(
0612: "The instance returned should be the param given it.",
0613: customSub, result);
0614: }
0615:
0616: /**
0617: * Make sure the code throws the correct exception when it can't create an
0618: * instance of the base config's custom class.
0619: */
0620: public void notestProcessExceptionConfigClassError()
0621: throws Exception {
0622: ExceptionConfig customBase = new CustomExceptionConfigArg(
0623: "java.lang.NullPointerException");
0624:
0625: moduleConfig.addExceptionConfig(customBase);
0626:
0627: ExceptionConfig customSub = new ExceptionConfig();
0628:
0629: customSub.setType("java.lang.IllegalStateException");
0630: customSub.setExtends("java.lang.NullPointerException");
0631: moduleConfig.addExceptionConfig(customSub);
0632:
0633: try {
0634: actionServlet.processExceptionConfigClass(customSub,
0635: moduleConfig, null);
0636: fail("Exception should be thrown");
0637: } catch (UnavailableException e) {
0638: // success
0639: } catch (Exception e) {
0640: fail("Unexpected exception thrown.");
0641: }
0642: }
0643:
0644: /**
0645: * Test the case where the subconfig has already specified its own config
0646: * class. If the code still attempts to create a new instance, an error
0647: * will be thrown.
0648: */
0649: public void testProcessExceptionConfigClassOverriddenSubFormClass()
0650: throws Exception {
0651: moduleConfig.addExceptionConfig(baseException);
0652:
0653: ExceptionConfig customSub = new CustomExceptionConfigArg(
0654: "java.lang.IllegalStateException");
0655:
0656: customSub.setExtends("java.lang.NullPointerException");
0657: moduleConfig.addExceptionConfig(customSub);
0658:
0659: try {
0660: actionServlet.processExceptionConfigClass(customSub,
0661: moduleConfig, null);
0662: } catch (Exception e) {
0663: fail("Exception should not be thrown");
0664: }
0665: }
0666:
0667: // ---------------------------------------------------- ForwardConfig Tests
0668:
0669: /**
0670: * Test that nothing fails if there are no extensions.
0671: */
0672: public void testInitModuleForwardConfigsNoExtends()
0673: throws ServletException {
0674: moduleConfig.addForwardConfig(baseForward);
0675:
0676: try {
0677: actionServlet.initModuleForwards(moduleConfig);
0678: } catch (Exception e) {
0679: fail("Unexpected exception caught.");
0680: }
0681: }
0682:
0683: /**
0684: * Test that initModuleForwards throws an exception when a forward with a
0685: * null path is present.
0686: */
0687: public void testInitModuleForwardsNullFormType()
0688: throws ServletException {
0689: ActionForward forward = new ActionForward("success", null,
0690: false);
0691:
0692: moduleConfig.addForwardConfig(forward);
0693:
0694: try {
0695: actionServlet.initModuleForwards(moduleConfig);
0696: fail("An exception should've been thrown here.");
0697: } catch (UnavailableException e) {
0698: // success
0699: } catch (Exception e) {
0700: fail("Unrecognized exception thrown: " + e);
0701: }
0702: }
0703:
0704: /**
0705: * Test that processForwardExtension() calls processExtends()
0706: */
0707: public void testProcessForwardExtension() throws ServletException {
0708: CustomForwardConfig forward = new CustomForwardConfig(
0709: "forward", "/forward.jsp");
0710:
0711: moduleConfig.addForwardConfig(forward);
0712: actionServlet.processForwardExtension(forward, moduleConfig,
0713: null);
0714:
0715: assertTrue("processExtends() was not called",
0716: forward.processExtendsCalled);
0717: }
0718:
0719: /**
0720: * Make sure processForwardConfigClass() returns an instance of the
0721: * correct class if the base config is using a custom class.
0722: */
0723: public void testProcessForwardConfigClass() throws Exception {
0724: CustomForwardConfig customBase = new CustomForwardConfig(
0725: "success", "/success.jsp");
0726:
0727: moduleConfig.addForwardConfig(customBase);
0728:
0729: ActionForward customSub = new ActionForward();
0730:
0731: customSub.setName("failure");
0732: customSub.setExtends("success");
0733: moduleConfig.addForwardConfig(customSub);
0734:
0735: ForwardConfig result = actionServlet.processForwardConfigClass(
0736: customSub, moduleConfig, null);
0737:
0738: assertTrue("Incorrect class of forward config",
0739: result instanceof CustomForwardConfig);
0740: assertEquals("Incorrect name", customSub.getName(), result
0741: .getName());
0742: assertEquals("Incorrect path", customSub.getPath(), result
0743: .getPath());
0744: assertEquals("Incorrect extends", customSub.getExtends(),
0745: result.getExtends());
0746:
0747: assertSame("Result was not registered in the module config",
0748: result, moduleConfig.findForwardConfig("failure"));
0749: }
0750:
0751: /**
0752: * Make sure processForwardConfigClass() returns what it was given if the
0753: * forward passed to it doesn't extend anything.
0754: */
0755: public void testProcessForwardConfigClassNoExtends()
0756: throws Exception {
0757: moduleConfig.addForwardConfig(baseForward);
0758:
0759: ForwardConfig result = null;
0760:
0761: try {
0762: result = actionServlet.processForwardConfigClass(
0763: baseForward, moduleConfig, null);
0764: } catch (UnavailableException e) {
0765: fail("An exception should not be thrown when there's nothing to do");
0766: }
0767:
0768: assertSame("Result should be the same as the input.",
0769: baseForward, result);
0770: }
0771:
0772: /**
0773: * Make sure processForwardConfigClass() returns the same class instance
0774: * if the base config isn't using a custom class.
0775: */
0776: public void testProcessForwardConfigClassSubConfigCustomClass()
0777: throws Exception {
0778: moduleConfig.addForwardConfig(baseForward);
0779:
0780: ForwardConfig customSub = new ActionForward();
0781:
0782: customSub.setName("failure");
0783: customSub.setExtends("success");
0784: moduleConfig.addForwardConfig(customSub);
0785:
0786: ForwardConfig result = actionServlet.processForwardConfigClass(
0787: customSub, moduleConfig, null);
0788:
0789: assertSame(
0790: "The instance returned should be the param given it.",
0791: customSub, result);
0792: }
0793:
0794: /**
0795: * Make sure the code throws the correct exception when it can't create an
0796: * instance of the base config's custom class.
0797: */
0798: public void notestProcessForwardConfigClassError() throws Exception {
0799: ForwardConfig customBase = new CustomForwardConfigArg(
0800: "success", "/success.jsp");
0801:
0802: moduleConfig.addForwardConfig(customBase);
0803:
0804: ForwardConfig customSub = new ActionForward();
0805:
0806: customSub.setName("failure");
0807: customSub.setExtends("success");
0808: moduleConfig.addForwardConfig(customSub);
0809:
0810: try {
0811: actionServlet.processForwardConfigClass(customSub,
0812: moduleConfig, null);
0813: fail("Exception should be thrown");
0814: } catch (UnavailableException e) {
0815: // success
0816: } catch (Exception e) {
0817: fail("Unexpected exception thrown.");
0818: }
0819: }
0820:
0821: /**
0822: * Test the case where the subconfig has already specified its own config
0823: * class. If the code still attempts to create a new instance, an error
0824: * will be thrown.
0825: */
0826: public void testProcessForwardConfigClassOverriddenSubConfigClass()
0827: throws Exception {
0828: moduleConfig.addForwardConfig(baseForward);
0829:
0830: ForwardConfig customSub = new CustomForwardConfigArg("failure",
0831: "/failure.jsp");
0832:
0833: customSub.setExtends("success");
0834: moduleConfig.addForwardConfig(customSub);
0835:
0836: try {
0837: actionServlet.processForwardConfigClass(customSub,
0838: moduleConfig, null);
0839: } catch (Exception e) {
0840: fail("Exception should not be thrown");
0841: }
0842: }
0843:
0844: // --------------------------------------------------- ActionConfig Tests
0845:
0846: /**
0847: * Test that nothing fails if there are no extensions.
0848: */
0849: public void testInitModuleActionConfigsNoExtends()
0850: throws ServletException {
0851: moduleConfig.addActionConfig(baseAction);
0852:
0853: try {
0854: actionServlet.initModuleActions(moduleConfig);
0855: } catch (Exception e) {
0856: fail("Unexpected exception caught.");
0857: }
0858: }
0859:
0860: /**
0861: * Test that processActionConfigExtension() calls processExtends()
0862: */
0863: public void testProcessActionExtension() throws ServletException {
0864: CustomActionConfig action = new CustomActionConfig("/action");
0865:
0866: moduleConfig.addActionConfig(action);
0867: actionServlet
0868: .processActionConfigExtension(action, moduleConfig);
0869:
0870: assertTrue("processExtends() was not called",
0871: action.processExtendsCalled);
0872: }
0873:
0874: /**
0875: * Test that an ActionConfig's ForwardConfig can inherit from a
0876: * global ForwardConfig.
0877: */
0878: public void testProcessActionExtensionWithForwardConfig()
0879: throws ServletException {
0880: ForwardConfig forwardConfig = new ForwardConfig();
0881: forwardConfig.setName("sub");
0882: forwardConfig.setExtends("success");
0883: baseAction.addForwardConfig(forwardConfig);
0884:
0885: moduleConfig.addActionConfig(baseAction);
0886: moduleConfig.addForwardConfig(baseForward);
0887: actionServlet.processActionConfigExtension(baseAction,
0888: moduleConfig);
0889:
0890: forwardConfig = baseAction.findForwardConfig("sub");
0891:
0892: assertEquals("'sub' forward's inheritance was not processed.",
0893: baseForward.getPath(), forwardConfig.getPath());
0894: }
0895:
0896: /**
0897: * Test that an ActionConfig's ExceptionConfig can inherit from a
0898: * global ExceptionConfig.
0899: */
0900: public void testProcessActionExtensionWithExceptionConfig()
0901: throws ServletException {
0902: ExceptionConfig exceptionConfig = new ExceptionConfig();
0903: exceptionConfig.setType("SomeException");
0904: exceptionConfig.setExtends("java.lang.NullPointerException");
0905: baseAction.addExceptionConfig(exceptionConfig);
0906:
0907: moduleConfig.addActionConfig(baseAction);
0908: moduleConfig.addExceptionConfig(baseException);
0909: actionServlet.processActionConfigExtension(baseAction,
0910: moduleConfig);
0911:
0912: exceptionConfig = baseAction
0913: .findExceptionConfig("SomeException");
0914:
0915: assertEquals("SomeException's inheritance was not processed.",
0916: baseException.getKey(), exceptionConfig.getKey());
0917: }
0918:
0919: /**
0920: * Make sure processActionConfigClass() returns an instance of the correct
0921: * class if the base config is using a custom class.
0922: */
0923: public void testProcessActionConfigClass() throws Exception {
0924: CustomActionConfig customBase = new CustomActionConfig("/base");
0925:
0926: moduleConfig.addActionConfig(customBase);
0927:
0928: ActionMapping customSub = new ActionMapping();
0929:
0930: customSub.setPath("/sub");
0931: customSub.setExtends("/base");
0932: moduleConfig.addActionConfig(customSub);
0933:
0934: ActionConfig result = actionServlet.processActionConfigClass(
0935: customSub, moduleConfig);
0936:
0937: assertTrue("Incorrect class of action config",
0938: result instanceof CustomActionConfig);
0939: assertEquals("Incorrect path", customSub.getPath(), result
0940: .getPath());
0941: assertEquals("Incorrect extends", customSub.getExtends(),
0942: result.getExtends());
0943:
0944: assertSame("Result was not registered in the module config",
0945: result, moduleConfig.findActionConfig("/sub"));
0946: }
0947:
0948: /**
0949: * Make sure processActionConfigClass() returns what it was given if the
0950: * action passed to it doesn't extend anything.
0951: */
0952: public void testProcessActionConfigClassNoExtends()
0953: throws Exception {
0954: moduleConfig.addActionConfig(baseAction);
0955:
0956: ActionConfig result = null;
0957:
0958: try {
0959: result = actionServlet.processActionConfigClass(baseAction,
0960: moduleConfig);
0961: } catch (UnavailableException e) {
0962: fail("An exception should not be thrown here");
0963: }
0964:
0965: assertSame("Result should be the same as the input.",
0966: baseAction, result);
0967: }
0968:
0969: /**
0970: * Make sure processActionConfigClass() returns the same class instance if
0971: * the base config isn't using a custom class.
0972: */
0973: public void testProcessActionConfigClassSubConfigCustomClass()
0974: throws Exception {
0975: moduleConfig.addActionConfig(baseAction);
0976:
0977: ActionConfig customSub = new ActionMapping();
0978:
0979: customSub.setPath("/sub");
0980: customSub.setExtends("/index");
0981: moduleConfig.addActionConfig(customSub);
0982:
0983: ActionConfig result = actionServlet.processActionConfigClass(
0984: customSub, moduleConfig);
0985:
0986: assertSame(
0987: "The instance returned should be the param given it.",
0988: customSub, result);
0989: }
0990:
0991: /**
0992: * Make sure the code throws the correct exception when it can't create an
0993: * instance of the base config's custom class.
0994: */
0995: public void notestProcessActionConfigClassError() throws Exception {
0996: ActionConfig customBase = new CustomActionConfigArg("/index");
0997:
0998: moduleConfig.addActionConfig(customBase);
0999:
1000: ActionConfig customSub = new ActionMapping();
1001:
1002: customSub.setPath("/sub");
1003: customSub.setExtends("/index");
1004: moduleConfig.addActionConfig(customSub);
1005:
1006: try {
1007: actionServlet.processActionConfigClass(customSub,
1008: moduleConfig);
1009: fail("Exception should be thrown");
1010: } catch (UnavailableException e) {
1011: // success
1012: } catch (Exception e) {
1013: fail("Unexpected exception thrown.");
1014: }
1015: }
1016:
1017: /**
1018: * Test the case where the subconfig has already specified its own config
1019: * class. If the code still attempts to create a new instance, an error
1020: * will be thrown.
1021: */
1022: public void testProcessActionConfigClassOverriddenSubConfigClass()
1023: throws Exception {
1024: moduleConfig.addActionConfig(baseAction);
1025:
1026: ActionConfig customSub = new CustomActionConfigArg("/sub");
1027:
1028: customSub.setExtends("/index");
1029: moduleConfig.addActionConfig(customSub);
1030:
1031: try {
1032: actionServlet.processActionConfigClass(customSub,
1033: moduleConfig);
1034: } catch (Exception e) {
1035: fail("Exception should not be thrown");
1036: }
1037: }
1038:
1039: /**
1040: * Used for testing custom FormBeanConfig classes.
1041: */
1042: public static class CustomFormBeanConfig extends FormBeanConfig {
1043: public boolean processExtendsCalled = false;
1044:
1045: public CustomFormBeanConfig() {
1046: super ();
1047: }
1048:
1049: /**
1050: * Set a flag so we know this method was called.
1051: */
1052: public void processExtends(ModuleConfig moduleConfig)
1053: throws ClassNotFoundException, IllegalAccessException,
1054: InstantiationException {
1055: processExtendsCalled = true;
1056: }
1057: }
1058:
1059: /**
1060: * Used to test cases where the subclass cannot be created with a no-arg
1061: * constructor.
1062: */
1063: private class CustomFormBeanConfigArg extends FormBeanConfig {
1064: CustomFormBeanConfigArg(String name) {
1065: super ();
1066: setName(name);
1067: }
1068: }
1069:
1070: /**
1071: * Used for testing custom ExceptionConfig classes.
1072: */
1073: public static class CustomExceptionConfig extends ExceptionConfig {
1074: public boolean processExtendsCalled = false;
1075:
1076: public CustomExceptionConfig() {
1077: super ();
1078: }
1079:
1080: /**
1081: * Set a flag so we know this method was called.
1082: */
1083: public void processExtends(ModuleConfig moduleConfig,
1084: ActionConfig actionConfig)
1085: throws ClassNotFoundException, IllegalAccessException,
1086: InstantiationException {
1087: processExtendsCalled = true;
1088: }
1089: }
1090:
1091: /**
1092: * Used to test cases where the subclass cannot be created with a no-arg
1093: * constructor.
1094: */
1095: private class CustomExceptionConfigArg extends ExceptionConfig {
1096: CustomExceptionConfigArg(String type) {
1097: super ();
1098: setType(type);
1099: }
1100: }
1101:
1102: /**
1103: * Used for testing custom ForwardConfig classes.
1104: */
1105: public static class CustomForwardConfig extends ForwardConfig {
1106: public boolean processExtendsCalled = false;
1107:
1108: public CustomForwardConfig() {
1109: super ();
1110: }
1111:
1112: public CustomForwardConfig(String name, String path) {
1113: super (name, path, false);
1114: }
1115:
1116: /**
1117: * Set a flag so we know this method was called.
1118: */
1119: public void processExtends(ModuleConfig moduleConfig,
1120: ActionConfig actionConfig)
1121: throws ClassNotFoundException, IllegalAccessException,
1122: InstantiationException {
1123: processExtendsCalled = true;
1124: }
1125: }
1126:
1127: /**
1128: * Used to test cases where the subclass cannot be created with a no-arg
1129: * constructor.
1130: */
1131: private class CustomForwardConfigArg extends ForwardConfig {
1132: CustomForwardConfigArg(String name, String path) {
1133: super ();
1134: setName(name);
1135: setPath(path);
1136: }
1137: }
1138:
1139: /**
1140: * Used for testing custom ActionConfig classes.
1141: */
1142: public static class CustomActionConfig extends ActionConfig {
1143: public boolean processExtendsCalled = false;
1144:
1145: public CustomActionConfig() {
1146: super ();
1147: }
1148:
1149: public CustomActionConfig(String path) {
1150: super ();
1151: setPath(path);
1152: }
1153:
1154: /**
1155: * Set a flag so we know this method was called.
1156: */
1157: public void processExtends(ModuleConfig moduleConfig)
1158: throws ClassNotFoundException, IllegalAccessException,
1159: InstantiationException {
1160: processExtendsCalled = true;
1161: }
1162: }
1163:
1164: /**
1165: * Used to test cases where the subclass cannot be created with a no-arg
1166: * constructor.
1167: */
1168: private class CustomActionConfigArg extends ActionConfig {
1169: CustomActionConfigArg(String path) {
1170: super ();
1171: setPath(path);
1172: }
1173: }
1174:
1175: // [...]
1176: }
|