1 15 package org.apache.tapestry.test.mock; 16 17 import java.io.BufferedReader ; 18 import java.io.IOException ; 19 import java.io.UnsupportedEncodingException ; 20 import java.security.Principal ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 30 import javax.servlet.RequestDispatcher ; 31 import javax.servlet.ServletInputStream ; 32 import javax.servlet.http.Cookie ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpSession ; 35 36 43 44 public class MockRequest extends AttributeHolder implements HttpServletRequest 45 { 46 49 private static final String CONTENT_TYPE_HEADER_KEY = "Content-type"; 50 54 55 private Map _parameters = new HashMap (); 56 57 61 62 private Map _headers = new HashMap (); 63 64 private String _method = "GET"; 65 66 private String _contextPath; 67 68 private MockContext _servletContext; 69 private MockSession _session; 70 private String _servletPath; 71 private List _cookies = new ArrayList (); 72 private String _contentPath; 73 74 78 private String _encoding = null; 79 80 public MockRequest(MockContext servletContext, String servletPath) 81 { 82 _servletContext = servletContext; 83 84 _contextPath = "/" + servletContext.getServletContextName(); 85 _servletPath = servletPath; 86 87 _session = _servletContext.getSession(); 88 } 89 90 public String getAuthType() 91 { 92 return null; 93 } 94 95 public Cookie [] getCookies() 96 { 97 return (Cookie []) _cookies.toArray(new Cookie [_cookies.size()]); 98 } 99 100 public long getDateHeader(String arg0) 101 { 102 return 0; 103 } 104 105 public String getHeader(String key) 106 { 107 String getHeader = null; 108 109 if (key != null) 110 { 111 getHeader = (String ) _headers.get(key.toLowerCase()); 112 } 113 return getHeader; 114 } 115 116 public Enumeration getHeaders(String name) 117 { 118 String [] headers = (String []) _headers.get(name); 119 120 if (headers == null) 121 return Collections.enumeration(Collections.EMPTY_LIST); 122 123 return Collections.enumeration(Arrays.asList(headers)); 124 } 125 126 public Enumeration getHeaderNames() 127 { 128 return getEnumeration(_headers); 129 } 130 131 public int getIntHeader(String arg0) 132 { 133 return 0; 134 } 135 136 public String getMethod() 137 { 138 return _method; 139 } 140 141 public String getPathInfo() 142 { 143 return null; 144 } 145 146 public String getPathTranslated() 147 { 148 return null; 149 } 150 151 public String getContextPath() 152 { 153 return _contextPath; 154 } 155 156 public String getQueryString() 157 { 158 return null; 159 } 160 161 public String getRemoteUser() 162 { 163 return null; 164 } 165 166 public boolean isUserInRole(String arg0) 167 { 168 return false; 169 } 170 171 public Principal getUserPrincipal() 172 { 173 return null; 174 } 175 176 public String getRequestedSessionId() 177 { 178 return null; 179 } 180 181 public String getRequestURI() 182 { 183 return null; 184 } 185 186 public StringBuffer getRequestURL() 187 { 188 return null; 189 } 190 191 public String getServletPath() 192 { 193 return _servletPath; 194 } 195 196 public HttpSession getSession(boolean create) 197 { 198 if (create && _session == null) 199 _session = _servletContext.createSession(); 200 201 return _session; 202 } 203 204 public HttpSession getSession() 205 { 206 return _session; 207 } 208 209 public boolean isRequestedSessionIdValid() 210 { 211 return false; 212 } 213 214 public boolean isRequestedSessionIdFromCookie() 215 { 216 return false; 217 } 218 219 public boolean isRequestedSessionIdFromURL() 220 { 221 return false; 222 } 223 224 public boolean isRequestedSessionIdFromUrl() 225 { 226 return false; 227 } 228 229 public String getCharacterEncoding() 230 { 231 return _encoding; 232 } 233 234 public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException 235 { 236 _encoding = arg0; 237 } 238 239 public int getContentLength() 240 { 241 return 0; 242 } 243 244 public String getContentType() 245 { 246 return getHeader(CONTENT_TYPE_HEADER_KEY); 247 } 248 249 public void setContentType(String contentType) 250 { 251 setHeader(CONTENT_TYPE_HEADER_KEY, contentType); 252 } 253 254 public ServletInputStream getInputStream() throws IOException 255 { 256 if (_contentPath == null) 257 return null; 258 259 return new MockServletInputStream(_contentPath); 260 } 261 262 public String getParameter(String name) 263 { 264 String [] values = getParameterValues(name); 265 266 if (values == null || values.length == 0) 267 return null; 268 269 return values[0]; 270 } 271 272 public Enumeration getParameterNames() 273 { 274 return Collections.enumeration(_parameters.keySet()); 275 } 276 277 public String [] getParameterValues(String name) 278 { 279 return (String []) _parameters.get(name); 280 } 281 282 286 287 public Map getParameterMap() 288 { 289 return null; 290 } 291 292 public String getProtocol() 293 { 294 return null; 295 } 296 297 public String getScheme() 298 { 299 return "http"; 300 } 301 302 public String getServerName() 303 { 304 return "junit-test"; 305 } 306 307 public int getServerPort() 308 { 309 return 80; 310 } 311 312 public BufferedReader getReader() throws IOException 313 { 314 return null; 315 } 316 317 public String getRemoteAddr() 318 { 319 return null; 320 } 321 322 public String getRemoteHost() 323 { 324 return null; 325 } 326 327 private Locale _locale = Locale.ENGLISH; 328 329 public Locale getLocale() 330 { 331 return _locale; 332 } 333 334 public void setLocale(Locale locale) 335 { 336 _locale = locale; 337 } 338 339 public Enumeration getLocales() 340 { 341 return Collections.enumeration(Collections.singleton(_locale)); 342 } 343 344 public boolean isSecure() 345 { 346 return false; 347 } 348 349 public RequestDispatcher getRequestDispatcher(String path) 350 { 351 return _servletContext.getRequestDispatcher(path); 352 } 353 354 public String getRealPath(String arg0) 355 { 356 return null; 357 } 358 359 public void setContextPath(String contextPath) 360 { 361 _contextPath = contextPath; 362 } 363 364 public void setMethod(String method) 365 { 366 _method = method; 367 } 368 369 public void setParameter(String name, String [] values) 370 { 371 _parameters.put(name, values); 372 } 373 374 public void setParameter(String name, String value) 375 { 376 setParameter(name, new String [] { value }); 377 } 378 379 public void addCookie(Cookie cookie) 380 { 381 _cookies.add(cookie); 382 } 383 384 public void addCookies(Cookie [] cookies) 385 { 386 if (cookies == null) 387 return; 388 389 for (int i = 0; i < cookies.length; i++) 390 addCookie(cookies[i]); 391 } 392 393 private void setHeader(String key, String value) 394 { 395 if (key != null) 396 { 397 _headers.put(key.toLowerCase(), value); 398 } 399 } 400 401 406 407 public void simulateFailover() 408 { 409 if (_session != null) 410 _session.simulateFailover(); 411 } 412 413 public String getContentPath() 414 { 415 return _contentPath; 416 } 417 418 public void setContentPath(String contentPath) 419 { 420 _contentPath = contentPath; 421 } 422 423 public int getRemotePort() 424 { 425 return 0; 426 } 427 428 public String getLocalName() 429 { 430 return null; 431 } 432 433 public String getLocalAddr() 434 { 435 return null; 436 } 437 438 public int getLocalPort() 439 { 440 return 0; 441 } 442 443 } 444 | Popular Tags |