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.rest;
23
24 import com.opensymphony.xwork2.config.Configuration;
25 import com.opensymphony.xwork2.config.ConfigurationManager;
26 import com.opensymphony.xwork2.config.entities.PackageConfig;
27 import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
28 import junit.framework.TestCase;
29 import org.apache.struts2.dispatcher.mapper.ActionMapping;
30 import org.springframework.mock.web.MockHttpServletRequest;
31
32 public class RestActionMapperTest extends TestCase {
33
34 private RestActionMapper mapper;
35 private ConfigurationManager configManager;
36 private Configuration config;
37 private MockHttpServletRequest req;
38
39 protected void setUp() throws Exception {
40 super.setUp();
41 req = new MockHttpServletRequest();
42 req.setContextPath("/myapp");
43 req.setMethod("GET");
44
45 mapper = new RestActionMapper();
46
47 config = new DefaultConfiguration();
48 PackageConfig pkg = new PackageConfig.Builder("myns").namespace("/animals").build();
49 PackageConfig pkg2 = new PackageConfig.Builder("my").namespace("/my").build();
50 config.addPackageConfig("mvns", pkg);
51 config.addPackageConfig("my", pkg2);
52 configManager = new ConfigurationManager() {
53 public Configuration getConfiguration() {
54 return config;
55 }
56 };
57 }
58
59 public void testGetMapping() throws Exception {
60 req.setRequestURI("/myapp/animals/dog");
61 req.setServletPath("/animals/dog");
62
63 ActionMapping mapping = mapper.getMapping(req, configManager);
64
65 assertEquals("/animals", mapping.getNamespace());
66 assertEquals("dog", mapping.getName());
67 assertEquals("index", mapping.getMethod());
68 }
69
70 public void testPostMapping() throws Exception {
71 req.setRequestURI("/myapp/animals/dog");
72 req.setServletPath("/animals/dog");
73 req.setMethod("POST");
74
75 ActionMapping mapping = mapper.getMapping(req, configManager);
76
77 assertEquals("/animals", mapping.getNamespace());
78 assertEquals("dog", mapping.getName());
79 assertEquals("create", mapping.getMethod());
80 }
81
82 public void testDeleteMapping() throws Exception {
83 req.setRequestURI("/myapp/animals/dog/fido");
84 req.setServletPath("/animals/dog/fido");
85 req.setMethod("DELETE");
86
87 ActionMapping mapping = mapper.getMapping(req, configManager);
88
89 assertEquals("/animals", mapping.getNamespace());
90 assertEquals("dog", mapping.getName());
91 assertEquals("destroy", mapping.getMethod());
92 assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
93 }
94
95 public void testPutMapping() throws Exception {
96 req.setRequestURI("/myapp/animals/dog/fido");
97 req.setServletPath("/animals/dog/fido");
98 req.setMethod("PUT");
99
100 ActionMapping mapping = mapper.getMapping(req, configManager);
101
102 assertEquals("/animals", mapping.getNamespace());
103 assertEquals("dog", mapping.getName());
104 assertEquals("update", mapping.getMethod());
105 assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
106 }
107
108 public void testGetIdMapping() throws Exception {
109 req.setRequestURI("/myapp/animals/dog/fido");
110 req.setServletPath("/animals/dog/fido");
111 req.setMethod("GET");
112
113 ActionMapping mapping = mapper.getMapping(req, configManager);
114
115 assertEquals("/animals", mapping.getNamespace());
116 assertEquals("dog", mapping.getName());
117 assertEquals("show", mapping.getMethod());
118 assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
119 }
120
121 public void testNewMapping() throws Exception {
122 req.setRequestURI("/myapp/animals/dog/new");
123 req.setServletPath("/animals/dog/new");
124 req.setMethod("GET");
125
126 ActionMapping mapping = mapper.getMapping(req, configManager);
127
128 assertEquals("/animals", mapping.getNamespace());
129 assertEquals("dog", mapping.getName());
130 assertEquals("editNew", mapping.getMethod());
131 }
132
133 public void testEditMapping() throws Exception {
134 req.setRequestURI("/myapp/animals/dog/fido/edit");
135 req.setServletPath("/animals/dog/fido/edit");
136 req.setMethod("GET");
137
138 ActionMapping mapping = mapper.getMapping(req, configManager);
139
140 assertEquals("/animals", mapping.getNamespace());
141 assertEquals("dog", mapping.getName());
142 assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
143 assertEquals("edit", mapping.getMethod());
144 }
145
146 public void testEditSemicolonMapping() throws Exception {
147 req.setRequestURI("/myapp/animals/dog/fido;edit");
148 req.setServletPath("/animals/dog/fido;edit");
149 req.setMethod("GET");
150
151 ActionMapping mapping = mapper.getMapping(req, configManager);
152
153 assertEquals("/animals", mapping.getNamespace());
154 assertEquals("dog", mapping.getName());
155 assertEquals("fido", ((String[])mapping.getParams().get("id"))[0]);
156 assertEquals("edit", mapping.getMethod());
157 }
158
159 public void testParseNameAndNamespace() {
160 tryUri("/foo/23", "", "foo/23");
161 tryUri("/foo/", "", "foo/");
162 tryUri("foo", "", "foo");
163 tryUri("/", "/", "");
164 }
165
166 public void testParseNameAndNamespaceWithNamespaces() {
167 tryUri("/my/foo/23", "/my", "foo/23");
168 tryUri("/my/foo/", "/my", "foo/");
169 }
170
171 public void testParseNameAndNamespaceWithEdit() {
172 tryUri("/my/foo/23;edit", "/my", "foo/23;edit");
173 }
174
175 private void tryUri(String uri, String expectedNamespace, String expectedName) {
176 ActionMapping mapping = new ActionMapping();
177 mapper.parseNameAndNamespace(uri, mapping, configManager);
178 assertEquals(expectedName, mapping.getName());
179 assertEquals(expectedNamespace, mapping.getNamespace());
180 }
181
182 }