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 org.apache.struts2.StrutsTestCase;
25 import org.apache.struts2.StrutsConstants;
26 import com.mockobjects.servlet.MockHttpServletRequest;
27 import com.opensymphony.xwork2.config.ConfigurationManager;
28 import com.opensymphony.xwork2.config.Configuration;
29 import com.opensymphony.xwork2.config.entities.PackageConfig;
30 import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
31
32 import java.util.HashMap;
33
34 public class Restful2ActionMapperTest extends StrutsTestCase {
35
36 private Restful2ActionMapper mapper;
37 private MockHttpServletRequest req;
38 private ConfigurationManager configManager;
39 private Configuration config;
40
41 @Override
42 protected void setUp() throws Exception {
43 super.setUp();
44 mapper = new Restful2ActionMapper();
45 mapper.setExtensions("");
46 req = new MockHttpServletRequest();
47 req.setupGetParameterMap(new HashMap());
48 req.setupGetContextPath("/my/namespace");
49
50 config = new DefaultConfiguration();
51 PackageConfig pkg = new PackageConfig.Builder("myns")
52 .namespace("/my/namespace").build();
53 PackageConfig pkg2 = new PackageConfig.Builder("my")
54 .namespace("/my").build();
55 config.addPackageConfig("mvns", pkg);
56 config.addPackageConfig("my", pkg2);
57 configManager = new ConfigurationManager() {
58 public Configuration getConfiguration() {
59 return config;
60 }
61 };
62 }
63
64 public void testGetIndex() throws Exception {
65 req.setupGetRequestURI("/my/namespace/foo/");
66 req.setupGetServletPath("/my/namespace/foo/");
67 req.setupGetAttribute(null);
68 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
69 req.setupGetMethod("GET");
70
71 ActionMapping mapping = mapper.getMapping(req, configManager);
72
73 assertEquals("/my/namespace", mapping.getNamespace());
74 assertEquals("foo/", mapping.getName());
75 assertEquals("index", mapping.getMethod());
76 }
77
78 public void testGetId() throws Exception {
79 mapper.setIdParameterName("id");
80 req.setupGetRequestURI("/my/namespace/foo/3");
81 req.setupGetServletPath("/my/namespace/foo/3");
82 req.setupGetAttribute(null);
83 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
84 req.setupGetMethod("GET");
85
86 ActionMapping mapping = mapper.getMapping(req, configManager);
87
88 assertEquals("/my/namespace", mapping.getNamespace());
89 assertEquals("foo/3", mapping.getName());
90 assertEquals("view", mapping.getMethod());
91 assertEquals("3", mapping.getParams().get("id"));
92 }
93
94 public void testGetEdit() throws Exception {
95 mapper.setIdParameterName("id");
96 req.setupGetRequestURI("/my/namespace/foo/3!edit");
97 req.setupGetServletPath("/my/namespace/foo/3!edit");
98 req.setupGetAttribute(null);
99 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
100 req.setupGetMethod("GET");
101
102 ActionMapping mapping = mapper.getMapping(req, configManager);
103
104 assertEquals("/my/namespace", mapping.getNamespace());
105 assertEquals("foo/3", mapping.getName());
106 assertEquals("edit", mapping.getMethod());
107 assertEquals("3", mapping.getParams().get("id"));
108 }
109
110 public void testGetIndexWithParams() throws Exception {
111 req.setupGetRequestURI("/my/namespace/bar/1/foo/");
112 req.setupGetServletPath("/my/namespace/bar/1/foo/");
113 req.setupGetAttribute(null);
114 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
115 req.setupGetMethod("GET");
116
117 ActionMapping mapping = mapper.getMapping(req, configManager);
118
119 assertEquals("/my/namespace", mapping.getNamespace());
120 assertEquals("foo/", mapping.getName());
121 assertEquals("index", mapping.getMethod());
122 assertEquals(1, mapping.getParams().size());
123 assertEquals("1", mapping.getParams().get("bar"));
124 }
125
126 public void testPostCreate() throws Exception {
127 req.setupGetRequestURI("/my/namespace/bar/1/foo/");
128 req.setupGetServletPath("/my/namespace/bar/1/foo/");
129 req.setupGetAttribute(null);
130 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
131 req.setupGetMethod("POST");
132
133 ActionMapping mapping = mapper.getMapping(req, configManager);
134
135 assertEquals("/my/namespace", mapping.getNamespace());
136 assertEquals("foo/", mapping.getName());
137 assertEquals("create", mapping.getMethod());
138 assertEquals(1, mapping.getParams().size());
139 assertEquals("1", mapping.getParams().get("bar"));
140 }
141
142 public void testPutUpdate() throws Exception {
143
144 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
145 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
146 req.setupGetAttribute(null);
147 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
148 req.setupGetMethod("PUT");
149
150 ActionMapping mapping = mapper.getMapping(req, configManager);
151
152 assertEquals("/my/namespace", mapping.getNamespace());
153 assertEquals("foo/2", mapping.getName());
154 assertEquals("update", mapping.getMethod());
155 assertEquals(1, mapping.getParams().size());
156 assertEquals("1", mapping.getParams().get("bar"));
157 }
158
159 public void testPutUpdateWithIdParam() throws Exception {
160
161 mapper.setIdParameterName("id");
162 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
163 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
164 req.setupGetAttribute(null);
165 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
166 req.setupGetMethod("PUT");
167
168 ActionMapping mapping = mapper.getMapping(req, configManager);
169
170 assertEquals("/my/namespace", mapping.getNamespace());
171 assertEquals("foo", mapping.getName());
172 assertEquals("update", mapping.getMethod());
173 assertEquals(2, mapping.getParams().size());
174 assertEquals("1", mapping.getParams().get("bar"));
175 assertEquals("2", mapping.getParams().get("id"));
176
177 }
178
179 public void testPutUpdateWithFakePut() throws Exception {
180
181 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
182 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
183 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put");
184 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put");
185 req.setupGetAttribute(null);
186 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
187 req.setupGetMethod("POST");
188
189 ActionMapping mapping = mapper.getMapping(req, configManager);
190
191 assertEquals("/my/namespace", mapping.getNamespace());
192 assertEquals("foo/2", mapping.getName());
193 assertEquals("update", mapping.getMethod());
194 assertEquals(1, mapping.getParams().size());
195 assertEquals("1", mapping.getParams().get("bar"));
196 }
197
198 public void testDeleteRemove() throws Exception {
199
200 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
201 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
202 req.setupGetAttribute(null);
203 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
204 req.setupGetMethod("DELETE");
205
206 ActionMapping mapping = mapper.getMapping(req, configManager);
207
208 assertEquals("/my/namespace", mapping.getNamespace());
209 assertEquals("foo/2", mapping.getName());
210 assertEquals("remove", mapping.getMethod());
211 assertEquals(1, mapping.getParams().size());
212 assertEquals("1", mapping.getParams().get("bar"));
213 }
214
215 public void testDeleteRemoveWithFakeDelete() throws Exception {
216
217 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
218 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
219 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
220 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
221 req.setupGetAttribute(null);
222 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
223 req.setupGetMethod("POST");
224
225 ActionMapping mapping = mapper.getMapping(req, configManager);
226
227 assertEquals("/my/namespace", mapping.getNamespace());
228 assertEquals("foo/2", mapping.getName());
229 assertEquals("remove", mapping.getMethod());
230 assertEquals(1, mapping.getParams().size());
231 assertEquals("1", mapping.getParams().get("bar"));
232 }
233 }