View Javadoc

    1   /*
    2    * $Id: DefaultActionMapperTest.java 498085 2007-01-20 08:13:11Z 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.dispatcher.mapper;
    22
    23  import java.util.HashMap;
    24  import java.util.Map;
    25
    26  import org.apache.struts2.StrutsConstants;
    27  import org.apache.struts2.StrutsTestCase;
    28  import org.apache.struts2.ServletActionContext;
    29  import org.apache.struts2.dispatcher.ServletRedirectResult;
    30  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
    31  import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
    32
    33  import com.mockobjects.servlet.MockHttpServletRequest;
    34  import com.mockobjects.dynamic.Mock;
    35  import com.opensymphony.xwork2.Result;
    36  import com.opensymphony.xwork2.DefaultActionInvocation;
    37  import com.opensymphony.xwork2.ActionInvocation;
    38  import com.opensymphony.xwork2.ActionContext;
    39  import com.opensymphony.xwork2.config.Configuration;
    40  import com.opensymphony.xwork2.config.ConfigurationManager;
    41  import com.opensymphony.xwork2.config.entities.PackageConfig;
    42  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
    43
    44  import javax.servlet.http.HttpServletResponse;
    45
    46  /***
    47   * DefaultActionMapper test case.
    48   *
    49   */
    50  public class DefaultActionMapperTest extends StrutsTestCase {
    51
    52      private MockHttpServletRequest req;
    53      private ConfigurationManager configManager;
    54      private Configuration config;
    55
    56      protected void setUp() throws Exception {
    57          super.setUp();
    58          req = new MockHttpServletRequest();
    59          req.setupGetParameterMap(new HashMap());
    60          req.setupGetContextPath("/my/namespace");
    61
    62          config = new DefaultConfiguration();
    63          PackageConfig pkg = new PackageConfig("myns", "/my/namespace", false, null);
    64          PackageConfig pkg2 = new PackageConfig("my", "/my", false, null);
    65          config.addPackageConfig("mvns", pkg);
    66          config.addPackageConfig("my", pkg2);
    67          configManager = new ConfigurationManager() {
    68              public Configuration getConfiguration() {
    69                  return config;
    70              }
    71          };
    72      }
    73
    74      public void testGetMapping() throws Exception {
    75          setUp();
    76          req.setupGetRequestURI("/my/namespace/actionName.action");
    77          req.setupGetServletPath("/my/namespace/actionName.action");
    78          req.setupGetAttribute(null);
    79          req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    80
    81          DefaultActionMapper mapper = new DefaultActionMapper();
    82          ActionMapping mapping = mapper.getMapping(req, configManager);
    83
    84          assertEquals("/my/namespace", mapping.getNamespace());
    85          assertEquals("actionName", mapping.getName());
    86          assertNull(mapping.getMethod());
    87      }
    88
    89      public void testGetMappingWithMethod() throws Exception {
    90          req.setupGetParameterMap(new HashMap());
    91          req.setupGetRequestURI("/my/namespace/actionName!add.action");
    92          req.setupGetServletPath("/my/namespace/actionName!add.action");
    93          req.setupGetAttribute(null);
    94          req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    95
    96          DefaultActionMapper mapper = new DefaultActionMapper();
    97          ActionMapping mapping = mapper.getMapping(req, configManager);
    98
    99          assertEquals("/my/namespace", mapping.getNamespace());
    100         assertEquals("actionName", mapping.getName());
    101         assertEquals("add", mapping.getMethod());
    102     }
    103
    104     public void testGetMappingWithSlashedName() throws Exception {
    105
    106         req.setupGetRequestURI("/my/foo/actionName.action");
    107         req.setupGetServletPath("/my/foo/actionName.action");
    108         req.setupGetAttribute(null);
    109         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    110
    111         DefaultActionMapper mapper = new DefaultActionMapper();
    112         mapper.setSlashesInActionNames("true");
    113         ActionMapping mapping = mapper.getMapping(req, configManager);
    114
    115         assertEquals("/my", mapping.getNamespace());
    116         assertEquals("foo/actionName", mapping.getName());
    117         assertNull(mapping.getMethod());
    118     }
    119
    120     public void testGetMappingWithNamespaceSlash() throws Exception {
    121
    122         req.setupGetRequestURI("/my.hh/abc.action");
    123         req.setupGetServletPath("/my.hh/abc.action");
    124         req.setupGetAttribute(null);
    125         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    126
    127         DefaultActionMapper mapper = new DefaultActionMapper();
    128         ActionMapping mapping = mapper.getMapping(req, configManager);
    129
    130         assertEquals("", mapping.getNamespace());
    131         assertEquals("abc", mapping.getName());
    132
    133         req.setupGetAttribute(null);
    134         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    135         mapper = new DefaultActionMapper();
    136         mapper.setSlashesInActionNames("true");
    137         mapping = mapper.getMapping(req, configManager);
    138
    139         assertEquals("", mapping.getNamespace());
    140         assertEquals("my.hh/abc", mapping.getName());
    141     }
    142
    143     public void testGetMappingWithUnknownNamespace() throws Exception {
    144         setUp();
    145         req.setupGetRequestURI("/bo/foo/actionName.action");
    146         req.setupGetServletPath("/bo/foo/actionName.action");
    147         req.setupGetAttribute(null);
    148         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    149
    150         DefaultActionMapper mapper = new DefaultActionMapper();
    151         ActionMapping mapping = mapper.getMapping(req, configManager);
    152
    153         assertEquals("", mapping.getNamespace());
    154         assertEquals("actionName", mapping.getName());
    155         assertNull(mapping.getMethod());
    156     }
    157
    158     public void testGetMappingWithUnknownNamespaceButFullNamespaceSelect() throws Exception {
    159         setUp();
    160         req.setupGetRequestURI("/bo/foo/actionName.action");
    161         req.setupGetServletPath("/bo/foo/actionName.action");
    162         req.setupGetAttribute(null);
    163         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    164
    165         DefaultActionMapper mapper = new DefaultActionMapper();
    166         mapper.setAlwaysSelectFullNamespace("true");
    167         ActionMapping mapping = mapper.getMapping(req, configManager);
    168
    169         assertEquals("/bo/foo", mapping.getNamespace());
    170         assertEquals("actionName", mapping.getName());
    171         assertNull(mapping.getMethod());
    172     }
    173
    174     public void testGetUri() throws Exception {
    175         req.setupGetParameterMap(new HashMap());
    176         req.setupGetRequestURI("/my/namespace/actionName.action");
    177         req.setupGetServletPath("/my/namespace/actionName.action");
    178         req.setupGetAttribute(null);
    179         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    180
    181         DefaultActionMapper mapper = new DefaultActionMapper();
    182         ActionMapping mapping = mapper.getMapping(req, configManager);
    183         assertEquals("/my/namespace/actionName.action", mapper.getUriFromActionMapping(mapping));
    184     }
    185
    186     public void testGetUriWithMethod() throws Exception {
    187         req.setupGetParameterMap(new HashMap());
    188         req.setupGetRequestURI("/my/namespace/actionName!add.action");
    189         req.setupGetServletPath("/my/namespace/actionName!add.action");
    190         req.setupGetAttribute(null);
    191         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    192
    193         DefaultActionMapper mapper = new DefaultActionMapper();
    194         ActionMapping mapping = mapper.getMapping(req, configManager);
    195
    196         assertEquals("/my/namespace/actionName!add.action", mapper.getUriFromActionMapping(mapping));
    197     }
    198
    199     public void testGetMappingWithNoExtension() throws Exception {
    200         req.setupGetParameterMap(new HashMap());
    201         req.setupGetRequestURI("/my/namespace/actionName");
    202         req.setupGetServletPath("/my/namespace/actionName");
    203         req.setupGetAttribute(null);
    204         req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    205
    206         DefaultActionMapper mapper = new DefaultActionMapper();
    207         mapper.setExtensions("");
    208         ActionMapping mapping = mapper.getMapping(req, configManager);
    209
    210         assertEquals("/my/namespace", mapping.getNamespace());
    211         assertEquals("actionName", mapping.getName());
    212         assertNull(mapping.getMethod());
    213     }
    214
    215     // =============================
    216     // === test name & namespace ===
    217     // =============================
    218
    219     public void testParseNameAndNamespace1() throws Exception {
    220         ActionMapping actionMapping = new ActionMapping();
    221
    222         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    223         defaultActionMapper.parseNameAndNamespace("someAction", actionMapping, configManager);
    224
    225         assertEquals(actionMapping.getName(), "someAction");
    226         assertEquals(actionMapping.getNamespace(), "");
    227     }
    228
    229     public void testParseNameAndNamespace2() throws Exception {
    230         ActionMapping actionMapping = new ActionMapping();
    231
    232         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    233         defaultActionMapper.parseNameAndNamespace("/someAction", actionMapping, configManager);
    234
    235         assertEquals(actionMapping.getName(), "someAction");
    236         assertEquals(actionMapping.getNamespace(), "/");
    237     }
    238
    239     public void testParseNameAndNamespace3() throws Exception {
    240         ActionMapping actionMapping = new ActionMapping();
    241
    242         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    243         defaultActionMapper.parseNameAndNamespace("/my/someAction", actionMapping, configManager);
    244
    245         assertEquals(actionMapping.getName(), "someAction");
    246         assertEquals(actionMapping.getNamespace(), "/my");
    247     }
    248
    249     public void testParseNameAndNamespace_NoSlashes() throws Exception {
    250         ActionMapping actionMapping = new ActionMapping();
    251
    252         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    253         defaultActionMapper.setSlashesInActionNames("false");
    254         defaultActionMapper.parseNameAndNamespace("/foo/someAction", actionMapping, configManager);
    255
    256         assertEquals(actionMapping.getName(), "someAction");
    257         assertEquals(actionMapping.getNamespace(), "");
    258     }
    259
    260     public void testParseNameAndNamespace_AllowSlashes() throws Exception {
    261         ActionMapping actionMapping = new ActionMapping();
    262
    263         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    264         defaultActionMapper.setSlashesInActionNames("true");
    265         defaultActionMapper.parseNameAndNamespace("/foo/someAction", actionMapping, configManager);
    266
    267         assertEquals(actionMapping.getName(), "foo/someAction");
    268         assertEquals(actionMapping.getNamespace(), "");
    269     }
    270
    271
    272     // ===========================
    273     // === test special prefix ===
    274     // ===========================
    275
    276     public void testActionPrefix() throws Exception {
    277         Map parameterMap = new HashMap();
    278         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction", "");
    279
    280         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    281         request.setParameterMap(parameterMap);
    282         request.setupGetServletPath("/someServletPath.action");
    283
    284         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    285         ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
    286
    287         assertEquals(actionMapping.getName(), "myAction");
    288     }
    289
    290     public void testActionPrefix_fromImageButton() throws Exception {
    291         Map parameterMap = new HashMap();
    292         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction", "");
    293         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction.x", "");
    294         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction.y", "");
    295
    296         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    297         request.setParameterMap(parameterMap);
    298         request.setupGetServletPath("/someServletPath.action");
    299
    300         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    301         ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
    302
    303         assertEquals(actionMapping.getName(), "myAction");
    304     }
    305
    306     public void testActionPrefix_fromIEImageButton() throws Exception {
    307         Map parameterMap = new HashMap();
    308         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction.x", "");
    309         parameterMap.put(DefaultActionMapper.ACTION_PREFIX + "myAction.y", "");
    310
    311         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    312         request.setParameterMap(parameterMap);
    313         request.setupGetServletPath("/someServletPath.action");
    314
    315         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    316         ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
    317
    318         assertEquals(actionMapping.getName(), "myAction");
    319     }
    320
    321     public void testRedirectPrefix() throws Exception {
    322         Map parameterMap = new HashMap();
    323         parameterMap.put(DefaultActionMapper.REDIRECT_PREFIX + "http://www.google.com", "");
    324
    325         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    326         request.setupGetServletPath("/someServletPath.action");
    327         request.setParameterMap(parameterMap);
    328
    329         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    330         defaultActionMapper.setContainer(container);
    331         ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
    332
    333         Result result = actionMapping.getResult();
    334         assertNotNull(result);
    335         assertTrue(result instanceof ServletRedirectResult);
    336
    337         Mock invMock = new Mock(ActionInvocation.class);
    338         ActionInvocation inv = (ActionInvocation) invMock.proxy();
    339         ActionContext ctx = ActionContext.getContext();
    340         ctx.put(ServletActionContext.HTTP_REQUEST, request);
    341         StrutsMockHttpServletResponse response = new StrutsMockHttpServletResponse();
    342         ctx.put(ServletActionContext.HTTP_RESPONSE, response);
    343         invMock.expectAndReturn("getInvocationContext", ctx);
    344         invMock.expectAndReturn("getStack", ctx.getValueStack());
    345         result.execute(inv);
    346         assertEquals("http://www.google.com", response.getRedirectURL());
    347         //TODO: need to test location but there's noaccess to the property/method, unless we use reflection
    348     }
    349
    350     public void testRedirectActionPrefix() throws Exception {
    351         Map parameterMap = new HashMap();
    352         parameterMap.put(DefaultActionMapper.REDIRECT_ACTION_PREFIX + "myAction", "");
    353
    354         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    355         request.setupGetServletPath("/someServletPath.action");
    356         request.setParameterMap(parameterMap);
    357
    358         DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
    359         defaultActionMapper.setContainer(container);
    360         ActionMapping actionMapping = defaultActionMapper.getMapping(request, configManager);
    361
    362
    363         Result result = actionMapping.getResult();
    364         assertNotNull(result);
    365         assertTrue(result instanceof ServletRedirectResult);
    366
    367         // TODO: need to test location but there's noaccess to the property/method, unless we use reflection
    368     }
    369
    370     public void testDropExtension() throws Exception {
    371         DefaultActionMapper mapper = new DefaultActionMapper();
    372         String name = mapper.dropExtension("foo.action");
    373         assertTrue("Name not right: "+name, "foo".equals(name));
    374
    375         name = mapper.dropExtension("foo.action.action");
    376         assertTrue("Name not right: "+name, "foo.action".equals(name));
    377
    378     }
    379
    380     public void testGetUriFromActionMapper1() throws Exception {
    381         DefaultActionMapper mapper = new DefaultActionMapper();
    382         ActionMapping actionMapping = new ActionMapping();
    383         actionMapping.setMethod("myMethod");
    384         actionMapping.setName("myActionName");
    385         actionMapping.setNamespace("/myNamespace");
    386         String uri = mapper.getUriFromActionMapping(actionMapping);
    387
    388         assertEquals("/myNamespace/myActionName!myMethod.action", uri);
    389     }
    390
    391     public void testGetUriFromActionMapper2() throws Exception {
    392         DefaultActionMapper mapper = new DefaultActionMapper();
    393         ActionMapping actionMapping = new ActionMapping();
    394         actionMapping.setMethod("myMethod");
    395         actionMapping.setName("myActionName");
    396         actionMapping.setNamespace("/");
    397         String uri = mapper.getUriFromActionMapping(actionMapping);
    398
    399         assertEquals("/myActionName!myMethod.action", uri);
    400     }
    401
    402     public void testGetUriFromActionMapper3() throws Exception {
    403         DefaultActionMapper mapper = new DefaultActionMapper();
    404         ActionMapping actionMapping = new ActionMapping();
    405         actionMapping.setMethod("myMethod");
    406         actionMapping.setName("myActionName");
    407         actionMapping.setNamespace("");
    408         String uri = mapper.getUriFromActionMapping(actionMapping);
    409
    410         assertEquals("/myActionName!myMethod.action", uri);
    411     }
    412
    413
    414     public void testGetUriFromActionMapper4() throws Exception {
    415         DefaultActionMapper mapper = new DefaultActionMapper();
    416         ActionMapping actionMapping = new ActionMapping();
    417         actionMapping.setName("myActionName");
    418         actionMapping.setNamespace("");
    419         String uri = mapper.getUriFromActionMapping(actionMapping);
    420
    421         assertEquals("/myActionName.action", uri);
    422     }
    423
    424     public void testGetUriFromActionMapper5() throws Exception {
    425         DefaultActionMapper mapper = new DefaultActionMapper();
    426         ActionMapping actionMapping = new ActionMapping();
    427         actionMapping.setName("myActionName");
    428         actionMapping.setNamespace("/");
    429         String uri = mapper.getUriFromActionMapping(actionMapping);
    430
    431         assertEquals("/myActionName.action", uri);
    432     }
    433
    434     //
    435     public void testGetUriFromActionMapper6() throws Exception {
    436         DefaultActionMapper mapper = new DefaultActionMapper();
    437         ActionMapping actionMapping = new ActionMapping();
    438         actionMapping.setMethod("myMethod");
    439         actionMapping.setName("myActionName?test=bla");
    440         actionMapping.setNamespace("/myNamespace");
    441         String uri = mapper.getUriFromActionMapping(actionMapping);
    442
    443         assertEquals("/myNamespace/myActionName!myMethod.action?test=bla", uri);
    444     }
    445
    446     public void testGetUriFromActionMapper7() throws Exception {
    447         DefaultActionMapper mapper = new DefaultActionMapper();
    448         ActionMapping actionMapping = new ActionMapping();
    449         actionMapping.setMethod("myMethod");
    450         actionMapping.setName("myActionName?test=bla");
    451         actionMapping.setNamespace("/");
    452         String uri = mapper.getUriFromActionMapping(actionMapping);
    453
    454         assertEquals("/myActionName!myMethod.action?test=bla", uri);
    455     }
    456
    457     public void testGetUriFromActionMapper8() throws Exception {
    458         DefaultActionMapper mapper = new DefaultActionMapper();
    459         ActionMapping actionMapping = new ActionMapping();
    460         actionMapping.setMethod("myMethod");
    461         actionMapping.setName("myActionName?test=bla");
    462         actionMapping.setNamespace("");
    463         String uri = mapper.getUriFromActionMapping(actionMapping);
    464
    465         assertEquals("/myActionName!myMethod.action?test=bla", uri);
    466     }
    467
    468
    469     public void testGetUriFromActionMapper9() throws Exception {
    470         DefaultActionMapper mapper = new DefaultActionMapper();
    471         ActionMapping actionMapping = new ActionMapping();
    472         actionMapping.setName("myActionName?test=bla");
    473         actionMapping.setNamespace("");
    474         String uri = mapper.getUriFromActionMapping(actionMapping);
    475
    476         assertEquals("/myActionName.action?test=bla", uri);
    477     }
    478
    479     public void testGetUriFromActionMapper10() throws Exception {
    480         DefaultActionMapper mapper = new DefaultActionMapper();
    481         ActionMapping actionMapping = new ActionMapping();
    482         actionMapping.setName("myActionName?test=bla");
    483         actionMapping.setNamespace("/");
    484         String uri = mapper.getUriFromActionMapping(actionMapping);
    485
    486         assertEquals("/myActionName.action?test=bla", uri);
    487     }
    488
    489     public void testGetUriFromActionMapper11() throws Exception {
    490         DefaultActionMapper mapper = new DefaultActionMapper();
    491         ActionMapping actionMapping = new ActionMapping();
    492         actionMapping.setName("myActionName.action");
    493         actionMapping.setNamespace("/");
    494         String uri = mapper.getUriFromActionMapping(actionMapping);
    495
    496         assertEquals("/myActionName.action", uri);
    497     }
    498
    499     public void testGetUriFromActionMapper12() throws Exception {
    500         DefaultActionMapper mapper = new DefaultActionMapper();
    501         ActionMapping actionMapping = new ActionMapping();
    502         actionMapping.setName("myActionName.action");
    503         actionMapping.setNamespace("/");
    504         String uri = mapper.getUriFromActionMapping(actionMapping);
    505
    506         assertEquals("/myActionName.action", uri);
    507     }
    508
    509 }