001: /*
002: * $Id: TestMockBase.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.mock;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.apache.struts.Globals;
028: import org.apache.struts.action.ActionFormBean;
029: import org.apache.struts.action.ActionForward;
030: import org.apache.struts.action.ActionMapping;
031: import org.apache.struts.config.ControllerConfig;
032: import org.apache.struts.config.FormPropertyConfig;
033: import org.apache.struts.config.ForwardConfig;
034: import org.apache.struts.config.ModuleConfig;
035: import org.apache.struts.config.ModuleConfigFactory;
036:
037: /**
038: * <p>Convenience base class for unit tests of the <code>org.apache.struts.util</code>
039: * package, and others that require a runtime environment similar to what the
040: * Struts controller servlet sets up. The <code>setUp()</code> method
041: * establishes a consistent basic environment for the various tests. The only
042: * tests included in this class are simple validations that the basic
043: * environment was set up correctly.</p>
044: *
045: * @version $Rev: 471754 $ $Date: 2005-08-14 17:24:39 -0400 (Sun, 14 Aug 2005)
046: * $
047: */
048: public class TestMockBase extends TestCase {
049: // ----------------------------------------------------- Instance Variables
050: protected ModuleConfig moduleConfig = null;
051: protected ModuleConfig moduleConfig2 = null;
052: protected ModuleConfig moduleConfig3 = null;
053: protected MockServletConfig config = null;
054: protected MockServletContext context = null;
055: protected MockPageContext page = null;
056: protected MockPrincipal principal = null;
057: protected MockHttpServletRequest request = null;
058: protected MockHttpServletResponse response = null;
059: protected MockHttpSession session = null;
060:
061: // ----------------------------------------------------------------- Basics
062: public TestMockBase(String name) {
063: super (name);
064: }
065:
066: public static void main(String[] args) {
067: junit.awtui.TestRunner.main(new String[] { TestMockBase.class
068: .getName() });
069: }
070:
071: public static Test suite() {
072: return (new TestSuite(TestMockBase.class));
073: }
074:
075: // ----------------------------------------------------- Setup and Teardown
076: public void setUp() {
077: // Set up the servlet API objects for a test scenario
078: context = new MockServletContext();
079: config = new MockServletConfig(context);
080: session = new MockHttpSession(context);
081: request = new MockHttpServletRequest(session);
082: principal = new MockPrincipal("username", new String[] {
083: "admin", "manager" });
084: request.setUserPrincipal(principal);
085: response = new MockHttpServletResponse();
086: page = new MockPageContext(config, request, response);
087:
088: // Set up application configurations for our supported modules
089: setUpDefaultApp();
090: setUpSecondApp();
091: setUpThirdApp();
092:
093: // NOTE - we do not initialize the request attribute
094: // for the selected module so that fallbacks to the
095: // default module can be tested. To select a module,
096: // tests should set the request attribute Globals.MODULE_KEY
097: // to the ModuleConfig instance for the selected module
098: }
099:
100: protected void setUpDefaultApp() {
101: ActionFormBean formBean = null;
102: ActionMapping mapping = null;
103:
104: ModuleConfigFactory factoryObject = ModuleConfigFactory
105: .createFactory();
106:
107: moduleConfig = factoryObject.createModuleConfig("");
108:
109: context.setAttribute(Globals.MODULE_KEY, moduleConfig);
110:
111: // Forward "external" to "http://jakarta.apache.org/"
112: moduleConfig.addForwardConfig(new ActionForward("external",
113: "http://jakarta.apache.org/", false));
114:
115: // Forward "foo" to "/bar.jsp"
116: moduleConfig.addForwardConfig(new ActionForward("foo",
117: "/bar.jsp", false));
118:
119: // Forward "relative1" to "relative.jsp" non-context-relative
120: moduleConfig.addForwardConfig(new ActionForward("relative1",
121: "relative.jsp", false));
122:
123: // Forward "relative2" to "relative.jsp" context-relative
124: moduleConfig.addForwardConfig(new ActionForward("relative2",
125: "relative.jsp", false));
126:
127: // Form Bean "static" is a standard ActionForm subclass
128: formBean = new ActionFormBean("static",
129: "org.apache.struts.mock.MockFormBean");
130: moduleConfig.addFormBeanConfig(formBean);
131:
132: // Action "/static" uses the "static" form bean in request scope
133: mapping = new ActionMapping();
134: mapping.setInput("/static.jsp");
135: mapping.setName("static");
136: mapping.setPath("/static");
137: mapping.setScope("request");
138: mapping.setType("org.apache.struts.mock.MockAction");
139: moduleConfig.addActionConfig(mapping);
140:
141: // Form Bean "dynamic" is a DynaActionForm with the same properties
142: formBean = new ActionFormBean("dynamic",
143: "org.apache.struts.action.DynaActionForm");
144: formBean.addFormPropertyConfig(new FormPropertyConfig(
145: "booleanProperty", "boolean", "false"));
146: formBean.addFormPropertyConfig(new FormPropertyConfig(
147: "stringProperty", "java.lang.String", null));
148: moduleConfig.addFormBeanConfig(formBean);
149:
150: // Action "/dynamic" uses the "dynamic" form bean in session scope
151: mapping = new ActionMapping();
152: mapping.setInput("/dynamic.jsp");
153: mapping.setName("dynamic");
154: mapping.setPath("/dynamic");
155: mapping.setScope("session");
156: mapping.setType("org.apache.struts.mock.MockAction");
157: moduleConfig.addActionConfig(mapping);
158:
159: // Form Bean "/dynamic0" is a DynaActionForm with initializers
160: formBean = new ActionFormBean("dynamic0",
161: "org.apache.struts.action.DynaActionForm");
162: formBean.addFormPropertyConfig(new FormPropertyConfig(
163: "booleanProperty", "boolean", "true"));
164: formBean
165: .addFormPropertyConfig(new FormPropertyConfig(
166: "stringProperty", "java.lang.String",
167: "String Property"));
168: formBean.addFormPropertyConfig(new FormPropertyConfig(
169: "intArray1", "int[]", "{1,2,3}", 4)); // 4 should be ignored
170: formBean.addFormPropertyConfig(new FormPropertyConfig(
171: "intArray2", "int[]", null, 5)); // 5 should be respected
172: formBean.addFormPropertyConfig(new FormPropertyConfig(
173: "principal", "org.apache.struts.mock.MockPrincipal",
174: null));
175: formBean.addFormPropertyConfig(new FormPropertyConfig(
176: "stringArray1", "java.lang.String[]", "{aaa,bbb,ccc}",
177: 2)); // 2 should be ignored
178: formBean.addFormPropertyConfig(new FormPropertyConfig(
179: "stringArray2", "java.lang.String[]", null, 3)); // 3 should be respected
180: moduleConfig.addFormBeanConfig(formBean);
181:
182: // Action "/dynamic0" uses the "dynamic0" form bean in request scope
183: mapping = new ActionMapping();
184: mapping.setName("dynamic0");
185: mapping.setPath("/dynamic0");
186: mapping.setScope("request");
187: mapping.setType("org.apache.struts.mock.MockAction");
188: moduleConfig.addActionConfig(mapping);
189:
190: // Action "/noform" has no form bean associated with it
191: mapping = new ActionMapping();
192: mapping.setPath("/noform");
193: mapping.setType("org.apache.struts.mock.MockAction");
194: moduleConfig.addActionConfig(mapping);
195:
196: // Configure global forward declarations
197: moduleConfig.addForwardConfig(new ForwardConfig(
198: "moduleForward", "/module/forward", false)); // No redirect, same module
199:
200: moduleConfig.addForwardConfig(new ForwardConfig(
201: "moduleRedirect", "/module/redirect", true)); // Redirect, same module
202:
203: moduleConfig.addForwardConfig(new ForwardConfig(
204: "contextForward", "/forward", false, // No redirect
205: "/context")); // Specify module
206:
207: moduleConfig.addForwardConfig(new ForwardConfig(
208: "contextRedirect", "/redirect", true, // Redirect
209: "/context")); // Specify module
210:
211: moduleConfig.addForwardConfig(new ForwardConfig(
212: "moduleNoslash", "module/noslash", false)); // No redirect, same module
213:
214: moduleConfig.addForwardConfig(new ForwardConfig(
215: "contextNoslash", "noslash", false, // No redirect
216: "/context")); // Specify module
217: }
218:
219: protected void setUpSecondApp() {
220: ActionFormBean formBean = null;
221: ActionMapping mapping = null;
222:
223: ModuleConfigFactory factoryObject = ModuleConfigFactory
224: .createFactory();
225:
226: moduleConfig2 = factoryObject.createModuleConfig("/2");
227:
228: context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2);
229:
230: // Forward "external" to "http://jakarta.apache.org/"
231: moduleConfig2.addForwardConfig(new ActionForward("external",
232: "http://jakarta.apache.org/", false));
233:
234: // Forward "foo" to "/baz.jsp" (different from default)
235: moduleConfig2.addForwardConfig(new ActionForward("foo",
236: "/baz.jsp", false));
237:
238: // Forward "relative1" to "relative.jsp" non-context-relative
239: moduleConfig2.addForwardConfig(new ActionForward("relative1",
240: "relative.jsp", false));
241:
242: // Forward "relative2" to "relative.jsp" context-relative
243: moduleConfig2.addForwardConfig(new ActionForward("relative2",
244: "relative.jsp", false));
245:
246: // Form Bean "static" is a standard ActionForm subclass (same as default)
247: formBean = new ActionFormBean("static",
248: "org.apache.struts.mock.MockFormBean");
249: moduleConfig2.addFormBeanConfig(formBean);
250:
251: // Action "/static" uses the "static" form bean in request scope (same as default)
252: mapping = new ActionMapping();
253: mapping.setInput("/static.jsp");
254: mapping.setName("static");
255: mapping.setPath("/static");
256: mapping.setScope("request");
257: mapping.setType("org.apache.struts.mock.MockAction");
258: moduleConfig2.addActionConfig(mapping);
259:
260: // Form Bean "dynamic2" is a DynaActionForm with the same properties
261: formBean = new ActionFormBean("dynamic2",
262: "org.apache.struts.action.DynaActionForm");
263: formBean.addFormPropertyConfig(new FormPropertyConfig(
264: "booleanProperty", "boolean", "false"));
265: formBean.addFormPropertyConfig(new FormPropertyConfig(
266: "stringProperty", "java.lang.String", null));
267: moduleConfig2.addFormBeanConfig(formBean);
268:
269: // Action "/dynamic2" uses the "dynamic2" form bean in session scope
270: mapping = new ActionMapping();
271: mapping.setInput("/dynamic2.jsp");
272: mapping.setName("dynamic2");
273: mapping.setPath("/dynamic2");
274: mapping.setScope("session");
275: mapping.setType("org.apache.struts.mock.MockAction");
276: moduleConfig2.addActionConfig(mapping);
277:
278: // Action "/noform" has no form bean associated with it (same as default)
279: mapping = new ActionMapping();
280: mapping.setPath("/noform");
281: mapping.setType("org.apache.struts.mock.MockAction");
282: moduleConfig2.addActionConfig(mapping);
283:
284: // Configure global forward declarations
285: moduleConfig2.addForwardConfig(new ForwardConfig(
286: "moduleForward", "/module/forward", false)); // No redirect, same module
287:
288: moduleConfig2.addForwardConfig(new ForwardConfig(
289: "moduleRedirect", "/module/redirect", true)); // Redirect, same module
290:
291: moduleConfig2.addForwardConfig(new ForwardConfig(
292: "contextForward", "/forward", false, // No redirect
293: "/context")); // Specify module
294:
295: moduleConfig2.addForwardConfig(new ForwardConfig(
296: "contextRedirect", "/redirect", true, // Redirect
297: "/context")); // Specify module
298:
299: moduleConfig2.addForwardConfig(new ForwardConfig(
300: "moduleNoslash", "module/noslash", false)); // No redirect, same module
301:
302: moduleConfig2.addForwardConfig(new ForwardConfig(
303: "contextNoslash", "noslash", false, // No redirect
304: "/context")); // Specify module
305: }
306:
307: // Set up third app for testing URL mapping
308: protected void setUpThirdApp() {
309: ModuleConfigFactory factoryObject = ModuleConfigFactory
310: .createFactory();
311:
312: moduleConfig3 = factoryObject.createModuleConfig("/3");
313:
314: context.setAttribute(Globals.MODULE_KEY + "/3", moduleConfig3);
315:
316: // Instantiate the controller configuration for this app
317: ControllerConfig controller = new ControllerConfig();
318:
319: moduleConfig3.setControllerConfig(controller);
320:
321: // Configure the properties we will be testing
322: controller.setForwardPattern("/forwarding$M$P");
323: controller.setInputForward(true);
324: controller.setPagePattern("/paging$M$P");
325:
326: // Configure global forward declarations
327: moduleConfig3.addForwardConfig(new ForwardConfig(
328: "moduleForward", "/module/forward", false)); // No redirect, same module
329:
330: moduleConfig3.addForwardConfig(new ForwardConfig(
331: "moduleRedirect", "/module/redirect", true)); // Redirect, same module
332:
333: moduleConfig3.addForwardConfig(new ForwardConfig(
334: "contextForward", "/forward", false, // No redirect
335: "/context")); // Specify module
336:
337: moduleConfig3.addForwardConfig(new ForwardConfig(
338: "contextRedirect", "/redirect", true, // Redirect
339: "/context")); // Specify module
340:
341: moduleConfig3.addForwardConfig(new ForwardConfig(
342: "moduleNoslash", "module/noslash", false)); // No redirect, same module
343:
344: moduleConfig3.addForwardConfig(new ForwardConfig(
345: "contextNoslash", "noslash", false, // No redirect
346: "/context")); // specify module
347: }
348:
349: public void tearDown() {
350: moduleConfig3 = null;
351: moduleConfig2 = null;
352: moduleConfig = null;
353: config = null;
354: context = null;
355: page = null;
356: principal = null;
357: request = null;
358: response = null;
359: session = null;
360: }
361:
362: // ------------------------------------------------------- Individual Tests
363: // Test the basic setup of the mock object environment
364: public void testUtilBaseEnvironment() {
365: // Validate the servlet API objects and inter-relationships
366: assertNotNull("config is present", config);
367: assertNotNull("context is present", context);
368: assertNotNull("page is present", page);
369: assertNotNull("principal is present", principal);
370: assertNotNull("request is present", request);
371: assertNotNull("response is present", response);
372: assertNotNull("session is present", session);
373: assertEquals("page-->config", config, page.getServletConfig());
374: assertEquals("config-->context", context, config
375: .getServletContext());
376: assertEquals("page-->context", context, page
377: .getServletContext());
378: assertEquals("page-->request", request, page.getRequest());
379: assertEquals("page-->response", response, page.getResponse());
380: assertEquals("page-->session", session, page.getSession());
381: assertEquals("request-->principal", principal, request
382: .getUserPrincipal());
383: assertEquals("request-->session", session, request.getSession());
384: assertEquals("session-->context", context, session
385: .getServletContext());
386:
387: // Validate the configuration for the default module
388: assertNotNull("moduleConfig is present", moduleConfig);
389: assertEquals("context-->moduleConfig", moduleConfig, context
390: .getAttribute(Globals.MODULE_KEY));
391:
392: // Validate the configuration for the second module
393: assertNotNull("moduleConfig2 is present", moduleConfig2);
394: assertEquals("context-->moduleConfig2", moduleConfig2, context
395: .getAttribute(Globals.MODULE_KEY + "/2"));
396: }
397: }
|