001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.test.mock; 016 017 import java.io.BufferedReader; 018 import java.io.IOException; 019 import java.io.UnsupportedEncodingException; 020 import java.security.Principal; 021 import java.util.ArrayList; 022 import java.util.Arrays; 023 import java.util.Collections; 024 import java.util.Enumeration; 025 import java.util.HashMap; 026 import java.util.List; 027 import java.util.Locale; 028 import java.util.Map; 029 030 import javax.servlet.RequestDispatcher; 031 import javax.servlet.ServletInputStream; 032 import javax.servlet.http.Cookie; 033 import javax.servlet.http.HttpServletRequest; 034 import javax.servlet.http.HttpSession; 035 036 /** 037 * Mock implementation of {@link javax.servlet.http.HttpServletRequest}. 038 * 039 * 040 * @author Howard Lewis Ship 041 * @since 4.0 042 */ 043 044 public class MockRequest extends AttributeHolder implements HttpServletRequest 045 { 046 /** 047 * HTTP content type header name. 048 */ 049 private static final String CONTENT_TYPE_HEADER_KEY = "Content-type"; 050 /** 051 * Map of String[]. 052 * 053 */ 054 055 private Map _parameters = new HashMap(); 056 057 /** 058 * Map of String[] 059 * 060 */ 061 062 private Map _headers = new HashMap(); 063 064 private String _method = "GET"; 065 066 private String _contextPath; 067 068 private MockContext _servletContext; 069 private MockSession _session; 070 private String _servletPath; 071 private List _cookies = new ArrayList(); 072 private String _contentPath; 073 074 /** 075 * This can be stored within the header, but doing it this way emulates a browser that 076 * does not put the encoding in the request, which appears to be the general case. 077 */ 078 private String _encoding = null; 079 080 public MockRequest(MockContext servletContext, String servletPath) 081 { 082 _servletContext = servletContext; 083 084 _contextPath = "/" + servletContext.getServletContextName(); 085 _servletPath = servletPath; 086 087 _session = _servletContext.getSession(); 088 } 089 090 public String getAuthType() 091 { 092 return null; 093 } 094 095 public Cookie[] getCookies() 096 { 097 return (Cookie[]) _cookies.toArray(new Cookie[_cookies.size()]); 098 } 099 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 /** 283 * Not part of 2.1 API, not used by Tapestry. 284 * 285 */ 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 /** 402 * Delegates this to the {@link MockSession}, if 403 * it exists. 404 * 405 */ 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 }