001: // StrutsTestCase - a JUnit extension for testing Struts actions
002: // within the context of the ActionServlet.
003: // Copyright (C) 2002 Deryl Seale
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the Apache Software License as
007: // published by the Apache Software Foundation; either version 1.1
008: // of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: // Apache Software Foundation Licens for more details.
014: // You may view the full text here: http://www.apache.org/LICENSE.txt
015:
016: package servletunit.struts.tests;
017:
018: import servletunit.struts.MockStrutsTestCase;
019: import junit.framework.AssertionFailedError;
020: import org.apache.struts.Globals;
021: import org.apache.struts.action.ActionErrors;
022: import org.apache.struts.action.ActionMessage;
023:
024: public class TestMessageAction extends MockStrutsTestCase {
025:
026: public TestMessageAction(String testName) {
027: super (testName);
028: }
029:
030: public void setUp() throws Exception {
031: super .setUp();
032: setServletConfigFile("/WEB-INF/web.xml");
033: }
034:
035: public void testNoMessages() {
036: addRequestParameter("username", "deryl");
037: addRequestParameter("password", "radar");
038: setRequestPathInfo("/login");
039: actionPerform();
040: verifyForward("success");
041: verifyForwardPath("/main/success.jsp");
042: assertEquals("deryl", getSession().getAttribute(
043: "authentication"));
044: verifyNoActionMessages();
045: }
046:
047: public void testMessageExists() {
048: setRequestPathInfo("test", "/testActionMessages");
049: actionPerform();
050: verifyForward("success");
051: verifyActionMessages(new String[] { "test.message" });
052: }
053:
054: public void testMessageExistsExpectedNone() {
055: setRequestPathInfo("test", "/testActionMessages");
056: actionPerform();
057: verifyForward("success");
058: try {
059: verifyNoActionMessages();
060: } catch (AssertionFailedError afe) {
061: return;
062: }
063: fail("Expected an AssertionFailedError!");
064: }
065:
066: public void testMessageMismatch() {
067: setRequestPathInfo("test", "/testActionMessages");
068: actionPerform();
069: verifyForward("success");
070: try {
071: verifyActionMessages(new String[] { "error.password.mismatch" });
072: } catch (AssertionFailedError afe) {
073: return;
074: }
075: fail("Expected an AssertionFailedError!");
076: }
077:
078: public void testExpectedMessagesNoneExist() {
079: addRequestParameter("username", "deryl");
080: addRequestParameter("password", "radar");
081: setRequestPathInfo("/login");
082: actionPerform();
083: verifyForward("success");
084: verifyForwardPath("/main/success.jsp");
085: assertEquals("deryl", getSession().getAttribute(
086: "authentication"));
087: try {
088: verifyActionMessages(new String[] { "test.message" });
089: } catch (AssertionFailedError afe) {
090: return;
091: }
092: fail("Expected AssertionFailedError!");
093: }
094:
095: public void testVerifiesComplexErrorMessageScenario() {
096: ActionErrors errors = new ActionErrors();
097: errors.add("error1", new ActionMessage("error1"));
098: errors.add("error2", new ActionMessage("error2"));
099: errors.add("error1", new ActionMessage("error1"));
100: getRequest().setAttribute(Globals.ERROR_KEY, errors);
101: try {
102: verifyActionErrors(new String[] { "error1", "error2",
103: "error2" });
104: } catch (AssertionFailedError ex) {
105: return;
106: }
107: fail("should not have passed!");
108: }
109:
110: public static void main(String[] args) {
111: junit.textui.TestRunner.run(TestMessageAction.class);
112: }
113:
114: }
|