1 15 package org.apache.tapestry.web; 16 17 import java.io.IOException ; 18 import java.util.List ; 19 20 import javax.servlet.RequestDispatcher ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 26 import org.apache.hivemind.ApplicationRuntimeException; 27 import org.easymock.MockControl; 28 29 35 public class TestServletWebRequest extends BaseWebTestCase 36 { 37 private HttpServletRequest newRequest() 38 { 39 return (HttpServletRequest ) newMock(HttpServletRequest .class); 40 } 41 42 private HttpServletResponse newResponse() 43 { 44 return (HttpServletResponse ) newMock(HttpServletResponse .class); 45 } 46 47 private RequestDispatcher newDispatcher() 48 { 49 return (RequestDispatcher ) newMock(RequestDispatcher .class); 50 } 51 52 public void testGetParameterNames() 53 { 54 MockControl control = newControl(HttpServletRequest .class); 55 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 56 57 request.getParameterNames(); 58 control.setReturnValue(newEnumeration()); 59 60 HttpServletResponse response = newResponse(); 61 62 replayControls(); 63 64 WebRequest wr = new ServletWebRequest(request, response); 65 66 List l = wr.getParameterNames(); 67 68 checkList(l); 69 70 verifyControls(); 71 } 72 73 public void testGetParameterValue() 74 { 75 String value = "William Orbit"; 76 77 MockControl control = newControl(HttpServletRequest .class); 78 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 79 80 request.getParameter("artist"); 81 control.setReturnValue(value); 82 83 HttpServletResponse response = newResponse(); 84 85 replayControls(); 86 87 WebRequest wr = new ServletWebRequest(request, response); 88 89 assertSame(value, wr.getParameterValue("artist")); 90 91 verifyControls(); 92 } 93 94 public void testGetParameterValues() 95 { 96 String [] values = 97 { "William Orbit", "Steely Dan" }; 98 MockControl control = newControl(HttpServletRequest .class); 99 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 100 101 request.getParameterValues("artist"); 102 control.setReturnValue(values); 103 104 HttpServletResponse response = newResponse(); 105 106 replayControls(); 107 108 WebRequest wr = new ServletWebRequest(request, response); 109 110 assertSame(values, wr.getParameterValues("artist")); 111 112 verifyControls(); 113 } 114 115 public void testGetContextPath() 116 { 117 MockControl control = newControl(HttpServletRequest .class); 118 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 119 120 request.getContextPath(); 121 control.setReturnValue("/foo"); 122 123 HttpServletResponse response = newResponse(); 124 125 replayControls(); 126 127 WebRequest wr = new ServletWebRequest(request, response); 128 129 assertEquals("/foo", wr.getContextPath()); 130 131 verifyControls(); 132 } 133 134 public void testGetAttributeNames() 135 { 136 MockControl control = newControl(HttpServletRequest .class); 137 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 138 139 request.getAttributeNames(); 140 control.setReturnValue(newEnumeration()); 141 142 HttpServletResponse response = newResponse(); 143 144 replayControls(); 145 146 WebRequest wr = new ServletWebRequest(request, response); 147 148 List l = wr.getAttributeNames(); 149 150 checkList(l); 151 152 verifyControls(); 153 } 154 155 public void testGetAttribute() 156 { 157 Object attribute = new Object (); 158 MockControl control = newControl(HttpServletRequest .class); 159 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 160 161 request.getAttribute("attr"); 162 control.setReturnValue(attribute); 163 164 HttpServletResponse response = newResponse(); 165 166 replayControls(); 167 168 WebRequest wr = new ServletWebRequest(request, response); 169 170 assertSame(attribute, wr.getAttribute("attr")); 171 172 verifyControls(); 173 } 174 175 public void testSetAttribute() 176 { 177 Object attribute = new Object (); 178 179 MockControl control = newControl(HttpServletRequest .class); 180 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 181 182 request.setAttribute("name", attribute); 183 184 HttpServletResponse response = newResponse(); 185 186 replayControls(); 187 188 WebRequest wr = new ServletWebRequest(request, response); 189 190 wr.setAttribute("name", attribute); 191 192 verifyControls(); 193 } 194 195 public void testSetAttributeToNull() 196 { 197 MockControl control = newControl(HttpServletRequest .class); 198 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 199 200 request.removeAttribute("tonull"); 201 202 HttpServletResponse response = newResponse(); 203 204 replayControls(); 205 206 WebRequest wr = new ServletWebRequest(request, response); 207 208 wr.setAttribute("tonull", null); 209 210 verifyControls(); 211 } 212 213 public void testGetSession() 214 { 215 HttpServletResponse response = newResponse(); 216 217 MockControl control = newControl(HttpServletRequest .class); 218 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 219 220 HttpSession session = (HttpSession ) newMock(HttpSession .class); 221 222 request.getSession(false); 223 control.setReturnValue(null); 224 225 227 replayControls(); 228 229 WebRequest wr = new ServletWebRequest(request, response); 230 231 assertNull(wr.getSession(false)); 232 233 verifyControls(); 234 235 request.getSession(true); 236 control.setReturnValue(session); 237 238 240 replayControls(); 241 242 WebSession cs = wr.getSession(true); 243 244 verifyControls(); 245 246 249 replayControls(); 250 251 assertSame(cs, wr.getSession(false)); 252 253 verifyControls(); 254 } 255 256 public void testGetScheme() 257 { 258 HttpServletResponse response = newResponse(); 259 260 MockControl control = newControl(HttpServletRequest .class); 261 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 262 263 request.getScheme(); 264 control.setReturnValue("http"); 265 266 replayControls(); 267 268 WebRequest wr = new ServletWebRequest(request, response); 269 270 assertEquals("http", wr.getScheme()); 271 272 verifyControls(); 273 } 274 275 public void testGetServerName() 276 { 277 HttpServletResponse response = newResponse(); 278 279 MockControl control = newControl(HttpServletRequest .class); 280 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 281 282 request.getServerName(); 283 control.setReturnValue("www.myhost.com"); 284 285 replayControls(); 286 287 WebRequest wr = new ServletWebRequest(request, response); 288 289 assertEquals("www.myhost.com", wr.getServerName()); 290 291 verifyControls(); 292 } 293 294 public void testGetServerPort() 295 { 296 HttpServletResponse response = newResponse(); 297 298 MockControl control = newControl(HttpServletRequest .class); 299 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 300 301 request.getServerPort(); 302 control.setReturnValue(80); 303 304 replayControls(); 305 306 WebRequest wr = new ServletWebRequest(request, response); 307 308 assertEquals(80, wr.getServerPort()); 309 310 verifyControls(); 311 } 312 313 public void testGetRequestURI() 314 { 315 HttpServletResponse response = newResponse(); 316 317 MockControl control = newControl(HttpServletRequest .class); 318 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 319 320 request.getRequestURI(); 321 control.setReturnValue("/foo/bar"); 322 323 replayControls(); 324 325 WebRequest wr = new ServletWebRequest(request, response); 326 327 assertEquals("/foo/bar", wr.getRequestURI()); 328 329 verifyControls(); 330 } 331 332 public void testForwardInternal() throws Exception 333 { 334 MockControl control = newControl(HttpServletRequest .class); 335 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 336 337 HttpServletResponse response = newResponse(); 338 339 RequestDispatcher dispatcher = newDispatcher(); 340 341 request.getRequestDispatcher("/local.html"); 342 control.setReturnValue(dispatcher); 343 344 dispatcher.forward(request, response); 345 346 replayControls(); 347 348 WebRequest wr = new ServletWebRequest(request, response); 349 350 wr.forward("local.html"); 351 352 verifyControls(); 353 } 354 355 public void testForwardNull() throws Exception 356 { 357 MockControl control = newControl(HttpServletRequest .class); 358 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 359 360 HttpServletResponse response = newResponse(); 361 362 RequestDispatcher dispatcher = newDispatcher(); 363 364 request.getRequestDispatcher("/"); 365 control.setReturnValue(dispatcher); 366 367 dispatcher.forward(request, response); 368 369 replayControls(); 370 371 WebRequest wr = new ServletWebRequest(request, response); 372 373 wr.forward(null); 374 375 verifyControls(); 376 } 377 378 public void testForwardInternalNoDispatcher() throws Exception 379 { 380 MockControl control = newControl(HttpServletRequest .class); 381 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 382 383 HttpServletResponse response = newResponse(); 384 request.getRequestDispatcher("/local.html"); 385 control.setReturnValue(null); 386 387 replayControls(); 388 389 WebRequest wr = new ServletWebRequest(request, response); 390 391 try 392 { 393 wr.forward("local.html"); 394 unreachable(); 395 } 396 catch (ApplicationRuntimeException ex) 397 { 398 assertEquals( 399 "Unable to find a request dispatcher for local resource '/local.html'.", 400 ex.getMessage()); 401 } 402 403 verifyControls(); 404 } 405 406 public void testForwardInternalFailure() throws Exception 407 { 408 MockControl control = newControl(HttpServletRequest .class); 409 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 410 411 HttpServletResponse response = newResponse(); 412 413 MockControl dispatcherc = newControl(RequestDispatcher .class); 414 RequestDispatcher dispatcher = (RequestDispatcher ) dispatcherc.getMock(); 415 416 Throwable t1 = new ServletException ("Mock Servlet Exception"); 417 418 request.getRequestDispatcher("/servlet-exception.html"); 419 control.setReturnValue(dispatcher); 420 421 dispatcher.forward(request, response); 422 dispatcherc.setThrowable(t1); 423 424 replayControls(); 425 426 WebRequest wr = new ServletWebRequest(request, response); 427 428 try 429 { 430 wr.forward("servlet-exception.html"); 431 unreachable(); 432 } 433 catch (ApplicationRuntimeException ex) 434 { 435 assertEquals( 436 "Unable to forward to local resource '/servlet-exception.html': Mock Servlet Exception", 437 ex.getMessage()); 438 assertSame(t1, ex.getRootCause()); 439 } 440 441 verifyControls(); 442 443 Throwable t2 = new IOException ("Mock IO Exception"); 444 445 request.getRequestDispatcher("/io-exception.html"); 446 control.setReturnValue(dispatcher); 447 448 dispatcher.forward(request, response); 449 dispatcherc.setThrowable(t2); 450 451 replayControls(); 452 453 try 454 { 455 wr.forward("io-exception.html"); 456 unreachable(); 457 } 458 catch (ApplicationRuntimeException ex) 459 { 460 assertEquals( 461 "Unable to forward to local resource '/io-exception.html': Mock IO Exception", 462 ex.getMessage()); 463 assertSame(t2, ex.getRootCause()); 464 } 465 466 verifyControls(); 467 } 468 469 public void testForwardExternal() throws Exception 470 { 471 HttpServletRequest request = newRequest(); 472 MockControl responsec = newControl(HttpServletResponse .class); 473 HttpServletResponse response = (HttpServletResponse ) responsec.getMock(); 474 475 response.encodeRedirectURL("http://foo.bar"); 476 responsec.setReturnValue("<encoded: http://foo.bar>"); 477 478 response.sendRedirect("<encoded: http://foo.bar>"); 479 480 replayControls(); 481 482 WebRequest wr = new ServletWebRequest(request, response); 483 484 wr.forward("http://foo.bar"); 485 486 verifyControls(); 487 } 488 489 public void testForwardExternalFailure() throws Exception 490 { 491 HttpServletRequest request = newRequest(); 492 MockControl responsec = newControl(HttpServletResponse .class); 493 HttpServletResponse response = (HttpServletResponse ) responsec.getMock(); 494 495 Throwable t = new IOException ("Mock IO Exception"); 496 497 response.encodeRedirectURL("/"); 498 responsec.setReturnValue("<encoded: http://foo.bar>"); 499 500 response.sendRedirect("<encoded: http://foo.bar>"); 501 responsec.setThrowable(t); 502 503 replayControls(); 504 505 WebRequest wr = new ServletWebRequest(request, response); 506 507 try 508 { 509 wr.forward("/"); 510 unreachable(); 511 } 512 catch (ApplicationRuntimeException ex) 513 { 514 assertEquals("Unable to redirect to /: Mock IO Exception", ex.getMessage()); 515 assertSame(t, ex.getRootCause()); 516 } 517 518 verifyControls(); 519 } 520 521 public void testGetActivationPathNoPathInfo() 522 { 523 MockControl control = newControl(HttpServletRequest .class); 524 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 525 526 HttpServletResponse response = newResponse(); 527 528 request.getServletPath(); 529 control.setReturnValue("/foo"); 530 531 request.getPathInfo(); 532 control.setReturnValue(null); 533 534 replayControls(); 535 536 WebRequest wr = new ServletWebRequest(request, response); 537 538 assertEquals("/foo", wr.getActivationPath()); 539 540 verifyControls(); 541 } 542 543 public void testGetActvationPathFull() 544 { 545 MockControl control = newControl(HttpServletRequest .class); 546 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 547 548 HttpServletResponse response = newResponse(); 549 550 request.getServletPath(); 551 control.setReturnValue("/foo.direct"); 552 553 request.getPathInfo(); 554 control.setReturnValue("/bar/baz"); 555 556 replayControls(); 557 558 WebRequest wr = new ServletWebRequest(request, response); 559 560 assertEquals("/foo.direct/bar/baz", wr.getActivationPath()); 561 562 verifyControls(); 563 } 564 } | Popular Tags |