View Javadoc

1   /*
2    * $Id: FormTagTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 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.struts2.views.jsp.ui;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.struts2.StrutsConstants;
25  import org.apache.struts2.TestAction;
26  import org.apache.struts2.TestConfigurationProvider;
27  import org.apache.struts2.config.Settings;
28  import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  import org.apache.struts2.views.jsp.ActionTag;
31  
32  import com.opensymphony.xwork2.ActionContext;
33  import com.opensymphony.xwork2.ActionSupport;
34  import com.opensymphony.xwork2.ObjectFactory;
35  import com.opensymphony.xwork2.config.RuntimeConfiguration;
36  import com.opensymphony.xwork2.config.entities.ActionConfig;
37  import com.opensymphony.xwork2.config.entities.InterceptorMapping;
38  import com.opensymphony.xwork2.validator.ValidationInterceptor;
39  
40  
41  /***
42   * FormTagTest
43   */
44  public class FormTagTest extends AbstractUITagTest {
45  
46      public void testFormWithActionAttributeContainingBothActionAndMethod() throws Exception {
47          FormTag tag = new FormTag();
48          tag.setPageContext(pageContext);
49          tag.setName("myForm");
50          tag.setMethod("POST");
51          tag.setAcceptcharset("UTF-8");
52          tag.setAction("testAction");
53          tag.setEnctype("myEncType");
54          tag.setTitle("mytitle");
55          tag.setOnsubmit("submitMe()");
56  
57          tag.doStartTag();
58          tag.doEndTag();
59  
60          verify(FormTag.class.getResource("Formtag-9.txt"));
61      }
62  
63  
64      public void testFormWithActionAttributeContainingBothActionAndMethodAndNamespace() throws Exception {
65          FormTag tag = new FormTag();
66          tag.setPageContext(pageContext);
67          tag.setName("myForm");
68          tag.setNamespace("/testNamespace");
69          tag.setMethod("POST");
70          tag.setAcceptcharset("UTF-8");
71          tag.setAction("testNamespaceAction");
72          tag.setEnctype("myEncType");
73          tag.setTitle("mytitle");
74          tag.setOnsubmit("submitMe()");
75  
76          tag.doStartTag();
77          tag.doEndTag();
78  
79          verify(FormTag.class.getResource("Formtag-10.txt"));
80      }
81  
82  
83      public void testForm() throws Exception {
84  
85          request.setupGetServletPath("/testAction");
86  
87          TestAction testAction = (TestAction) action;
88          testAction.setFoo("bar");
89  
90          FormTag tag = new FormTag();
91          tag.setPageContext(pageContext);
92          tag.setName("myForm");
93          tag.setMethod("POST");
94          tag.setAcceptcharset("UTF-8");
95          tag.setAction("myAction");
96          tag.setEnctype("myEncType");
97          tag.setTitle("mytitle");
98          tag.setOnsubmit("submitMe()");
99  
100         tag.doStartTag();
101         tag.doEndTag();
102 
103         verify(FormTag.class.getResource("Formtag-1.txt"));
104     }
105 
106     /***
107      * This test with form tag validation enabled. Js validation script will appear
108      * cause action submited by the form is intercepted by validation interceptor which
109      * "include" all methods.
110      */
111     public void testFormWithCustomOnsubmitEnabledWithValidateEnabled1() throws Exception {
112 
113         com.opensymphony.xwork2.config.Configuration originalConfiguration = configurationManager.getConfiguration();
114         ObjectFactory originalObjectFactory = ObjectFactory.getObjectFactory();
115 
116         try {
117             // used to determined if the form action needs js validation
118             configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() {
119                 public RuntimeConfiguration getRuntimeConfiguration() {
120                     return new RuntimeConfiguration() {
121                         public ActionConfig getActionConfig(String namespace, String name) {
122                             ActionConfig actionConfig = new ActionConfig() {
123                                 public List getInterceptors() {
124                                     List interceptors = new ArrayList();
125 
126                                     ValidationInterceptor validationInterceptor = new ValidationInterceptor();
127                                     validationInterceptor.setIncludeMethods("*");
128 
129                                     InterceptorMapping interceptorMapping = new InterceptorMapping();
130                                     interceptorMapping.setName("validation");
131                                     interceptorMapping.setInterceptor(validationInterceptor);
132                                     interceptors.add(interceptorMapping);
133 
134                                     return interceptors;
135                                 }
136                             };
137                             return actionConfig;
138                         }
139 
140                         public Map getActionConfigs() {
141                             return null;
142                         }
143                     };
144                 }
145             });
146 
147             // used by form tag to get "actionClass" parameter
148             ObjectFactory.setObjectFactory(new ObjectFactory() {
149                 public Class getClassInstance(String className) throws ClassNotFoundException {
150                     if (DefaultActionMapper.class.getName().equals(className)) {
151                         return DefaultActionMapper.class;
152                     }
153                     return ActionSupport.class;
154                 }
155             });
156 
157 
158             FormTag tag = new FormTag();
159             tag.setPageContext(pageContext);
160             tag.setName("myForm");
161             tag.setMethod("POST");
162             tag.setAction("myAction");
163             tag.setAcceptcharset("UTF-8");
164             tag.setEnctype("myEncType");
165             tag.setTitle("mytitle");
166             tag.setOnsubmit("submitMe()");
167             tag.setValidate("true");
168             tag.setNamespace("");
169 
170             UpDownSelectTag t = new UpDownSelectTag();
171             t.setPageContext(pageContext);
172             t.setName("myUpDownSelectTag");
173             t.setList("{}");
174 
175             tag.doStartTag();
176             t.doStartTag();
177             t.doEndTag();
178             tag.doEndTag();
179 
180             verify(FormTag.class.getResource("Formtag-2.txt"));
181         }
182         finally {
183             configurationManager.setConfiguration(originalConfiguration);
184             ObjectFactory.setObjectFactory(originalObjectFactory);
185         }
186     }
187 
188 
189     /***
190      * This test with form tag validation enabled. Js validation script will not appear
191      * cause action submited by the form is intercepted by validation interceptor which
192      * "excludes" all methods.
193      */
194     public void testFormWithCustomOnsubmitEnabledWithValidateEnabled2() throws Exception {
195 
196         com.opensymphony.xwork2.config.Configuration originalConfiguration = configurationManager.getConfiguration();
197         ObjectFactory originalObjectFactory = ObjectFactory.getObjectFactory();
198 
199         try {
200             // used to determined if the form action needs js validation
201             configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() {
202                 public RuntimeConfiguration getRuntimeConfiguration() {
203                     return new RuntimeConfiguration() {
204                         public ActionConfig getActionConfig(String namespace, String name) {
205                             ActionConfig actionConfig = new ActionConfig() {
206                                 public List getInterceptors() {
207                                     List interceptors = new ArrayList();
208 
209                                     ValidationInterceptor validationInterceptor = new ValidationInterceptor();
210                                     validationInterceptor.setExcludeMethods("*");
211 
212                                     InterceptorMapping interceptorMapping = new InterceptorMapping();
213                                     interceptorMapping.setName("validation");
214                                     interceptorMapping.setInterceptor(validationInterceptor);
215                                     interceptors.add(interceptorMapping);
216 
217                                     return interceptors;
218                                 }
219                             };
220                             return actionConfig;
221                         }
222 
223                         public Map getActionConfigs() {
224                             return null;
225                         }
226                     };
227                 }
228             });
229 
230             // used by form tag to get "actionClass" parameter
231             ObjectFactory.setObjectFactory(new ObjectFactory() {
232                 public Class getClassInstance(String className) throws ClassNotFoundException {
233                     if (DefaultActionMapper.class.getName().equals(className)) {
234                         return DefaultActionMapper.class;
235                     }
236                     return ActionSupport.class;
237                 }
238             });
239 
240 
241             FormTag tag = new FormTag();
242             tag.setPageContext(pageContext);
243             tag.setName("myForm");
244             tag.setMethod("POST");
245             tag.setAction("myAction");
246             tag.setAcceptcharset("UTF-8");
247             tag.setEnctype("myEncType");
248             tag.setTitle("mytitle");
249             tag.setOnsubmit("submitMe()");
250             tag.setValidate("true");
251             tag.setNamespace("");
252 
253             UpDownSelectTag t = new UpDownSelectTag();
254             t.setPageContext(pageContext);
255             t.setName("myUpDownSelectTag");
256             t.setList("{}");
257 
258             tag.doStartTag();
259             t.doStartTag();
260             t.doEndTag();
261             tag.doEndTag();
262 
263             verify(FormTag.class.getResource("Formtag-11.txt"));
264         }
265         finally {
266             configurationManager.setConfiguration(originalConfiguration);
267             ObjectFactory.setObjectFactory(originalObjectFactory);
268         }
269     }
270 
271     /***
272      * This test with form tag validation disabled.
273      */
274     public void testFormWithCustomOnsubmitEnabledWithValidateDisabled() throws Exception {
275         FormTag tag = new FormTag();
276         tag.setPageContext(pageContext);
277         tag.setName("myForm");
278         tag.setMethod("POST");
279         tag.setAction("myAction");
280         tag.setEnctype("myEncType");
281         tag.setTitle("mytitle");
282         tag.setOnsubmit("submitMe()");
283         tag.setValidate("false");
284 
285         UpDownSelectTag t = new UpDownSelectTag();
286         t.setPageContext(pageContext);
287         t.setName("myUpDownSelectTag");
288         t.setList("{}");
289 
290         tag.doStartTag();
291         t.doStartTag();
292         t.doEndTag();
293         tag.doEndTag();
294 
295         verify(FormTag.class.getResource("Formtag-6.txt"));
296     }
297 
298 
299     /***
300      * Testing that this: <p>
301      * &lt;a:form name=&quot;'myForm'&quot; namespace=&quot;'/testNamespace'&quot; action=&quot;'testNamespaceAction'&quot; method=&quot;'POST'&quot;&gt;
302      * <p/>
303      * doesn't create an action of &quot;/testNamespace/testNamespaceAction.action&quot; when the &quot;struts.action.extension&quot;
304      * config property is set to &quot;jspa&quot;.
305      */
306     public void testFormTagWithDifferentActionExtension() throws Exception {
307         request.setupGetServletPath("/testNamespace/testNamespaceAction");
308         String oldConfiguration = (String) Settings.get(StrutsConstants.STRUTS_ACTION_EXTENSION);
309         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, "jspa");
310 
311         FormTag tag = new FormTag();
312         tag.setPageContext(pageContext);
313         tag.setNamespace("/testNamespace");
314         tag.setAction("testNamespaceAction");
315         tag.setMethod("POST");
316         tag.setName("myForm");
317 
318         tag.doStartTag();
319         tag.doEndTag();
320 
321         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, oldConfiguration);
322 
323         verify(FormTag.class.getResource("Formtag-5.txt"));
324 
325         // set it back to the default
326         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, "action");
327     }
328 
329     /***
330      * Testing that this: <p>
331      * &lt;a:form name=&quot;'myForm'&quot; action=&quot;'/testNamespace/testNamespaceAction.jspa'&quot; method=&quot;'POST'&quot;&gt;
332      * <p/>
333      * doesn't create an action of &quot;/testNamespace/testNamespaceAction.action&quot;
334      */
335     public void testFormTagWithDifferentActionExtensionHardcoded() throws Exception {
336         request.setupGetServletPath("/testNamespace/testNamespaceAction");
337 
338         FormTag tag = new FormTag();
339         tag.setPageContext(pageContext);
340         tag.setAction("/testNamespace/testNamespaceAction.jspa");
341         tag.setMethod("POST");
342         tag.setName("myForm");
343 
344         tag.doStartTag();
345         tag.doEndTag();
346 
347         verify(FormTag.class.getResource("Formtag-5.txt"));
348     }
349 
350     public void testFormWithNamespaceDefaulting() throws Exception {
351         request.setupGetServletPath("/testNamespace/testNamespaceAction");
352 
353         TestAction testAction = (TestAction) action;
354         testAction.setFoo("bar");
355 
356         FormTag tag = new FormTag();
357         tag.setPageContext(pageContext);
358         tag.setName("myForm");
359         tag.setMethod("POST");
360         tag.setAction("testNamespaceAction");
361 
362         tag.doStartTag();
363         tag.doEndTag();
364 
365         verify(FormTag.class.getResource("Formtag-3.txt"));
366     }
367 
368     public void testFormTagForStackOverflowException1() throws Exception {
369         request.setRequestURI("/requestUri");
370 
371         FormTag form1 = new FormTag();
372         form1.setPageContext(pageContext);
373         form1.doStartTag();
374 
375         assertEquals(form1.getComponent().getComponentStack().size(), 1);
376 
377         ActionTag tag = new ActionTag();
378         tag.setPageContext(pageContext);
379         tag.setName("testAction");
380         tag.doStartTag();
381 
382         assertEquals(tag.getComponent().getComponentStack().size(), 2);
383 
384         tag.doEndTag();
385 
386         assertEquals(form1.getComponent().getComponentStack().size(), 1);
387 
388         form1.doEndTag();
389 
390         assertNull(form1.getComponent()); // component is removed after end tag
391     }
392 
393     public void testFormTagForStackOverflowException2() throws Exception {
394         request.setRequestURI("/requestUri");
395 
396         FormTag form1 = new FormTag();
397         form1.setPageContext(pageContext);
398         form1.doStartTag();
399 
400         assertEquals(form1.getComponent().getComponentStack().size(), 1);
401 
402         FormTag form2 = new FormTag();
403         form2.setPageContext(pageContext);
404         form2.doStartTag();
405 
406         assertEquals(form2.getComponent().getComponentStack().size(), 2);
407 
408         ActionTag tag = new ActionTag();
409         tag.setPageContext(pageContext);
410         tag.setName("testAction");
411         tag.doStartTag();
412 
413         assertEquals(tag.getComponent().getComponentStack().size(), 3);
414 
415         tag.doEndTag();
416 
417         assertEquals(form2.getComponent().getComponentStack().size(), 2);
418 
419         form2.doEndTag();
420 
421         assertEquals(form1.getComponent().getComponentStack().size(), 1);
422 
423         form1.doEndTag();
424 
425         assertNull(form1.getComponent()); // component is removed after end tag
426     }
427 
428 
429     public void testFormTagForStackOverflowException3() throws Exception {
430         request.setRequestURI("/requestUri");
431 
432         FormTag form1 = new FormTag();
433         form1.setPageContext(pageContext);
434         form1.doStartTag();
435 
436         assertEquals(form1.getComponent().getComponentStack().size(), 1);
437 
438         FormTag form2 = new FormTag();
439         form2.setPageContext(pageContext);
440         form2.doStartTag();
441 
442         assertEquals(form2.getComponent().getComponentStack().size(), 2);
443 
444         FormTag form3 = new FormTag();
445         form3.setPageContext(pageContext);
446         form3.doStartTag();
447 
448         assertEquals(form3.getComponent().getComponentStack().size(), 3);
449 
450         ActionTag tag = new ActionTag();
451         tag.setPageContext(pageContext);
452         tag.setName("testAction");
453         tag.doStartTag();
454 
455         assertEquals(tag.getComponent().getComponentStack().size(), 4);
456 
457         tag.doEndTag();
458 
459         assertEquals(form3.getComponent().getComponentStack().size(), 3);
460 
461         form3.doEndTag();
462 
463         assertEquals(form2.getComponent().getComponentStack().size(), 2);
464 
465         form2.doEndTag();
466 
467         assertEquals(form1.getComponent().getComponentStack().size(), 1);
468 
469         form1.doEndTag();
470 
471         assertNull(form1.getComponent()); // component is removed after end tag
472     }
473 
474 
475     public void testFormComponentIsRemoved() throws Exception {
476         request.setRequestURI("/requestUri");
477 
478         FormTag form = new FormTag();
479         form.setPageContext(pageContext);
480         form.doStartTag();
481 
482         assertEquals(form.getComponent().getComponentStack().size(), 1);
483 
484         form.doEndTag();
485 
486         assertNull(form.getComponent());
487     }
488 
489 
490     public void testFormWithNoAction() throws Exception {
491         request.setupGetServletPath("/");
492         request.setupGetContextPath("/");
493         request.setRequestURI("/foo.jsp");
494 
495         FormTag tag = new FormTag();
496         tag.setPageContext(pageContext);
497         tag.doStartTag();
498         tag.doEndTag();
499 
500         verify(FormTag.class.getResource("Formtag-4.txt"));
501     }
502 
503     public void testFormWithStaticAction() throws Exception {
504         request.setupGetServletPath("/");
505         request.setupGetContextPath("/");
506         request.setRequestURI("/foo.jsp");
507 
508         FormTag tag = new FormTag();
509         tag.setPageContext(pageContext);
510         tag.setAction("test.html");
511         tag.doStartTag();
512         tag.doEndTag();
513 
514         verify(FormTag.class.getResource("Formtag-7.txt"));
515     }
516 
517     public void testFormWithActionAndExtension() throws Exception {
518         request.setupGetServletPath("/BLA");
519         String oldConfiguration = (String) Settings.get(StrutsConstants.STRUTS_ACTION_EXTENSION);
520         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, "jspa");
521 
522         FormTag tag = new FormTag();
523         tag.setPageContext(pageContext);
524         tag.setAction("/testNamespace/testNamespaceAction.jspa");
525         tag.setMethod("POST");
526         tag.setName("myForm");
527 
528         tag.doStartTag();
529         tag.doEndTag();
530         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, oldConfiguration);
531 
532         verify(FormTag.class.getResource("Formtag-8.txt"));
533 
534         // set it back to the default
535         Settings.set(StrutsConstants.STRUTS_ACTION_EXTENSION, "action");
536 
537     }
538 
539     protected void setUp() throws Exception {
540         super.setUp();
541         configurationManager.clearConfigurationProviders();
542         configurationManager.addConfigurationProvider(new TestConfigurationProvider());
543         ActionContext.getContext().setValueStack(stack);
544     }
545 }