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