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 org.apache.struts2.StrutsTestCase;
24 import org.apache.struts2.StrutsConstants;
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("myns", "/my/namespace", false, null);
51 PackageConfig pkg2 = new PackageConfig("my", "/my", false, null);
52 config.addPackageConfig("mvns", pkg);
53 config.addPackageConfig("my", pkg2);
54 configManager = new ConfigurationManager() {
55 public Configuration getConfiguration() {
56 return config;
57 }
58 };
59 }
60
61 public void testGetIndex() throws Exception {
62 req.setupGetRequestURI("/my/namespace/foo/");
63 req.setupGetServletPath("/my/namespace/foo/");
64 req.setupGetAttribute(null);
65 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
66 req.setupGetMethod("GET");
67
68 ActionMapping mapping = mapper.getMapping(req, configManager);
69
70 assertEquals("/my/namespace", mapping.getNamespace());
71 assertEquals("foo/", mapping.getName());
72 assertEquals("index", mapping.getMethod());
73 }
74
75 public void testGetIndexWithParams() throws Exception {
76 req.setupGetRequestURI("/my/namespace/bar/1/foo/");
77 req.setupGetServletPath("/my/namespace/bar/1/foo/");
78 req.setupGetAttribute(null);
79 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
80 req.setupGetMethod("GET");
81
82 ActionMapping mapping = mapper.getMapping(req, configManager);
83
84 assertEquals("/my/namespace", mapping.getNamespace());
85 assertEquals("foo/", mapping.getName());
86 assertEquals("index", mapping.getMethod());
87 assertEquals(1, mapping.getParams().size());
88 assertEquals("1", mapping.getParams().get("bar"));
89 }
90
91 public void testPostCreate() throws Exception {
92 req.setupGetRequestURI("/my/namespace/bar/1/foo/");
93 req.setupGetServletPath("/my/namespace/bar/1/foo/");
94 req.setupGetAttribute(null);
95 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
96 req.setupGetMethod("POST");
97
98 ActionMapping mapping = mapper.getMapping(req, configManager);
99
100 assertEquals("/my/namespace", mapping.getNamespace());
101 assertEquals("foo/", mapping.getName());
102 assertEquals("create", mapping.getMethod());
103 assertEquals(1, mapping.getParams().size());
104 assertEquals("1", mapping.getParams().get("bar"));
105 }
106
107 public void testPutUpdate() throws Exception {
108
109 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
110 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
111 req.setupGetAttribute(null);
112 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
113 req.setupGetMethod("PUT");
114
115 ActionMapping mapping = mapper.getMapping(req, configManager);
116
117 assertEquals("/my/namespace", mapping.getNamespace());
118 assertEquals("foo/2", mapping.getName());
119 assertEquals("update", mapping.getMethod());
120 assertEquals(1, mapping.getParams().size());
121 assertEquals("1", mapping.getParams().get("bar"));
122 }
123
124 public void testPutUpdateWithIdParam() throws Exception {
125
126 mapper.setIdParameterName("id");
127 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
128 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
129 req.setupGetAttribute(null);
130 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
131 req.setupGetMethod("PUT");
132
133 ActionMapping mapping = mapper.getMapping(req, configManager);
134
135 assertEquals("/my/namespace", mapping.getNamespace());
136 assertEquals("foo", mapping.getName());
137 assertEquals("update", mapping.getMethod());
138 assertEquals(2, mapping.getParams().size());
139 assertEquals("1", mapping.getParams().get("bar"));
140 assertEquals("2", mapping.getParams().get("id"));
141
142 }
143
144 public void testPutUpdateWithFakePut() throws Exception {
145
146 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
147 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
148 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put");
149 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put");
150 req.setupGetAttribute(null);
151 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
152 req.setupGetMethod("POST");
153
154 ActionMapping mapping = mapper.getMapping(req, configManager);
155
156 assertEquals("/my/namespace", mapping.getNamespace());
157 assertEquals("foo/2", mapping.getName());
158 assertEquals("update", mapping.getMethod());
159 assertEquals(1, mapping.getParams().size());
160 assertEquals("1", mapping.getParams().get("bar"));
161 }
162
163 public void testDeleteRemove() throws Exception {
164
165 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
166 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
167 req.setupGetAttribute(null);
168 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
169 req.setupGetMethod("DELETE");
170
171 ActionMapping mapping = mapper.getMapping(req, configManager);
172
173 assertEquals("/my/namespace", mapping.getNamespace());
174 assertEquals("foo/2", mapping.getName());
175 assertEquals("remove", mapping.getMethod());
176 assertEquals(1, mapping.getParams().size());
177 assertEquals("1", mapping.getParams().get("bar"));
178 }
179
180 public void testDeleteRemoveWithFakeDelete() throws Exception {
181
182 req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
183 req.setupGetServletPath("/my/namespace/bar/1/foo/2");
184 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
185 req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
186 req.setupGetAttribute(null);
187 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
188 req.setupGetMethod("POST");
189
190 ActionMapping mapping = mapper.getMapping(req, configManager);
191
192 assertEquals("/my/namespace", mapping.getNamespace());
193 assertEquals("foo/2", mapping.getName());
194 assertEquals("remove", mapping.getMethod());
195 assertEquals(1, mapping.getParams().size());
196 assertEquals("1", mapping.getParams().get("bar"));
197 }
198 }