View Javadoc

1   /*
2    * $Id: Restful2ActionMapperTest.java 495026 2007-01-10 22:55:44Z 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 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 }