1 19 package za.org.coefficient.util.testing; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.UnsupportedEncodingException ; 24 import java.security.Principal ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 32 import javax.servlet.RequestDispatcher ; 33 import javax.servlet.ServletInputStream ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpSession ; 37 38 50 public class TestingHttpServletRequest implements HttpServletRequest { 51 52 private HttpSession session; 53 private Map attributes; 54 private Map parameters; 55 private boolean streamOpened; 56 57 public TestingHttpServletRequest(Map sessionData, Map parameters, Map attributes){ 58 this.parameters = parameters; 59 this.attributes = attributes; 60 this.session = new TestingHttpSession(sessionData); 61 } 62 63 66 public String getAuthType() { 67 return null; 69 } 70 71 74 public Cookie [] getCookies() { 75 return null; 76 } 77 78 81 public long getDateHeader(String arg0) { 82 return -1; 83 } 84 85 88 public String getHeader(String arg0) { 89 return null; 90 } 91 92 95 public Enumeration getHeaders(String arg0) { 96 return null; 97 } 98 99 102 public Enumeration getHeaderNames() { 103 return null; 104 } 105 106 109 public int getIntHeader(String arg0) { 110 return -1; 111 } 112 113 116 public String getMethod() { 117 return "POST"; 119 } 120 121 124 public String getPathInfo() { 125 return null; 126 } 127 128 131 public String getPathTranslated() { 132 return null; 133 } 134 135 138 public String getContextPath() { 139 return ""; 140 } 141 142 145 public String getQueryString() { 146 return null; 147 } 148 149 152 public String getRemoteUser() { 153 return null; 154 } 155 156 159 public boolean isUserInRole(String arg0) { 160 return true; 161 } 162 163 166 public Principal getUserPrincipal() { 167 return null; 168 } 169 170 173 public String getRequestedSessionId() { 174 return "sessionID"; 175 } 176 177 180 public String getRequestURI() { 181 return "/unit-testing"; 182 } 183 184 187 public StringBuffer getRequestURL() { 188 return new StringBuffer ("/unit-testing"); 189 } 190 191 194 public String getServletPath() { 195 return getRequestURL().toString(); 196 } 197 198 201 public HttpSession getSession(boolean arg0) { 202 if (arg0){ 203 if (session == null){ 204 session = new TestingHttpSession(new HashMap ()); 205 } 206 } 207 return session; 208 } 209 210 213 public HttpSession getSession() { 214 return session; 215 } 216 217 220 public boolean isRequestedSessionIdValid() { 221 return true; 222 } 223 224 227 public boolean isRequestedSessionIdFromCookie() { 228 return true; 229 } 230 231 234 public boolean isRequestedSessionIdFromURL() { 235 return false; 236 } 237 238 241 public boolean isRequestedSessionIdFromUrl() { 242 return isRequestedSessionIdFromURL(); 243 } 244 245 248 public Object getAttribute(String arg0) { 249 return attributes.get(arg0); 250 } 251 252 255 public Enumeration getAttributeNames() { 256 Set keys = attributes.keySet(); 257 return new Vector (keys).elements(); 258 } 259 260 263 public String getCharacterEncoding() { 264 return null; 265 } 266 267 270 public void setCharacterEncoding(String arg0) 271 throws UnsupportedEncodingException { 272 throw new UnsupportedOperationException (); 273 } 274 275 278 public int getContentLength() { 279 return -1; 280 } 281 282 285 public String getLocalName() { 286 throw new UnsupportedOperationException (); 287 } 288 289 292 public String getLocalAddr() { 293 throw new UnsupportedOperationException (); 294 } 295 296 299 public int getRemotePort() { 300 throw new UnsupportedOperationException (); 301 } 302 303 306 public int getLocalPort() { 307 throw new UnsupportedOperationException (); 308 } 309 310 313 public String getContentType() { 314 return null; 315 } 316 317 320 public ServletInputStream getInputStream() throws IOException { 321 if (streamOpened) throw new IllegalStateException ("Reader already opened!"); 322 streamOpened = true; 323 return null; 324 } 325 326 329 public String getParameter(String arg0) { 330 String [] vals = getParameterValues(arg0); 331 if (vals == null || vals.length == 0) { 332 return null; 333 } else return vals[0]; 334 } 335 336 339 public Enumeration getParameterNames() { 340 return new Vector (parameters.keySet()).elements(); 341 } 342 343 346 public String [] getParameterValues(String arg0) { 347 Object obj = parameters.get(arg0); 348 if (obj == null){ 349 return null; 350 } else if (obj instanceof String []){ 351 return (String [])obj; 352 } else if (obj instanceof String ){ 353 return new String []{(String )obj}; 354 } else return null; 355 } 356 357 360 public Map getParameterMap() { 361 return parameters; 362 } 363 364 367 public String getProtocol() { 368 return "HTTP/1.1"; 369 } 370 371 374 public String getScheme() { 375 return "http"; 376 } 377 378 381 public String getServerName() { 382 return "unit-testing"; 383 } 384 385 388 public int getServerPort() { 389 return 8080; 390 } 391 392 395 public BufferedReader getReader() throws IOException { 396 if (streamOpened) throw new IllegalStateException ("Stream already opened!"); 397 streamOpened = true; 398 return null; 399 } 400 401 404 public String getRemoteAddr() { 405 return "127.0.0.1"; 406 } 407 408 411 public String getRemoteHost() { 412 return "localhost"; 413 } 414 415 418 public void setAttribute(String arg0, Object arg1) { 419 attributes.put(arg0, arg1); 420 } 421 422 425 public void removeAttribute(String arg0) { 426 attributes.remove(arg0); 427 428 } 429 430 433 public Locale getLocale() { 434 return Locale.getDefault(); 435 } 436 437 440 public Enumeration getLocales() { 441 return new Enumeration (){ 442 Locale [] locales = new Locale []{Locale.getDefault()}; 443 int index = 0; 444 public boolean hasMoreElements(){ 445 return index < locales.length; 446 } 447 public Object nextElement(){ 448 return locales[index++]; 449 } 450 }; 451 } 452 453 456 public boolean isSecure() { 457 return false; 458 } 459 460 463 public RequestDispatcher getRequestDispatcher(String arg0) { 464 throw new UnsupportedOperationException (); 465 } 466 467 470 public String getRealPath(String arg0) { 471 throw new UnsupportedOperationException (); 472 } 473 474 } 475 | Popular Tags |