View Javadoc

1   /*
2    * $Id: TestMockBase.java 421119 2006-07-12 04:49:11Z wsmoak $
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  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      // ----------------------------------------------------- Instance Variables
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      // ----------------------------------------------------------------- Basics
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      // ----------------------------------------------------- Setup and Teardown
72      public void setUp() {
73          // Set up the servlet API objects for a test scenario
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          // Set up application configurations for our supported modules
85          setUpDefaultApp();
86          setUpSecondApp();
87          setUpThirdApp();
88  
89          // NOTE - we do not initialize the request attribute
90          // for the selected module so that fallbacks to the
91          // default module can be tested.  To select a module,
92          // tests should set the request attribute Globals.MODULE_KEY
93          // to the ModuleConfig instance for the selected module
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         // Forward "external" to "http://jakarta.apache.org/"
107         moduleConfig.addForwardConfig(new ActionForward("external",
108                 "http://jakarta.apache.org/", false));
109 
110         // Forward "foo" to "/bar.jsp"
111         moduleConfig.addForwardConfig(new ActionForward("foo", "/bar.jsp", false));
112 
113         // Forward "relative1" to "relative.jsp" non-context-relative
114         moduleConfig.addForwardConfig(new ActionForward("relative1",
115                 "relative.jsp", false));
116 
117         // Forward "relative2" to "relative.jsp" context-relative
118         moduleConfig.addForwardConfig(new ActionForward("relative2",
119                 "relative.jsp", false));
120 
121         // Form Bean "static" is a standard ActionForm subclass
122         formBean =
123             new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
124         moduleConfig.addFormBeanConfig(formBean);
125 
126         // Action "/static" uses the "static" form bean in request scope
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         // Form Bean "dynamic" is a DynaActionForm with the same properties
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         // Action "/dynamic" uses the "dynamic" form bean in session scope
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         // Form Bean "/dynamic0" is a DynaActionForm with initializers
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)); // 4 should be ignored
164         formBean.addFormPropertyConfig(new FormPropertyConfig("intArray2",
165                 "int[]", null, 5)); // 5 should be respected
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)); // 2 should be ignored
170         formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray2",
171                 "java.lang.String[]", null, 3)); // 3 should be respected
172         moduleConfig.addFormBeanConfig(formBean);
173 
174         // Action "/dynamic0" uses the "dynamic0" form bean in request scope
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         // Action "/noform" has no form bean associated with it
183         mapping = new ActionMapping();
184         mapping.setPath("/noform");
185         mapping.setType("org.apache.struts.mock.MockAction");
186         moduleConfig.addActionConfig(mapping);
187 
188         // Configure global forward declarations
189         moduleConfig.addForwardConfig(new ForwardConfig("moduleForward",
190                 "/module/forward", false)); // No redirect, same module
191 
192         moduleConfig.addForwardConfig(new ForwardConfig("moduleRedirect",
193                 "/module/redirect", true)); // Redirect, same module
194 
195         moduleConfig.addForwardConfig(new ForwardConfig("contextForward",
196                 "/forward", false, // No redirect
197                 "/context")); // Specify module
198 
199         moduleConfig.addForwardConfig(new ForwardConfig("contextRedirect",
200                 "/redirect", true, // Redirect
201                 "/context")); // Specify module
202 
203         moduleConfig.addForwardConfig(new ForwardConfig("moduleNoslash",
204                 "module/noslash", false)); // No redirect, same module
205 
206         moduleConfig.addForwardConfig(new ForwardConfig("contextNoslash",
207                 "noslash", false, // No redirect
208                 "/context")); // Specify module
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         // Forward "external" to "http://jakarta.apache.org/"
222         moduleConfig2.addForwardConfig(new ActionForward("external",
223                 "http://jakarta.apache.org/", false));
224 
225         // Forward "foo" to "/baz.jsp" (different from default)
226         moduleConfig2.addForwardConfig(new ActionForward("foo", "/baz.jsp",
227                 false));
228 
229         // Forward "relative1" to "relative.jsp" non-context-relative
230         moduleConfig2.addForwardConfig(new ActionForward("relative1",
231                 "relative.jsp", false));
232 
233         // Forward "relative2" to "relative.jsp" context-relative
234         moduleConfig2.addForwardConfig(new ActionForward("relative2",
235                 "relative.jsp", false));
236 
237         // Form Bean "static" is a standard ActionForm subclass (same as default)
238         formBean =
239             new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
240         moduleConfig2.addFormBeanConfig(formBean);
241 
242         // Action "/static" uses the "static" form bean in request scope (same as default)
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         // Form Bean "dynamic2" is a DynaActionForm with the same properties
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         // Action "/dynamic2" uses the "dynamic2" form bean in session scope
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         // Action "/noform" has no form bean associated with it (same as default)
271         mapping = new ActionMapping();
272         mapping.setPath("/noform");
273         mapping.setType("org.apache.struts.mock.MockAction");
274         moduleConfig2.addActionConfig(mapping);
275 
276         // Configure global forward declarations
277         moduleConfig2.addForwardConfig(new ForwardConfig("moduleForward",
278                 "/module/forward", false)); // No redirect, same module
279 
280         moduleConfig2.addForwardConfig(new ForwardConfig("moduleRedirect",
281                 "/module/redirect", true)); // Redirect, same module
282 
283         moduleConfig2.addForwardConfig(new ForwardConfig("contextForward",
284                 "/forward", false, // No redirect
285                 "/context")); // Specify module
286 
287         moduleConfig2.addForwardConfig(new ForwardConfig("contextRedirect",
288                 "/redirect", true, // Redirect
289                 "/context")); // Specify module
290 
291         moduleConfig2.addForwardConfig(new ForwardConfig("moduleNoslash",
292                 "module/noslash", false)); // No redirect, same module
293 
294         moduleConfig2.addForwardConfig(new ForwardConfig("contextNoslash",
295                 "noslash", false, // No redirect
296                 "/context")); // Specify module
297     }
298 
299     // Set up third app for testing URL mapping
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         // Instantiate the controller configuration for this app
308         ControllerConfig controller = new ControllerConfig();
309 
310         moduleConfig3.setControllerConfig(controller);
311 
312         // Configure the properties we will be testing
313         controller.setForwardPattern("/forwarding$M$P");
314         controller.setInputForward(true);
315         controller.setPagePattern("/paging$M$P");
316 
317         // Configure global forward declarations
318         moduleConfig3.addForwardConfig(new ForwardConfig("moduleForward",
319                 "/module/forward", false)); // No redirect, same module
320 
321         moduleConfig3.addForwardConfig(new ForwardConfig("moduleRedirect",
322                 "/module/redirect", true)); // Redirect, same module
323 
324         moduleConfig3.addForwardConfig(new ForwardConfig("contextForward",
325                 "/forward", false, // No redirect
326                 "/context")); // Specify module
327 
328         moduleConfig3.addForwardConfig(new ForwardConfig("contextRedirect",
329                 "/redirect", true, // Redirect
330                 "/context")); // Specify module
331 
332         moduleConfig3.addForwardConfig(new ForwardConfig("moduleNoslash",
333                 "module/noslash", false)); // No redirect, same module
334 
335         moduleConfig3.addForwardConfig(new ForwardConfig("contextNoslash",
336                 "noslash", false, // No redirect
337                 "/context")); // specify module
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     // ------------------------------------------------------- Individual Tests
354     // Test the basic setup of the mock object environment
355     public void testUtilBaseEnvironment() {
356         // Validate the servlet API objects and inter-relationships
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         // Validate the configuration for the default module
376         assertNotNull("moduleConfig is present", moduleConfig);
377         assertEquals("context-->moduleConfig", moduleConfig,
378             context.getAttribute(Globals.MODULE_KEY));
379 
380         // Validate the configuration for the second module
381         assertNotNull("moduleConfig2 is present", moduleConfig2);
382         assertEquals("context-->moduleConfig2", moduleConfig2,
383             context.getAttribute(Globals.MODULE_KEY + "/2"));
384     }
385 }