1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }