1 package com.mockobjects.servlet; 2 3 import com.mockobjects.*; 4 5 import javax.servlet.ServletInputStream ; 6 import javax.servlet.RequestDispatcher ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpSession ; 9 import javax.servlet.http.Cookie ; 10 import java.util.*; 11 import java.io.BufferedReader ; 12 import java.io.IOException ; 13 import java.security.Principal ; 14 15 import com.mockobjects.ReturnValue; 16 17 20 public class MockHttpServletRequest extends MockObject 21 implements HttpServletRequest { 22 23 private final ReturnObjectBag myParameters = new ReturnObjectBag("parameters"); 24 private final ReturnObjectBag myHeaders = new ReturnObjectBag("headers"); 25 private final ReturnValue myHttpSession = new ReturnValue("session"); 26 private final ReturnValue myContentTypeToReturn = new ReturnValue("content type"); 27 private final ReturnValue myContextPath = new ReturnValue("context path"); 28 private final ReturnValue myPathInfo = new ReturnValue("path info"); 29 private final ReturnValue myRemoteAddress = new ReturnValue("remote address"); 30 private final ReturnValue myRequestURI = new ReturnValue("request uri"); 31 private final ReturnValue method = new ReturnValue("method"); 32 private final ReturnValue protocol = new ReturnValue("protocol"); 33 private final ReturnValue inputStream = new ReturnValue("input stream"); 34 private final ReturnValue myUserPrincipal = new ReturnValue("user principal"); 35 private final ReturnValue myAttributesNames = new ReturnValue("attribute names"); 36 private final ReturnValue queryString = new ReturnValue("query string"); 37 private final ReturnValue scheme = new ReturnValue("scheme"); 38 private final ReturnValue serverName = new ReturnValue("server name"); 39 private final ReturnValue reader = new ReturnValue("reader"); 40 private final ExpectationSet mySetAttributes = new ExpectationSet("HttpServletRequest.setAttribute"); 41 private final ExpectationSet myRemoveAttributes = new ExpectationSet("HttpServletRequest.removeAttribute"); 42 private final ReturnObjectList myAttributesToReturn = new ReturnObjectList("attributes"); 43 private final ExpectationValue myContentType = new ExpectationValue("content type"); 44 private final ExpectationList myGetAttributeNames = new ExpectationList("get attribute names"); 45 private final ReturnValue servletPath = new ReturnValue("servlet path"); 46 private final ReturnValue parameterMap = new ReturnValue("parameter map"); 47 private final ReturnValue myParameterNames = new ReturnValue("parameter names"); 48 private final ReturnValue requestDispatcher = new ReturnValue("request dispatcher"); 49 private final ExpectationValue requestDispatcherURI = new ExpectationValue("request dispatcher uri"); 50 private final ExpectationValue createSession = new ExpectationValue("create session"); 51 52 public void setupGetAttribute(Object anAttributeToReturn) { 53 myAttributesToReturn.addObjectToReturn(anAttributeToReturn); 54 } 55 56 public void addExpectedGetAttributeName(String anAttributeName) { 57 myGetAttributeNames.addExpected(anAttributeName); 58 } 59 60 public Object getAttribute(String anAttributeName) { 61 myGetAttributeNames.addActual(anAttributeName); 62 return myAttributesToReturn.nextReturnObject(); 63 } 64 65 public void setupGetAttrubuteNames(Enumeration attributeNames) { 66 myAttributesNames.setValue(attributeNames); 67 } 68 69 public Enumeration getAttributeNames() { 70 return (Enumeration) myAttributesNames.getValue(); 71 } 72 73 public String getAuthType() { 74 notImplemented(); 75 return null; 76 } 77 78 public String getCharacterEncoding() { 79 notImplemented(); 80 return null; 81 } 82 83 public int getContentLength() { 84 notImplemented(); 85 return 0; 86 } 87 88 public String getContentType() { 89 return (String ) myContentTypeToReturn.getValue(); 90 } 91 92 public void setupGetContentType(String aContentType) { 93 myContentTypeToReturn.setValue(aContentType); 94 } 95 96 public void setExpectedContentType(String aContentType) { 97 this.myContentType.setExpected(aContentType); 98 } 99 100 public void setContentType(String contentType) { 101 this.myContentType.setActual(contentType); 102 } 103 104 public String getContextPath() { 105 return (String ) myContextPath.getValue(); 106 } 107 108 public void setupGetContextPath(String contextPath) { 109 myContextPath.setValue(contextPath); 110 } 111 112 public Cookie [] getCookies() { 113 notImplemented(); 114 return null; 115 } 116 117 public long getDateHeader(String arg1) { 118 notImplemented(); 119 return 0; 120 } 121 122 public String getHeader(String key) { 123 return (String ) myHeaders.getNextReturnObject(key); 124 } 125 126 public Enumeration getHeaderNames() { 127 notImplemented(); 128 return null; 129 } 130 131 public Enumeration getHeaders(String arg1) { 132 notImplemented(); 133 return null; 134 } 135 136 public void setupGetInputStream(ServletInputStream inputStream) { 137 this.inputStream.setValue(inputStream); 138 } 139 140 public ServletInputStream getInputStream() 141 throws IOException { 142 return (ServletInputStream ) inputStream.getValue(); 143 } 144 145 public int getIntHeader(String arg1) { 146 notImplemented(); 147 return 0; 148 } 149 150 public Locale getLocale() { 151 notImplemented(); 152 return null; 153 } 154 155 public Enumeration getLocales() { 156 notImplemented(); 157 return null; 158 } 159 160 public void setupGetMethod(String aMethod) { 161 this.method.setValue(aMethod); 162 } 163 164 public String getMethod() { 165 return (String ) method.getValue(); 166 } 167 168 public String getParameter(String paramName) { 169 String [] values = getParameterValues(paramName); 170 if (values == null) 171 return null; 172 173 return values[0]; 174 } 175 176 public void setupGetParameterNames(Enumeration names) { 177 this.myParameterNames.setValue(names); 178 } 179 180 public Enumeration getParameterNames() { 181 return (Enumeration) myParameterNames.getValue(); 182 } 183 184 public String [] getParameterValues(String key) { 185 return (String []) myParameters.getNextReturnObject(key); 186 } 187 188 public String getPathInfo() { 189 return (String ) myPathInfo.getValue(); 190 } 191 192 public String getPathTranslated() { 193 notImplemented(); 194 return null; 195 } 196 197 public String getProtocol() { 198 return (String ) protocol.getValue(); 199 } 200 201 public void setupGetProtocol(String protocol) { 202 this.protocol.setValue(protocol); 203 } 204 205 public String getQueryString() { 206 return (String ) queryString.getValue(); 207 } 208 209 public BufferedReader getReader() { 210 return (BufferedReader ) reader.getValue(); 211 } 212 213 public void setupGetReader(BufferedReader reader) throws IOException { 214 this.reader.setValue(reader); 215 } 216 217 public String getRealPath(String arg1) { 218 notImplemented(); 219 return null; 220 } 221 222 public void setupGetRemoteAddr(String remoteAddress) { 223 this.myRemoteAddress.setValue(remoteAddress); 224 } 225 226 public String getRemoteAddr() { 227 return (String ) myRemoteAddress.getValue(); 228 } 229 230 public String getRemoteHost() { 231 notImplemented(); 232 return null; 233 } 234 235 public String getRemoteUser() { 236 notImplemented(); 237 return null; 238 } 239 240 public void setupGetRequestDispatcher(RequestDispatcher requestDispatcher) { 241 this.requestDispatcher.setValue(requestDispatcher); 242 } 243 244 public RequestDispatcher getRequestDispatcher(String uri) { 245 this.requestDispatcherURI.setActual(uri); 246 return (RequestDispatcher ) requestDispatcher.getValue(); 247 } 248 249 public void setExpectedRequestDispatcherURI(String uri) { 250 this.requestDispatcherURI.setExpected(uri); 251 } 252 253 public String getRequestedSessionId() { 254 notImplemented(); 255 return null; 256 } 257 258 public void setupGetRequestURI(String aRequestURI) { 259 myRequestURI.setValue(aRequestURI); 260 } 261 262 public String getRequestURI() { 263 return (String ) myRequestURI.getValue(); 264 } 265 266 public String getScheme() { 267 return (String ) scheme.getValue(); 268 } 269 270 public String getServerName() { 271 return (String ) serverName.getValue(); 272 } 273 274 public int getServerPort() { 275 notImplemented(); 276 return 0; 277 } 278 279 public void setupGetServletPath(String path) { 280 this.servletPath.setValue(path); 281 } 282 283 public String getServletPath() { 284 return (String ) servletPath.getValue(); 285 } 286 287 public HttpSession getSession() { 288 return (HttpSession ) myHttpSession.getValue(); 289 } 290 291 public void setSession(HttpSession httpSession) { 292 this.myHttpSession.setValue(httpSession); 293 } 294 295 public void setExpectedCreateSession(boolean createSession) { 296 this.createSession.setExpected(createSession); 297 } 298 299 public HttpSession getSession(boolean createSession) { 300 this.createSession.setActual(createSession); 301 return getSession(); 302 } 303 304 public void setupGetUserPrincipal(Principal userPrincipal) { 305 this.myUserPrincipal.setValue(userPrincipal); 306 } 307 308 public Principal getUserPrincipal() { 309 return (Principal ) myUserPrincipal.getValue(); 310 } 311 312 public boolean isRequestedSessionIdFromCookie() { 313 notImplemented(); 314 return false; 315 } 316 317 public boolean isRequestedSessionIdFromUrl() { 318 notImplemented(); 319 return false; 320 } 321 322 public boolean isRequestedSessionIdFromURL() { 323 notImplemented(); 324 return false; 325 } 326 327 public boolean isRequestedSessionIdValid() { 328 notImplemented(); 329 return false; 330 } 331 332 public boolean isSecure() { 333 notImplemented(); 334 return false; 335 } 336 337 public boolean isUserInRole(java.lang.String arg1) { 338 notImplemented(); 339 return false; 340 } 341 342 public void setupRemoveAttribute(String anAttributeToRemove) { 343 myRemoveAttributes.addExpected(anAttributeToRemove); 344 } 345 346 public void removeAttribute(String anAttributeToRemove) { 347 myRemoveAttributes.addActual(anAttributeToRemove); 348 } 349 350 public void addExpectedSetAttribute(String attributeName, 351 Object attributeValue) { 352 mySetAttributes.addExpected( 353 new MapEntry(attributeName, attributeValue)); 354 } 355 356 public void setAttribute(String attributeName, Object attributeValue) { 357 mySetAttributes.addActual( 358 new MapEntry(attributeName, attributeValue)); 359 } 360 361 public void setupAddParameter(String paramName, String [] values) { 362 myParameters.putObjectToReturn(paramName, values); 363 } 364 365 public void setupAddParameter(String paramName, String value) { 366 setupAddParameter(paramName, new String []{value}); 367 } 368 369 public void setupAddHeader(String headerName, String value) { 370 myHeaders.putObjectToReturn(headerName, value); 371 } 372 373 public void setupPathInfo(String pathInfo) { 374 myPathInfo.setValue(pathInfo); 375 } 376 377 public void setupQueryString(String aQueryString) { 378 queryString.setValue(aQueryString); 379 } 380 381 public void setupScheme(String aScheme) { 382 scheme.setValue(aScheme); 383 } 384 385 public void setupServerName(String aServerName) { 386 serverName.setValue(aServerName); 387 } 388 389 public StringBuffer getRequestURL() { 390 notImplemented(); 391 return null; 392 } 393 394 public void setCharacterEncoding(String s) { 395 notImplemented(); 396 } 397 398 public void setupGetParameterMap(Map map) { 399 this.parameterMap.setValue(map); 400 } 401 402 public Map getParameterMap() { 403 return (Map) parameterMap.getValue(); 404 } 405 } 406 | Popular Tags |