0001: package com.mockrunner.test.web;
0002:
0003: import java.io.FileNotFoundException;
0004: import java.io.IOException;
0005: import java.sql.SQLException;
0006: import java.util.HashMap;
0007: import java.util.Locale;
0008: import java.util.Map;
0009:
0010: import javax.servlet.ServletException;
0011: import javax.servlet.http.HttpServletRequest;
0012: import javax.servlet.http.HttpServletRequestWrapper;
0013: import javax.servlet.http.HttpServletResponse;
0014: import javax.servlet.http.HttpServletResponseWrapper;
0015:
0016: import org.apache.struts.Globals;
0017: import org.apache.struts.action.Action;
0018: import org.apache.struts.action.ActionErrors;
0019: import org.apache.struts.action.ActionForm;
0020: import org.apache.struts.action.ActionForward;
0021: import org.apache.struts.action.ActionMapping;
0022: import org.apache.struts.action.ActionMessage;
0023: import org.apache.struts.action.ActionMessages;
0024: import org.apache.struts.action.ActionServlet;
0025: import org.apache.struts.action.DynaActionForm;
0026: import org.apache.struts.action.ExceptionHandler;
0027: import org.apache.struts.config.ExceptionConfig;
0028: import org.apache.struts.config.FormBeanConfig;
0029: import org.apache.struts.config.FormPropertyConfig;
0030: import org.apache.struts.util.MessageResources;
0031: import org.apache.struts.validator.ValidatorForm;
0032:
0033: import com.mockrunner.base.BaseTestCase;
0034: import com.mockrunner.base.NestedApplicationException;
0035: import com.mockrunner.base.VerifyFailedException;
0036: import com.mockrunner.mock.web.MockActionForward;
0037: import com.mockrunner.struts.ActionTestModule;
0038: import com.mockrunner.struts.DefaultExceptionHandlerConfig;
0039: import com.mockrunner.struts.ExceptionHandlerConfig;
0040: import com.mockrunner.struts.MapMessageResources;
0041:
0042: public class ActionTestModuleTest extends BaseTestCase {
0043: private ActionTestModule module;
0044:
0045: protected void setUp() throws Exception {
0046: super .setUp();
0047: module = new ActionTestModule(getActionMockObjectFactory());
0048: }
0049:
0050: private ActionErrors createTestActionErrors() {
0051: ActionErrors errors = new ActionErrors();
0052: ActionMessage error1 = new ActionMessage("key1");
0053: ActionMessage error2 = new ActionMessage("key2", new String[] {
0054: "value1", "value2" });
0055: ActionMessage error3 = new ActionMessage("key3", "value");
0056: errors.add(ActionMessages.GLOBAL_MESSAGE, error1);
0057: errors.add(ActionMessages.GLOBAL_MESSAGE, error2);
0058: errors.add(ActionMessages.GLOBAL_MESSAGE, error3);
0059: return errors;
0060: }
0061:
0062: private ActionMessages createTestActionMessages() {
0063: ActionMessages messages = new ActionMessages();
0064: ActionMessage message1 = new ActionMessage("key1");
0065: ActionMessage message2 = new ActionMessage("key2",
0066: new String[] { "value1", "value2" });
0067: messages.add(ActionErrors.GLOBAL_MESSAGE, message1);
0068: messages.add(ActionMessages.GLOBAL_MESSAGE, message2);
0069: return messages;
0070: }
0071:
0072: private ActionErrors createEmptyTestActionErrors() {
0073: return new ActionErrors();
0074: }
0075:
0076: private ActionMessages createEmptyTestActionMessages() {
0077: return new ActionMessages();
0078: }
0079:
0080: public void testVerifyOutput() throws Exception {
0081: getActionMockObjectFactory().getMockResponse().getWriter()
0082: .write("This is a test");
0083: try {
0084: module.verifyOutput("this is a test");
0085: fail();
0086: } catch (VerifyFailedException exc) {
0087: //should throw exception
0088: }
0089: module.setCaseSensitive(false);
0090: module.verifyOutput("this is a test");
0091: module.verifyOutputContains("TeSt");
0092: module.verifyOutputRegularExpression("THIS.*");
0093: module.setCaseSensitive(true);
0094: try {
0095: module.verifyOutputContains("THIS");
0096: fail();
0097: } catch (VerifyFailedException exc) {
0098: //should throw exception
0099: }
0100: try {
0101: module.verifyOutputRegularExpression(".*TEST");
0102: fail();
0103: } catch (VerifyFailedException exc) {
0104: //should throw exception
0105: }
0106: }
0107:
0108: public void testGetActionErrorByKey() {
0109: assertFalse(module.hasActionErrors());
0110: module.setActionErrors(createTestActionErrors());
0111: assertTrue(module.hasActionErrors());
0112: ActionMessage error = module.getActionErrorByKey("key3");
0113: assertEquals("value", error.getValues()[0]);
0114: }
0115:
0116: public void testGetActionMessageByKey() {
0117: assertFalse(module.hasActionMessages());
0118: module.setActionMessages(createTestActionMessages());
0119: assertTrue(module.hasActionMessages());
0120: ActionMessage message = module.getActionMessageByKey("key2");
0121: assertEquals("value2", message.getValues()[1]);
0122: }
0123:
0124: public void testGetActionMessages() {
0125: assertFalse(module.hasActionMessages());
0126: ActionMessages theMessages1 = createTestActionMessages();
0127: module.setActionMessagesToSession(theMessages1);
0128: assertTrue(module.hasActionMessages());
0129: module.setRecognizeMessagesInSession(false);
0130: assertFalse(module.hasActionMessages());
0131: ActionMessages theMessages2 = createTestActionMessages();
0132: module.setActionMessages(theMessages2);
0133: assertTrue(module.hasActionMessages());
0134: assertSame(theMessages2, module.getActionMessages());
0135: module.setRecognizeMessagesInSession(true);
0136: ActionMessages messages = module.getActionMessages();
0137: assertEquals(4, messages.size());
0138: module.setActionMessages(null);
0139: assertSame(theMessages1, module.getActionMessages());
0140: module.setActionMessages(new ActionMessages());
0141: assertSame(theMessages1, module.getActionMessages());
0142: module.setActionMessagesToSession(new ActionMessages());
0143: module.setActionMessages(theMessages1);
0144: assertSame(theMessages1, module.getActionMessages());
0145: }
0146:
0147: public void testGetActionErrors() {
0148: assertFalse(module.hasActionErrors());
0149: ActionErrors theErrors1 = createTestActionErrors();
0150: module.setActionErrorsToSession(theErrors1);
0151: assertTrue(module.hasActionErrors());
0152: module.setRecognizeMessagesInSession(false);
0153: assertFalse(module.hasActionErrors());
0154: ActionErrors theErrors2 = createTestActionErrors();
0155: module.setActionErrors(theErrors2);
0156: assertTrue(module.hasActionErrors());
0157: assertSame(theErrors2, module.getActionErrors());
0158: module.setRecognizeMessagesInSession(true);
0159: ActionMessages errors = module.getActionErrors();
0160: assertEquals(6, errors.size());
0161: module.setActionErrors(null);
0162: assertSame(theErrors1, module.getActionErrors());
0163: module.setActionErrors(new ActionErrors());
0164: assertSame(theErrors1, module.getActionErrors());
0165: module.setActionErrorsToSession(new ActionMessages());
0166: module.setActionErrors(theErrors1);
0167: assertSame(theErrors1, module.getActionErrors());
0168: }
0169:
0170: public void testActionMessagesInRequestAndSession() {
0171: ActionMessages theMessages1 = createTestActionMessages();
0172: ActionMessages theMessages2 = new ActionMessages();
0173: ActionMessage message1 = new ActionMessage("key3");
0174: ActionMessage message2 = new ActionMessage("key4",
0175: new String[] { "value1", "value2" });
0176: theMessages2.add("abc", message1);
0177: theMessages2.add(ActionMessages.GLOBAL_MESSAGE, message2);
0178: module.setActionMessages(theMessages1);
0179: module.setActionMessagesToSession(theMessages2);
0180: module.verifyNumberActionMessages(4);
0181: module.verifyActionMessagePresent("key1");
0182: module.verifyActionMessagePresent("key2");
0183: module.verifyActionMessagePresent("key3");
0184: module.verifyActionMessagePresent("key4");
0185: module.verifyActionMessageNotPresent("key5");
0186: module.verifyActionMessageValues("key2", new String[] {
0187: "value1", "value2" });
0188: module.verifyActionMessageValues("key4", new String[] {
0189: "value1", "value2" });
0190: module.verifyActionMessageProperty("key3", "abc");
0191: module.setRecognizeMessagesInSession(false);
0192: module.verifyNumberActionMessages(2);
0193: module.verifyActionMessagePresent("key1");
0194: module.verifyActionMessagePresent("key2");
0195: module.verifyActionMessageNotPresent("key3");
0196: }
0197:
0198: public void testActionErrorsInRequestAndSession() {
0199: ActionErrors theErrors1 = createTestActionErrors();
0200: ActionErrors theErrors2 = new ActionErrors();
0201: ActionMessage error1 = new ActionMessage("key4");
0202: ActionMessage error2 = new ActionMessage("key5", new String[] {
0203: "value1", "value2" });
0204: theErrors2.add(ActionMessages.GLOBAL_MESSAGE, error1);
0205: theErrors2.add("abc", error2);
0206: module.setActionErrors(theErrors1);
0207: module.setActionErrorsToSession(theErrors2);
0208: module.verifyNumberActionErrors(5);
0209: module.verifyActionErrorPresent("key1");
0210: module.verifyActionErrorPresent("key2");
0211: module.verifyActionErrorPresent("key3");
0212: module.verifyActionErrorPresent("key4");
0213: module.verifyActionErrorPresent("key5");
0214: module.verifyActionErrorNotPresent("key6");
0215: module.verifyActionErrorValues("key2", new String[] { "value1",
0216: "value2" });
0217: module.verifyActionErrorValues("key5", new String[] { "value1",
0218: "value2" });
0219: module.verifyActionErrorProperty("key5", "abc");
0220: module.setRecognizeMessagesInSession(false);
0221: module.verifyNumberActionErrors(3);
0222: module.verifyActionErrorPresent("key1");
0223: module.verifyActionErrorPresent("key2");
0224: module.verifyActionErrorPresent("key3");
0225: module.verifyActionErrorNotPresent("key4");
0226: }
0227:
0228: public void testActionErrorsInRequestAndSessionInstance() {
0229: module.setActionErrors(createTestActionMessages());
0230: module.setActionErrorsToSession(createTestActionMessages());
0231: assertTrue(module.getActionErrors() instanceof ActionMessages);
0232: module.setActionErrors(createTestActionErrors());
0233: assertTrue(module.getActionErrors() instanceof ActionErrors);
0234: module.setActionErrorsToSession(createTestActionErrors());
0235: assertTrue(module.getActionErrors() instanceof ActionErrors);
0236: module.setActionErrors(createTestActionMessages());
0237: assertTrue(module.getActionErrors() instanceof ActionErrors);
0238: }
0239:
0240: public void testSetMessageAttributeKey() {
0241: ActionMessages messages = createTestActionMessages();
0242: module.setActionMessages(messages);
0243: assertSame(messages, getActionMockObjectFactory()
0244: .getMockRequest().getAttribute(Globals.MESSAGE_KEY));
0245: module.setActionMessagesToSession(messages);
0246: assertSame(messages, getActionMockObjectFactory()
0247: .getMockSession().getAttribute(Globals.MESSAGE_KEY));
0248: ActionMessages otherMessages = createTestActionMessages();
0249: module.setMessageAttributeKey("mymessages");
0250: module.setActionMessages(otherMessages);
0251: assertSame(otherMessages, getActionMockObjectFactory()
0252: .getMockRequest().getAttribute("mymessages"));
0253: module.setActionMessagesToSession(otherMessages);
0254: assertSame(otherMessages, getActionMockObjectFactory()
0255: .getMockSession().getAttribute("mymessages"));
0256: assertEquals(4, module.getActionMessages().size());
0257: module.verifyActionMessagePresent("key1");
0258: module.verifyActionMessageNotPresent("key3");
0259: module.setMessageAttributeKey("test");
0260: module.verifyActionMessageNotPresent("key1");
0261: assertNull(module.getActionMessages());
0262: try {
0263: module.verifyHasActionMessages();
0264: fail();
0265: } catch (VerifyFailedException exc) {
0266: //should throw exception
0267: }
0268: module.setMessageAttributeKey("mymessages");
0269: module.verifyHasActionMessages();
0270: module.verifyActionMessages(new String[] { "key1", "key2",
0271: "key1", "key2" });
0272: module.verifyNumberActionMessages(4);
0273: module.verifyActionMessageValues("key2", new String[] {
0274: "value1", "value2" });
0275: }
0276:
0277: public void testSetErrorAttributeKey() {
0278: ActionErrors errors = createTestActionErrors();
0279: module.setActionErrors(errors);
0280: assertSame(errors, getActionMockObjectFactory()
0281: .getMockRequest().getAttribute(Globals.ERROR_KEY));
0282: module.setActionErrorsToSession(errors);
0283: assertSame(errors, getActionMockObjectFactory()
0284: .getMockSession().getAttribute(Globals.ERROR_KEY));
0285: ActionErrors otherErrors = createTestActionErrors();
0286: module.setErrorAttributeKey("othereerrors");
0287: module.setActionErrors(otherErrors);
0288: assertSame(otherErrors, getActionMockObjectFactory()
0289: .getMockRequest().getAttribute("othereerrors"));
0290: module.setActionErrorsToSession(otherErrors);
0291: assertSame(otherErrors, getActionMockObjectFactory()
0292: .getMockSession().getAttribute("othereerrors"));
0293: assertEquals(6, module.getActionErrors().size());
0294: module.verifyActionErrorPresent("key2");
0295: module.verifyActionErrorNotPresent("key4");
0296: module.setErrorAttributeKey("test");
0297: module.verifyActionErrorNotPresent("key2");
0298: assertNull(module.getActionErrors());
0299: try {
0300: module.verifyHasActionErrors();
0301: fail();
0302: } catch (VerifyFailedException exc) {
0303: //should throw exception
0304: }
0305: module.setErrorAttributeKey("othereerrors");
0306: module.verifyHasActionErrors();
0307: module.verifyActionErrorPresent("key1");
0308: module.verifyActionErrorPresent("key2");
0309: module.verifyActionErrorPresent("key3");
0310: module.verifyNumberActionErrors(6);
0311: module
0312: .verifyActionErrorValues("key3",
0313: new String[] { "value" });
0314: }
0315:
0316: public void testVerifyHasActionErrors() {
0317: module.setActionErrors(createEmptyTestActionErrors());
0318: module.verifyNoActionErrors();
0319: try {
0320: module.verifyHasActionErrors();
0321: fail();
0322: } catch (VerifyFailedException exc) {
0323: //should throw exception
0324: }
0325: module.setActionErrors(createTestActionErrors());
0326: module.verifyHasActionErrors();
0327: try {
0328: module.verifyNoActionErrors();
0329: fail();
0330: } catch (VerifyFailedException exc) {
0331: //should throw exception
0332: }
0333: module.setActionErrors(null);
0334: module.verifyNoActionErrors();
0335: module.setActionErrorsToSession(createTestActionErrors());
0336: module.verifyHasActionErrors();
0337: module.setRecognizeMessagesInSession(false);
0338: module.verifyNoActionErrors();
0339: }
0340:
0341: public void testVerifyHasActionMessages() {
0342: module.setActionMessages(createEmptyTestActionMessages());
0343: module.verifyNoActionMessages();
0344: try {
0345: module.verifyHasActionMessages();
0346: fail();
0347: } catch (VerifyFailedException exc) {
0348: //should throw exception
0349: }
0350: module.setActionMessages(createTestActionMessages());
0351: module.verifyHasActionMessages();
0352: try {
0353: module.verifyNoActionMessages();
0354: fail();
0355: } catch (VerifyFailedException exc) {
0356: //should throw exception
0357: }
0358: module.setActionMessages(null);
0359: module.verifyNoActionMessages();
0360: module.setActionMessagesToSession(createTestActionMessages());
0361: module.verifyHasActionMessages();
0362: module.setRecognizeMessagesInSession(false);
0363: module.verifyNoActionMessages();
0364: }
0365:
0366: public void testVerifyActionErrorPresent() {
0367: module.setActionErrors(createEmptyTestActionErrors());
0368: module.verifyActionErrorNotPresent("key1");
0369: module.setActionErrors(createTestActionErrors());
0370: module.verifyActionErrorPresent("key1");
0371: module.verifyActionErrorNotPresent("key4");
0372: try {
0373: module.verifyActionErrorPresent("key5");
0374: fail();
0375: } catch (VerifyFailedException exc) {
0376: //should throw exception
0377: }
0378: try {
0379: module.verifyActionErrorNotPresent("key3");
0380: fail();
0381: } catch (VerifyFailedException exc) {
0382: //should throw exception
0383: }
0384: module.setActionErrors(null);
0385: module.verifyActionErrorNotPresent("key1");
0386: module.setActionErrorsToSession(createTestActionErrors());
0387: module.verifyActionErrorPresent("key1");
0388: }
0389:
0390: public void testVerifyActionMessagePresent() {
0391: module.setActionMessages(createEmptyTestActionMessages());
0392: module.verifyActionMessageNotPresent("key1");
0393: module.setActionMessages(createTestActionMessages());
0394: module.verifyActionMessagePresent("key1");
0395: module.verifyActionMessageNotPresent("key3");
0396: try {
0397: module.verifyActionMessagePresent("key3");
0398: fail();
0399: } catch (VerifyFailedException exc) {
0400: //should throw exception
0401: }
0402: try {
0403: module.verifyActionMessageNotPresent("key1");
0404: fail();
0405: } catch (VerifyFailedException exc) {
0406: //should throw exception
0407: }
0408: module.setActionMessages(null);
0409: module.verifyActionMessageNotPresent("key1");
0410: module.setActionMessagesToSession(createTestActionMessages());
0411: module.verifyActionMessagePresent("key1");
0412: }
0413:
0414: public void testVerifyNumberActionErrors() {
0415: module.setActionErrors(createEmptyTestActionErrors());
0416: module.verifyNumberActionErrors(0);
0417: module.setActionErrors(createTestActionErrors());
0418: module.verifyNumberActionErrors(3);
0419: try {
0420: module.verifyNumberActionErrors(4);
0421: fail();
0422: } catch (VerifyFailedException exc) {
0423: //should throw exception
0424: }
0425: module.setActionErrors(null);
0426: module.verifyNumberActionErrors(0);
0427: module.setActionErrorsToSession(createTestActionErrors());
0428: module.verifyNumberActionErrors(3);
0429: }
0430:
0431: public void testVerifyNumberActionMessages() {
0432: module.setActionMessages(createEmptyTestActionMessages());
0433: module.verifyNumberActionMessages(0);
0434: module.setActionMessages(createTestActionMessages());
0435: module.verifyNumberActionMessages(2);
0436: try {
0437: module.verifyNumberActionMessages(0);
0438: fail();
0439: } catch (VerifyFailedException exc) {
0440: //should throw exception
0441: }
0442: module.setActionMessages(null);
0443: module.verifyNumberActionMessages(0);
0444: module.setActionMessagesToSession(createTestActionMessages());
0445: module.verifyNumberActionMessages(2);
0446: }
0447:
0448: public void testVerifyActionErrors() {
0449: module.setActionErrors(createTestActionErrors());
0450: module
0451: .verifyActionErrors(new String[] { "key1", "key2",
0452: "key3" });
0453: try {
0454: module.verifyActionErrors(new String[] { "key1", "key2",
0455: "key3", "key4" });
0456: fail();
0457: } catch (VerifyFailedException exc) {
0458: //should throw exception
0459: }
0460: try {
0461: module.verifyActionErrors(new String[] { "key1", "key2" });
0462: fail();
0463: } catch (VerifyFailedException exc) {
0464: //should throw exception
0465: }
0466: try {
0467: module.verifyActionErrors(new String[] { "key4", "key2",
0468: "key3" });
0469: fail();
0470: } catch (VerifyFailedException exc) {
0471: //should throw exception
0472: }
0473: module.setActionErrors(null);
0474: module.setActionErrorsToSession(createTestActionErrors());
0475: module
0476: .verifyActionErrors(new String[] { "key1", "key2",
0477: "key3" });
0478: module.setRecognizeMessagesInSession(false);
0479: try {
0480: module.verifyActionErrors(new String[] { "key1", "key2",
0481: "key3" });
0482: fail();
0483: } catch (VerifyFailedException exc) {
0484: //should throw exception
0485: }
0486: }
0487:
0488: public void testVerifyActionMessages() {
0489: module.setActionMessages(createTestActionMessages());
0490: module.verifyActionMessages(new String[] { "key1", "key2" });
0491: try {
0492: module.verifyActionMessages(new String[] { "key1", "key3",
0493: "key4" });
0494: fail();
0495: } catch (VerifyFailedException exc) {
0496: //should throw exception
0497: }
0498: try {
0499: module.verifyActionMessages(new String[] { "key1" });
0500: fail();
0501: } catch (VerifyFailedException exc) {
0502: //should throw exception
0503: }
0504: module.setActionMessages(null);
0505: module.setActionMessagesToSession(createTestActionMessages());
0506: module.verifyActionMessages(new String[] { "key1", "key2" });
0507: module.setRecognizeMessagesInSession(false);
0508: try {
0509: module
0510: .verifyActionMessages(new String[] { "key1", "key2" });
0511: fail();
0512: } catch (VerifyFailedException exc) {
0513: //should throw exception
0514: }
0515: }
0516:
0517: public void testVerifyActionErrorValues() {
0518: module.setActionErrors(createTestActionErrors());
0519: module.verifyActionErrorValue("key3", "value");
0520: module.verifyActionErrorValues("key2", new String[] { "value1",
0521: "value2" });
0522: try {
0523: module.verifyActionErrorValue("key1", "test");
0524: fail();
0525: } catch (VerifyFailedException exc) {
0526: //should throw exception
0527: }
0528: try {
0529: module.verifyActionErrorValue("key3", "test");
0530: fail();
0531: } catch (VerifyFailedException exc) {
0532: //should throw exception
0533: }
0534: try {
0535: module.verifyActionErrorValue("key2", "value1");
0536: fail();
0537: } catch (VerifyFailedException exc) {
0538: //should throw exception
0539: }
0540: try {
0541: module.verifyActionErrorValue("key2", new String[] {
0542: "value2", "value1" });
0543: fail();
0544: } catch (VerifyFailedException exc) {
0545: //should throw exception
0546: }
0547: module.setActionErrors(null);
0548: module.setActionErrorsToSession(createTestActionErrors());
0549: module.verifyActionErrorValues("key2", new String[] { "value1",
0550: "value2" });
0551: module.setRecognizeMessagesInSession(false);
0552: try {
0553: module.verifyActionErrorValues("key2", new String[] {
0554: "value1", "value2" });
0555: fail();
0556: } catch (VerifyFailedException exc) {
0557: //should throw exception
0558: }
0559: }
0560:
0561: public void testVerifyActionMessageValues() {
0562: module.setActionMessages(createTestActionMessages());
0563: module.verifyActionMessageValues("key2", new String[] {
0564: "value1", "value2" });
0565: try {
0566: module.verifyActionMessageValue("key2", "value1");
0567: fail();
0568: } catch (VerifyFailedException exc) {
0569: //should throw exception
0570: }
0571: try {
0572: module.verifyActionMessageValue("key1", "value1");
0573: fail();
0574: } catch (VerifyFailedException exc) {
0575: //should throw exception
0576: }
0577: }
0578:
0579: public void testVerifyActionErrorProperty() {
0580: ActionErrors errors = new ActionErrors();
0581: ActionMessage error1 = new ActionMessage("error1");
0582: ActionMessage error2 = new ActionMessage("error2");
0583: errors.add("property", error1);
0584: errors.add(ActionMessages.GLOBAL_MESSAGE, error2);
0585: module.setActionErrors(errors);
0586: module.verifyActionErrorProperty("error1", "property");
0587: try {
0588: module.verifyActionMessageProperty("error2", "property");
0589: fail();
0590: } catch (VerifyFailedException exc) {
0591: //should throw exception
0592: }
0593: module.verifyActionErrorProperty("error2",
0594: ActionMessages.GLOBAL_MESSAGE);
0595: module.setActionErrors(null);
0596: module.setActionErrorsToSession(errors);
0597: module.verifyActionErrorProperty("error1", "property");
0598: }
0599:
0600: public void testVerifyActionMessageProperty() {
0601: ActionMessages messages = new ActionMessages();
0602: ActionMessage message1 = new ActionMessage("message1");
0603: ActionMessage message2 = new ActionMessage("message2");
0604: messages.add("property", message1);
0605: messages.add("property", message2);
0606: module.setActionMessages(messages);
0607: module.verifyActionMessageProperty("message1", "property");
0608: module.verifyActionMessageProperty("message2", "property");
0609: try {
0610: module.verifyActionMessageProperty("message2", "property1");
0611: fail();
0612: } catch (VerifyFailedException exc) {
0613: //should throw exception
0614: }
0615: module.setActionMessages(null);
0616: module.setActionMessagesToSession(messages);
0617: module.verifyActionMessageProperty("message1", "property");
0618: }
0619:
0620: public void testCreateActionForm() {
0621: module.createActionForm(null);
0622: assertNull(module.getActionForm());
0623: module.createActionForm(TestForm.class);
0624: assertNotNull(module.getActionForm());
0625: assertTrue(module.getActionForm() instanceof TestForm);
0626: }
0627:
0628: public void testPopulateRequestToForm() {
0629: module.addRequestParameter("value(key1)", "value1");
0630: module.addRequestParameter("value(key2)", "value2");
0631: module.addRequestParameter("property", "value3");
0632: module
0633: .addRequestParameter("indexedProperty[1]",
0634: "indexedValue");
0635: module.addRequestParameter("nested.property", "nestedValue");
0636: module.createActionForm(TestForm.class);
0637: module.populateRequestToForm();
0638: TestForm form = (TestForm) module.getActionForm();
0639: assertEquals("value1", form.getValue("key1"));
0640: assertEquals("value2", form.getValue("key2"));
0641: assertEquals("value3", form.getProperty());
0642: assertEquals("indexedValue", form.getIndexedProperty(1));
0643: assertEquals("nestedValue", form.getNested().getProperty());
0644: assertEquals(null, form.getValue("key3"));
0645: }
0646:
0647: public void testSetDoPopulateAndReset() {
0648: TestForm form = (TestForm) module
0649: .createActionForm(TestForm.class);
0650: module.addRequestParameter("property", "value");
0651: module.actionPerform(TestAction.class, form);
0652: assertEquals("value", form.getProperty());
0653: assertTrue(form.wasResetCalled());
0654:
0655: form = (TestForm) module.createActionForm(TestForm.class);
0656: module.addRequestParameter("property", "value");
0657: module.setDoPopulate(false);
0658: module.actionPerform(TestAction.class, form);
0659: assertNull(form.getProperty());
0660: assertTrue(form.wasResetCalled());
0661:
0662: form = (TestForm) module.createActionForm(TestForm.class);
0663: assertFalse(form.wasResetCalled());
0664: module.addRequestParameter("property", "value");
0665: module.setDoPopulate(true);
0666: module.setReset(false);
0667: module.actionPerform(TestAction.class, form);
0668: assertEquals("value", form.getProperty());
0669: assertFalse(form.wasResetCalled());
0670: }
0671:
0672: public void testActionPerform() {
0673: module.addRequestParameter("property", "value");
0674:
0675: TestForm form = (TestForm) module
0676: .createActionForm(TestForm.class);
0677: form.setValidationOk(false);
0678: module.setValidate(true);
0679: module.setInput("input");
0680: module.actionPerform(TestAction.class, form);
0681: module.verifyForward("input");
0682: assertEquals("value", form.getProperty());
0683: module.verifyHasActionErrors();
0684: module.verifyActionErrorPresent("testkey");
0685:
0686: form = (TestForm) module.createActionForm(TestForm.class);
0687: form.setValidationOk(false);
0688: module.setValidate(false);
0689: module.actionPerform(TestAction.class, form);
0690: assertEquals("success", ((MockActionForward) module
0691: .getActionForward()).getPath());
0692: module.verifyForward("success");
0693: assertEquals("value", form.getProperty());
0694: module.verifyNoActionErrors();
0695:
0696: form = (TestForm) module.createActionForm(TestForm.class);
0697: form.setValidationOk(true);
0698: module.setValidate(true);
0699: module.actionPerform(TestAction.class, form);
0700: module.verifyForward("success");
0701: module.verifyNoActionErrors();
0702:
0703: module.actionPerform(TestAction.class, TestForm.class);
0704: module.verifyForward("success");
0705: module.verifyNoActionErrors();
0706: assertEquals("value", ((TestForm) module.getActionForm())
0707: .getProperty());
0708: module.actionPerform(TestAction.class);
0709: module.verifyForward("success");
0710: module.verifyNoActionErrors();
0711: assertNull(module.getActionForm());
0712: }
0713:
0714: public void testActionPerformServletSet() {
0715: TestForm form = (TestForm) module
0716: .createActionForm(TestForm.class);
0717: module.actionPerform(TestAction.class, form);
0718: assertEquals(getActionMockObjectFactory()
0719: .getMockActionServlet(), form.getServlet());
0720: assertEquals(getActionMockObjectFactory()
0721: .getMockActionServlet(), ((TestForm) module
0722: .getActionForm()).getServlet());
0723: assertEquals(getActionMockObjectFactory()
0724: .getMockActionServlet(), module.getLastAction()
0725: .getServlet());
0726: TestAction testAction = new TestAction();
0727: module.actionPerform(testAction, form);
0728: assertEquals(getActionMockObjectFactory()
0729: .getMockActionServlet(), form.getServlet());
0730: assertEquals(getActionMockObjectFactory()
0731: .getMockActionServlet(), testAction.getServlet());
0732: module.actionPerform(TestAction.class, TestForm.class);
0733: assertEquals(getActionMockObjectFactory()
0734: .getMockActionServlet(), module.getLastAction()
0735: .getServlet());
0736: assertEquals(getActionMockObjectFactory()
0737: .getMockActionServlet(), ((TestForm) module
0738: .getActionForm()).getServlet());
0739: }
0740:
0741: public void testVerifyForwardPathAndName() {
0742: TestForwardAction action = new TestForwardAction();
0743: module.actionPerform(action);
0744: module.verifyForward("success");
0745: module.verifyForwardName("success");
0746: MockActionForward forward = new MockActionForward("myName");
0747: action.setActionForward(forward);
0748: module.actionPerform(action);
0749: module.verifyForwardName("myName");
0750: try {
0751: module.verifyForward("myName");
0752: fail();
0753: } catch (VerifyFailedException exc) {
0754: //should throw exception
0755: }
0756: forward = new MockActionForward("myName", "myPath", true);
0757: action.setActionForward(forward);
0758: module.actionPerform(action);
0759: module.verifyForward("myPath");
0760: module.verifyForwardName("myName");
0761: }
0762:
0763: public void testVerifyForwardWithPresetForward() {
0764: getActionMockObjectFactory().getMockActionMapping().addForward(
0765: "success", "myPath");
0766: TestForwardAction action = new TestForwardAction();
0767: module.actionPerform(action);
0768: module.verifyForward("myPath");
0769: module.verifyForwardName("success");
0770: getActionMockObjectFactory().getMockActionMapping().addForward(
0771: "success", "anotherPath");
0772: module.actionPerform(action);
0773: module.verifyForward("anotherPath");
0774: module.verifyForwardName("success");
0775: }
0776:
0777: public void testActionPerformMappingTypeSet() {
0778: module.actionPerform(TestAction.class);
0779: assertEquals(TestAction.class.getName(),
0780: getActionMockObjectFactory().getMockActionMapping()
0781: .getType());
0782: }
0783:
0784: public void testSetResourcesAndLocale() {
0785: getActionMockObjectFactory().getMockRequest().getSession(true);
0786: MapMessageResources resources1 = new MapMessageResources();
0787: MapMessageResources resources2 = new MapMessageResources();
0788: module.setResources(resources1);
0789: module.setResources("test", resources2);
0790: module.setLocale(Locale.TRADITIONAL_CHINESE);
0791: TestAction testAction = new TestAction();
0792: module.actionPerform(testAction);
0793: assertEquals(resources1, testAction.getTestResources());
0794: assertEquals(resources2, testAction.getTestResourcesForKey());
0795: assertEquals(Locale.TRADITIONAL_CHINESE, testAction
0796: .getTestLocale());
0797: }
0798:
0799: public void testSetResourcesAddToModuleConfig() {
0800: MapMessageResources resources1 = new MapMessageResources();
0801: MapMessageResources resources2 = new MapMessageResources();
0802: MapMessageResources resources3 = new MapMessageResources();
0803: module.setResources("test1", resources1);
0804: module.setResources("test2", resources2);
0805: module.setResources("test3", resources3);
0806: assertEquals(3,
0807: getActionMockObjectFactory().getMockModuleConfig()
0808: .findMessageResourcesConfigs().length);
0809: assertNotNull(getActionMockObjectFactory()
0810: .getMockModuleConfig().findMessageResourcesConfig(
0811: "test1"));
0812: assertNotNull(getActionMockObjectFactory()
0813: .getMockModuleConfig().findMessageResourcesConfig(
0814: "test2"));
0815: assertNotNull(getActionMockObjectFactory()
0816: .getMockModuleConfig().findMessageResourcesConfig(
0817: "test3"));
0818: }
0819:
0820: public void testDynaActionForm() {
0821: FormBeanConfig config = new FormBeanConfig();
0822: config.setType(TestDynaForm.class.getName());
0823: FormPropertyConfig property1 = new FormPropertyConfig();
0824: property1.setName("property1");
0825: property1.setType("java.lang.String");
0826: property1.setInitial("testValue1");
0827: FormPropertyConfig property2 = new FormPropertyConfig();
0828: property2.setName("property2");
0829: property2.setType("java.lang.Integer");
0830: property2.setInitial("2");
0831: config.addFormPropertyConfig(property1);
0832: config.addFormPropertyConfig(property2);
0833: DynaActionForm form = module.createDynaActionForm(config);
0834: String prop1Value = (String) form.get("property1");
0835: assertEquals("testValue1", prop1Value);
0836: Integer prop2Value = (Integer) form.get("property2");
0837: assertEquals(new Integer(2), prop2Value);
0838: try {
0839: form.set("property3", "3");
0840: fail();
0841: } catch (IllegalArgumentException exc) {
0842: //should throw exception
0843: }
0844: module.addRequestParameter("property2", "123");
0845: module.actionPerform(TestAction.class, form);
0846: assertEquals(new Integer(123), form.get("property2"));
0847: }
0848:
0849: public void testCreateValidatorResources() {
0850: String[] files = new String[2];
0851: files[0] = "src/com/mockrunner/test/web/validator-rules.xml";
0852: files[1] = "src/com/mockrunner/test/web/validation.xml";
0853: module.createValidatorResources(files);
0854: module.setResources(new MapMessageResources());
0855: TestValidatorForm form = new TestValidatorForm();
0856: form.setServlet(getActionMockObjectFactory()
0857: .getMockActionServlet());
0858: getActionMockObjectFactory().getMockActionMapping().setName(
0859: "testForm");
0860: form.setFirstName("ABCDEF");
0861: form.setLastName("ABCDEF");
0862: ActionErrors errors = form.validate(
0863: getActionMockObjectFactory().getMockActionMapping(),
0864: getActionMockObjectFactory().getMockRequest());
0865: assertTrue(errors.isEmpty());
0866: ;
0867: form.setFirstName("ABCD");
0868: form.setLastName("12345678901");
0869: errors = form.validate(getActionMockObjectFactory()
0870: .getMockActionMapping(), getActionMockObjectFactory()
0871: .getMockRequest());
0872: System.out.println(errors.size());
0873: assertTrue(errors.size() == 2);
0874: form.setLastName("ABCDEF");
0875: errors = form.validate(getActionMockObjectFactory()
0876: .getMockActionMapping(), getActionMockObjectFactory()
0877: .getMockRequest());
0878: assertTrue(errors.size() == 1);
0879: ActionMessage error = (ActionMessage) errors.get().next();
0880: assertEquals("errors.minlength", error.getKey());
0881: }
0882:
0883: public void testWrappedRequest() {
0884: HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(
0885: getActionMockObjectFactory().getMockRequest());
0886: HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
0887: getActionMockObjectFactory().getMockResponse());
0888: getActionMockObjectFactory().addRequestWrapper(requestWrapper);
0889: getActionMockObjectFactory()
0890: .addResponseWrapper(responseWrapper);
0891: TestAction action = new TestAction();
0892: module.actionPerform(action);
0893: assertSame(requestWrapper, action.getRequest());
0894: assertSame(responseWrapper, action.getResponse());
0895: }
0896:
0897: public void testCustomActionMapping() {
0898: TestMapping mapping = (TestMapping) getActionMockObjectFactory()
0899: .prepareActionMapping(TestMapping.class);
0900: TestMappingAction action = new TestMappingAction();
0901: TestMappingForm form = new TestMappingForm();
0902: module.actionPerform(action, form);
0903: assertSame(mapping, action.getActionMapping());
0904: assertSame(mapping, form.getResetActionMapping());
0905: assertSame(mapping, form.getValidateActionMapping());
0906: }
0907:
0908: public void testCustomActionMappingDelegation() {
0909: TestMapping mapping = (TestMapping) getActionMockObjectFactory()
0910: .prepareActionMapping(TestMapping.class);
0911: module.setValidate(true);
0912: module.setParameter("testParameter");
0913: module.setInput("testInput");
0914: assertTrue(mapping.getValidate());
0915: assertEquals("testParameter", mapping.getParameter());
0916: assertEquals("testInput", mapping.getInput());
0917: assertTrue(getActionMockObjectFactory().getMockActionMapping()
0918: .getValidate());
0919: assertEquals("testParameter", getActionMockObjectFactory()
0920: .getMockActionMapping().getParameter());
0921: assertEquals("testInput", getActionMockObjectFactory()
0922: .getMockActionMapping().getInput());
0923: }
0924:
0925: public void testExceptionHandler() {
0926: TestExceptionHandler handler1 = new TestExceptionHandler();
0927: TestExceptionHandler handler2 = new TestExceptionHandler();
0928: TestExceptionHandler handler3 = new TestExceptionHandler();
0929: DefaultExceptionHandlerConfig config1 = new DefaultExceptionHandlerConfig(
0930: handler1, FileNotFoundException.class);
0931: DefaultExceptionHandlerConfig config2 = new DefaultExceptionHandlerConfig(
0932: handler2, SQLException.class);
0933: DefaultExceptionHandlerConfig config3 = new DefaultExceptionHandlerConfig(
0934: handler3, IOException.class);
0935: module.addExceptionHandler(config1);
0936: module.addExceptionHandler(config2);
0937: module.addExceptionHandler(config3);
0938: IOException ioException = new IOException();
0939: ActionForward forward = module
0940: .actionPerform(new TestFailureAction(ioException));
0941: assertEquals("testname", forward.getName());
0942: assertFalse(handler1.wasExecuteCalled());
0943: assertFalse(handler2.wasExecuteCalled());
0944: assertTrue(handler3.wasExecuteCalled());
0945: assertSame(ioException, handler3.getException());
0946: handler3.reset();
0947: FileNotFoundException fileNotFoundException = new FileNotFoundException();
0948: forward = module.actionPerform(new TestFailureAction(
0949: fileNotFoundException));
0950: assertEquals("testname", forward.getName());
0951: assertTrue(handler1.wasExecuteCalled());
0952: assertFalse(handler2.wasExecuteCalled());
0953: assertFalse(handler3.wasExecuteCalled());
0954: assertSame(fileNotFoundException, handler1.getException());
0955: handler1.reset();
0956: SQLException sqlException = new SQLException();
0957: forward = module.actionPerform(new TestFailureAction(
0958: sqlException));
0959: assertEquals("testname", forward.getName());
0960: assertFalse(handler1.wasExecuteCalled());
0961: assertTrue(handler2.wasExecuteCalled());
0962: assertFalse(handler3.wasExecuteCalled());
0963: assertSame(sqlException, handler2.getException());
0964: handler2.reset();
0965: Exception exception = new Exception();
0966: try {
0967: module.actionPerform(new TestFailureAction(exception));
0968: fail();
0969: } catch (NestedApplicationException exc) {
0970: assertSame(exception, exc.getRootCause());
0971: }
0972: }
0973:
0974: public void testExceptionHandlerActionForward() {
0975: TestExceptionHandlerConfig config1 = new TestExceptionHandlerConfig(
0976: true, new MockActionForward("test"));
0977: TestExceptionHandlerConfig config2 = new TestExceptionHandlerConfig(
0978: false, new Integer(1));
0979: module.addExceptionHandler(config1);
0980: module.addExceptionHandler(config2);
0981: ActionForward forward = module
0982: .actionPerform(new TestFailureAction(new Exception()));
0983: assertEquals("test", forward.getName());
0984: assertSame(forward, module.getActionForward());
0985: config1.setCanHandle(false);
0986: config2.setCanHandle(true);
0987: assertNull(module.actionPerform(new TestFailureAction(
0988: new Exception())));
0989: assertNull(module.getActionForward());
0990: }
0991:
0992: public void testNestedException() {
0993: try {
0994: module.actionPerform(TestFailureAction.class);
0995: fail();
0996: } catch (NestedApplicationException exc) {
0997: assertEquals("Expected", exc.getRootCause().getMessage());
0998: }
0999: }
1000:
1001: public static class TestMappingAction extends Action {
1002: private ActionMapping mapping;
1003:
1004: public ActionMapping getActionMapping() {
1005: return mapping;
1006: }
1007:
1008: public ActionForward execute(ActionMapping mapping,
1009: ActionForm form, HttpServletRequest request,
1010: HttpServletResponse response) throws Exception {
1011: this .mapping = mapping;
1012: return mapping.findForward("success");
1013: }
1014: }
1015:
1016: public static class TestFailureAction extends Action {
1017: private Exception exception;
1018:
1019: public TestFailureAction() {
1020: exception = new Exception("Expected");
1021: }
1022:
1023: public TestFailureAction(Exception exception) {
1024: this .exception = exception;
1025: }
1026:
1027: public ActionForward execute(ActionMapping mapping,
1028: ActionForm form, HttpServletRequest request,
1029: HttpServletResponse response) throws Exception {
1030: throw exception;
1031: }
1032: }
1033:
1034: public static class TestAction extends Action {
1035: private MessageResources resources;
1036: private MessageResources resourcesForKey;
1037: private Locale locale;
1038: private HttpServletRequest request;
1039: private HttpServletResponse response;
1040:
1041: public ActionForward execute(ActionMapping mapping,
1042: ActionForm form, HttpServletRequest request,
1043: HttpServletResponse response) throws Exception {
1044: resources = getResources(request);
1045: resourcesForKey = getResources(request, "test");
1046: locale = getLocale(request);
1047: this .request = request;
1048: this .response = response;
1049: return mapping.findForward("success");
1050: }
1051:
1052: public HttpServletRequest getRequest() {
1053: return request;
1054: }
1055:
1056: public HttpServletResponse getResponse() {
1057: return response;
1058: }
1059:
1060: public MessageResources getTestResourcesForKey() {
1061: return resourcesForKey;
1062: }
1063:
1064: public MessageResources getTestResources() {
1065: return resources;
1066: }
1067:
1068: public Locale getTestLocale() {
1069: return locale;
1070: }
1071: }
1072:
1073: public static class TestForwardAction extends Action {
1074: private ActionForward forward;
1075:
1076: public ActionForward execute(ActionMapping mapping,
1077: ActionForm form, HttpServletRequest request,
1078: HttpServletResponse response) throws Exception {
1079:
1080: if (null != forward)
1081: return forward;
1082: return mapping.findForward("success");
1083: }
1084:
1085: public void setActionForward(ActionForward forward) {
1086: this .forward = forward;
1087: }
1088: }
1089:
1090: public static class TestMappingForm extends ActionForm {
1091: private ActionMapping resetMapping;
1092: private ActionMapping validateMapping;
1093:
1094: public ActionMapping getResetActionMapping() {
1095: return resetMapping;
1096: }
1097:
1098: public ActionMapping getValidateActionMapping() {
1099: return validateMapping;
1100: }
1101:
1102: public void reset(ActionMapping mapping,
1103: HttpServletRequest request) {
1104: resetMapping = mapping;
1105: }
1106:
1107: public ActionErrors validate(ActionMapping mapping,
1108: HttpServletRequest request) {
1109: validateMapping = mapping;
1110: return null;
1111: }
1112: }
1113:
1114: public static class TestForm extends ActionForm {
1115: private boolean validationOk = true;
1116: private Map mappedProperties = new HashMap();
1117: private String property;
1118: private Map indexedProperties = new HashMap();
1119: private TestNested nested = new TestNested();
1120: private boolean resetCalled = false;
1121:
1122: public ActionServlet getServlet() {
1123: return super .getServlet();
1124: }
1125:
1126: public void setValidationOk(boolean validationOk) {
1127: this .validationOk = validationOk;
1128: }
1129:
1130: public String getProperty() {
1131: return property;
1132: }
1133:
1134: public void setProperty(String string) {
1135: property = string;
1136: }
1137:
1138: public String getIndexedProperty(int index) {
1139: return (String) indexedProperties.get(new Integer(index));
1140: }
1141:
1142: public void setIndexedProperty(int index, String string) {
1143: indexedProperties.put(new Integer(index), string);
1144: }
1145:
1146: public Object getValue(String name) {
1147: return mappedProperties.get(name);
1148: }
1149:
1150: public void setValue(String name, Object value) {
1151: mappedProperties.put(name, value);
1152: }
1153:
1154: public TestNested getNested() {
1155: return nested;
1156: }
1157:
1158: public void setNested(TestNested nested) {
1159: this .nested = nested;
1160: }
1161:
1162: public boolean wasResetCalled() {
1163: return resetCalled;
1164: }
1165:
1166: public void reset(ActionMapping mapping,
1167: HttpServletRequest request) {
1168: super .reset(mapping, request);
1169: resetCalled = true;
1170: }
1171:
1172: public ActionErrors validate(ActionMapping mapping,
1173: HttpServletRequest request) {
1174: ActionErrors errors = new ActionErrors();
1175: if (!validationOk) {
1176: errors.add(ActionMessages.GLOBAL_MESSAGE,
1177: new ActionMessage("testkey"));
1178: }
1179: return errors;
1180: }
1181: }
1182:
1183: public static class TestDynaForm extends DynaActionForm {
1184:
1185: }
1186:
1187: public static class TestValidatorForm extends ValidatorForm {
1188: private String firstName;
1189: private String lastName;
1190:
1191: public String getFirstName() {
1192: return firstName;
1193: }
1194:
1195: public String getLastName() {
1196: return lastName;
1197: }
1198:
1199: public void setFirstName(String firstName) {
1200: this .firstName = firstName;
1201: }
1202:
1203: public void setLastName(String lastName) {
1204: this .lastName = lastName;
1205: }
1206: }
1207:
1208: public static class TestNested {
1209: private String property;
1210:
1211: public String getProperty() {
1212: return property;
1213: }
1214:
1215: public void setProperty(String string) {
1216: property = string;
1217: }
1218: }
1219:
1220: public static class TestMapping extends ActionMapping {
1221:
1222: }
1223:
1224: public static class TestExceptionHandler extends ExceptionHandler {
1225: private boolean executeCalled = false;
1226: private Exception exception;
1227:
1228: public ActionForward execute(Exception exc,
1229: ExceptionConfig config, ActionMapping mapping,
1230: ActionForm form, HttpServletRequest request,
1231: HttpServletResponse response) throws ServletException {
1232: executeCalled = true;
1233: exception = exc;
1234: return new MockActionForward("testname");
1235: }
1236:
1237: public void reset() {
1238: executeCalled = false;
1239: exception = null;
1240: }
1241:
1242: public Exception getException() {
1243: return exception;
1244: }
1245:
1246: public boolean wasExecuteCalled() {
1247: return executeCalled;
1248: }
1249: }
1250:
1251: public static class TestExceptionHandlerConfig implements
1252: ExceptionHandlerConfig {
1253: private boolean canHandle;
1254: private Object returnValue;
1255:
1256: public TestExceptionHandlerConfig(boolean handle, Object value) {
1257: canHandle = handle;
1258: returnValue = value;
1259: }
1260:
1261: public void setCanHandle(boolean canHandle) {
1262: this .canHandle = canHandle;
1263: }
1264:
1265: public boolean canHandle(Exception exception) {
1266: return canHandle;
1267: }
1268:
1269: public Object handle(Exception exception,
1270: ActionMapping mapping, ActionForm form,
1271: HttpServletRequest request, HttpServletResponse response)
1272: throws Exception {
1273: return returnValue;
1274: }
1275: }
1276: }
|