| 1 18 19 20 package org.apache.struts.util; 21 22 23 import java.net.MalformedURLException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.servlet.jsp.JspException ; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.DynaActionForm; 36 import org.apache.struts.action.RequestProcessor; 37 import org.apache.struts.config.ForwardConfig; 38 import org.apache.struts.config.ModuleConfig; 39 import org.apache.struts.mock.MockFormBean; 40 import org.apache.struts.mock.MockPrincipal; 41 import org.apache.struts.mock.TestMockBase; 42 import org.apache.struts.taglib.html.Constants; 43 44 45 50 51 public class TestRequestUtils extends TestMockBase { 52 53 54 56 57 public TestRequestUtils(String name) { 58 super(name); 59 } 60 61 62 public static void main(String args[]) { 63 junit.awtui.TestRunner.main 64 (new String [] { TestRequestUtils.class.getName() } ); 65 } 66 67 68 public static Test suite() { 69 return (new TestSuite(TestRequestUtils.class)); 70 } 71 72 73 75 76 77 79 80 public void setUp() { 81 82 super.setUp(); 83 84 } 85 86 87 public void tearDown() { 88 89 super.tearDown(); 90 91 } 92 93 94 96 97 99 100 public void testAbsoluteURL() { 101 102 request.setPathElements("/myapp", "/action.do", null, null); 103 String url = null; 104 try { 105 url = RequestUtils.absoluteURL(request, "/foo/bar.jsp").toString(); 106 assertEquals("absoluteURL is correct", 107 "http://localhost:8080/myapp/foo/bar.jsp", 108 url); 109 } catch (MalformedURLException e) { 110 fail("Threw MalformedURLException: " + e); 111 } 112 113 } 114 115 116 118 119 public void testActionURL1() { 121 122 request.setAttribute(Globals.MODULE_KEY, moduleConfig); 123 request.setPathElements("/myapp", "/foo.do", null, null); 124 String url = RequestUtils.actionURL 125 (request, moduleConfig.findActionConfig("/dynamic"), "*.do"); 126 assertNotNull("URL was returned", url); 127 assertEquals("URL value", 128 "/dynamic.do", 129 url); 130 131 } 132 133 134 public void testActionURL2() { 136 137 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 138 request.setPathElements("/myapp", "/2/foo.do", null, null); 139 String url = RequestUtils.actionURL 140 (request, moduleConfig2.findActionConfig("/dynamic2"), "*.do"); 141 assertNotNull("URL was returned", url); 142 assertEquals("URL value", 143 "/2/dynamic2.do", 144 url); 145 146 } 147 148 149 public void testActionURL3() { 151 152 request.setAttribute(Globals.MODULE_KEY, moduleConfig); 153 request.setPathElements("/myapp", "/do/foo", null, null); 154 String url = RequestUtils.actionURL 155 (request, moduleConfig.findActionConfig("/dynamic"), "/do/*"); 156 assertNotNull("URL was returned", url); 157 assertEquals("URL value", 158 "/do/dynamic", 159 url); 160 161 } 162 163 164 166 167 public void testComputeParameters0a() { 169 170 Map map = null; 171 try { 172 map = RequestUtils.computeParameters(page, 173 null, null, null, null, 174 null, null, null, false); 175 } catch (JspException e) { 176 fail("JspException: " + e); 177 } 178 assertNull("Map is null", map); 179 180 } 181 182 183 public void testComputeParameters0b() { 185 186 session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token"); 187 Map map = null; 188 try { 189 map = RequestUtils.computeParameters 190 (page, null, null, null, null, 191 null, null, null, true); 192 } catch (JspException e) { 193 fail("JspException: " + e); 194 } 195 assertNotNull("Map is not null", map); 196 assertEquals("One parameter in the returned map", 197 1, map.size()); 198 assertTrue("Transaction token parameter present", 199 map.containsKey(Constants.TOKEN_KEY)); 200 assertEquals("Transaction token parameter value", 201 "token", 202 (String ) map.get(Constants.TOKEN_KEY)); 203 204 } 205 206 207 public void testComputeParameters1a() { 209 210 session.setAttribute("attr", "bar"); 211 Map map = null; 212 try { 213 map = RequestUtils.computeParameters 214 (page, "foo", "attr", null, null, 215 null, null, null, false); 216 } catch (JspException e) { 217 fail("JspException: " + e); 218 } 219 assertNotNull("Map is not null", map); 220 assertEquals("One parameter in the returned map", 221 1, map.size()); 222 assertTrue("Parameter present", 223 map.containsKey("foo")); 224 assertEquals("Parameter value", 225 "bar", 226 (String ) map.get("foo")); 227 228 } 229 230 231 public void testComputeParameters1b() { 233 234 request.setAttribute("attr", "bar"); 235 Map map = null; 236 try { 237 map = RequestUtils.computeParameters 238 (page, "foo", "attr", null, "request", 239 null, null, null, false); 240 } catch (JspException e) { 241 fail("JspException: " + e); 242 } 243 assertNotNull("Map is not null", map); 244 assertEquals("One parameter in the returned map", 245 1, map.size()); 246 assertTrue("Parameter present", 247 map.containsKey("foo")); 248 assertEquals("Parameter value", 249 "bar", 250 (String ) map.get("foo")); 251 252 } 253 254 255 public void testComputeParameters1c() { 257 258 request.setAttribute("attr", new MockFormBean("bar")); 259 260 Map map = null; 261 try { 262 map = RequestUtils.computeParameters 263 (page, "foo", "attr", "stringProperty", "request", 264 null, null, null, false); 265 } catch (JspException e) { 266 fail("JspException: " + e); 267 } 268 assertNotNull("Map is not null", map); 269 assertEquals("One parameter in the returned map", 270 1, map.size()); 271 assertTrue("Parameter present", 272 map.containsKey("foo")); 273 assertEquals("Parameter value", 274 "bar", 275 (String ) map.get("foo")); 276 277 } 278 279 280 public void testComputeParameters2a() { 282 283 Map map = new HashMap (); 284 map.put("foo1", "bar1"); 285 map.put("foo2", "bar2"); 286 session.setAttribute("attr", map); 287 288 try { 289 map = RequestUtils.computeParameters 290 (page, null, null, null, null, 291 "attr", null, null, false); 292 } catch (JspException e) { 293 fail("JspException: " + e); 294 } 295 assertNotNull("Map is not null", map); 296 assertEquals("Two parameter in the returned map", 297 2, map.size()); 298 assertTrue("Parameter foo1 present", 299 map.containsKey("foo1")); 300 assertEquals("Parameter foo1 value", 301 "bar1", 302 (String ) map.get("foo1")); 303 assertTrue("Parameter foo2 present", 304 map.containsKey("foo2")); 305 assertEquals("Parameter foo2 value", 306 "bar2", 307 (String ) map.get("foo2")); 308 309 } 310 311 312 public void testComputeParameters2b() { 314 315 Map map = new HashMap (); 316 map.put("foo1", "bar1"); 317 map.put("foo2", "bar2"); 318 request.setAttribute("attr", map); 319 320 try { 321 map = RequestUtils.computeParameters 322 (page, null, null, null, null, 323 "attr", null, "request", false); 324 } catch (JspException e) { 325 fail("JspException: " + e); 326 } 327 assertNotNull("Map is not null", map); 328 assertEquals("Two parameter in the returned map", 329 2, map.size()); 330 assertTrue("Parameter foo1 present", 331 map.containsKey("foo1")); 332 assertEquals("Parameter foo1 value", 333 "bar1", 334 (String ) map.get("foo1")); 335 assertTrue("Parameter foo2 present", 336 map.containsKey("foo2")); 337 assertEquals("Parameter foo2 value", 338 "bar2", 339 (String ) map.get("foo2")); 340 341 } 342 343 344 public void testComputeParameters2c() { 346 347 request.setAttribute("attr", new MockFormBean()); 348 349 Map map = null; 350 try { 351 map = RequestUtils.computeParameters 352 (page, null, null, null, null, 353 "attr", "mapProperty", "request", false); 354 } catch (JspException e) { 355 fail("JspException: " + e); 356 } 357 assertNotNull("Map is not null", map); 358 assertEquals("Two parameter in the returned map", 359 2, map.size()); 360 assertTrue("Parameter foo1 present", 361 map.containsKey("foo1")); 362 assertEquals("Parameter foo1 value", 363 "bar1", 364 (String ) map.get("foo1")); 365 assertTrue("Parameter foo2 present", 366 map.containsKey("foo2")); 367 assertEquals("Parameter foo2 value", 368 "bar2", 369 (String ) map.get("foo2")); 370 371 } 372 373 374 public void testComputeParameters2d() { 376 377 Map map = new HashMap (); 378 map.put("foo", new String [] { "bar1", "bar2" }); 379 session.setAttribute("attr", map); 380 381 try { 382 map = RequestUtils.computeParameters 383 (page, null, null, null, null, 384 "attr", null, null, false); 385 } catch (JspException e) { 386 fail("JspException: " + e); 387 } 388 assertNotNull("Map is not null", map); 389 assertEquals("One parameter in the returned map", 390 1, map.size()); 391 assertTrue("Parameter foo present", 392 map.containsKey("foo")); 393 assertTrue("Parameter foo value type", 394 map.get("foo") instanceof String []); 395 String values[] = (String []) map.get("foo"); 396 assertEquals("Values count", 397 2, values.length); 398 399 } 400 401 402 public void testComputeParameters3a() { 404 405 request.setAttribute("attr", new MockFormBean("bar3")); 406 session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token"); 407 408 Map map = null; 409 try { 410 map = RequestUtils.computeParameters 411 (page, "foo1", "attr", "stringProperty", "request", 412 "attr", "mapProperty", "request", true); 413 } catch (JspException e) { 414 fail("JspException: " + e); 415 } 416 assertNotNull("Map is not null", map); 417 assertEquals("Three parameter in the returned map", 418 3, map.size()); 419 420 assertTrue("Parameter foo1 present", 421 map.containsKey("foo1")); 422 assertTrue("Parameter foo1 value type", 423 map.get("foo1") instanceof String []); 424 String values[] = (String []) map.get("foo1"); 425 assertEquals("Values count", 426 2, values.length); 427 428 assertTrue("Parameter foo2 present", 429 map.containsKey("foo2")); 430 assertEquals("Parameter foo2 value", 431 "bar2", 432 (String ) map.get("foo2")); 433 434 assertTrue("Transaction token parameter present", 435 map.containsKey(Constants.TOKEN_KEY)); 436 assertEquals("Transaction token parameter value", 437 "token", 438 (String ) map.get(Constants.TOKEN_KEY)); 439 } 440 441 442 444 445 public void testComputeURL1a() { 447 448 request.setPathElements("/myapp", "/action.do", null, null); 449 String url = null; 450 try { 451 url = RequestUtils.computeURL 452 (page, "foo", 453 null, null, 454 null, null, false); 455 } catch (MalformedURLException e) { 456 fail("MalformedURLException: " + e); 457 } 458 assertNotNull("url present", url); 459 assertEquals("url value", 460 "/myapp/bar.jsp", 461 url); 462 463 } 464 465 466 public void testComputeURL1b() { 468 469 request.setPathElements("/myapp", "/action.do", null, null); 470 String url = null; 471 try { 472 url = RequestUtils.computeURL 473 (page, null, 474 "http://foo.com/bar", null, 475 null, null, false); 476 } catch (MalformedURLException e) { 477 fail("MalformedURLException: " + e); 478 } 479 assertNotNull("url present", url); 480 assertEquals("url value", 481 "http://foo.com/bar", 482 url); 483 484 } 485 486 487 public void testComputeURL1c() { 489 490 request.setPathElements("/myapp", "/action.do", null, null); 491 String url = null; 492 try { 493 url = RequestUtils.computeURL 494 (page, null, 495 null, "/bar", 496 null, null, false); 497 } catch (MalformedURLException e) { 498 fail("MalformedURLException: " + e); 499 } 500 assertNotNull("url present", url); 501 assertEquals("url value", 502 "/myapp/bar", 503 url); 504 505 } 506 507 508 public void testComputeURL1d() { 510 511 moduleConfig.getControllerConfig().setForwardPattern 512 ("$C/WEB-INF/pages$M$P"); 513 request.setPathElements("/myapp", "/action.do", null, null); 514 String url = null; 515 try { 516 url = RequestUtils.computeURL 517 (page, "foo", 518 null, null, 519 null, null, false); 520 } catch (MalformedURLException e) { 521 fail("MalformedURLException: " + e); 522 } 523 assertNotNull("url present", url); 524 assertEquals("url value", 525 "/myapp/WEB-INF/pages/bar.jsp", 526 url); 527 528 } 529 530 531 public void testComputeURL1e() { 533 534 moduleConfig.getControllerConfig().setPagePattern 535 ("$C/WEB-INF/pages$M$P"); 536 request.setPathElements("/myapp", "/action.do", null, null); 537 String url = null; 538 try { 539 url = RequestUtils.computeURL 540 (page, null, 541 null, "/bar", 542 null, null, false); 543 } catch (MalformedURLException e) { 544 fail("MalformedURLException: " + e); 545 } 546 assertNotNull("url present", url); 547 assertEquals("url value", 548 "/myapp/WEB-INF/pages/bar", 549 url); 550 551 } 552 553 554 public void testComputeURL1f() { 556 557 request.setPathElements("/myapp", "/action.do", null, null); 558 String url = null; 559 try { 560 url = RequestUtils.computeURL 561 (page, "relative1", 562 null, null, 563 null, null, false); 564 } catch (MalformedURLException e) { 565 fail("MalformedURLException: " + e); 566 } 567 assertNotNull("url present", url); 568 assertEquals("url value", 569 "relative.jsp", 571 url); 572 } 573 574 575 public void testComputeURL1g() { 577 578 request.setPathElements("/myapp", "/action.do", null, null); 579 String url = null; 580 try { 581 url = RequestUtils.computeURL 582 (page, "relative2", 583 null, null, 584 null, null, false); 585 } catch (MalformedURLException e) { 586 fail("MalformedURLException: " + e); 587 } 588 assertNotNull("url present", url); 589 assertEquals("url value", 590 "relative.jsp", 592 url); 593 } 594 595 596 public void testComputeURL1h() { 598 599 request.setPathElements("/myapp", "/action.do", null, null); 600 String url = null; 601 try { 602 url = RequestUtils.computeURL 603 (page, "external", 604 null, null, 605 null, null, false); 606 } catch (MalformedURLException e) { 607 fail("MalformedURLException: " + e); 608 } 609 assertNotNull("url present", url); 610 assertEquals("url value", 611 "http://jakarta.apache.org/", 612 url); 613 } 614 615 616 public void testComputeURL2a() { 618 619 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 620 request.setPathElements("/myapp", "/2/action.do", null, null); 621 String url = null; 622 try { 623 url = RequestUtils.computeURL 624 (page, "foo", 625 null, null, 626 null, null, false); 627 } catch (MalformedURLException e) { 628 fail("MalformedURLException: " + e); 629 } 630 assertNotNull("url present", url); 631 assertEquals("url value", 632 "/myapp/2/baz.jsp", 633 url); 634 635 } 636 637 638 public void testComputeURL2b() { 640 641 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 642 request.setPathElements("/myapp", "/2/action.do", null, null); 643 String url = null; 644 try { 645 url = RequestUtils.computeURL 646 (page, null, 647 "http://foo.com/bar", null, 648 null, null, false); 649 } catch (MalformedURLException e) { 650 fail("MalformedURLException: " + e); 651 } 652 assertNotNull("url present", url); 653 assertEquals("url value", 654 "http://foo.com/bar", 655 url); 656 657 } 658 659 660 public void testComputeURL2c() { 662 663 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 664 request.setPathElements("/myapp", "/2/action.do", null, null); 665 String url = null; 666 try { 667 url = RequestUtils.computeURL 668 (page, null, 669 null, "/bar", 670 null, null, false); 671 } catch (MalformedURLException e) { 672 fail("MalformedURLException: " + e); 673 } 674 assertNotNull("url present", url); 675 assertEquals("url value", 676 "/myapp/2/bar", 677 url); 678 679 } 680 681 682 public void testComputeURL2d() { 684 685 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 686 moduleConfig2.getControllerConfig().setForwardPattern 687 ("$C/WEB-INF/pages$M$P"); 688 request.setPathElements("/myapp", "/2/action.do", null, null); 689 String url = null; 690 try { 691 url = RequestUtils.computeURL 692 (page, "foo", 693 null, null, 694 null, null, false); 695 } catch (MalformedURLException e) { 696 fail("MalformedURLException: " + e); 697 } 698 assertNotNull("url present", url); 699 assertEquals("url value", 700 "/myapp/WEB-INF/pages/2/baz.jsp", 701 url); 702 703 } 704 705 706 public void testComputeURL2e() { 708 709 moduleConfig2.getControllerConfig().setPagePattern 710 ("$C/WEB-INF/pages$M$P"); 711 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 712 request.setPathElements("/myapp", "/2/action.do", null, null); 713 String url = null; 714 try { 715 url = RequestUtils.computeURL 716 (page, null, 717 null, "/bar", 718 null, null, false); 719 } catch (MalformedURLException e) { 720 fail("MalformedURLException: " + e); 721 } 722 assertNotNull("url present", url); 723 assertEquals("url value", 724 "/myapp/WEB-INF/pages/2/bar", 725 url); 726 727 } 728 729 730 public void testComputeURL2f() { 732 733 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 734 request.setPathElements("/myapp", "/2/action.do", null, null); 735 String url = null; 736 try { 737 url = RequestUtils.computeURL 738 (page, "relative1", 739 null, null, 740 null, null, false); 741 } catch (MalformedURLException e) { 742 fail("MalformedURLException: " + e); 743 } 744 assertNotNull("url present", url); 745 assertEquals("url value", 746 "relative.jsp", 748 url); 749 } 750 751 752 public void testComputeURL2g() { 754 755 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 756 request.setPathElements("/myapp", "/2/action.do", null, null); 757 String url = null; 758 try { 759 url = RequestUtils.computeURL 760 (page, "relative2", 761 null, null, 762 null, null, false); 763 } catch (MalformedURLException e) { 764 fail("MalformedURLException: " + e); 765 } 766 assertNotNull("url present", url); 767 assertEquals("url value", 768 "relative.jsp", 770 url); 771 } 772 773 774 public void testComputeURL2h() { 776 777 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 778 request.setPathElements("/myapp", "/2/action.do", null, null); 779 String url = null; 780 try { 781 url = RequestUtils.computeURL 782 (page, "external", 783 null, null, 784 null, null, false); 785 } catch (MalformedURLException e) { 786 fail("MalformedURLException: " + e); 787 } 788 assertNotNull("url present", url); 789 assertEquals("url value", 790 "http://jakarta.apache.org/", 791 url); 792 } 793 794 795 public void testComputeURL3a() { 797 |