KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > TestMockBase


1 /*
2  * $Id: TestMockBase.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.mock;
21
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.ModuleConfig;
32 import org.apache.struts.config.ControllerConfig;
33 import org.apache.struts.config.FormPropertyConfig;
34 import org.apache.struts.config.ForwardConfig;
35 import org.apache.struts.config.ModuleConfigFactory;
36
37
38
39 /**
40  * <p>Convenience base class for unit tests of the
41  * <code>org.apache.struts.util</code> package, and others that require
42  * a runtime environment similar to what the Struts controller servlet
43  * sets up. The <code>setUp()</code> method establishes
44  * a consistent basic environment for the various tests. The only
45  * tests included in this class are simple validations that the basic
46  * environment was set up correctly.</p>
47  *
48  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
49  */

50
51 public class TestMockBase extends TestCase {
52
53
54     // ----------------------------------------------------------------- Basics
55

56
57     public TestMockBase(String JavaDoc name) {
58         super(name);
59     }
60
61
62     public static void main(String JavaDoc args[]) {
63         junit.awtui.TestRunner.main
64             (new String JavaDoc[] { TestMockBase.class.getName() } );
65     }
66
67
68     public static Test suite() {
69         return (new TestSuite(TestMockBase.class));
70     }
71
72
73     // ----------------------------------------------------- Instance Variables
74

75
76     protected ModuleConfig moduleConfig = null;
77     protected ModuleConfig moduleConfig2 = null;
78     protected ModuleConfig moduleConfig3 = null;
79     protected MockServletConfig config = null;
80     protected MockServletContext context = null;
81     protected MockPageContext page = null;
82     protected MockPrincipal principal = null;
83     protected MockHttpServletRequest request = null;
84     protected MockHttpServletResponse response = null;
85     protected MockHttpSession session = null;
86
87
88     // ----------------------------------------------------- Setup and Teardown
89

90
91     public void setUp() {
92
93         // Set up the servlet API objects for a test scenario
94
context = new MockServletContext();
95         config = new MockServletConfig(context);
96         session = new MockHttpSession(context);
97         request = new MockHttpServletRequest(session);
98         principal = new MockPrincipal("username",
99                                       new String JavaDoc[] { "admin", "manager" });
100         request.setUserPrincipal(principal);
101         response = new MockHttpServletResponse();
102         page = new MockPageContext(config, request, response);
103
104         // Set up application configurations for our supported modules
105
setUpDefaultApp();
106         setUpSecondApp();
107         setUpThirdApp();
108
109         // NOTE - we do not initialize the request attribute
110
// for the selected module so that fallbacks to the
111
// default module can be tested. To select a module,
112
// tests should set the request attribute Globals.MODULE_KEY
113
// to the ModuleConfig instance for the selected module
114

115     }
116
117
118     protected void setUpDefaultApp() {
119
120         ActionFormBean formBean = null;
121         ActionMapping mapping = null;
122
123         ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
124         moduleConfig = factoryObject.createModuleConfig("");
125
126         context.setAttribute(Globals.MODULE_KEY, moduleConfig);
127
128         // Forward "external" to "http://jakarta.apache.org/"
129
moduleConfig.addForwardConfig
130             (new ActionForward("external", "http://jakarta.apache.org/",
131                                false, false));
132
133         // Forward "foo" to "/bar.jsp"
134
moduleConfig.addForwardConfig
135             (new ActionForward("foo", "/bar.jsp", false, false));
136
137         // Forward "relative1" to "relative.jsp" non-context-relative
138
moduleConfig.addForwardConfig
139             (new ActionForward("relative1", "relative.jsp", false, false));
140
141         // Forward "relative2" to "relative.jsp" context-relative
142
moduleConfig.addForwardConfig
143             (new ActionForward("relative2", "relative.jsp", false, true));
144
145         // Form Bean "static" is a standard ActionForm subclass
146
formBean = new ActionFormBean
147             ("static",
148              "org.apache.struts.mock.MockFormBean");
149         moduleConfig.addFormBeanConfig(formBean);
150
151         // Action "/static" uses the "static" form bean in request scope
152
mapping = new ActionMapping();
153         mapping.setInput("/static.jsp");
154         mapping.setName("static");
155         mapping.setPath("/static");
156         mapping.setScope("request");
157         mapping.setType("org.apache.struts.mock.MockAction");
158         moduleConfig.addActionConfig(mapping);
159
160         // Form Bean "dynamic" is a DynaActionForm with the same properties
161
formBean = new ActionFormBean
162             ("dynamic",
163              "org.apache.struts.action.DynaActionForm");
164         formBean.addFormPropertyConfig
165             (new FormPropertyConfig("booleanProperty", "boolean", "false"));
166         formBean.addFormPropertyConfig
167             (new FormPropertyConfig("stringProperty", "java.lang.String",
168                                     null));
169         moduleConfig.addFormBeanConfig(formBean);
170
171         // Action "/dynamic" uses the "dynamic" form bean in session scope
172
mapping = new ActionMapping();
173         mapping.setInput("/dynamic.jsp");
174         mapping.setName("dynamic");
175         mapping.setPath("/dynamic");
176         mapping.setScope("session");
177         mapping.setType("org.apache.struts.mock.MockAction");
178         moduleConfig.addActionConfig(mapping);
179
180         // Form Bean "/dynamic0" is a DynaActionForm with initializers
181
formBean = new ActionFormBean
182             ("dynamic0",
183              "org.apache.struts.action.DynaActionForm");
184         formBean.addFormPropertyConfig
185             (new FormPropertyConfig("booleanProperty", "boolean", "true"));
186         formBean.addFormPropertyConfig
187             (new FormPropertyConfig("stringProperty", "java.lang.String",
188                                     "String Property"));
189         formBean.addFormPropertyConfig
190             (new FormPropertyConfig("intArray1", "int[]",
191                                     "{1,2,3}", 4)); // 4 should be ignored
192
formBean.addFormPropertyConfig
193             (new FormPropertyConfig("intArray2", "int[]",
194                                     null, 5)); // 5 should be respected
195
formBean.addFormPropertyConfig
196             (new FormPropertyConfig("principal",
197                                     "org.apache.struts.mock.MockPrincipal",
198                                     null));
199         formBean.addFormPropertyConfig
200             (new FormPropertyConfig("stringArray1", "java.lang.String[]",
201                                     "{aaa,bbb,ccc}", 2)); // 2 should be ignored
202
formBean.addFormPropertyConfig
203             (new FormPropertyConfig("stringArray2", "java.lang.String[]",
204                                     null, 3)); // 3 should be respected
205
moduleConfig.addFormBeanConfig(formBean);
206
207         // Action "/dynamic0" uses the "dynamic0" form bean in request scope
208
mapping = new ActionMapping();
209         mapping.setName("dynamic0");
210         mapping.setPath("/dynamic0");
211         mapping.setScope("request");
212         mapping.setType("org.apache.struts.mock.MockAction");
213         moduleConfig.addActionConfig(mapping);
214
215         // Action "/noform" has no form bean associated with it
216
mapping = new ActionMapping();
217         mapping.setPath("/noform");
218         mapping.setType("org.apache.struts.mock.MockAction");
219         moduleConfig.addActionConfig(mapping);
220
221         // Configure global forward declarations
222
moduleConfig.addForwardConfig
223             (new ForwardConfig("moduleForward",
224                                "/module/forward",
225                                false, // No redirect
226
false)); // Not context relative
227

228         moduleConfig.addForwardConfig
229             (new ForwardConfig("moduleRedirect",
230                                "/module/redirect",
231                                true, // Redirect
232
false)); // Not context relative
233

234         moduleConfig.addForwardConfig
235             (new ForwardConfig("contextForward",
236                                "/context/forward",
237                                false, // No redirect
238
true)); // Context relative
239

240         moduleConfig.addForwardConfig
241             (new ForwardConfig("contextRedirect",
242                                "/context/redirect",
243                                true, // Redirect
244
true)); // Context relative
245

246         moduleConfig.addForwardConfig
247             (new ForwardConfig("moduleNoslash",
248                                "module/noslash",
249                                false, // No redirect
250
false)); // Not context relative
251

252         moduleConfig.addForwardConfig
253             (new ForwardConfig("contextNoslash",
254                                "context/noslash",
255                                false, // No redirect
256
true)); // Not context relative
257

258     }
259
260
261     protected void setUpSecondApp() {
262
263         ActionFormBean formBean = null;
264         ActionMapping mapping = null;
265
266
267         ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
268         moduleConfig2 = factoryObject.createModuleConfig("/2");
269
270         context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2);
271
272         // Forward "external" to "http://jakarta.apache.org/"
273
moduleConfig2.addForwardConfig
274             (new ActionForward("external", "http://jakarta.apache.org/",
275                                false, false));
276
277         // Forward "foo" to "/baz.jsp" (different from default)
278
moduleConfig2.addForwardConfig
279             (new ActionForward("foo", "/baz.jsp", false, false));
280
281         // Forward "relative1" to "relative.jsp" non-context-relative
282
moduleConfig2.addForwardConfig
283             (new ActionForward("relative1", "relative.jsp", false, false));
284
285         // Forward "relative2" to "relative.jsp" context-relative
286
moduleConfig2.addForwardConfig
287             (new ActionForward("relative2", "relative.jsp", false, true));
288
289         // Form Bean "static" is a standard ActionForm subclass (same as default)
290
formBean = new ActionFormBean
291             ("static",
292              "org.apache.struts.mock.MockFormBean");
293         moduleConfig2.addFormBeanConfig(formBean);
294
295         // Action "/static" uses the "static" form bean in request scope (same as default)
296
mapping = new ActionMapping();
297         mapping.setInput("/static.jsp");
298         mapping.setName("static");
299         mapping.setPath("/static");
300         mapping.setScope("request");
301         mapping.setType("org.apache.struts.mock.MockAction");
302         moduleConfig2.addActionConfig(mapping);
303
304         // Form Bean "dynamic2" is a DynaActionForm with the same properties
305
formBean = new ActionFormBean
306             ("dynamic2",
307              "org.apache.struts.action.DynaActionForm");
308         formBean.addFormPropertyConfig
309             (new FormPropertyConfig("booleanProperty", "boolean", "false"));
310         formBean.addFormPropertyConfig
311             (new FormPropertyConfig("stringProperty", "java.lang.String",
312                                     null));
313         moduleConfig2.addFormBeanConfig(formBean);
314
315         // Action "/dynamic2" uses the "dynamic2" form bean in session scope
316
mapping = new ActionMapping();
317         mapping.setInput("/dynamic2.jsp");
318         mapping.setName("dynamic2");
319         mapping.setPath("/dynamic2");
320         mapping.setScope("session");
321         mapping.setType("org.apache.struts.mock.MockAction");
322         moduleConfig2.addActionConfig(mapping);
323
324         // Action "/noform" has no form bean associated with it (same as default)
325
mapping = new ActionMapping();
326         mapping.setPath("/noform");
327         mapping.setType("org.apache.struts.mock.MockAction");
328         moduleConfig2.addActionConfig(mapping);
329
330         // Configure global forward declarations
331
moduleConfig2.addForwardConfig
332             (new ForwardConfig("moduleForward",
333                                "/module/forward",
334                                false, // No redirect
335
false)); // Not context relative
336

337         moduleConfig2.addForwardConfig
338             (new ForwardConfig("moduleRedirect",
339                                "/module/redirect",
340                                true, // Redirect
341
false)); // Not context relative
342

343         moduleConfig2.addForwardConfig
344             (new ForwardConfig("contextForward",
345                                "/context/forward",
346                                false, // No redirect
347
true)); // Context relative
348

349         moduleConfig2.addForwardConfig
350             (new ForwardConfig("contextRedirect",
351                                "/context/redirect",
352                                true, // Redirect
353
true)); // Context relative
354

355         moduleConfig2.addForwardConfig
356             (new ForwardConfig("moduleNoslash",
357                                "module/noslash",
358                                false, // No redirect
359
false)); // Not context relative
360

361         moduleConfig2.addForwardConfig
362             (new ForwardConfig("contextNoslash",
363                                "context/noslash",
364                                false, // No redirect
365
true)); // Not context relative
366

367     }
368
369
370     // Set up third app for testing URL mapping
371
protected void setUpThirdApp() {
372
373
374         ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
375         moduleConfig3 = factoryObject.createModuleConfig("/3");
376
377         context.setAttribute(Globals.MODULE_KEY + "/3", moduleConfig3);
378
379         // Instantiate the controller configuration for this app
380
ControllerConfig controller = new ControllerConfig();
381         moduleConfig3.setControllerConfig(controller);
382
383         // Configure the properties we will be testing
384
controller.setForwardPattern("/forwarding$M$P");
385         controller.setInputForward(true);
386         controller.setPagePattern("/paging$M$P");
387
388         // Configure global forward declarations
389
moduleConfig3.addForwardConfig
390             (new ForwardConfig("moduleForward",
391                                "/module/forward",
392                                false, // No redirect
393
false)); // Not context relative
394

395         moduleConfig3.addForwardConfig
396             (new ForwardConfig("moduleRedirect",
397                                "/module/redirect",
398                                true, // Redirect
399
false)); // Not context relative
400

401         moduleConfig3.addForwardConfig
402             (new ForwardConfig("contextForward",
403                                "/context/forward",
404                                false, // No redirect
405
true)); // Context relative
406

407         moduleConfig3.addForwardConfig
408             (new ForwardConfig("contextRedirect",
409                                "/context/redirect",
410                                true, // Redirect
411
true)); // Context relative
412

413         moduleConfig3.addForwardConfig
414             (new ForwardConfig("moduleNoslash",
415                                "module/noslash",
416                                false, // No redirect
417
false)); // Not context relative
418

419         moduleConfig3.addForwardConfig
420             (new ForwardConfig("contextNoslash",
421                                "context/noslash",
422                                false, // No redirect
423
true)); // Not context relative
424

425     }
426
427
428     public void tearDown() {
429
430         moduleConfig3 = null;
431         moduleConfig2 = null;
432         moduleConfig = null;
433         config = null;
434         context = null;
435         page = null;
436         principal = null;
437         request = null;
438         response = null;
439         session = null;
440
441     }
442
443
444     // ------------------------------------------------------- Individual Tests
445

