View Javadoc

1   /*
2    * $Id: FormTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.jsp.ui;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.struts2.StrutsConstants;
31  import org.apache.struts2.TestAction;
32  import org.apache.struts2.TestConfigurationProvider;
33  import org.apache.struts2.components.Form;
34  import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
35  import org.apache.struts2.views.jsp.AbstractUITagTest;
36  import org.apache.struts2.views.jsp.ActionTag;
37  
38  import com.opensymphony.xwork2.ActionContext;
39  import com.opensymphony.xwork2.ActionSupport;
40  import com.opensymphony.xwork2.ObjectFactory;
41  import com.opensymphony.xwork2.config.RuntimeConfiguration;
42  import com.opensymphony.xwork2.config.entities.ActionConfig;
43  import com.opensymphony.xwork2.config.entities.InterceptorMapping;
44  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
45  import com.opensymphony.xwork2.inject.Container;
46  import com.opensymphony.xwork2.inject.Scope.Strategy;
47  import com.opensymphony.xwork2.validator.ValidationInterceptor;
48  
49  
50  /***
51   * FormTagTest
52   */
53  public class FormTagTest extends AbstractUITagTest {
54  
55      public void testFormWithActionAttributeContainingBothActionAndMethod() throws Exception {
56          FormTag tag = new FormTag();
57          tag.setPageContext(pageContext);
58          tag.setName("myForm");
59          tag.setMethod("post");
60          tag.setAcceptcharset("UTF-8");
61          tag.setAction("testAction");
62          tag.setEnctype("myEncType");
63          tag.setTitle("mytitle");
64          tag.setOnsubmit("submitMe()");
65          tag.doStartTag();
66          tag.doEndTag();
67  
68          verify(FormTag.class.getResource("Formtag-9.txt"));
69      }
70  
71      public void testFormWithFocusElement() throws Exception {
72          FormTag tag = new FormTag();
73          tag.setTheme("xhtml");
74          tag.setPageContext(pageContext);
75          tag.setAction("testAction");
76          tag.setFocusElement("felement");
77          tag.doStartTag();
78          tag.doEndTag();
79  
80          verify(FormTag.class.getResource("Formtag-12.txt"));
81      }
82  
83      public void testFormWithActionAttributeContainingBothActionAndMethodAndNamespace() throws Exception {
84          FormTag tag = new FormTag();
85          tag.setPageContext(pageContext);
86          tag.setName("myForm");
87          tag.setNamespace("/testNamespace");
88          tag.setMethod("post");
89          tag.setAcceptcharset("UTF-8");
90          tag.setAction("testNamespaceAction");
91          tag.setEnctype("myEncType");
92          tag.setTitle("mytitle");
93          tag.setOnsubmit("submitMe()");
94  
95          tag.doStartTag();
96          tag.doEndTag();
97  
98          verify(FormTag.class.getResource("Formtag-10.txt"));
99      }
100 
101 
102     public void testForm() throws Exception {
103 
104         request.setupGetServletPath("/testAction");
105 
106         TestAction testAction = (TestAction) action;
107         testAction.setFoo("bar");
108 
109         FormTag tag = new FormTag();
110         tag.setPageContext(pageContext);
111         tag.setName("myForm");
112         tag.setMethod("post");
113         tag.setAcceptcharset("UTF-8");
114         tag.setAction("myAction");
115         tag.setEnctype("myEncType");
116         tag.setTitle("mytitle");
117         tag.setOnsubmit("submitMe()");
118 
119         tag.doStartTag();
120         tag.doEndTag();
121 
122         verify(FormTag.class.getResource("Formtag-1.txt"));
123     }
124 
125     /***
126      * This test with form tag validation enabled. Js validation script will appear
127      * cause action submited by the form is intercepted by validation interceptor which
128      * "include" all methods.
129      */
130     public void testFormWithCustomOnsubmitEnabledWithValidateEnabled1() throws Exception {
131 
132         final Container cont = container;
133         // used to determined if the form action needs js validation
134         configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() {
135             private DefaultConfiguration self = this;
136             public Container getContainer() {
137                 return new Container() {
138                     public <T> T inject(Class<T> implementation) {return null;}
139                     public void removeScopeStrategy() {}
140                     public void setScopeStrategy(Strategy scopeStrategy) {}
141                     public <T> T getInstance(Class<T> type, String name) {return null;}
142                     public <T> T getInstance(Class<T> type) {return null;}
143                     public Set<String> getInstanceNames(Class<?> type) {return null;}
144 
145                     public void inject(Object o) {
146                         cont.inject(o);
147                         if (o instanceof Form) {
148                             ((Form)o).setConfiguration(self);
149                         }
150                     }
151                 };
152             }
153             public RuntimeConfiguration getRuntimeConfiguration() {
154                 return new RuntimeConfiguration() {
155                     public ActionConfig getActionConfig(String namespace, String name) {
156                         ActionConfig actionConfig = new ActionConfig("", name, "") {
157                             public List getInterceptors() {
158                                 List interceptors = new ArrayList();
159 
160                                 ValidationInterceptor validationInterceptor = new ValidationInterceptor();
161                                 validationInterceptor.setIncludeMethods("*");
162 
163                                 InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor);
164                                 interceptors.add(interceptorMapping);
165 
166                                 return interceptors;
167                             }
168                             public String getClassName() {
169                                 return ActionSupport.class.getName();
170                             }
171                         };
172                         return actionConfig;
173                     }
174 
175                     public Map getActionConfigs() {
176                         return null;
177                     }
178                 };
179             }
180         });
181 
182         FormTag tag = new FormTag();
183         tag.setPageContext(pageContext);
184         tag.setName("myForm");
185         tag.setMethod("post");
186         tag.setAction("myAction");
187         tag.setAcceptcharset("UTF-8");
188         tag.setEnctype("myEncType");
189         tag.setTitle("mytitle");
190         tag.setOnsubmit("submitMe()");
191         tag.setValidate("true");
192         tag.setNamespace("");
193 
194         UpDownSelectTag t = new UpDownSelectTag();
195         t.setPageContext(pageContext);
196         t.setName("myUpDownSelectTag");
197         t.setList("{}");
198 
199         tag.doStartTag();
200         t.doStartTag();
201         t.doEndTag();
202         tag.doEndTag();
203 
204         verify(FormTag.class.getResource("Formtag-2.txt"));
205     }
206 
207 
208     /***
209      * This test with form tag validation enabled. Js validation script will not appear
210      * cause action submited by the form is intercepted by validation interceptor which
211      * "excludes" all methods.
212      */
213     public void testFormWithCustomOnsubmitEnabledWithValidateEnabled2() throws Exception {
214 
215         com.opensymphony.xwork2.config.Configuration originalConfiguration = configurationManager.getConfiguration();
216         ObjectFactory originalObjectFactory = ObjectFactory.getObjectFactory();
217 
218         final Container cont = container;
219         try {
220             // used to determined if the form action needs js validation
221             configurationManager.setConfiguration(new DefaultConfiguration() {
222                 private DefaultConfiguration self = this;
223                 public Container getContainer() {
224                     return new Container() {
225                         public <T> T inject(Class<T> implementation) {return null;}
226                         public void removeScopeStrategy() {}
227                         public void setScopeStrategy(Strategy scopeStrategy) {}
228                         public <T> T getInstance(Class<T> type, String name) {return null;}
229                         public <T> T getInstance(Class<T> type) {return null;}
230                         public Set<String> getInstanceNames(Class<?> type) {return null;}
231 
232                         public void inject(Object o) {
233                             cont.inject(o);
234                             if (o instanceof Form) {
235                                 ((Form)o).setConfiguration(self);
236                             }
237                         }
238                     };
239                 }
240                 public RuntimeConfiguration getRuntimeConfiguration() {
241                     return new RuntimeConfiguration() {
242                         public ActionConfig getActionConfig(String namespace, String name) {
243                             ActionConfig actionConfig = new ActionConfig("", name, "") {
244                                 public List getInterceptors() {
245                                     List interceptors = new ArrayList();
246 
247                                     ValidationInterceptor validationInterceptor = new ValidationInterceptor();
248                                     validationInterceptor.setExcludeMethods("*");
249 
250                                     InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor);
251                                     interceptors.add(interceptorMapping);
252 
253                                     return interceptors;
254                                 }
255                                 public String getClassName() {
256                                     return ActionSupport.class.getName();
257                                 }
258                             };
259                             return actionConfig;
260                         }
261 
262                         public Map getActionConfigs() {
263                             return null;
264                         }
265                     };
266                 }
267             });
268 
269             FormTag tag = new FormTag();
270             tag.setPageContext(pageContext);
271             tag.setName("myForm");
272             tag.setMethod("post");
273             tag.setAction("myAction");
274             tag.setAcceptcharset("UTF-8");
275             tag.setEnctype("myEncType");
276             tag.setTitle("mytitle");
277             tag.setOnsubmit("submitMe()");
278             tag.setValidate("true");
279             tag.setNamespace("");
280 
281             UpDownSelectTag t = new UpDownSelectTag();
282             t.setPageContext(pageContext);
283             t.setName("myUpDownSelectTag");
284             t.setList("{}");
285 
286             tag.doStartTag();
287             t.doStartTag();
288             t.doEndTag();
289             tag.doEndTag();
290 
291             verify(FormTag.class.getResource("Formtag-11.txt"));
292         }
293         finally {
294             configurationManager.setConfiguration(originalConfiguration);
295         }
296     }
297 
298     /***
299      * Tests the numbers are formatted correctly to not break the javascript
300      */
301     public void testFormWithCustomOnsubmitEnabledWithValidateEnabled3() throws Exception {
302 
303         final Container cont = container;
304         // used to determined if the form action needs js validation
305         configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() {
306             private DefaultConfiguration self = this;
307             public Container getContainer() {
308                 return new Container() {
309                     public <T> T inject(Class<T> implementation) {return null;}
310                     public void removeScopeStrategy() {}
311                     public void setScopeStrategy(Strategy scopeStrategy) {}
312                     public <T> T getInstance(Class<T> type, String name) {return null;}
313                     public <T> T getInstance(Class<T> type) {return null;}
314                     public Set<String> getInstanceNames(Class<?> type) {return null;}
315 
316                     public void inject(Object o) {
317                         cont.inject(o);
318                         if (o instanceof Form) {
319                             ((Form)o).setConfiguration(self);
320                         }
321                     }
322                 };
323             }
324             public RuntimeConfiguration getRuntimeConfiguration() {
325                 return new RuntimeConfiguration() {
326                     public ActionConfig getActionConfig(String namespace, String name) {
327                         ActionConfig actionConfig = new ActionConfig("", name, IntValidationAction.class.getName()) {
328                             public List getInterceptors() {
329                                 List interceptors = new ArrayList();
330 
331                                 ValidationInterceptor validationInterceptor = new ValidationInterceptor();
332                                 validationInterceptor.setIncludeMethods("*");
333 
334                                 InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor);
335                                 interceptors.add(interceptorMapping);
336 
337                                 return interceptors;
338                             }
339                             public String getClassName() {
340                                 return IntValidationAction.class.getName();
341                             }
342                         };
343                         return actionConfig;
344                     }
345 
346                     public Map getActionConfigs() {
347                         return null;
348                     }
349                 };
350             }
351         });
352 
353         FormTag tag = new FormTag();
354         tag.setPageContext(pageContext);
355         tag.setName("myForm");
356         tag.setMethod("post");
357         tag.setAction("myAction");
358         tag.setAcceptcharset("UTF-8");
359         tag.setEnctype("myEncType");
360         tag.setTitle("mytitle");
361         tag.setOnsubmit("submitMe()");
362         tag.setValidate("true");
363         tag.setNamespace("");
364 
365         UpDownSelectTag t = new UpDownSelectTag();
366         t.setPageContext(pageContext);
367         t.setName("myUpDownSelectTag");
368         t.setList("{}");
369 
370         tag.doStartTag();
371         tag.getComponent().getParameters().put("actionClass", IntValidationAction.class);
372         t.doStartTag();
373         t.doEndTag();
374         tag.doEndTag();
375 
376         verify(FormTag.class.getResource("Formtag-22.txt"));
377     }
378 
379     /***
380      * This test with form tag validation disabled.
381      */
382     public void testFormWithCustomOnsubmitEnabledWithValidateDisabled() throws Exception {
383         FormTag tag = new FormTag();
384         tag.setPageContext(pageContext);
385         tag.setName("myForm");
386         tag.setMethod("post");
387         tag.setAction("myAction");
388         tag.setEnctype("myEncType");
389         tag.setTitle("mytitle");
390         tag.setOnsubmit("submitMe()");
391         tag.setValidate("false");
392 
393         UpDownSelectTag t = new UpDownSelectTag();
394         t.setPageContext(pageContext);
395         t.setName("myUpDownSelectTag");
396         t.setList("{}");
397 
398         tag.doStartTag();
399         t.doStartTag();
400         t.doEndTag();
401         tag.doEndTag();
402 
403         verify(FormTag.class.getResource("Formtag-6.txt"));
404     }
405 
406 
407     /***
408      * Testing that this: <p>
409      * &lt;a:form name=&quot;'myForm'&quot; namespace=&quot;'/testNamespace'&quot; action=&quot;'testNamespaceAction'&quot; method=&quot;'post'&quot;&gt;
410      * <p/>
411      * doesn't create an action of &quot;/testNamespace/testNamespaceAction.action&quot; when the &quot;struts.action.extension&quot;
412      * config property is set to &quot;jspa&quot;.
413      */
414     public void testFormTagWithDifferentActionExtension() throws Exception {
415         initDispatcher(new HashMap<String,String>(){{ 
416             put(StrutsConstants.STRUTS_ACTION_EXTENSION, "jspa");
417             put("configProviders", TestConfigurationProvider.class.getName());
418         }});
419         request.setupGetServletPath("/testNamespace/testNamespaceAction");
420 
421         FormTag tag = new FormTag();
422         tag.setPageContext(pageContext);
423         tag.setNamespace("/testNamespace");
424         tag.setAction("testNamespaceAction");
425         tag.setMethod("post");
426         tag.setName("myForm");
427 
428         tag.doStartTag();
429         tag.doEndTag();
430 
431         verify(FormTag.class.getResource("Formtag-5.txt"));
432     }
433 
434     /***
435      * Testing that this: <p>
436      * &lt;a:form name=&quot;'myForm'&quot; action=&quot;'/testNamespace/testNamespaceAction.jspa'&quot; method=&quot;'post'&quot;&gt;
437      * <p/>
438      * doesn't create an action of &quot;/testNamespace/testNamespaceAction.action&quot;
439      */
440     public void testFormTagWithDifferentActionExtensionHardcoded() throws Exception {
441         request.setupGetServletPath("/testNamespace/testNamespaceAction");
442 
443         FormTag tag = new FormTag();
444         tag.setPageContext(pageContext);
445         tag.setAction("/testNamespace/testNamespaceAction.jspa");
446         tag.setMethod("post");
447         tag.setName("myForm");
448 
449         tag.doStartTag();
450         tag.doEndTag();
451 
452         verify(FormTag.class.getResource("Formtag-5.txt"));
453     }
454 
455     public void testFormWithNamespaceDefaulting() throws Exception {
456         request.setupGetServletPath("/testNamespace/testNamespaceAction");
457 
458         TestAction testAction = (TestAction) action;
459         testAction.setFoo("bar");
460 
461         FormTag tag = new FormTag();
462         tag.setPageContext(pageContext);
463         tag.setName("myForm");
464         tag.setMethod("post");
465         tag.setAction("testNamespaceAction");
466 
467         tag.doStartTag();
468         tag.doEndTag();
469 
470         verify(FormTag.class.getResource("Formtag-3.txt"));
471     }
472 
473     public void testFormTagForStackOverflowException1() throws Exception {
474         request.setRequestURI("/requestUri");
475 
476         FormTag form1 = new FormTag();
477         form1.setPageContext(pageContext);
478         form1.doStartTag();
479 
480         assertEquals(form1.getComponent().getComponentStack().size(), 1);
481 
482         ActionTag tag = new ActionTag();
483         tag.setPageContext(pageContext);
484         tag.setName("testAction");
485         tag.doStartTag();
486 
487         assertEquals(tag.getComponent().getComponentStack().size(), 2);
488 
489         tag.doEndTag();
490 
491         assertEquals(form1.getComponent().getComponentStack().size(), 1);
492 
493         form1.doEndTag();
494 
495         assertNull(form1.getComponent()); // component is removed after end tag
496     }
497 
498     public void testFormTagForStackOverflowException2() throws Exception {
499         request.setRequestURI("/requestUri");
500 
501         FormTag form1 = new FormTag();
502         form1.setPageContext(pageContext);
503         form1.doStartTag();
504 
505         assertEquals(form1.getComponent().getComponentStack().size(), 1);
506 
507         FormTag form2 = new FormTag();
508         form2.setPageContext(pageContext);
509         form2.doStartTag();
510 
511         assertEquals(form2.getComponent().getComponentStack().size(), 2);
512 
513         ActionTag tag = new ActionTag();
514         tag.setPageContext(pageContext);
515         tag.setName("testAction");
516         tag.doStartTag();
517 
518         assertEquals(tag.getComponent().getComponentStack().size(), 3);
519 
520         tag.doEndTag();
521 
522         assertEquals(form2.getComponent().getComponentStack().size(), 2);
523 
524         form2.doEndTag();
525 
526         assertEquals(form1.getComponent().getComponentStack().size(), 1);
527 
528         form1.doEndTag();
529 
530         assertNull(form1.getComponent()); // component is removed after end tag
531     }
532 
533 
534     public void testFormTagForStackOverflowException3() throws Exception {
535         request.setRequestURI("/requestUri");
536 
537         FormTag form1 = new FormTag();
538         form1.setPageContext(pageContext);
539         form1.doStartTag();
540 
541         assertEquals(form1.getComponent().getComponentStack().size(), 1);
542 
543         FormTag form2 = new FormTag();
544         form2.setPageContext(pageContext);
545         form2.doStartTag();
546 
547         assertEquals(form2.getComponent().getComponentStack().size(), 2);
548 
549         FormTag form3 = new FormTag();
550         form3.setPageContext(pageContext);
551         form3.doStartTag();
552 
553         assertEquals(form3.getComponent().getComponentStack().size(), 3);
554 
555         ActionTag tag = new ActionTag();
556         tag.setPageContext(pageContext);
557         tag.setName("testAction");
558         tag.doStartTag();
559 
560         assertEquals(tag.getComponent().getComponentStack().size(), 4);
561 
562         tag.doEndTag();
563 
564         assertEquals(form3.getComponent().getComponentStack().size(), 3);
565 
566         form3.doEndTag();
567 
568         assertEquals(form2.getComponent().getComponentStack().size(), 2);
569 
570         form2.doEndTag();
571 
572         assertEquals(form1.getComponent().getComponentStack().size(), 1);
573 
574         form1.doEndTag();
575 
576         assertNull(form1.getComponent()); // component is removed after end tag
577     }
578 
579 
580     public void testFormComponentIsRemoved() throws Exception {
581         request.setRequestURI("/requestUri");
582 
583         FormTag form = new FormTag();
584         form.setPageContext(pageContext);
585         form.doStartTag();
586 
587         assertEquals(form.getComponent().getComponentStack().size(), 1);
588 
589         form.doEndTag();
590 
591         assertNull(form.getComponent());
592     }
593 
594 
595     public void testFormWithNoAction() throws Exception {
596         request.setupGetServletPath("/");
597         request.setupGetContextPath("/");
598         request.setRequestURI("/foo.jsp");
599 
600         FormTag tag = new FormTag();
601         tag.setPageContext(pageContext);
602         tag.doStartTag();
603         tag.doEndTag();
604 
605         verify(FormTag.class.getResource("Formtag-4.txt"));
606     }
607 
608     public void testFormWithStaticAction() throws Exception {
609         request.setupGetServletPath("/");
610         request.setupGetContextPath("/");
611         request.setRequestURI("/foo.jsp");
612 
613         FormTag tag = new FormTag();
614         tag.setPageContext(pageContext);
615         tag.setAction("test.html");
616         tag.doStartTag();
617         tag.doEndTag();
618 
619         verify(FormTag.class.getResource("Formtag-7.txt"));
620     }
621 
622     public void testFormWithActionAndExtension() throws Exception {
623         request.setupGetServletPath("/BLA");
624         
625         FormTag tag = new FormTag();
626         tag.setPageContext(pageContext);
627         tag.setAction("/testNamespace/testNamespaceAction.jspa");
628         tag.setMethod("post");
629         tag.setName("myForm");
630 
631         tag.doStartTag();
632         tag.doEndTag();
633 
634         verify(FormTag.class.getResource("Formtag-8.txt"));
635 
636     }
637 
638     @Override
639     protected void setUp() throws Exception {
640         super.setUp();
641         initDispatcher(new HashMap<String,String>(){{ 
642             put("configProviders", TestConfigurationProvider.class.getName());
643         }});
644         ActionContext.getContext().setValueStack(stack);
645     }
646 }