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