View Javadoc

1   /*
2    * $Id: StrutsMockPageContext.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.views.jsp;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.ServletResponse;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  
27  import com.mockobjects.servlet.MockPageContext;
28  
29  
30  /***
31   */
32  public class StrutsMockPageContext extends MockPageContext {
33  
34      private Map attributes = new HashMap();
35      private ServletResponse response;
36  
37  
38      public void setAttribute(String s, Object o) {
39          if ((s == null) || (o == null)) {
40              throw new NullPointerException("PageContext does not accept null attributes");
41          }
42  
43          this.attributes.put(s, o);
44      }
45  
46      public Object getAttribute(String key) {
47          return attributes.get(key);
48      }
49  
50      public Object getAttributes(String key) {
51          return this.attributes.get(key);
52      }
53  
54      public void setResponse(ServletResponse response) {
55          this.response = response;
56      }
57  
58      public ServletResponse getResponse() {
59          return response;
60      }
61  
62      public HttpSession getSession() {
63          HttpSession session = super.getSession();
64  
65          if (session == null) {
66              session = ((HttpServletRequest) getRequest()).getSession(true);
67          }
68  
69          return session;
70      }
71  
72      public Object findAttribute(String s) {
73          return attributes.get(s);
74      }
75  
76      public void removeAttribute(String key) {
77          this.attributes.remove(key);
78      }
79  }