1 16 17 package org.springframework.mock.web; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 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.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 import javax.servlet.jsp.JspWriter ; 33 import javax.servlet.jsp.PageContext ; 34 import javax.servlet.jsp.el.ExpressionEvaluator ; 35 import javax.servlet.jsp.el.VariableResolver ; 36 37 import org.springframework.util.Assert; 38 39 52 public class MockPageContext extends PageContext { 53 54 private final ServletContext servletContext; 55 56 private final HttpServletRequest request; 57 58 private final HttpServletResponse response; 59 60 private final ServletConfig servletConfig; 61 62 private final Hashtable attributes = new Hashtable (); 63 64 65 70 public MockPageContext() { 71 this(null, null, null, null); 72 } 73 74 80 public MockPageContext(ServletContext servletContext) { 81 this(servletContext, null, null, null); 82 } 83 84 91 public MockPageContext(ServletContext servletContext, HttpServletRequest request) { 92 this(servletContext, request, null, null); 93 } 94 95 102 public MockPageContext(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) { 103 this(servletContext, request, response, null); 104 } 105 106 113 public MockPageContext(ServletContext servletContext, HttpServletRequest request, 114 HttpServletResponse response, ServletConfig servletConfig) { 115 116 this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); 117 this.request = (request != null ? request : new MockHttpServletRequest(servletContext)); 118 this.response = (response != null ? response : new MockHttpServletResponse()); 119 this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext)); 120 } 121 122 123 public void initialize( 124 Servlet servlet, ServletRequest request, ServletResponse response, 125 String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) { 126 127 throw new UnsupportedOperationException ("Use appropriate constructor"); 128 } 129 130 public void release() { 131 } 132 133 public void setAttribute(String name, Object value) { 134 Assert.notNull(name, "Attribute name must not be null"); 135 if (value != null) { 136 this.attributes.put(name, value); 137 } 138 else { 139 this.attributes.remove(name); 140 } 141 } 142 143 public void setAttribute(String name, Object value, int scope) { 144 Assert.notNull(name, "Attribute name must not be null"); 145 switch (scope) { 146 case PAGE_SCOPE: 147 setAttribute(name, value); 148 break; 149 case REQUEST_SCOPE: 150 this.request.setAttribute(name, value); 151 break; 152 case SESSION_SCOPE: 153 this.request.getSession().setAttribute(name, value); 154 break; 155 case APPLICATION_SCOPE: 156 this.servletContext.setAttribute(name, value); 157 break; 158 default: 159 throw new IllegalArgumentException ("Invalid scope: " + scope); 160 } 161 } 162 163 public Object getAttribute(String name) { 164 Assert.notNull(name, "Attribute name must not be null"); 165 return this.attributes.get(name); 166 } 167 168 public Object getAttribute(String name, int scope) { 169 Assert.notNull(name, "Attribute name must not be null"); 170 switch (scope) { 171 case PAGE_SCOPE: 172 return getAttribute(name); 173 case REQUEST_SCOPE: 174 return this.request.getAttribute(name); 175 case SESSION_SCOPE: 176 HttpSession session = this.request.getSession(false); 177 return (session != null ? session.getAttribute(name) : null); 178 case APPLICATION_SCOPE: 179 return this.servletContext.getAttribute(name); 180 default: 181 throw new IllegalArgumentException ("Invalid scope: " + scope); 182 } 183 } 184 185 public Object findAttribute(String name) { 186 Object value = getAttribute(name); 187 if (value == null) { 188 value = getAttribute(name, REQUEST_SCOPE); 189 if (value == null) { 190 value = getAttribute(name, SESSION_SCOPE); 191 if (value == null) { 192 value = getAttribute(name, APPLICATION_SCOPE); 193 } 194 } 195 } 196 return value; 197 } 198 199 public void removeAttribute(String name) { 200 Assert.notNull(name, "Attribute name must not be null"); 201 this.removeAttribute(name, PageContext.PAGE_SCOPE); 202 this.removeAttribute(name, PageContext.REQUEST_SCOPE); 203 this.removeAttribute(name, PageContext.SESSION_SCOPE); 204 this.removeAttribute(name, PageContext.APPLICATION_SCOPE); 205 } 206 207 public void removeAttribute(String name, int scope) { 208 Assert.notNull(name, "Attribute name must not be null"); 209 switch (scope) { 210 case PAGE_SCOPE: 211 this.attributes.remove(name); 212 break; 213 case REQUEST_SCOPE: 214 this.request.removeAttribute(name); 215 break; 216 case SESSION_SCOPE: 217 this.request.getSession().removeAttribute(name); 218 break; 219 case APPLICATION_SCOPE: 220 this.servletContext.removeAttribute(name); 221 break; 222 default: 223 throw new IllegalArgumentException ("Invalid scope: " + scope); 224 } 225 } 226 227 public int getAttributesScope(String name) { 228 if (getAttribute(name) != null) { 229 return PAGE_SCOPE; 230 } 231 else if (getAttribute(name, REQUEST_SCOPE) != null) { 232 return REQUEST_SCOPE; 233 } 234 else if (getAttribute(name, SESSION_SCOPE) != null) { 235 return SESSION_SCOPE; 236 } 237 else if (getAttribute(name, APPLICATION_SCOPE) != null) { 238 return APPLICATION_SCOPE; 239 } 240 else { 241 return 0; 242 } 243 } 244 245 public Enumeration getAttributeNames() { 246 return this.attributes.keys(); 247 } 248 249 public Enumeration getAttributeNamesInScope(int scope) { 250 switch (scope) { 251 case PAGE_SCOPE: 252 return getAttributeNames(); 253 case REQUEST_SCOPE: 254 return this.request.getAttributeNames(); 255 case SESSION_SCOPE: 256 HttpSession session = this.request.getSession(false); 257 return (session != null ? session.getAttributeNames() : null); 258 case APPLICATION_SCOPE: 259 return this.servletContext.getAttributeNames(); 260 default: 261 throw new IllegalArgumentException ("Invalid scope: " + scope); 262 } 263 } 264 265 public JspWriter getOut() { 266 throw new UnsupportedOperationException ("getOut"); 267 } 268 269 public ExpressionEvaluator getExpressionEvaluator() { 270 return new MockExpressionEvaluator(this); 271 } 272 273 public VariableResolver getVariableResolver() { 274 return null; 275 } 276 277 public HttpSession getSession() { 278 return this.request.getSession(); 279 } 280 281 public Object getPage() { 282 throw new UnsupportedOperationException ("getPage"); 283 } 284 285 public ServletRequest getRequest() { 286 return request; 287 } 288 289 public ServletResponse getResponse() { 290 return response; 291 } 292 293 public Exception getException() { 294 throw new UnsupportedOperationException ("getException"); 295 } 296 297 public ServletConfig getServletConfig() { 298 return servletConfig; 299 } 300 301 public ServletContext getServletContext() { 302 return servletContext; 303 } 304 305 public void forward(String url) throws ServletException , IOException { 306 throw new UnsupportedOperationException ("forward"); 307 } 308 309 public void include(String url) throws ServletException , IOException { 310 throw new UnsupportedOperationException ("include"); 311 } 312 313 public void include(String url, boolean flush) throws ServletException , IOException { 314 throw new UnsupportedOperationException ("include"); 315 } 316 317 public void handlePageException(Exception ex) throws ServletException , IOException { 318 throw new UnsupportedOperationException ("handlePageException"); 319 } 320 321 public void handlePageException(Throwable ex) throws ServletException , IOException { 322 throw new UnsupportedOperationException ("handlePageException"); 323 } 324 325 } 326 | Popular Tags |