View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.mock.servlet;
21  
22  import javax.el.ELContext;
23  import javax.servlet.Servlet;
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpSession;
30  import javax.servlet.jsp.JspWriter;
31  import javax.servlet.jsp.PageContext;
32  import javax.servlet.jsp.el.ExpressionEvaluator;
33  import javax.servlet.jsp.el.VariableResolver;
34  import java.io.IOException;
35  import java.io.PrintWriter;
36  import java.util.Enumeration;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  public class MockPageContext extends PageContext {
41  
42    private ServletRequest request = null;
43    private JspWriter out = new MockJspWriter(new PrintWriter(System.out));
44    private ServletContext servletContext = new MockServletContext();
45    private Map attributes = new HashMap();
46  
47  
48    public MockPageContext() {
49      request = new MockHttpServletRequest();
50    }
51  
52    public MockPageContext(ServletRequest servletRequest) {
53      this.request = servletRequest;
54    }
55  
56    public Object findAttribute(String s) {
57      return null;
58    }
59  
60    public void forward(String s) throws ServletException, IOException {
61    }
62  
63    public Object getAttribute(String s) {
64      return attributes.get(s);
65    }
66  
67    public Object getAttribute(String name, int scope) {
68      switch (scope) {
69        case PageContext.REQUEST_SCOPE:
70          return getRequest().getAttribute(name);
71        // other not supported
72        default:
73          return null;
74      }
75    }
76  
77    public Enumeration getAttributeNamesInScope(int i) {
78      return null;
79    }
80  
81    public int getAttributesScope(String s) {
82      return 0;
83    }
84  
85    public Exception getException() {
86      return null;
87    }
88  
89    public JspWriter getOut() {
90      return out;
91    }
92  
93    public Object getPage() {
94      return null;
95    }
96  
97    public ServletRequest getRequest() {
98      return request;
99    }
100 
101   public ServletResponse getResponse() {
102     return new MockHttpServletResponse();
103   }
104 
105   public ServletConfig getServletConfig() {
106     return null;
107   }
108 
109   public ServletContext getServletContext() {
110     return servletContext;
111   }
112 
113   public HttpSession getSession() {
114     return null;
115   }
116 
117   public void handlePageException(Exception e) throws ServletException, IOException {
118   }
119 
120   public void handlePageException(Throwable e) {
121   }
122 
123   public void include(String s) throws ServletException, IOException {
124   }
125 
126   public void initialize(Servlet servlet, ServletRequest request, ServletResponse response, String s, boolean b, int i,
127       boolean b1) throws IOException, IllegalStateException, IllegalArgumentException {
128   }
129 
130   public void release() {
131   }
132 
133   public void removeAttribute(String s) {
134     attributes.remove(s);
135   }
136 
137   public void removeAttribute(String s, int i) {
138   }
139 
140   public void setAttribute(String s, Object o) {
141     attributes.put(s, o);
142   }
143 
144   public void setAttribute(String s, Object o, int i) {
145     switch (i) {
146       case PageContext.REQUEST_SCOPE:
147         getRequest().setAttribute(s, o);
148         break;
149       default:
150         // other not supported
151     }
152   }
153 
154   public void include(String reference, boolean b) throws ServletException, IOException {
155   }
156 
157   public ExpressionEvaluator getExpressionEvaluator() {
158     return null;
159   }
160 
161   public VariableResolver getVariableResolver() {
162     return null;
163   }
164 
165   public ELContext getELContext() {
166     return null;
167   }
168 }