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(final ServletRequest servletRequest) {
53      this.request = servletRequest;
54    }
55  
56    public Object findAttribute(final String s) {
57      return null;
58    }
59  
60    public void forward(final String s) throws ServletException, IOException {
61    }
62  
63    public Object getAttribute(final String s) {
64      return attributes.get(s);
65    }
66  
67    public Object getAttribute(final String name, final 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(final int i) {
78      return null;
79    }
80  
81    public int getAttributesScope(final 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(final Exception e) throws ServletException, IOException {
118   }
119 
120   public void handlePageException(final Throwable e) {
121   }
122 
123   public void include(final String s) throws ServletException, IOException {
124   }
125 
126   public void initialize(
127       final Servlet servlet, final ServletRequest request, final ServletResponse response, final String s,
128       final boolean b, final int i,
129       final boolean b1) throws IOException, IllegalStateException, IllegalArgumentException {
130   }
131 
132   public void release() {
133   }
134 
135   public void removeAttribute(final String s) {
136     attributes.remove(s);
137   }
138 
139   public void removeAttribute(final String s, final int i) {
140   }
141 
142   public void setAttribute(final String s, final Object o) {
143     attributes.put(s, o);
144   }
145 
146   public void setAttribute(final String s, final Object o, final int i) {
147     switch (i) {
148       case PageContext.REQUEST_SCOPE:
149         getRequest().setAttribute(s, o);
150         break;
151       default:
152         // other not supported
153     }
154   }
155 
156   public void include(final String reference, final boolean b) throws ServletException, IOException {
157   }
158 
159   public ExpressionEvaluator getExpressionEvaluator() {
160     return null;
161   }
162 
163   public VariableResolver getVariableResolver() {
164     return null;
165   }
166 
167   public ELContext getELContext() {
168     return null;
169   }
170 }