1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.tags;
17  
18  import java.io.IOException;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Stack;
23  
24  import javax.el.ELContext;
25  import javax.servlet.Servlet;
26  import javax.servlet.ServletConfig;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletException;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletResponse;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  import javax.servlet.jsp.JspWriter;
35  import javax.servlet.jsp.PageContext;
36  import javax.servlet.jsp.tagext.BodyContent;
37  
38  public class MockJspContext extends PageContext {
39  
40  	private ServletContext servletContext;
41  	private HttpServletRequest request;
42  	private HttpServletResponse response;
43  	private JspWriter writer = new MockJspWriter();
44  	private Stack<JspWriter> outStack = new Stack<JspWriter>();
45  	private Map<Integer, Map<String, Object>> attributes = new HashMap<Integer, Map<String, Object>>();
46  	private int[] FIND_ATTRIBUTE_SEQ = { PageContext.PAGE_SCOPE,
47  			PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE,
48  			PageContext.APPLICATION_SCOPE };
49  
50  	public MockJspContext() {
51  		this(null, null, null);
52  	}
53  
54  	public MockJspContext(ServletContext servletContext,
55  			HttpServletRequest request, HttpServletResponse response) {
56  		for (int scope : FIND_ATTRIBUTE_SEQ) {
57  			attributes.put(scope, new HashMap<String, Object>());
58  		}
59  		this.servletContext = servletContext;
60  		this.request = request;
61  		this.response = response;
62  	}
63  
64  	public MockJspWriter getMockJspWriter() {
65  		return (MockJspWriter) this.writer;
66  	}
67  
68  	public String getResult() {
69  		return getMockJspWriter().getResult();
70  	}
71  
72  	@Override
73  	public Object findAttribute(String name) {
74  		Object value = null;
75  		for (int scope : FIND_ATTRIBUTE_SEQ) {
76  			value = getAttribute(name, scope);
77  			if (value != null) {
78  				return value;
79  			}
80  		}
81  		return null;
82  	}
83  
84  	@Override
85  	public Object getAttribute(String name) {
86  		return getAttribute(name, PageContext.PAGE_SCOPE);
87  	}
88  
89  	@Override
90  	public Object getAttribute(String name, int scope) {
91  		Map<String, Object> scopeMap = attributes.get(scope);
92  		return scopeMap != null ? scopeMap.get(name) : null;
93  	}
94  
95  	@SuppressWarnings("unchecked")
96  	@Override
97  	public Enumeration getAttributeNamesInScope(int scope) {
98  		throw new UnsupportedOperationException();
99  	}
100 
101 	@Override
102 	public int getAttributesScope(String name) {
103 		throw new UnsupportedOperationException();
104 	}
105 
106 	@Override
107 	public JspWriter getOut() {
108 		return writer;
109 	}
110 
111 	@Override
112 	public void removeAttribute(String name) {
113 		throw new UnsupportedOperationException();
114 	}
115 
116 	@Override
117 	public void removeAttribute(String name, int scope) {
118 		this.attributes.get(scope).remove(name);
119 	}
120 
121 	@Override
122 	public void setAttribute(String name, Object value) {
123 		setAttribute(name, value, PageContext.PAGE_SCOPE);
124 	}
125 
126 	@Override
127 	public void setAttribute(String name, Object value, int scope) {
128 		attributes.get(scope).put(name, value);
129 	}
130 
131 	@Override
132 	public void forward(String relativeUrlPath) throws ServletException,
133 			IOException {
134 	}
135 
136 	@Override
137 	public Exception getException() {
138 		return null;
139 	}
140 
141 	@Override
142 	public Object getPage() {
143 		return null;
144 	}
145 
146 	@Override
147 	public ServletRequest getRequest() {
148 		return request;
149 	}
150 
151 	@Override
152 	public ServletResponse getResponse() {
153 		return response;
154 	}
155 
156 	@Override
157 	public ServletConfig getServletConfig() {
158 		return null;
159 	}
160 
161 	@Override
162 	public ServletContext getServletContext() {
163 		return servletContext;
164 	}
165 
166 	@Override
167 	public HttpSession getSession() {
168 		return request.getSession();
169 	}
170 
171 	@Override
172 	public void handlePageException(Exception e) throws ServletException,
173 			IOException {
174 	}
175 
176 	@Override
177 	public void handlePageException(Throwable t) throws ServletException,
178 			IOException {
179 	}
180 
181 	@Override
182 	public void include(String relativeUrlPath) throws ServletException,
183 			IOException {
184 	}
185 
186 	@Override
187 	public void include(String relativeUrlPath, boolean flush)
188 			throws ServletException, IOException {
189 	}
190 
191 	@Override
192 	public void initialize(Servlet servlet, ServletRequest request,
193 			ServletResponse response, String errorPageURL,
194 			boolean needsSession, int bufferSize, boolean autoFlush)
195 			throws IOException, IllegalStateException, IllegalArgumentException {
196 	}
197 
198 	@Override
199 	public void release() {
200 	}
201 
202 	public JspWriter popBody() {
203 		writer = outStack.pop();
204 		return writer;
205 	}
206 
207 	public BodyContent pushBody() {
208 		outStack.push(writer);
209 		writer = new MockBodyContent(writer);
210 		return (BodyContent) writer;
211 	}
212 
213 	@Override
214 	public ELContext getELContext() {
215 		throw new UnsupportedOperationException();
216 	}
217 
218 	@SuppressWarnings("deprecation")
219 	@Override
220 	public javax.servlet.jsp.el.ExpressionEvaluator getExpressionEvaluator() {
221 		throw new UnsupportedOperationException();
222 	}
223 
224 	@SuppressWarnings("deprecation")
225 	@Override
226 	public javax.servlet.jsp.el.VariableResolver getVariableResolver() {
227 		throw new UnsupportedOperationException();
228 	}
229 
230 }