View Javadoc

    1   /*
    2    * $Id: RestfulActionMapperTest.java 471756 2006-11-06 15:01:43Z husted $
    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.Collections;
    24  import java.util.HashMap;
    25  import java.util.Map;
    26
    27  import junit.framework.TestCase;
    28
    29  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
    30
    31  /***
    32   * Unit test for {@link RestfulActionMapper}.
    33   *
    34   */
    35  public class RestfulActionMapperTest extends TestCase {
    36
    37      private RestfulActionMapper mapper;
    38
    39      public void testGetUri() {
    40          ActionMapping am = new ActionMapping();
    41          am.setName("view");
    42          am.setNamespace("secure");
    43          am.setParams(Collections.EMPTY_MAP);
    44
    45          assertEquals("secureview", mapper.getUriFromActionMapping(am));
    46      }
    47
    48      public void testGetUriParam() {
    49          Map param = new HashMap();
    50          param.put("article", "123");
    51          ActionMapping am = new ActionMapping();
    52          am.setName("view");
    53          am.setNamespace("secure");
    54          am.setParams(param);
    55
    56          assertEquals("secureview", mapper.getUriFromActionMapping(am));
    57      }
    58
    59      public void testGetUriParamId() {
    60          Map param = new HashMap();
    61          param.put("article", "123");
    62          param.put("viewId", "456");
    63          ActionMapping am = new ActionMapping();
    64          am.setName("view");
    65          am.setNamespace("secure");
    66          am.setParams(param);
    67
    68          assertEquals("secureview/456", mapper.getUriFromActionMapping(am));
    69      }
    70
    71      public void testGetMappingNoSlash() throws Exception {
    72          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    73          request.setupGetServletPath("noslash");
    74
    75          assertNull(mapper.getMapping(request, null));
    76      }
    77
    78      public void testGetMapping() throws Exception {
    79          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    80          request.setupGetServletPath("/myapp/view/12");
    81
    82          ActionMapping am = mapper.getMapping(request, null);
    83          assertEquals("myapp", am.getName());
    84          assertEquals(1, am.getParams().size());
    85          assertEquals("12", am.getParams().get("view"));
    86      }
    87
    88      public void testGetMapping2() throws Exception {
    89          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    90          request.setupGetServletPath("/myapp/12/region/europe");
    91
    92          ActionMapping am = mapper.getMapping(request, null);
    93          assertEquals("myapp", am.getName());
    94          assertEquals(2, am.getParams().size());
    95          assertEquals("12", am.getParams().get("myappId"));
    96          assertEquals("europe", am.getParams().get("region"));
    97      }
    98
    99      public void testGetMapping3() throws Exception {
    100         StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
    101         request.setupGetServletPath("/myapp/view/12/region/europe");
    102
    103         ActionMapping am = mapper.getMapping(request, null);
    104         assertEquals("myapp", am.getName());
    105         assertEquals(2, am.getParams().size());
    106         assertEquals("12", am.getParams().get("view"));
    107         assertEquals("europe", am.getParams().get("region"));
    108     }
    109
    110     protected void setUp() throws Exception {
    111         mapper = new RestfulActionMapper();
    112     }
    113
    114     protected void tearDown() throws Exception {
    115         mapper = null;
    116     }
    117
    118 }