KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > tests > TestMessageAction


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
// You may view the full text here: http://www.apache.org/LICENSE.txt
15

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 JavaDoc testName) {
28         super(testName);
29     }
30
31     public void setUp() throws Exception JavaDoc {
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 JavaDoc[] {"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 JavaDoc[] {"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 JavaDoc[] {"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 JavaDoc[] {"error1","error2","error2"});
102         } catch (AssertionFailedError ex) {
103             return;
104         }
105         fail("should not have passed!");
106     }
107
108     public static void main(String JavaDoc[] args) {
109         junit.textui.TestRunner.run(TestMessageAction.class);
110     }
111
112
113 }
Popular Tags