View Javadoc

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