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 testPutCreate() throws Exception {
92 req.setupGetRequestURI("/my/namespace/foo/");
93 req.setupGetServletPath("/my/namespace/foo/");
94 req.setupGetAttribute(null);
95 req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
96 req.setupGetMethod("PUT");
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 }
104 }