001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.ui;
006:
007: import com.opensymphony.webwork.TestAction;
008: import com.opensymphony.webwork.TestConfigurationProvider;
009: import com.opensymphony.webwork.WebWorkConstants;
010: import com.opensymphony.webwork.dispatcher.mapper.DefaultActionMapper;
011: import com.opensymphony.webwork.config.Configuration;
012: import com.opensymphony.webwork.views.jsp.AbstractUITagTest;
013: import com.opensymphony.webwork.views.jsp.ActionTag;
014: import com.opensymphony.xwork.ActionContext;
015: import com.opensymphony.xwork.ObjectFactory;
016: import com.opensymphony.xwork.ActionSupport;
017: import com.opensymphony.xwork.validator.ValidationInterceptor;
018: import com.opensymphony.xwork.config.ConfigurationManager;
019: import com.opensymphony.xwork.config.RuntimeConfiguration;
020: import com.opensymphony.xwork.config.entities.ActionConfig;
021: import com.opensymphony.xwork.config.entities.InterceptorMapping;
022:
023: import java.util.Collections;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.ArrayList;
027:
028: /**
029: * FormTagTest
030: *
031: * @author Jason Carreira
032: * Created Apr 3, 2003 10:28:58 AM
033: *
034: * @version $Date: 2007-06-02 11:47:26 +0200 (Sat, 02 Jun 2007) $ $Id: FormTagTest.java 2915 2007-06-02 09:47:26Z tm_jee $
035: */
036: public class FormTagTest extends AbstractUITagTest {
037:
038: public void testFormWithActionAttributeContainingBothActionAndMethod()
039: throws Exception {
040: TestAction testAction = (TestAction) action;
041:
042: FormTag tag = new FormTag();
043: tag.setPageContext(pageContext);
044: tag.setName("myForm");
045: tag.setMethod("POST");
046: tag.setAcceptcharset("UTF-8");
047: tag.setAction("testAction!myLittleMethod");
048: tag.setEnctype("myEncType");
049: tag.setTitle("mytitle");
050: tag.setOnsubmit("submitMe()");
051:
052: tag.doStartTag();
053: tag.doEndTag();
054:
055: verify(FormTag.class.getResource("Formtag-9.txt"));
056: }
057:
058: public void testFormWithActionAttributeContainingBothActionAndMethodAndNamespace()
059: throws Exception {
060: TestAction testAction = (TestAction) action;
061:
062: FormTag tag = new FormTag();
063: tag.setPageContext(pageContext);
064: tag.setName("myForm");
065: tag.setNamespace("/testNamespace");
066: tag.setMethod("POST");
067: tag.setAcceptcharset("UTF-8");
068: tag.setAction("testNamespaceAction!myLittleMethod");
069: tag.setEnctype("myEncType");
070: tag.setTitle("mytitle");
071: tag.setOnsubmit("submitMe()");
072:
073: tag.doStartTag();
074: tag.doEndTag();
075:
076: verify(FormTag.class.getResource("Formtag-10.txt"));
077: }
078:
079: public void testForm() throws Exception {
080: request.setupGetServletPath("/testAction");
081:
082: TestAction testAction = (TestAction) action;
083: testAction.setFoo("bar");
084:
085: FormTag tag = new FormTag();
086: tag.setPageContext(pageContext);
087: tag.setName("myForm");
088: tag.setMethod("POST");
089: tag.setAction("myAction");
090: tag.setEnctype("myEncType");
091: tag.setTitle("mytitle");
092: tag.setOnsubmit("submitMe()");
093:
094: tag.doStartTag();
095: tag.doEndTag();
096:
097: verify(FormTag.class.getResource("Formtag-1.txt"));
098: }
099:
100: /**
101: * This test with form tag validation enabled. Js validation script will appear
102: * cause action submited by the form is intercepted by validation interceptor which
103: * "include" all methods.
104: */
105: public void testFormWithCustomOnsubmitEnabledWithValidateEnabled1()
106: throws Exception {
107:
108: com.opensymphony.xwork.config.Configuration originalConfiguration = ConfigurationManager
109: .getConfiguration();
110: ObjectFactory originalObjectFactory = ObjectFactory
111: .getObjectFactory();
112:
113: try {
114: // used to determined if the form action needs js validation
115: ConfigurationManager
116: .setConfiguration(new com.opensymphony.xwork.config.impl.DefaultConfiguration() {
117: public RuntimeConfiguration getRuntimeConfiguration() {
118: return new RuntimeConfiguration() {
119: public ActionConfig getActionConfig(
120: String namespace, String name) {
121: ActionConfig actionConfig = new ActionConfig() {
122: public List getInterceptors() {
123: List interceptors = new ArrayList();
124:
125: ValidationInterceptor validationInterceptor = new ValidationInterceptor();
126: validationInterceptor
127: .setIncludeMethods("*");
128:
129: InterceptorMapping interceptorMapping = new InterceptorMapping();
130: interceptorMapping
131: .setName("validation");
132: interceptorMapping
133: .setInterceptor(validationInterceptor);
134: interceptors
135: .add(interceptorMapping);
136:
137: return interceptors;
138: }
139: };
140: return actionConfig;
141: }
142:
143: public Map getActionConfigs() {
144: return Collections.EMPTY_MAP;
145: }
146: };
147: }
148: });
149:
150: // used by form tag to get "actionClass" parameter
151: ObjectFactory.setObjectFactory(new ObjectFactory() {
152: public Class getClassInstance(String className)
153: throws ClassNotFoundException {
154: if (DefaultActionMapper.class.getName().equals(
155: className)) {
156: return DefaultActionMapper.class;
157: }
158: return ActionSupport.class;
159: }
160: });
161:
162: FormTag tag = new FormTag();
163: tag.setPageContext(pageContext);
164: tag.setName("myForm");
165: tag.setMethod("POST");
166: tag.setAction("myAction");
167: tag.setAcceptcharset("UTF-8");
168: tag.setEnctype("myEncType");
169: tag.setTitle("mytitle");
170: tag.setOnsubmit("submitMe()");
171: tag.setValidate("true");
172: tag.setNamespace("");
173:
174: UpDownSelectTag t = new UpDownSelectTag();
175: t.setPageContext(pageContext);
176: t.setName("myUpDownSelectTag");
177: t.setList("{}");
178:
179: tag.doStartTag();
180: t.doStartTag();
181: t.doEndTag();
182: tag.doEndTag();
183:
184: verify(FormTag.class.getResource("Formtag-2.txt"));
185: } finally {
186: ConfigurationManager
187: .setConfiguration(originalConfiguration);
188: ObjectFactory.setObjectFactory(originalObjectFactory);
189: }
190: }
191:
192: /**
193: * This test with form tag validation enabled. Js validation script will not appear
194: * cause action submited by the form is intercepted by validation interceptor which
195: * "excludes" all methods.
196: */
197: public void testFormWithCustomOnsubmitEnabledWithValidateEnabled2()
198: throws Exception {
199:
200: com.opensymphony.xwork.config.Configuration originalConfiguration = ConfigurationManager
201: .getConfiguration();
202: ObjectFactory originalObjectFactory = ObjectFactory
203: .getObjectFactory();
204:
205: try {
206: // used to determined if the form action needs js validation
207: ConfigurationManager
208: .setConfiguration(new com.opensymphony.xwork.config.impl.DefaultConfiguration() {
209: public RuntimeConfiguration getRuntimeConfiguration() {
210: return new RuntimeConfiguration() {
211: public ActionConfig getActionConfig(
212: String namespace, String name) {
213: ActionConfig actionConfig = new ActionConfig() {
214: public List getInterceptors() {
215: List interceptors = new ArrayList();
216:
217: ValidationInterceptor validationInterceptor = new ValidationInterceptor();
218: validationInterceptor
219: .setExcludeMethods("*");
220:
221: InterceptorMapping interceptorMapping = new InterceptorMapping();
222: interceptorMapping
223: .setName("validation");
224: interceptorMapping
225: .setInterceptor(validationInterceptor);
226: interceptors
227: .add(interceptorMapping);
228:
229: return interceptors;
230: }
231: };
232: return actionConfig;
233: }
234:
235: public Map getActionConfigs() {
236: return Collections.EMPTY_MAP;
237: }
238: };
239: }
240: });
241:
242: // used by form tag to get "actionClass" parameter
243: ObjectFactory.setObjectFactory(new ObjectFactory() {
244: public Class getClassInstance(String className)
245: throws ClassNotFoundException {
246: if (DefaultActionMapper.class.getName().equals(
247: className)) {
248: return DefaultActionMapper.class;
249: }
250: return ActionSupport.class;
251: }
252: });
253:
254: FormTag tag = new FormTag();
255: tag.setPageContext(pageContext);
256: tag.setName("myForm");
257: tag.setMethod("POST");
258: tag.setAction("myAction");
259: tag.setAcceptcharset("UTF-8");
260: tag.setEnctype("myEncType");
261: tag.setTitle("mytitle");
262: tag.setOnsubmit("submitMe()");
263: tag.setValidate("true");
264: tag.setNamespace("");
265:
266: UpDownSelectTag t = new UpDownSelectTag();
267: t.setPageContext(pageContext);
268: t.setName("myUpDownSelectTag");
269: t.setList("{}");
270:
271: tag.doStartTag();
272: t.doStartTag();
273: t.doEndTag();
274: tag.doEndTag();
275:
276: verify(FormTag.class.getResource("Formtag-11.txt"));
277: } finally {
278: ConfigurationManager
279: .setConfiguration(originalConfiguration);
280: ObjectFactory.setObjectFactory(originalObjectFactory);
281: }
282: }
283:
284: /**
285: * This test with form tag validation disabled.
286: */
287: public void testFormWithCustomOnsubmitEnabledWithValidateDisabled()
288: throws Exception {
289: FormTag tag = new FormTag();
290: tag.setPageContext(pageContext);
291: tag.setName("myForm");
292: tag.setMethod("POST");
293: tag.setAction("myAction");
294: tag.setEnctype("myEncType");
295: tag.setTitle("mytitle");
296: tag.setOnsubmit("submitMe()");
297: tag.setValidate("false");
298:
299: UpDownSelectTag t = new UpDownSelectTag();
300: t.setPageContext(pageContext);
301: t.setName("myUpDownSelectTag");
302: t.setList("{}");
303:
304: tag.doStartTag();
305: t.doStartTag();
306: t.doEndTag();
307: tag.doEndTag();
308:
309: verify(FormTag.class.getResource("Formtag-6.txt"));
310: }
311:
312: /**
313: * Testing that this: <p>
314: * <ww:form name="'myForm'" namespace="'/testNamespace'" action="'testNamespaceAction'" method="'POST'">
315: * <p/>
316: * doesn't create an action of "/testNamespace/testNamespaceAction.action" when the "webwork.action.extension"
317: * config property is set to "jspa".
318: */
319: public void testFormTagWithDifferentActionExtension()
320: throws Exception {
321: request
322: .setupGetServletPath("/testNamespace/testNamespaceAction");
323: String oldConfiguration = (String) Configuration
324: .get(WebWorkConstants.WEBWORK_ACTION_EXTENSION);
325: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
326: "jspa");
327:
328: FormTag tag = new FormTag();
329: tag.setPageContext(pageContext);
330: tag.setNamespace("/testNamespace");
331: tag.setAction("testNamespaceAction");
332: tag.setMethod("POST");
333: tag.setName("myForm");
334:
335: tag.doStartTag();
336: tag.doEndTag();
337:
338: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
339: oldConfiguration);
340:
341: verify(FormTag.class.getResource("Formtag-5.txt"));
342:
343: // set it back to the default
344: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
345: "action");
346: }
347:
348: /**
349: * Testing that this: <p>
350: * <ww:form name="'myForm'" action="'/testNamespace/testNamespaceAction.jspa'" method="'POST'">
351: * <p/>
352: * doesn't create an action of "/testNamespace/testNamespaceAction.action"
353: */
354: public void testFormTagWithDifferentActionExtensionHardcoded()
355: throws Exception {
356: request
357: .setupGetServletPath("/testNamespace/testNamespaceAction");
358:
359: FormTag tag = new FormTag();
360: tag.setPageContext(pageContext);
361: tag.setAction("/testNamespace/testNamespaceAction.jspa");
362: tag.setMethod("POST");
363: tag.setName("myForm");
364:
365: tag.doStartTag();
366: tag.doEndTag();
367:
368: verify(FormTag.class.getResource("Formtag-5.txt"));
369: }
370:
371: public void testFormWithNamespaceDefaulting() throws Exception {
372: request
373: .setupGetServletPath("/testNamespace/testNamespaceAction");
374:
375: TestAction testAction = (TestAction) action;
376: testAction.setFoo("bar");
377:
378: FormTag tag = new FormTag();
379: tag.setPageContext(pageContext);
380: tag.setName("myForm");
381: tag.setMethod("POST");
382: tag.setAction("testNamespaceAction");
383:
384: tag.doStartTag();
385: tag.doEndTag();
386:
387: verify(FormTag.class.getResource("Formtag-3.txt"));
388: }
389:
390: public void testFormTagForStackOverflowException1()
391: throws Exception {
392: request.setRequestURI("/requestUri");
393:
394: FormTag form1 = new FormTag();
395: form1.setPageContext(pageContext);
396: form1.doStartTag();
397:
398: assertEquals(form1.getComponent().getComponentStack().size(), 1);
399:
400: ActionTag tag = new ActionTag();
401: tag.setPageContext(pageContext);
402: tag.setName("testAction");
403: tag.doStartTag();
404:
405: assertEquals(tag.getComponent().getComponentStack().size(), 2);
406:
407: tag.doEndTag();
408:
409: assertEquals(form1.getComponent().getComponentStack().size(), 1);
410:
411: form1.doEndTag();
412:
413: assertNull(form1.getComponent()); // component is removed after end tag
414: }
415:
416: public void testFormTagForStackOverflowException2()
417: throws Exception {
418: request.setRequestURI("/requestUri");
419:
420: FormTag form1 = new FormTag();
421: form1.setPageContext(pageContext);
422: form1.doStartTag();
423:
424: assertEquals(form1.getComponent().getComponentStack().size(), 1);
425:
426: FormTag form2 = new FormTag();
427: form2.setPageContext(pageContext);
428: form2.doStartTag();
429:
430: assertEquals(form2.getComponent().getComponentStack().size(), 2);
431:
432: ActionTag tag = new ActionTag();
433: tag.setPageContext(pageContext);
434: tag.setName("testAction");
435: tag.doStartTag();
436:
437: assertEquals(tag.getComponent().getComponentStack().size(), 3);
438:
439: tag.doEndTag();
440:
441: assertEquals(form2.getComponent().getComponentStack().size(), 2);
442:
443: form2.doEndTag();
444:
445: assertEquals(form1.getComponent().getComponentStack().size(), 1);
446:
447: form1.doEndTag();
448:
449: assertNull(form1.getComponent()); // component is removed after end tag
450: }
451:
452: public void testFormTagForStackOverflowException3()
453: throws Exception {
454: request.setRequestURI("/requestUri");
455:
456: FormTag form1 = new FormTag();
457: form1.setPageContext(pageContext);
458: form1.doStartTag();
459:
460: assertEquals(form1.getComponent().getComponentStack().size(), 1);
461:
462: FormTag form2 = new FormTag();
463: form2.setPageContext(pageContext);
464: form2.doStartTag();
465:
466: assertEquals(form2.getComponent().getComponentStack().size(), 2);
467:
468: FormTag form3 = new FormTag();
469: form3.setPageContext(pageContext);
470: form3.doStartTag();
471:
472: assertEquals(form3.getComponent().getComponentStack().size(), 3);
473:
474: ActionTag tag = new ActionTag();
475: tag.setPageContext(pageContext);
476: tag.setName("testAction");
477: tag.doStartTag();
478:
479: assertEquals(tag.getComponent().getComponentStack().size(), 4);
480:
481: tag.doEndTag();
482:
483: assertEquals(form3.getComponent().getComponentStack().size(), 3);
484:
485: form3.doEndTag();
486:
487: assertEquals(form2.getComponent().getComponentStack().size(), 2);
488:
489: form2.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 testFormComponentIsRemoved() throws Exception {
499: request.setRequestURI("/requestUri");
500:
501: FormTag form = new FormTag();
502: form.setPageContext(pageContext);
503: form.doStartTag();
504:
505: assertEquals(form.getComponent().getComponentStack().size(), 1);
506:
507: form.doEndTag();
508:
509: assertNull(form.getComponent());
510: }
511:
512: public void testFormWithNoAction() throws Exception {
513: request.setupGetServletPath("/");
514: request.setupGetContextPath("/");
515: request.setRequestURI("/foo.jsp");
516:
517: FormTag tag = new FormTag();
518: tag.setPageContext(pageContext);
519: tag.doStartTag();
520: tag.doEndTag();
521:
522: verify(FormTag.class.getResource("Formtag-4.txt"));
523: }
524:
525: public void testFormWithStaticAction() throws Exception {
526: request.setupGetServletPath("/");
527: request.setupGetContextPath("/");
528: request.setRequestURI("/foo.jsp");
529:
530: FormTag tag = new FormTag();
531: tag.setPageContext(pageContext);
532: tag.setAction("test.html");
533: tag.doStartTag();
534: tag.doEndTag();
535:
536: verify(FormTag.class.getResource("Formtag-7.txt"));
537: }
538:
539: public void testFormWithActionAndExtension() throws Exception {
540: request.setupGetServletPath("/BLA");
541: String oldConfiguration = (String) Configuration
542: .get(WebWorkConstants.WEBWORK_ACTION_EXTENSION);
543: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
544: "jspa");
545:
546: FormTag tag = new FormTag();
547: tag.setPageContext(pageContext);
548: tag.setAction("/testNamespace/testNamespaceAction.jspa");
549: tag.setMethod("POST");
550: tag.setName("myForm");
551:
552: tag.doStartTag();
553: tag.doEndTag();
554: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
555: oldConfiguration);
556:
557: verify(FormTag.class.getResource("Formtag-8.txt"));
558:
559: // set it back to the default
560: Configuration.set(WebWorkConstants.WEBWORK_ACTION_EXTENSION,
561: "action");
562:
563: }
564:
565: protected void setUp() throws Exception {
566: super .setUp();
567: ConfigurationManager.clearConfigurationProviders();
568: ConfigurationManager
569: .addConfigurationProvider(new TestConfigurationProvider());
570: ActionContext.getContext().setValueStack(stack);
571: }
572: }
|