View Javadoc

1   /*
2    * $Id: TestCopyFormToContext.java 376714 2006-02-10 14:50:57Z husted $
3    *
4    * Copyright 2005 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.chain.commands.generic;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.DynaActionForm;
24  import org.apache.struts.chain.contexts.MockActionContext;
25  import org.apache.struts.config.ActionConfig;
26  import org.apache.struts.config.FormBeanConfig;
27  import org.apache.struts.config.FormPropertyConfig;
28  import org.apache.struts.config.impl.ModuleConfigImpl;
29  import org.apache.struts.mock.MockFormBean;
30  
31  /***
32   * @version $Id: TestCopyFormToContext.java 161516 2005-04-15 19:22:47Z
33   *          germuska $
34   */
35  public class TestCopyFormToContext extends TestCase {
36      private static final String POST_EXECUTION_CONTEXT_KEY = "afterTest";
37      private MockActionContext context = null;
38  
39      public static void main(String[] args) {
40          junit.textui.TestRunner.run(TestCopyFormToContext.class);
41      }
42  
43      /*
44       * @see TestCase#setUp()
45       */
46      protected void setUp() throws Exception {
47          context = new MockActionContext();
48  
49          ModuleConfigImpl moduleConfig = new ModuleConfigImpl("/");
50  
51          context.setModuleConfig(moduleConfig);
52  
53          FormBeanConfig fooFBC = new FormBeanConfig();
54  
55          fooFBC.setName("foo");
56          fooFBC.setType("org.apache.struts.mock.MockFormBean");
57          moduleConfig.addFormBeanConfig(fooFBC);
58  
59          FormBeanConfig barFBC = new FormBeanConfig();
60  
61          barFBC.setName("bar");
62          barFBC.setType("org.apache.struts.action.DynaActionForm"); // use a different type so we can verify lookups better
63  
64          FormPropertyConfig fpc = new FormPropertyConfig();
65  
66          fpc.setName("property");
67          fpc.setType("java.lang.String");
68          fpc.setInitial("test");
69          barFBC.addFormPropertyConfig(fpc);
70          moduleConfig.addFormBeanConfig(barFBC);
71  
72          ActionConfig testActionConfig = new ActionConfig();
73  
74          testActionConfig.setPath("/Test");
75          testActionConfig.setName("foo");
76          testActionConfig.setScope("request");
77          moduleConfig.addActionConfig(testActionConfig);
78  
79          moduleConfig.freeze(); // otherwise, ActionConfigMatcher will be null and we'll get an NPE...
80      }
81  
82      public void testLookupByNameAndRequestScope()
83          throws Exception {
84          CopyFormToContext command = new CopyFormToContext();
85          String formName = "foo";
86  
87          command.setFormName(formName);
88          command.setScope("request");
89          command.setToKey(POST_EXECUTION_CONTEXT_KEY);
90  
91          assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
92          assertNull(context.getRequestScope().get(formName));
93          assertNull(context.getSessionScope().get(formName));
94  
95          command.execute(context);
96  
97          assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
98          assertNotNull(context.getRequestScope().get(formName));
99          assertNull(context.getSessionScope().get(formName));
100 
101         assertSame(context.get(POST_EXECUTION_CONTEXT_KEY),
102             context.getRequestScope().get(formName));
103 
104         ActionForm theForm =
105             (ActionForm) context.get(POST_EXECUTION_CONTEXT_KEY);
106 
107         assertTrue(theForm instanceof MockFormBean);
108     }
109 
110     public void testLookupByActionPath()
111         throws Exception {
112         CopyFormToContext command = new CopyFormToContext();
113 
114         command.setActionPath("/Test");
115         command.setToKey(POST_EXECUTION_CONTEXT_KEY);
116 
117         String formName = "foo"; // we know this, even though it's not being used for the lookup.
118 
119         assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
120         assertNull(context.getRequestScope().get(formName));
121         assertNull(context.getSessionScope().get(formName));
122 
123         command.execute(context);
124 
125         assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
126         assertNotNull(context.getRequestScope().get(formName));
127         assertNull(context.getSessionScope().get(formName));
128 
129         assertSame(context.get(POST_EXECUTION_CONTEXT_KEY),
130             context.getRequestScope().get(formName));
131 
132         ActionForm theForm =
133             (ActionForm) context.get(POST_EXECUTION_CONTEXT_KEY);
134 
135         assertTrue(theForm instanceof MockFormBean);
136     }
137 
138     public void testLookupByNameAndSessionScope()
139         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),
158             context.getSessionScope().get(formName));
159 
160         ActionForm theForm =
161             (ActionForm) context.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(
187                 "Execution should throw an exception when form name is not set.");
188         } catch (IllegalStateException e) {
189             ; // expected.
190         }
191     }
192 
193     public void testExceptionHandlingWithNullEverything()
194         throws Exception {
195         CopyFormToContext command = new CopyFormToContext();
196         String formName = "bar";
197 
198         // skip setting form name to test exception
199         // command.setFormName(formName);
200         // command.setScope("session");
201         // command.setToKey(POST_EXECUTION_CONTEXT_KEY);
202         assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
203         assertNull(context.getRequestScope().get(formName));
204         assertNull(context.getSessionScope().get(formName));
205 
206         try {
207             command.execute(context);
208             fail(
209                 "Execution should throw an exception when no properties are set.");
210         } catch (IllegalStateException e) {
211             ; // expected.
212         }
213     }
214 
215     public void testCopyToDefaultContextKey()
216         throws Exception {
217         CopyFormToContext command = new CopyFormToContext();
218         String formName = "foo";
219 
220         command.setFormName(formName);
221         command.setScope("request");
222 
223         assertNull(context.getActionForm());
224         assertNull(context.getRequestScope().get(POST_EXECUTION_CONTEXT_KEY));
225         assertNull(context.getSessionScope().get(POST_EXECUTION_CONTEXT_KEY));
226 
227         command.execute(context);
228 
229         assertNotNull(context.getActionForm());
230         assertNotNull(context.getRequestScope().get(formName));
231         assertNull(context.getSessionScope().get(formName));
232 
233         assertSame(context.getActionForm(),
234             context.getRequestScope().get(formName));
235 
236         ActionForm theForm = (ActionForm) context.getActionForm();
237 
238         assertTrue(theForm instanceof MockFormBean);
239     }
240 }