1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web;
24
25 import java.io.File;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionMapping;
32
33 import servletunit.struts.MockStrutsTestCase;
34
35
36
37
38
39 public class DynaActionTest extends MockStrutsTestCase {
40
41
42
43
44 protected void setUp() throws Exception {
45 super.setUp();
46 super.setContextDirectory(new File("res/test"));
47 super.setConfigFile("/WEB-INF/struts.xml");
48 super.setServletConfigFile("/WEB-INF/web.xml");
49 }
50
51
52
53
54
55
56 public void testExecuteMother() {
57 super.setRequestPathInfo("/command");
58 super.addRequestParameter("submit", "Mamma");
59 super.actionPerform();
60 super.verifyForward("mother");
61 super.assertEquals("mother", (String)super.getRequest().getAttribute("parent"));
62 super.verifyNoActionErrors();
63 super.setRequestPathInfo("/mother");
64 super.actionPerform();
65 super.verifyForward("mother");
66 super.assertEquals("mother", (String)super.getRequest().getAttribute("parent"));
67 super.verifyNoActionErrors();
68 }
69
70
71
72
73
74
75 public void testExecuteFather() {
76 super.setRequestPathInfo("/command");
77 super.addRequestParameter("submit", "PapĂ ");
78 super.actionPerform();
79 super.verifyForward("father");
80 super.assertEquals("father", (String)super.getRequest().getAttribute("parent"));
81 super.verifyNoActionErrors();
82 super.setRequestPathInfo("/father");
83 super.actionPerform();
84 super.verifyForward("father");
85 super.assertEquals("father", (String)super.getRequest().getAttribute("parent"));
86 super.verifyNoActionErrors();
87 }
88
89
90
91
92
93
94 public void testExecuteMissing() {
95 super.setRequestPathInfo("/command");
96 super.addRequestParameter("submit", "Fratello");
97 super.actionPerform();
98 super.verifyForward("error");
99 super.assertNull(super.getRequest().getAttribute("parent"));
100 super.verifyActionErrors(new String[] {"action.error.method.unknown"});
101 }
102
103
104
105
106
107
108 public void testForward() {
109 super.setRequestPathInfo("/forward");
110 super.actionPerform();
111 super.verifyForward("success");
112 super.verifyNoActionErrors();
113 }
114
115
116 public static class Stub extends DynaAction {
117
118 public String father(ActionForm form, HttpServletRequest request, HttpServletResponse response,
119 ActionMapping mapping) throws Exception {
120 request.setAttribute("parent", "father");
121 return "father";
122 }
123
124 public String mother(ActionForm form, HttpServletRequest request, HttpServletResponse response,
125 ActionMapping mapping) throws Exception {
126 request.setAttribute("parent", "mother");
127 return "mother";
128 }
129 }
130 }