01: package com.mockrunner.example.struts;
02:
03: import org.apache.commons.validator.ValidatorResources;
04:
05: import com.mockrunner.struts.BasicActionTestCaseAdapter;
06: import com.mockrunner.struts.MapMessageResources;
07:
08: /**
09: * Example test for the {@link GreetingsAction}.
10: * Please note that we cache the <code>ValidatorResources</code>
11: * in a static field. You don't need to do this, but the parsing
12: * of the files before every test method will slow down your tests.
13: */
14: public class GreetingsActionTest extends BasicActionTestCaseAdapter {
15: private static ValidatorResources validatorRes = null;
16:
17: protected void setUp() throws Exception {
18: super .setUp();
19: if (null == validatorRes) {
20: String[] files = new String[2];
21: files[0] = "src/com/mockrunner/example/struts/validator-rules.xml";
22: files[1] = "src/com/mockrunner/example/struts/validation.xml";
23: validatorRes = createValidatorResources(files);
24: }
25: setValidatorResources(validatorRes);
26: MapMessageResources resources = new MapMessageResources();
27: resources
28: .putMessages("src/com/mockrunner/example/struts/Application.properties");
29: setResources(resources);
30: setValidate(true);
31: }
32:
33: public void testSuccesfulGreetings() {
34: getActionMockObjectFactory().getMockServletContext()
35: .setAttribute("counter", new Integer(0));
36: getActionMockObjectFactory().getMockActionMapping().setPath(
37: "/greetings");
38: addRequestParameter("name", "testname");
39: actionPerform(GreetingsAction.class,
40: GreetingsValidatorForm.class);
41: assertEquals("Hello testname, you are visitor 1",
42: getRequestAttribute("greetings"));
43: getActionMockObjectFactory().getMockServletContext()
44: .setAttribute("counter", new Integer(6));
45: verifyNoActionErrors();
46: actionPerform(GreetingsAction.class,
47: GreetingsValidatorForm.class);
48: assertEquals("Hello testname, you are visitor 7",
49: getRequestAttribute("greetings"));
50: verifyNoActionErrors();
51: verifyForward("success");
52: }
53:
54: public void testValidationError() {
55: getActionMockObjectFactory().getMockServletContext()
56: .setAttribute("counter", new Integer(0));
57: getActionMockObjectFactory().getMockActionMapping().setPath(
58: "/greetings");
59: actionPerform(GreetingsAction.class,
60: GreetingsValidatorForm.class);
61: verifyNumberActionErrors(1);
62: verifyActionErrorPresent("errors.required");
63: addRequestParameter("name", "y");
64: actionPerform(GreetingsAction.class,
65: GreetingsValidatorForm.class);
66: verifyNumberActionErrors(1);
67: verifyActionErrorPresent("errors.minlength");
68: }
69: }
|