1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.EMPTY_MAP);
45
46 assertEquals("secureview", mapper.getUriFromActionMapping(am));
47 }
48
49 public void testGetUriParam() {
50 Map param = new HashMap();
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 param = new HashMap();
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 }