1 7 package com.inversoft.junit.internal.http; 8 9 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.security.Principal ; 13 import java.util.Enumeration ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Locale ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import javax.servlet.RequestDispatcher ; 20 import javax.servlet.ServletInputStream ; 21 import javax.servlet.http.Cookie ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpSession ; 24 25 import com.inversoft.junit.Request; 26 import com.inversoft.junit.URL; 27 28 29 37 public class MockHttpServletRequest implements HttpServletRequest { 38 39 40 private Request request; 41 private Map attributes; 42 private String encoding; 43 private MockHttpSession session; 44 private MockRequestDispatcher dispatcher; 45 46 47 50 public MockHttpServletRequest(Request request) { 51 this.request = request; 52 this.attributes = new HashMap (); 53 this.session = new MockHttpSession(); 54 } 55 56 57 61 63 public Object getAttribute(String name) { 64 return attributes.get(name); 65 } 66 67 69 public Enumeration getAttributeNames() { 70 Set keys = attributes.keySet(); 71 final Iterator iter = keys.iterator(); 72 73 return new Enumeration () { 74 public boolean hasMoreElements() { 75 return iter.hasNext(); 76 } 77 public Object nextElement() { 78 return iter.next(); 79 } 80 }; 81 } 82 83 86 public String getCharacterEncoding() { 87 return encoding; 88 } 89 90 93 public void setCharacterEncoding(String encoding) { 94 this.encoding = encoding; 95 } 96 97 99 public int getContentLength() { 100 return -1; 101 } 102 103 105 public String getContentType() { 106 throw new UnsupportedOperationException (); 107 } 108 109 111 public ServletInputStream getInputStream() throws IOException { 112 throw new UnsupportedOperationException (); 113 } 114 115 117 public Locale getLocale() { 118 throw new UnsupportedOperationException (); 119 } 120 121 123 public Enumeration getLocales() { 124 throw new UnsupportedOperationException (); 125 } 126 127 129 public String getParameter(String name) { 130 return request.getParameter(name); 131 } 132 133 134 public Map getParameterMap() { 135 return request.getParameterMap(); 136 } 137 138 140 public Enumeration getParameterNames() { 141 return request.getParameterNames(); 142 } 143 144 146 public String [] getParameterValues(String name) { 147 return request.getParameterValues(name); 148 } 149 150 152 public String getProtocol() { 153 return request.getURL().getProtocol().toUpperCase() + "/1.0"; 154 } 155 156 158 public BufferedReader getReader() throws IOException { 159 throw new UnsupportedOperationException (); 160 } 161 162 165 public String getRealPath(String url) { 166 throw new UnsupportedOperationException (); 167 } 168 169 171 public String getRemoteAddr() { 172 throw new UnsupportedOperationException (); 173 } 174 175 177 public String getRemoteHost() { 178 throw new UnsupportedOperationException (); 179 } 180 181 183 public RequestDispatcher getRequestDispatcher(String thePath) 184 { 185 186 if (thePath == null) { 187 return null; 188 } 189 190 String fullPath; 191 192 if (thePath.startsWith("/")) { 196 197 fullPath = thePath; 198 199 } else { 200 201 String pI = getPathInfo(); 202 if (pI == null) { 203 fullPath = catPath(getServletPath(), thePath); 204 } else { 205 fullPath = catPath(getServletPath() + pI, thePath); 206 } 207 208 if (fullPath == null) { 209 return null; 210 } 211 } 212 213 dispatcher = new MockRequestDispatcher(fullPath); 214 return dispatcher; 215 } 216 217 219 public String getScheme() { 220 return request.getURL().getProtocol(); 221 } 222 223 225 public String getServerName() { 226 return request.getURL().getServerName(); 227 } 228 229 231 public int getServerPort() { 232 return request.getURL().getServerPort(); 233 } 234 235 237 public boolean isSecure() { 238 throw new UnsupportedOperationException (); 239 } 240 241 243 public void removeAttribute(String name) { 244 attributes.remove(name); 245 } 246 247 249 public void setAttribute(String name, Object value) { 250 attributes.put(name, value); 251 } 252 253 254 258 259 262 public String getAuthType() { 263 return null; 264 } 265 266 268 public String getContextPath() { 269 URL urlObj = request.getURL(); 270 String url = ""; 271 if (urlObj != null) { 272 url = urlObj.getContextPath(); 273 } 274 275 return url; 276 } 277 278 281 public Cookie [] getCookies() { 282 return request.getCookies(); 283 } 284 285 287 public long getDateHeader(String name) { 288 throw new UnsupportedOperationException (); 289 } 290 291 293 public String getHeader(String name) { 294 throw new UnsupportedOperationException (); 295 } 296 297 299 public Enumeration getHeaderNames() { 300 throw new UnsupportedOperationException (); 301 } 302 303 305 public Enumeration getHeaders(String name) { 306 throw new UnsupportedOperationException (); 307 } 308 309 311 public int getIntHeader(String name) { 312 throw new UnsupportedOperationException (); 313 } 314 315 317 public String getMethod() { 318 throw new UnsupportedOperationException (); 319 } 320 321 323 public String getPathInfo() { 324 if (request.getURL() == null) { 325 return null; 326 } 327 328 return request.getURL().getPathInfo(); 329 } 330 331 333 public String getPathTranslated() { 334 throw new UnsupportedOperationException (); 335 364 } 365 366 368 public String getQueryString() { 369 return request.getURL().getQueryString(); 370 } 371 372 374 public String getRemoteUser() { 375 throw new UnsupportedOperationException (); 376 } 377 378 380 public String getRequestedSessionId() { 381 throw new UnsupportedOperationException (); 382 } 383 384 386 public String getRequestURI() { 387 return getContextPath() + 388 ((getServletPath() == null) ? "" : getServletPath()) + 389 ((getPathInfo() == null) ? "" : getPathInfo()); 390 } 391 392 394 public StringBuffer getRequestURL() { 395 throw new UnsupportedOperationException (); 396 } 397 398 400 public String getServletPath() { 401 if (request.getURL() == null) { 402 return ""; 403 } 404 405 return request.getURL().getServletPath(); 406 } 407 408 410 public HttpSession getSession() { 411 return session; 412 } 413 414 416 public HttpSession getSession(boolean create) { 417 return session; 418 } 419 420 422 public Principal getUserPrincipal() { 423 throw new UnsupportedOperationException (); 424 } 425 426 428 public boolean isRequestedSessionIdFromCookie() { 429 throw new UnsupportedOperationException (); 430 } 431 432 435 public boolean isRequestedSessionIdFromUrl() { 436 throw new UnsupportedOperationException (); 437 } 438 439 441 public boolean isRequestedSessionIdFromURL() { 442 throw new UnsupportedOperationException (); 443 } 444 445 447 public boolean isRequestedSessionIdValid() { 448 throw new UnsupportedOperationException (); 449 } 450 451 453 public boolean isUserInRole(String role) { 454 throw new UnsupportedOperationException (); 455 } 456 457 461 462 471 String catPath(String theLookupPath, String thePath) 472 { 473 int index = theLookupPath.lastIndexOf("/"); 475 if (index == -1) { 476 return thePath; 477 } 478 479 theLookupPath = theLookupPath.substring(0, index); 480 481 while (thePath.startsWith("../")) { 483 if (theLookupPath.length() > 0) { 484 index = theLookupPath.lastIndexOf("/"); 485 theLookupPath = theLookupPath.substring(0, index); 486 } else { 487 return null; 489 } 490 491 index = thePath.indexOf("../") + 3; 492 thePath = thePath.substring(index); 493 } 494 495 return theLookupPath + "/" + thePath; 496 } 497 498 499 503 504 507 public void setParameter(String name, String value) { 508 request.addParameter(name, value); 509 } 510 511 514 public void removeParameter(String name) { 515 request.removeParameter(name); 516 } 517 518 521 public void clearParameters() { 522 request.clearParameters(); 523 } 524 525 528 public void setParameter(String name, String [] values) { 529 for (int i = 0; i < values.length; i++) { 530 request.addParameter(name, values[i]); 531 } 532 } 533 534 537 public void clearAttributes() { 538 attributes.clear(); 539 } 540 541 544 public MockRequestDispatcher getRequestDispatcher() { 545 return dispatcher; 546 } 547 548 553 public void setRequest(Request request) { 554 this.request = request; 555 } 556 } | Popular Tags |