1   /*
2    * Copyright 2004-2008 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.servlet.Servlet;
25  import javax.servlet.ServletConfig;
26  import javax.servlet.ServletContext;
27  import javax.servlet.ServletException;
28  import javax.servlet.ServletRequest;
29  import javax.servlet.ServletResponse;
30  import javax.servlet.http.HttpSession;
31  import javax.servlet.jsp.JspWriter;
32  import javax.servlet.jsp.PageContext;
33  import javax.servlet.jsp.el.ExpressionEvaluator;
34  import javax.servlet.jsp.el.VariableResolver;
35  import javax.servlet.jsp.tagext.BodyContent;
36  
37  import org.seasar.framework.mock.servlet.MockHttpServletRequest;
38  import org.seasar.framework.mock.servlet.MockHttpServletRequestImpl;
39  import org.seasar.framework.mock.servlet.MockHttpServletResponse;
40  import org.seasar.framework.mock.servlet.MockHttpServletResponseImpl;
41  import org.seasar.framework.mock.servlet.MockServletContext;
42  import org.seasar.framework.mock.servlet.MockServletContextImpl;
43  
44  public class MockJspContext extends PageContext {
45  
46  	private MockServletContext servletContext = new MockServletContextImpl(
47  			"cubby");
48  	private MockHttpServletRequest request = new MockHttpServletRequestImpl(
49  			servletContext, "/mock");
50  	private MockHttpServletResponse response = new MockHttpServletResponseImpl(request);
51  	private JspWriter writer = new MockJspWriter();
52  	private Stack<JspWriter> outStack = new Stack<JspWriter>();
53  	private Map<Integer, Map<String, Object>> attributes = new HashMap<Integer, Map<String, Object>>();
54  	private int[] FIND_ATTRIBUTE_SEQ = { PageContext.PAGE_SCOPE,
55  			PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE,
56  			PageContext.APPLICATION_SCOPE };
57  
58  	public MockJspContext() {
59  		for (int scope : FIND_ATTRIBUTE_SEQ) {
60  			attributes.put(scope, new HashMap<String, Object>());
61  		}
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 ExpressionEvaluator getExpressionEvaluator() {
108 		throw new UnsupportedOperationException();
109 	}
110 
111 	@Override
112 	public JspWriter getOut() {
113 		return writer;
114 	}
115 
116 	@Override
117 	public VariableResolver getVariableResolver() {
118 		throw new UnsupportedOperationException();
119 	}
120 
121 	@Override
122 	public void removeAttribute(String name) {
123 		throw new UnsupportedOperationException();
124 	}
125 
126 	@Override
127 	public void removeAttribute(String name, int scope) {
128 		this.attributes.get(scope).remove(name);
129 	}
130 
131 	@Override
132 	public void setAttribute(String name, Object value) {
133 		setAttribute(name, value, PageContext.PAGE_SCOPE);
134 	}
135 
136 	@Override
137 	public void setAttribute(String name, Object value, int scope) {
138 		attributes.get(scope).put(name, value);
139 	}
140 
141 	@Override
142 	public void forward(String relativeUrlPath) throws ServletException,
143 			IOException {
144 		// TODO 自動生成されたメソッド・スタブ
145 
146 	}
147 
148 	@Override
149 	public Exception getException() {
150 		// TODO 自動生成されたメソッド・スタブ
151 		return null;
152 	}
153 
154 	@Override
155 	public Object getPage() {
156 		// TODO 自動生成されたメソッド・スタブ
157 		return null;
158 	}
159 
160 	@Override
161 	public ServletRequest getRequest() {
162 		return request;
163 	}
164 
165 	@Override
166 	public ServletResponse getResponse() {
167 		return response;
168 	}
169 
170 	@Override
171 	public ServletConfig getServletConfig() {
172 		// TODO 自動生成されたメソッド・スタブ
173 		return null;
174 	}
175 
176 	@Override
177 	public ServletContext getServletContext() {
178 		return servletContext;
179 	}
180 
181 	@Override
182 	public HttpSession getSession() {
183 		return request.getSession();
184 	}
185 
186 	@Override
187 	public void handlePageException(Exception e) throws ServletException,
188 			IOException {
189 		// TODO 自動生成されたメソッド・スタブ
190 
191 	}
192 
193 	@Override
194 	public void handlePageException(Throwable t) throws ServletException,
195 			IOException {
196 		// TODO 自動生成されたメソッド・スタブ
197 
198 	}
199 
200 	@Override
201 	public void include(String relativeUrlPath) throws ServletException,
202 			IOException {
203 		// TODO 自動生成されたメソッド・スタブ
204 
205 	}
206 
207 	@Override
208 	public void include(String relativeUrlPath, boolean flush)
209 			throws ServletException, IOException {
210 		// TODO 自動生成されたメソッド・スタブ
211 
212 	}
213 
214 	@Override
215 	public void initialize(Servlet servlet, ServletRequest request,
216 			ServletResponse response, String errorPageURL,
217 			boolean needsSession, int bufferSize, boolean autoFlush)
218 			throws IOException, IllegalStateException, IllegalArgumentException {
219 		// TODO 自動生成されたメソッド・スタブ
220 
221 	}
222 
223 	@Override
224 	public void release() {
225 		// TODO 自動生成されたメソッド・スタブ
226 
227 	}
228 
229 	public JspWriter popBody() {
230 		writer = outStack.pop();
231 		return writer;
232 	}
233 
234 	public BodyContent pushBody() {
235 		outStack.push(writer);
236 		writer = new MockBodyContent(writer);
237 		return (BodyContent) writer;
238 	}
239 
240 }