001: /*
002: * $Id: Restful2ActionMapperTest.java 540141 2007-05-21 13:46:48Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher.mapper;
022:
023: import org.apache.struts2.StrutsTestCase;
024: import org.apache.struts2.StrutsConstants;
025: import com.mockobjects.servlet.MockHttpServletRequest;
026: import com.opensymphony.xwork2.config.ConfigurationManager;
027: import com.opensymphony.xwork2.config.Configuration;
028: import com.opensymphony.xwork2.config.entities.PackageConfig;
029: import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
030:
031: import java.util.HashMap;
032:
033: public class Restful2ActionMapperTest extends StrutsTestCase {
034:
035: private Restful2ActionMapper mapper;
036: private MockHttpServletRequest req;
037: private ConfigurationManager configManager;
038: private Configuration config;
039:
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043: mapper = new Restful2ActionMapper();
044: mapper.setExtensions("");
045: req = new MockHttpServletRequest();
046: req.setupGetParameterMap(new HashMap());
047: req.setupGetContextPath("/my/namespace");
048:
049: config = new DefaultConfiguration();
050: PackageConfig pkg = new PackageConfig("myns", "/my/namespace",
051: false, null);
052: PackageConfig pkg2 = new PackageConfig("my", "/my", false, null);
053: config.addPackageConfig("mvns", pkg);
054: config.addPackageConfig("my", pkg2);
055: configManager = new ConfigurationManager() {
056: public Configuration getConfiguration() {
057: return config;
058: }
059: };
060: }
061:
062: public void testGetIndex() throws Exception {
063: req.setupGetRequestURI("/my/namespace/foo/");
064: req.setupGetServletPath("/my/namespace/foo/");
065: req.setupGetAttribute(null);
066: req
067: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
068: req.setupGetMethod("GET");
069:
070: ActionMapping mapping = mapper.getMapping(req, configManager);
071:
072: assertEquals("/my/namespace", mapping.getNamespace());
073: assertEquals("foo/", mapping.getName());
074: assertEquals("index", mapping.getMethod());
075: }
076:
077: public void testGetIndexWithParams() throws Exception {
078: req.setupGetRequestURI("/my/namespace/bar/1/foo/");
079: req.setupGetServletPath("/my/namespace/bar/1/foo/");
080: req.setupGetAttribute(null);
081: req
082: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
083: req.setupGetMethod("GET");
084:
085: ActionMapping mapping = mapper.getMapping(req, configManager);
086:
087: assertEquals("/my/namespace", mapping.getNamespace());
088: assertEquals("foo/", mapping.getName());
089: assertEquals("index", mapping.getMethod());
090: assertEquals(1, mapping.getParams().size());
091: assertEquals("1", mapping.getParams().get("bar"));
092: }
093:
094: public void testPostCreate() throws Exception {
095: req.setupGetRequestURI("/my/namespace/bar/1/foo/");
096: req.setupGetServletPath("/my/namespace/bar/1/foo/");
097: req.setupGetAttribute(null);
098: req
099: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
100: req.setupGetMethod("POST");
101:
102: ActionMapping mapping = mapper.getMapping(req, configManager);
103:
104: assertEquals("/my/namespace", mapping.getNamespace());
105: assertEquals("foo/", mapping.getName());
106: assertEquals("create", mapping.getMethod());
107: assertEquals(1, mapping.getParams().size());
108: assertEquals("1", mapping.getParams().get("bar"));
109: }
110:
111: public void testPutUpdate() throws Exception {
112:
113: req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
114: req.setupGetServletPath("/my/namespace/bar/1/foo/2");
115: req.setupGetAttribute(null);
116: req
117: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
118: req.setupGetMethod("PUT");
119:
120: ActionMapping mapping = mapper.getMapping(req, configManager);
121:
122: assertEquals("/my/namespace", mapping.getNamespace());
123: assertEquals("foo/2", mapping.getName());
124: assertEquals("update", mapping.getMethod());
125: assertEquals(1, mapping.getParams().size());
126: assertEquals("1", mapping.getParams().get("bar"));
127: }
128:
129: public void testPutUpdateWithIdParam() throws Exception {
130:
131: mapper.setIdParameterName("id");
132: req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
133: req.setupGetServletPath("/my/namespace/bar/1/foo/2");
134: req.setupGetAttribute(null);
135: req
136: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
137: req.setupGetMethod("PUT");
138:
139: ActionMapping mapping = mapper.getMapping(req, configManager);
140:
141: assertEquals("/my/namespace", mapping.getNamespace());
142: assertEquals("foo", mapping.getName());
143: assertEquals("update", mapping.getMethod());
144: assertEquals(2, mapping.getParams().size());
145: assertEquals("1", mapping.getParams().get("bar"));
146: assertEquals("2", mapping.getParams().get("id"));
147:
148: }
149:
150: public void testPutUpdateWithFakePut() throws Exception {
151:
152: req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
153: req.setupGetServletPath("/my/namespace/bar/1/foo/2");
154: req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM,
155: "put");
156: req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM,
157: "put");
158: req.setupGetAttribute(null);
159: req
160: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
161: req.setupGetMethod("POST");
162:
163: ActionMapping mapping = mapper.getMapping(req, configManager);
164:
165: assertEquals("/my/namespace", mapping.getNamespace());
166: assertEquals("foo/2", mapping.getName());
167: assertEquals("update", mapping.getMethod());
168: assertEquals(1, mapping.getParams().size());
169: assertEquals("1", mapping.getParams().get("bar"));
170: }
171:
172: public void testDeleteRemove() throws Exception {
173:
174: req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
175: req.setupGetServletPath("/my/namespace/bar/1/foo/2");
176: req.setupGetAttribute(null);
177: req
178: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
179: req.setupGetMethod("DELETE");
180:
181: ActionMapping mapping = mapper.getMapping(req, configManager);
182:
183: assertEquals("/my/namespace", mapping.getNamespace());
184: assertEquals("foo/2", mapping.getName());
185: assertEquals("remove", mapping.getMethod());
186: assertEquals(1, mapping.getParams().size());
187: assertEquals("1", mapping.getParams().get("bar"));
188: }
189:
190: public void testDeleteRemoveWithFakeDelete() throws Exception {
191:
192: req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
193: req.setupGetServletPath("/my/namespace/bar/1/foo/2");
194: req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM,
195: "DELETE");
196: req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM,
197: "DELETE");
198: req.setupGetAttribute(null);
199: req
200: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
201: req.setupGetMethod("POST");
202:
203: ActionMapping mapping = mapper.getMapping(req, configManager);
204:
205: assertEquals("/my/namespace", mapping.getNamespace());
206: assertEquals("foo/2", mapping.getName());
207: assertEquals("remove", mapping.getMethod());
208: assertEquals(1, mapping.getParams().size());
209: assertEquals("1", mapping.getParams().get("bar"));
210: }
211: }
|