View Javadoc

1   /*
2    * $Id: ServletActionContextTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import junit.framework.TestCase;
27  
28  import com.mockobjects.servlet.MockHttpServletRequest;
29  import com.mockobjects.servlet.MockHttpServletResponse;
30  import com.mockobjects.servlet.MockServletContext;
31  import com.opensymphony.xwork2.ActionContext;
32  
33  
34  /***
35   * Unit test for ServletActionContext. Based loosly on Jason's ActionContextTest.
36   * My first attempt at unit testing. Please hack away as needed.
37   *
38   */
39  public class ServletActionContextTest extends TestCase implements StrutsStatics {
40  
41      ActionContext actionContext;
42      ServletActionContext servletActionContext;
43      private HttpServletRequest request;
44      private HttpServletResponse response;
45      private MockServletContext servletContext;
46  
47  
48      public void setUp() {
49          Map extraContext = new HashMap();
50  
51          request = new MockHttpServletRequest();
52          response = new MockHttpServletResponse();
53          servletContext = new MockServletContext();
54  
55          extraContext.put(HTTP_REQUEST, request);
56          extraContext.put(HTTP_RESPONSE, response);
57          extraContext.put(SERVLET_CONTEXT, servletContext);
58  
59          actionContext = new ActionContext(extraContext);
60          ServletActionContext.setContext(actionContext);
61      }
62  
63      public void testContextParams() {
64          assertEquals(ServletActionContext.getRequest(), request);
65          assertEquals(ServletActionContext.getResponse(), response);
66          assertEquals(ServletActionContext.getServletContext(), servletContext);
67      }
68  
69      public void testGetContext() {
70          ActionContext threadContext = ServletActionContext.getContext();
71          assertEquals(actionContext, threadContext);
72      }
73  }