446
447     // Test the basic setup of the mock object environment
448
public void testUtilBaseEnvironment() {
449
450         // Validate the servlet API objects and inter-relationships
451
assertNotNull("config is present", config);
452         assertNotNull("context is present", context);
453         assertNotNull("page is present", page);
454         assertNotNull("principal is present", principal);
455         assertNotNull("request is present", request);
456         assertNotNull("response is present", response);
457         assertNotNull("session is present", session);
458         assertEquals("page-->config",
459                      config, page.getServletConfig());
460         assertEquals("config-->context",
461                      context, config.getServletContext());
462         assertEquals("page-->context",
463                      context, page.getServletContext());
464         assertEquals("page-->request",
465                      request, page.getRequest());
466         assertEquals("page-->response",
467                      response, page.getResponse());
468         assertEquals("page-->session",
469                      session, page.getSession());
470         assertEquals("request-->principal",
471                      principal, request.getUserPrincipal());
472         assertEquals("request-->session",
473                      session, request.getSession());
474         assertEquals("session-->context",
475                      context, session.getServletContext());
476
477         // Validate the configuration for the default module
478
assertNotNull("moduleConfig is present", moduleConfig);
479         assertEquals("context-->moduleConfig",
480                      moduleConfig,
481                      context.getAttribute(Globals.MODULE_KEY));
482
483         // Validate the configuration for the second module
484
assertNotNull("moduleConfig2 is present", moduleConfig2);
485         assertEquals("context-->moduleConfig2",
486                      moduleConfig2,
487                      context.getAttribute(Globals.MODULE_KEY + "/2"));
488
489     }
490
491
492 }
493
Popular Tags