1 16 package servletunit.struts.tests; 17 18 import servletunit.struts.MockStrutsTestCase; 19 import junit.framework.AssertionFailedError; 20 import org.apache.struts.Globals; 21 import org.apache.struts.action.ActionErrors; 22 import org.apache.struts.action.ActionError; 23 import org.apache.struts.action.ActionMessage; 24 25 public class TestMessageAction extends MockStrutsTestCase { 26 27 public TestMessageAction(String testName) { 28 super(testName); 29 } 30 31 public void setUp() throws Exception { 32 super.setUp(); 33 setServletConfigFile("/WEB-INF/web.xml"); 34 } 35 36 public void testNoMessages() { 37 addRequestParameter("username","deryl"); 38 addRequestParameter("password","radar"); 39 setRequestPathInfo("/login"); 40 actionPerform(); 41 verifyForward("success"); 42 verifyForwardPath("/main/success.jsp"); 43 assertEquals("deryl",getSession().getAttribute("authentication")); 44 verifyNoActionMessages(); 45 } 46 47 public void testMessageExists() { 48 setRequestPathInfo("test","/testActionMessages"); 49 actionPerform(); 50 verifyForward("success"); 51 verifyActionMessages(new String [] {"test.message"}); 52 } 53 54 public void testMessageExistsExpectedNone() { 55 setRequestPathInfo("test","/testActionMessages"); 56 actionPerform(); 57 verifyForward("success"); 58 try { 59 verifyNoActionMessages(); 60 } catch (AssertionFailedError afe) { 61 return; 62 } 63 fail("Expected an AssertionFailedError!"); 64 } 65 66 public void testMessageMismatch() { 67 setRequestPathInfo("test","/testActionMessages"); 68 actionPerform(); 69 verifyForward("success"); 70 try { 71 verifyActionMessages(new String [] {"error.password.mismatch"}); 72 } catch (AssertionFailedError afe) { 73 return; 74 } 75 fail("Expected an AssertionFailedError!"); 76 } 77 78 public void testExpectedMessagesNoneExist() { 79 addRequestParameter("username","deryl"); 80 addRequestParameter("password","radar"); 81 setRequestPathInfo("/login"); 82 actionPerform(); 83 verifyForward("success"); 84 verifyForwardPath("/main/success.jsp"); 85 assertEquals("deryl",getSession().getAttribute("authentication")); 86 try { 87 verifyActionMessages(new String [] {"test.message"}); 88 } catch (AssertionFailedError afe) { 89 return; 90 } 91 fail("Expected AssertionFailedError!"); 92 } 93 94 public void testVerifiesComplexErrorMessageScenario() { 95 ActionErrors errors = new ActionErrors(); 96 errors.add("error1",new ActionError("error1")); 97 errors.add("error2",new ActionError("error2")); 98 errors.add("error1",new ActionError("error1")); 99 getRequest().setAttribute(Globals.ERROR_KEY,errors); 100 try { 101 verifyActionErrors(new String [] {"error1","error2","error2"}); 102 } catch (AssertionFailedError ex) { 103 return; 104 } 105 fail("should not have passed!"); 106 } 107 108 public static void main(String [] args) { 109 junit.textui.TestRunner.run(TestMessageAction.class); 110 } 111 112 113 } | Popular Tags |