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