1 7 package com.inversoft.junit.internal.http; 8 9 10 import java.io.IOException ; 11 import java.util.Enumeration ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.servlet.Servlet ; 18 import javax.servlet.ServletConfig ; 19 import javax.servlet.ServletContext ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.ServletRequest ; 22 import javax.servlet.ServletResponse ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.PageContext ; 27 28 29 38 public class MockPageContext extends PageContext { 39 40 private ServletConfig config; 41 private ServletRequest request; 42 private ServletResponse response; 43 private Map attributes; 44 private Throwable t; 45 private JspWriter out = new MockJspWriter(MockJspWriter.NO_BUFFER, true); 46 47 48 51 public MockPageContext(ServletConfig config, ServletRequest request, 52 ServletResponse response) { 53 super(); 54 this.config = config; 55 this.request = request; 56 this.response = response; 57 this.attributes = new HashMap (); 58 } 59 60 61 63 public void initialize(Servlet servlet, ServletRequest request, 64 ServletResponse response, String errorPageURL, boolean session, 65 int bufferSize, boolean autoFlush) 66 throws IOException , IllegalStateException , IllegalArgumentException { 67 this.config = servlet.getServletConfig(); 68 this.request = request; 69 this.response = response; 70 } 75 76 79 public void release() { 80 } 81 82 85 public void setAttribute(String name, Object value) { 86 attributes.put(name, value); 87 } 88 89 92 public void setAttribute(String name, Object value, int scope) { 93 switch (scope) { 94 case PAGE_SCOPE: 95 setAttribute(name, value); 96 break; 97 case REQUEST_SCOPE: 98 request.setAttribute(name, value); 99 break; 100 case SESSION_SCOPE: 101 ((HttpServletRequest ) request).getSession().setAttribute(name, value); 102 break; 103 case APPLICATION_SCOPE: 104 ((HttpServletRequest ) request).getSession().getServletContext().setAttribute(name, value); 105 break; 106 default: 107 throw new IllegalArgumentException ("Invalid scope"); 108 } 109 } 110 111 114 public Object getAttribute(String name) { 115 return attributes.get(name); 116 } 117 118 121 public Object getAttribute(String name, int scope) { 122 Object value = null; 123 switch (scope) { 124 case PAGE_SCOPE: 125 value = getAttribute(name); 126 break; 127 case REQUEST_SCOPE: 128 value = request.getAttribute(name); 129 break; 130 case SESSION_SCOPE: 131 value = ((HttpServletRequest ) request).getSession().getAttribute(name); 132 break; 133 case APPLICATION_SCOPE: 134 value = ((HttpServletRequest ) request).getSession().getServletContext().getAttribute(name); 135 break; 136 default: 137 throw new IllegalArgumentException ("Invalid scope"); 138 } 139 return value; 140 } 141 142 145 public Object findAttribute(String name) { 146 Object value = getAttribute(name); 147 if (value == null) { 148 value = request.getAttribute(name); 149 } 150 151 if (value == null) { 152 value = ((HttpServletRequest ) request).getSession().getAttribute(name); 153 } 154 155 if (value == null) { 156 value = ((HttpServletRequest ) request).getSession().getServletContext().getAttribute(name); 157 } 158 return value; 159 } 160 161 164 public void removeAttribute(String name) { 165 attributes.remove(name); 166 } 167 168 171 public void removeAttribute(String name, int scope) { 172 switch (scope) { 173 case PAGE_SCOPE: 174 removeAttribute(name); 175 case REQUEST_SCOPE: 176 request.removeAttribute(name); 177 break; 178 case SESSION_SCOPE: 179 ((HttpServletRequest ) request).getSession().removeAttribute(name); 180 break; 181 case APPLICATION_SCOPE: 182 ((HttpServletRequest ) request).getSession().getServletContext().removeAttribute(name); 183 break; 184 default: 185 throw new IllegalArgumentException ("Invalid scope"); 186 } 187 } 188 189 192 public int getAttributesScope(String name) { 193 Object value = getAttribute(name); 194 if (value == null) { 195 value = request.getAttribute(name); 196 } else { 197 return PAGE_SCOPE; 198 } 199 200 if (value == null) { 201 value = ((HttpServletRequest ) request).getSession().getAttribute(name); 202 } else { 203 return REQUEST_SCOPE; 204 } 205 206 if (value == null) { 207 value = ((HttpServletRequest ) request).getSession().getServletContext().getAttribute(name); 208 } else { 209 return SESSION_SCOPE; 210 } 211 212 if (value != null) { 213 return APPLICATION_SCOPE; 214 } 215 return 0; 216 } 217 218 221 public Enumeration getAttributeNamesInScope(int scope) { 222 switch (scope) { 223 case PAGE_SCOPE: 224 Set keys = attributes.keySet(); 225 final Iterator iter = keys.iterator(); 226 227 return new Enumeration () { 228 public boolean hasMoreElements() { 229 return iter.hasNext(); 230 } 231 public Object nextElement() { 232 return iter.next(); 233 } 234 }; 235 case REQUEST_SCOPE: 236 return request.getAttributeNames(); 237 case SESSION_SCOPE: 238 return ((HttpServletRequest ) request).getSession().getAttributeNames(); 239 case APPLICATION_SCOPE: 240 return ((HttpServletRequest ) request).getSession().getServletContext().getAttributeNames(); 241 default: 242 throw new IllegalArgumentException ("Invalid scope"); 243 } 244 } 245 246 248 public JspWriter getOut() { 249 return out; 250 } 251 252 255 public HttpSession getSession() { 256 return ((HttpServletRequest ) request).getSession(); 257 } 258 259 262 public Object getPage() { 263 return null; 264 } 265 266 269 public ServletRequest getRequest() { 270 return request; 271 } 272 273 276 public ServletResponse getResponse() { 277 return response; 278 } 279 280 283 public Exception getException() { 284 return (Exception ) t; 285 } 286 287 290 public ServletConfig getServletConfig() { 291 return config; 292 } 293 294 297 public ServletContext getServletContext() { 298 return ((HttpServletRequest ) request).getSession().getServletContext(); 299 } 300 301 304 public void forward(String url) throws ServletException , IOException { 305 } 306 307 310 public void include(String url) throws ServletException , IOException { 311 } 312 313 316 public void handlePageException(Exception e) 317 throws ServletException , IOException { 318 this.t = e; 319 } 320 321 324 public void handlePageException(Throwable t) 325 throws ServletException , IOException { 326 this.t = t; 327 } 328 329 330 334 337 public MockJspWriter getMockOut() { 338 return (MockJspWriter) out; 339 } 340 341 344 public void clearMockOut() { 345 ((MockJspWriter) out).clear(); 346 } 347 } | Popular Tags |