1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.mock;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.apache.struts.Globals;
25 import org.apache.struts.action.ActionFormBean;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28 import org.apache.struts.config.ControllerConfig;
29 import org.apache.struts.config.FormPropertyConfig;
30 import org.apache.struts.config.ForwardConfig;
31 import org.apache.struts.config.ModuleConfig;
32 import org.apache.struts.config.ModuleConfigFactory;
33
34 /***
35 * <p>Convenience base class for unit tests of the <code>org.apache.struts.util</code>
36 * package, and others that require a runtime environment similar to what the
37 * Struts controller servlet sets up. The <code>setUp()</code> method
38 * establishes a consistent basic environment for the various tests. The only
39 * tests included in this class are simple validations that the basic
40 * environment was set up correctly.</p>
41 *
42 * @version $Rev: 421119 $ $Date: 2005-08-14 17:24:39 -0400 (Sun, 14 Aug 2005)
43 * $
44 */
45 public class TestMockBase extends TestCase {
46
47 protected ModuleConfig moduleConfig = null;
48 protected ModuleConfig moduleConfig2 = null;
49 protected ModuleConfig moduleConfig3 = null;
50 protected MockServletConfig config = null;
51 protected MockServletContext context = null;
52 protected MockPageContext page = null;
53 protected MockPrincipal principal = null;
54 protected MockHttpServletRequest request = null;
55 protected MockHttpServletResponse response = null;
56 protected MockHttpSession session = null;
57
58
59 public TestMockBase(String name) {
60 super(name);
61 }
62
63 public static void main(String[] args) {
64 junit.awtui.TestRunner.main(new String[] { TestMockBase.class.getName() });
65 }
66
67 public static Test suite() {
68 return (new TestSuite(TestMockBase.class));
69 }
70
71
72 public void setUp() {
73
74 context = new MockServletContext();
75 config = new MockServletConfig(context);
76 session = new MockHttpSession(context);
77 request = new MockHttpServletRequest(session);
78 principal =
79 new MockPrincipal("username", new String[] { "admin", "manager" });
80 request.setUserPrincipal(principal);
81 response = new MockHttpServletResponse();
82 page = new MockPageContext(config, request, response);
83
84
85 setUpDefaultApp();
86 setUpSecondApp();
87 setUpThirdApp();
88
89
90
91
92
93
94 }
95
96 protected void setUpDefaultApp() {
97 ActionFormBean formBean = null;
98 ActionMapping mapping = null;
99
100 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
101
102 moduleConfig = factoryObject.createModuleConfig("");
103
104 context.setAttribute(Globals.MODULE_KEY, moduleConfig);
105
106
107 moduleConfig.addForwardConfig(new ActionForward("external",
108 "http://jakarta.apache.org/", false));
109
110
111 moduleConfig.addForwardConfig(new ActionForward("foo", "/bar.jsp", false));
112
113
114 moduleConfig.addForwardConfig(new ActionForward("relative1",
115 "relative.jsp", false));
116
117
118 moduleConfig.addForwardConfig(new ActionForward("relative2",
119 "relative.jsp", false));
120
121
122 formBean =
123 new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
124 moduleConfig.addFormBeanConfig(formBean);
125
126
127 mapping = new ActionMapping();
128 mapping.setInput("/static.jsp");
129 mapping.setName("static");
130 mapping.setPath("/static");
131 mapping.setScope("request");
132 mapping.setType("org.apache.struts.mock.MockAction");
133 moduleConfig.addActionConfig(mapping);
134
135
136 formBean =
137 new ActionFormBean("dynamic",
138 "org.apache.struts.action.DynaActionForm");
139 formBean.addFormPropertyConfig(new FormPropertyConfig(
140 "booleanProperty", "boolean", "false"));
141 formBean.addFormPropertyConfig(new FormPropertyConfig(
142 "stringProperty", "java.lang.String", null));
143 moduleConfig.addFormBeanConfig(formBean);
144
145
146 mapping = new ActionMapping();
147 mapping.setInput("/dynamic.jsp");
148 mapping.setName("dynamic");
149 mapping.setPath("/dynamic");
150 mapping.setScope("session");
151 mapping.setType("org.apache.struts.mock.MockAction");
152 moduleConfig.addActionConfig(mapping);
153
154
155 formBean =
156 new ActionFormBean("dynamic0",
157 "org.apache.struts.action.DynaActionForm");
158 formBean.addFormPropertyConfig(new FormPropertyConfig(
159 "booleanProperty", "boolean", "true"));
160 formBean.addFormPropertyConfig(new FormPropertyConfig(
161 "stringProperty", "java.lang.String", "String Property"));
162 formBean.addFormPropertyConfig(new FormPropertyConfig("intArray1",
163 "int[]", "{1,2,3}", 4));
164 formBean.addFormPropertyConfig(new FormPropertyConfig("intArray2",
165 "int[]", null, 5));
166 formBean.addFormPropertyConfig(new FormPropertyConfig("principal",
167 "org.apache.struts.mock.MockPrincipal", null));
168 formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray1",
169 "java.lang.String[]", "{aaa,bbb,ccc}", 2));
170 formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray2",
171 "java.lang.String[]", null, 3));
172 moduleConfig.addFormBeanConfig(formBean);
173
174
175 mapping = new ActionMapping();
176 mapping.setName("dynamic0");
177 mapping.setPath("/dynamic0");
178 mapping.setScope("request");
179 mapping.setType("org.apache.struts.mock.MockAction");
180 moduleConfig.addActionConfig(mapping);
181
182
183 mapping = new ActionMapping();
184 mapping.setPath("/noform");
185 mapping.setType("org.apache.struts.mock.MockAction");
186 moduleConfig.addActionConfig(mapping);
187
188
189 moduleConfig.addForwardConfig(new ForwardConfig("moduleForward",
190 "/module/forward", false));
191
192 moduleConfig.addForwardConfig(new ForwardConfig("moduleRedirect",
193 "/module/redirect", true));
194
195 moduleConfig.addForwardConfig(new ForwardConfig("contextForward",
196 "/forward", false,
197 "/context"));
198
199 moduleConfig.addForwardConfig(new ForwardConfig("contextRedirect",
200 "/redirect", true,
201 "/context"));
202
203 moduleConfig.addForwardConfig(new ForwardConfig("moduleNoslash",
204 "module/noslash", false));
205
206 moduleConfig.addForwardConfig(new ForwardConfig("contextNoslash",
207 "noslash", false,
208 "/context"));
209 }
210
211 protected void setUpSecondApp() {
212 ActionFormBean formBean = null;
213 ActionMapping mapping = null;
214
215 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
216
217 moduleConfig2 = factoryObject.createModuleConfig("/2");
218
219 context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2);
220
221
222 moduleConfig2.addForwardConfig(new ActionForward("external",
223 "http://jakarta.apache.org/", false));
224
225
226 moduleConfig2.addForwardConfig(new ActionForward("foo", "/baz.jsp",
227 false));
228
229
230 moduleConfig2.addForwardConfig(new ActionForward("relative1",
231 "relative.jsp", false));
232
233
234 moduleConfig2.addForwardConfig(new ActionForward("relative2",
235 "relative.jsp", false));
236
237
238 formBean =
239 new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
240 moduleConfig2.addFormBeanConfig(formBean);
241
242
243 mapping = new ActionMapping();
244 mapping.setInput("/static.jsp");
245 mapping.setName("static");
246 mapping.setPath("/static");
247 mapping.setScope("request");
248 mapping.setType("org.apache.struts.mock.MockAction");
249 moduleConfig2.addActionConfig(mapping);
250
251
252 formBean =
253 new ActionFormBean("dynamic2",
254 "org.apache.struts.action.DynaActionForm");
255 formBean.addFormPropertyConfig(new FormPropertyConfig(
256 "booleanProperty", "boolean", "false"));
257 formBean.addFormPropertyConfig(new FormPropertyConfig(
258 "stringProperty", "java.lang.String", null));
259 moduleConfig2.addFormBeanConfig(formBean);
260
261
262 mapping = new ActionMapping();
263 mapping.setInput("/dynamic2.jsp");
264 mapping.setName("dynamic2");
265 mapping.setPath("/dynamic2");
266 mapping.setScope("session");
267 mapping.setType("org.apache.struts.mock.MockAction");
268 moduleConfig2.addActionConfig(mapping);
269
270
271 mapping = new ActionMapping();
272 mapping.setPath("/noform");
273 mapping.setType("org.apache.struts.mock.MockAction");
274 moduleConfig2.addActionConfig(mapping);
275
276
277 moduleConfig2.addForwardConfig(new ForwardConfig("moduleForward",
278 "/module/forward", false));
279
280 moduleConfig2.addForwardConfig(new ForwardConfig("moduleRedirect",
281 "/module/redirect", true));
282
283 moduleConfig2.addForwardConfig(new ForwardConfig("contextForward",
284 "/forward", false,
285 "/context"));
286
287 moduleConfig2.addForwardConfig(new ForwardConfig("contextRedirect",
288 "/redirect", true,
289 "/context"));
290
291 moduleConfig2.addForwardConfig(new ForwardConfig("moduleNoslash",
292 "module/noslash", false));
293
294 moduleConfig2.addForwardConfig(new ForwardConfig("contextNoslash",
295 "noslash", false,
296 "/context"));
297 }
298
299
300 protected void setUpThirdApp() {
301 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
302
303 moduleConfig3 = factoryObject.createModuleConfig("/3");
304
305 context.setAttribute(Globals.MODULE_KEY + "/3", moduleConfig3);
306
307
308 ControllerConfig controller = new ControllerConfig();
309
310 moduleConfig3.setControllerConfig(controller);
311
312
313 controller.setForwardPattern("/forwarding$M$P");
314 controller.setInputForward(true);
315 controller.setPagePattern("/paging$M$P");
316
317
318 moduleConfig3.addForwardConfig(new ForwardConfig("moduleForward",
319 "/module/forward", false));
320
321 moduleConfig3.addForwardConfig(new ForwardConfig("moduleRedirect",
322 "/module/redirect", true));
323
324 moduleConfig3.addForwardConfig(new ForwardConfig("contextForward",
325 "/forward", false,
326 "/context"));
327
328 moduleConfig3.addForwardConfig(new ForwardConfig("contextRedirect",
329 "/redirect", true,
330 "/context"));
331
332 moduleConfig3.addForwardConfig(new ForwardConfig("moduleNoslash",
333 "module/noslash", false));
334
335 moduleConfig3.addForwardConfig(new ForwardConfig("contextNoslash",
336 "noslash", false,
337 "/context"));
338 }
339
340 public void tearDown() {
341 moduleConfig3 = null;
342 moduleConfig2 = null;
343 moduleConfig = null;
344 config = null;
345 context = null;
346 page = null;
347 principal = null;
348 request = null;
349 response = null;
350 session = null;
351 }
352
353
354
355 public void testUtilBaseEnvironment() {
356
357 assertNotNull("config is present", config);
358 assertNotNull("context is present", context);
359 assertNotNull("page is present", page);
360 assertNotNull("principal is present", principal);
361 assertNotNull("request is present", request);
362 assertNotNull("response is present", response);
363 assertNotNull("session is present", session);
364 assertEquals("page-->config", config, page.getServletConfig());
365 assertEquals("config-->context", context, config.getServletContext());
366 assertEquals("page-->context", context, page.getServletContext());
367 assertEquals("page-->request", request, page.getRequest());
368 assertEquals("page-->response", response, page.getResponse());
369 assertEquals("page-->session", session, page.getSession());
370 assertEquals("request-->principal", principal,
371 request.getUserPrincipal());
372 assertEquals("request-->session", session, request.getSession());
373 assertEquals("session-->context", context, session.getServletContext());
374
375
376 assertNotNull("moduleConfig is present", moduleConfig);
377 assertEquals("context-->moduleConfig", moduleConfig,
378 context.getAttribute(Globals.MODULE_KEY));
379
380
381 assertNotNull("moduleConfig2 is present", moduleConfig2);
382 assertEquals("context-->moduleConfig2", moduleConfig2,
383 context.getAttribute(Globals.MODULE_KEY + "/2"));
384 }
385 }