01: package com.mockrunner.example.struts;
02:
03: import com.mockrunner.struts.BasicActionTestCaseAdapter;
04:
05: /**
06: * Example test for the {@link AuthenticationAction}.
07: * Demonstrates most of the features of {@link com.mockrunner.struts.ActionTestModule}
08: * and {@link com.mockrunner.struts.BasicActionTestCaseAdapter}.
09: */
10: public class AuthenticationActionTest extends
11: BasicActionTestCaseAdapter {
12: private MockAuthenticationStrategy strategy;
13: private AuthenticationForm form;
14:
15: protected void setUp() throws Exception {
16: super .setUp();
17: strategy = new MockAuthenticationStrategy();
18: getActionMockObjectFactory().getMockServletContext()
19: .setAttribute(AuthenticationStrategy.class.getName(),
20: strategy);
21: form = (AuthenticationForm) createActionForm(AuthenticationForm.class);
22: setValidate(true);
23: }
24:
25: public void testSuccessfulLogin() {
26: addRequestParameter("username", "test");
27: addRequestParameter("password", "test");
28: strategy.setupLoginOk(true);
29: actionPerform(AuthenticationAction.class, form);
30: verifyNoActionErrors();
31: verifyNumberActionMessages(1);
32: verifyActionMessagePresent("auth.login.successful");
33: verifyForward("success");
34: }
35:
36: public void testEmptyInput() {
37: form.setUsername("");
38: form.setPassword("test");
39: strategy.setupLoginOk(true);
40: actionPerform(AuthenticationAction.class, form);
41: verifyNumberActionErrors(1);
42: verifyActionErrorPresent("field.value.missing");
43: verifyActionErrorValue("field.value.missing", "username");
44: form.setUsername("test");
45: form.setPassword("");
46: actionPerform(AuthenticationAction.class, form);
47: verifyNumberActionErrors(1);
48: verifyActionErrorPresent("field.value.missing");
49: verifyActionErrorValue("field.value.missing", "password");
50: strategy.setupLoginOk(false);
51: strategy.setupUserKnown(false);
52: actionPerform(AuthenticationAction.class, form);
53: verifyActionErrorNotPresent("auth.username.unknown");
54: }
55:
56: public void testUnknownUser() {
57: form.setUsername("test");
58: form.setPassword("test");
59: strategy.setupLoginOk(false);
60: strategy.setupUserKnown(false);
61: actionPerform(AuthenticationAction.class, form);
62: verifyNumberActionErrors(1);
63: verifyActionErrorPresent("auth.username.unknown");
64: verifyActionErrorValue("auth.username.unknown", "test");
65: verifyForward("failure");
66: }
67:
68: public void testWrongPassword() {
69: form.setUsername("test");
70: form.setPassword("test");
71: strategy.setupLoginOk(false);
72: strategy.setupUserKnown(true);
73: strategy.setupPasswordOk(false);
74: actionPerform(AuthenticationAction.class, form);
75: verifyNumberActionErrors(1);
76: verifyActionErrorPresent("auth.password.wrong");
77: verifyForward("failure");
78: }
79:
80: public void testGeneralError() {
81: form.setUsername("test");
82: form.setPassword("test");
83: strategy.setupLoginOk(false);
84: strategy.setupUserKnown(true);
85: strategy.setupPasswordOk(true);
86: actionPerform(AuthenticationAction.class, form);
87: verifyNumberActionErrors(1);
88: verifyActionErrorPresent("auth.general.error");
89: verifyForward("failure");
90: }
91: }
|