View Javadoc

1   /*
2    * $Id: ServletActionContextTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import junit.framework.TestCase;
31  
32  import com.mockobjects.servlet.MockHttpServletRequest;
33  import com.mockobjects.servlet.MockHttpServletResponse;
34  import com.mockobjects.servlet.MockServletContext;
35  import com.opensymphony.xwork2.ActionContext;
36  
37  
38  /***
39   * Unit test for ServletActionContext. Based loosly on Jason's ActionContextTest.
40   * My first attempt at unit testing. Please hack away as needed.
41   *
42   */
43  public class ServletActionContextTest extends TestCase implements StrutsStatics {
44  
45      ActionContext actionContext;
46      ServletActionContext servletActionContext;
47      private HttpServletRequest request;
48      private HttpServletResponse response;
49      private MockServletContext servletContext;
50  
51  
52      public void setUp() {
53          Map extraContext = new HashMap();
54  
55          request = new MockHttpServletRequest();
56          response = new MockHttpServletResponse();
57          servletContext = new MockServletContext();
58  
59          extraContext.put(HTTP_REQUEST, request);
60          extraContext.put(HTTP_RESPONSE, response);
61          extraContext.put(SERVLET_CONTEXT, servletContext);
62  
63          actionContext = new ActionContext(extraContext);
64          ServletActionContext.setContext(actionContext);
65      }
66  
67      public void testContextParams() {
68          assertEquals(ServletActionContext.getRequest(), request);
69          assertEquals(ServletActionContext.getResponse(), response);
70          assertEquals(ServletActionContext.getServletContext(), servletContext);
71      }
72  
73      public void testGetContext() {
74          ActionContext threadContext = ServletActionContext.getContext();
75          assertEquals(actionContext, threadContext);
76      }
77  }