View Javadoc

1   /*
2    * $Id: Restful2ActionMapperTest.java 540141 2007-05-21 13:46:48Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }