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