001: /*
002: * $Id: TestCopyFormToContext.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.chain.commands.generic;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.DynaActionForm;
027: import org.apache.struts.chain.contexts.MockActionContext;
028: import org.apache.struts.config.ActionConfig;
029: import org.apache.struts.config.FormBeanConfig;
030: import org.apache.struts.config.FormPropertyConfig;
031: import org.apache.struts.config.impl.ModuleConfigImpl;
032: import org.apache.struts.mock.MockFormBean;
033:
034: /**
035: * @version $Id: TestCopyFormToContext.java 161516 2005-04-15 19:22:47Z
036: * germuska $
037: */
038: public class TestCopyFormToContext extends TestCase {
039: private static final String POST_EXECUTION_CONTEXT_KEY = "afterTest";
040: private MockActionContext context = null;
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(TestCopyFormToContext.class);
044: }
045:
046: /*
047: * @see TestCase#setUp()
048: */
049: protected void setUp() throws Exception {
050: context = new MockActionContext();
051:
052: ModuleConfigImpl moduleConfig = new ModuleConfigImpl("/");
053:
054: context.setModuleConfig(moduleConfig);
055:
056: FormBeanConfig fooFBC = new FormBeanConfig();
057:
058: fooFBC.setName("foo");
059: fooFBC.setType("org.apache.struts.mock.MockFormBean");
060: moduleConfig.addFormBeanConfig(fooFBC);
061:
062: FormBeanConfig barFBC = new FormBeanConfig();
063:
064: barFBC.setName("bar");
065: barFBC.setType("org.apache.struts.action.DynaActionForm"); // use a different type so we can verify lookups better
066:
067: FormPropertyConfig fpc = new FormPropertyConfig();
068:
069: fpc.setName("property");
070: fpc.setType("java.lang.String");
071: fpc.setInitial("test");
072: barFBC.addFormPropertyConfig(fpc);
073: moduleConfig.addFormBeanConfig(barFBC);
074:
075: ActionConfig testActionConfig = new ActionConfig();
076:
077: testActionConfig.setPath("/Test");
078: testActionConfig.setName("foo");
079: testActionConfig.setScope("request");
080: moduleConfig.addActionConfig(testActionConfig);
081:
082: moduleConfig.freeze(); // otherwise, ActionConfigMatcher will be null and we'll get an NPE...
083: }
084:
085: public void testLookupByNameAndRequestScope() throws Exception {
086: CopyFormToContext command = new CopyFormToContext();
087: String formName = "foo";
088:
089: command.setFormName(formName);
090: command.setScope("request");
091: command.setToKey(POST_EXECUTION_CONTEXT_KEY);
092:
093: assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
094: assertNull(context.getRequestScope().get(formName));
095: assertNull(context.getSessionScope().get(formName));
096:
097: command.execute(context);
098:
099: assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
100: assertNotNull(context.getRequestScope().get(formName));
101: assertNull(context.getSessionScope().get(formName));
102:
103: assertSame(context.get(POST_EXECUTION_CONTEXT_KEY), context
104: .getRequestScope().get(formName));
105:
106: ActionForm theForm = (ActionForm) context
107: .get(POST_EXECUTION_CONTEXT_KEY);
108:
109: assertTrue(theForm instanceof MockFormBean);
110: }
111:
112: public void testLookupByActionPath() throws Exception {
113: CopyFormToContext command = new CopyFormToContext();
114:
115: command.setActionPath("/Test");
116: command.setToKey(POST_EXECUTION_CONTEXT_KEY);
117:
118: String formName = "foo"; // we know this, even though it's not being used for the lookup.
119:
120: assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
121: assertNull(context.getRequestScope().get(formName));
122: assertNull(context.getSessionScope().get(formName));
123:
124: command.execute(context);
125:
126: assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
127: assertNotNull(context.getRequestScope().get(formName));
128: assertNull(context.getSessionScope().get(formName));
129:
130: assertSame(context.get(POST_EXECUTION_CONTEXT_KEY), context
131: .getRequestScope().get(formName));
132:
133: ActionForm theForm = (ActionForm) context
134: .get(POST_EXECUTION_CONTEXT_KEY);
135:
136: assertTrue(theForm instanceof MockFormBean);
137: }
138:
139: public void testLookupByNameAndSessionScope() throws Exception {
140: CopyFormToContext command = new CopyFormToContext();
141: String formName = "bar";
142:
143: command.setFormName(formName);
144: command.setScope("session");
145: command.setToKey(POST_EXECUTION_CONTEXT_KEY);
146:
147: assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
148: assertNull(context.getRequestScope().get(formName));
149: assertNull(context.getSessionScope().get(formName));
150:
151: command.execute(context);
152:
153: assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
154: assertNull(context.getRequestScope().get(formName));
155: assertNotNull(context.getSessionScope().get(formName));
156:
157: assertSame(context.get(POST_EXECUTION_CONTEXT_KEY), context
158: .getSessionScope().get(formName));
159:
160: ActionForm theForm = (ActionForm) context
161: .get(POST_EXECUTION_CONTEXT_KEY);
162:
163: assertTrue(theForm instanceof DynaActionForm);
164:
165: DynaActionForm dForm = (DynaActionForm) theForm;
166:
167: assertEquals("test", dForm.get("property"));
168: }
169:
170: public void testExceptionHandlingWithNullFormName()
171: throws Exception {
172: CopyFormToContext command = new CopyFormToContext();
173: String formName = "bar";
174:
175: // skip setting form name to test exception
176: // command.setFormName(formName);
177: command.setScope("session");
178: command.setToKey(POST_EXECUTION_CONTEXT_KEY);
179:
180: assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
181: assertNull(context.getRequestScope().get(formName));
182: assertNull(context.getSessionScope().get(formName));
183:
184: try {
185: command.execute(context);
186: fail("Execution should throw an exception when form name is not set.");
187: } catch (IllegalStateException e) {
188: ; // expected.
189: }
190: }
191:
192: public void testExceptionHandlingWithNullEverything()
193: throws Exception {
194: CopyFormToContext command = new CopyFormToContext();
195: String formName = "bar";
196:
197: // skip setting form name to test exception
198: // command.setFormName(formName);
199: // command.setScope("session");
200: // command.setToKey(POST_EXECUTION_CONTEXT_KEY);
201: assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
202: assertNull(context.getRequestScope().get(formName));
203: assertNull(context.getSessionScope().get(formName));
204:
205: try {
206: command.execute(context);
207: fail("Execution should throw an exception when no properties are set.");
208: } catch (IllegalStateException e) {
209: ; // expected.
210: }
211: }
212:
213: public void testCopyToDefaultContextKey() throws Exception {
214: CopyFormToContext command = new CopyFormToContext();
215: String formName = "foo";
216:
217: command.setFormName(formName);
218: command.setScope("request");
219:
220: assertNull(context.getActionForm());
221: assertNull(context.getRequestScope().get(
222: POST_EXECUTION_CONTEXT_KEY));
223: assertNull(context.getSessionScope().get(
224: POST_EXECUTION_CONTEXT_KEY));
225:
226: command.execute(context);
227:
228: assertNotNull(context.getActionForm());
229: assertNotNull(context.getRequestScope().get(formName));
230: assertNull(context.getSessionScope().get(formName));
231:
232: assertSame(context.getActionForm(), context.getRequestScope()
233: .get(formName));
234:
235: ActionForm theForm = (ActionForm) context.getActionForm();
236:
237: assertTrue(theForm instanceof MockFormBean);
238: }
239: }
|