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