View Javadoc

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