View Javadoc

1   /*
2    * $Id: RestfulActionMapperTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher.mapper;
19  
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
27  
28  /***
29   * Unit test for {@link RestfulActionMapper}.
30   *
31   */
32  public class RestfulActionMapperTest extends TestCase {
33  
34      private RestfulActionMapper mapper;
35  
36      public void testGetUri() {
37          ActionMapping am = new ActionMapping();
38          am.setName("view");
39          am.setNamespace("secure");
40          am.setParams(Collections.EMPTY_MAP);
41  
42          assertEquals("secureview", mapper.getUriFromActionMapping(am));
43      }
44  
45      public void testGetUriParam() {
46          Map param = new HashMap();
47          param.put("article", "123");
48          ActionMapping am = new ActionMapping();
49          am.setName("view");
50          am.setNamespace("secure");
51          am.setParams(param);
52  
53          assertEquals("secureview", mapper.getUriFromActionMapping(am));
54      }
55  
56      public void testGetUriParamId() {
57          Map param = new HashMap();
58          param.put("article", "123");
59          param.put("viewId", "456");
60          ActionMapping am = new ActionMapping();
61          am.setName("view");
62          am.setNamespace("secure");
63          am.setParams(param);
64  
65          assertEquals("secureview/456", mapper.getUriFromActionMapping(am));
66      }
67  
68      public void testGetMappingNoSlash() throws Exception {
69          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
70          request.setupGetServletPath("noslash");
71  
72          assertNull(mapper.getMapping(request, null));
73      }
74  
75      public void testGetMapping() throws Exception {
76          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
77          request.setupGetServletPath("/myapp/view/12");
78  
79          ActionMapping am = mapper.getMapping(request, null);
80          assertEquals("myapp", am.getName());
81          assertEquals(1, am.getParams().size());
82          assertEquals("12", am.getParams().get("view"));
83      }
84  
85      public void testGetMapping2() throws Exception {
86          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
87          request.setupGetServletPath("/myapp/12/region/europe");
88  
89          ActionMapping am = mapper.getMapping(request, null);
90          assertEquals("myapp", am.getName());
91          assertEquals(2, am.getParams().size());
92          assertEquals("12", am.getParams().get("myappId"));
93          assertEquals("europe", am.getParams().get("region"));
94      }
95  
96      public void testGetMapping3() throws Exception {
97          StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
98          request.setupGetServletPath("/myapp/view/12/region/europe");
99  
100         ActionMapping am = mapper.getMapping(request, null);
101         assertEquals("myapp", am.getName());
102         assertEquals(2, am.getParams().size());
103         assertEquals("12", am.getParams().get("view"));
104         assertEquals("europe", am.getParams().get("region"));
105     }
106 
107     protected void setUp() throws Exception {
108         mapper = new RestfulActionMapper();
109     }
110 
111     protected void tearDown() throws Exception {
112         mapper = null;
113     }
114 
115 }