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 798 request.setPathElements("/myapp", "/action.do", null, null); 799 Map map = new HashMap (); 800 map.put("foo1", "bar1"); 801 map.put("foo2", "bar2"); 802 String url = null; 803 try { 804 url = RequestUtils.computeURL 805 (page, null, 806 null, "/bar", 807 map, null, false); 808 } catch (MalformedURLException e) { 809 fail("MalformedURLException: " + e); 810 } 811 assertNotNull("url present", url); 812 assertTrue("url value", 813 url.equals("/myapp/bar?foo1=bar1&foo2=bar2") || 814 url.equals("/myapp/bar?foo2=bar2&foo1=bar1")); 815 816 } 817 818 819 public void testComputeURL3b() { 821 822 request.setPathElements("/myapp", "/action.do", null, null); 823 String url = null; 824 try { 825 url = RequestUtils.computeURL 826 (page, null, 827 null, "/bar", 828 null, "anchor", false); 829 } catch (MalformedURLException e) { 830 fail("MalformedURLException: " + e); 831 } 832 assertNotNull("url present", url); 833 assertEquals("url value", 834 "/myapp/bar#anchor", 835 url); 836 837 } 838 839 840 public void testComputeURL3c() { 842 843 request.setPathElements("/myapp", "/action.do", null, null); 844 Map map = new HashMap (); 845 map.put("foo1", "bar1"); 846 map.put("foo2", "bar2"); 847 String url = null; 848 try { 849 url = RequestUtils.computeURL 850 (page, null, 851 null, "/bar", 852 map, "anchor", false); 853 } catch (MalformedURLException e) { 854 fail("MalformedURLException: " + e); 855 } 856 assertNotNull("url present", url); 857 assertTrue("url value", 858 url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor") || 859 url.equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor")); 860 861 } 862 863 864 public void testComputeURL3d() { 866 867 request.setPathElements("/myapp", "/action.do", null, null); 868 Map map = new HashMap (); 869 map.put("foo1", "bar1"); 870 map.put("foo2", "bar2"); 871 String url = null; 872 try { 873 url = RequestUtils.computeURL 874 (page, null, 875 null, "/bar", 876 map, null, true); 877 } catch (MalformedURLException e) { 878 fail("MalformedURLException: " + e); 879 } 880 assertNotNull("url present", url); 881 assertTrue("url value", 882 url.equals("/myapp/bar?foo1=bar1&foo2=bar2") || 883 url.equals("/myapp/bar?foo2=bar2&foo1=bar1")); 884 885 } 886 887 888 public void testComputeURL3e() { 890 891 request.setPathElements("/myapp", "/action.do", null, null); 892 String url = null; 893 try { 894 url = RequestUtils.computeURL 895 (page, null, 896 null, "/bar", 897 null, "anchor", true); 898 } catch (MalformedURLException e) { 899 fail("MalformedURLException: " + e); 900 } 901 assertNotNull("url present", url); 902 assertEquals("url value", 903 "/myapp/bar#anchor", 904 url); 905 906 } 907 908 909 public void testComputeURL3f() { 911 912 request.setPathElements("/myapp", "/action.do", null, null); 913 Map map = new HashMap (); 914 map.put("foo1", "bar1"); 915 map.put("foo2", "bar2"); 916 String url = null; 917 try { 918 url = RequestUtils.computeURL 919 (page, null, 920 null, "/bar", 921 map, "anchor", false); 922 } catch (MalformedURLException e) { 923 fail("MalformedURLException: " + e); 924 } 925 assertNotNull("url present", url); 926 assertTrue("url value", 927 url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor") || 928 url.equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor")); 929 930 } 931 932 933 935 936 937 public void testCreateActionForm1a() { 939 940 request.setPathElements("/myapp", "/noform.do", null, null); 941 ActionMapping mapping = (ActionMapping) 942 moduleConfig.findActionConfig("/noform"); 943 assertNotNull("Found /noform mapping", mapping); 944 ActionForm form = RequestUtils.createActionForm 945 (request, mapping, moduleConfig, null); 946 assertNull("No ActionForm returned", form); 947 948 } 949 950 951 public void testCreateActionForm1b() { 953 954 request.setPathElements("/myapp", "/2/noform.do", null, null); 955 ActionMapping mapping = (ActionMapping) 956 moduleConfig2.findActionConfig("/noform"); 957 assertNotNull("Found /noform mapping", mapping); 958 ActionForm form = RequestUtils.createActionForm 959 (request, mapping, moduleConfig2, null); 960 assertNull("No ActionForm returned", form); 961 962 } 963 964 965 public void testCreateActionForm2a() { 967 968 request.setPathElements("/myapp", "/static.do", null, null); 969 ActionMapping mapping = (ActionMapping) 970 moduleConfig.findActionConfig("/static"); 971 assertNotNull("Found /static mapping", mapping); 972 assertNotNull("Mapping has non-null name", 973 mapping.getName()); 974 assertEquals("Mapping has correct name", 975 "static", 976 mapping.getName()); 977 assertNotNull("AppConfig has form bean " + mapping.getName(), 978 moduleConfig.findFormBeanConfig(mapping.getName())); 979 ActionForm form = RequestUtils.createActionForm 980 (request, mapping, moduleConfig, null); 981 assertNotNull("ActionForm returned", form); 982 assertTrue("ActionForm of correct type", 983 form instanceof MockFormBean); 984 985 } 986 987 988 public void testCreateActionForm2b() { 990 991 request.setPathElements("/myapp", "/2/static.do", null, null); 992 ActionMapping mapping = (ActionMapping) 993 moduleConfig2.findActionConfig("/static"); 994 assertNotNull("Found /static mapping", mapping); 995 assertNotNull("Mapping has non-null name", 996 mapping.getName()); 997 assertEquals("Mapping has correct name", 998 "static", 999 mapping.getName()); 1000 assertNotNull("AppConfig has form bean " + mapping.getName(), 1001 moduleConfig.findFormBeanConfig(mapping.getName())); 1002 ActionForm form = RequestUtils.createActionForm 1003 (request, mapping, moduleConfig2, null); 1004 assertNotNull("ActionForm returned", form); 1005 assertTrue("ActionForm of correct type", 1006 form instanceof MockFormBean); 1007 1008 } 1009 1010 1011 public void testCreateActionForm3a() { 1013 1014 request.setPathElements("/myapp", "/dynamic.do", null, null); 1015 ActionMapping mapping = (ActionMapping) 1016 moduleConfig.findActionConfig("/dynamic"); 1017 assertNotNull("Found /dynamic mapping", mapping); 1018 assertNotNull("Mapping has non-null name", 1019 mapping.getName()); 1020 assertEquals("Mapping has correct name", 1021 "dynamic", 1022 mapping.getName()); 1023 assertNotNull("AppConfig has form bean " + mapping.getName(), 1024 moduleConfig.findFormBeanConfig(mapping.getName())); 1025 ActionForm form = RequestUtils.createActionForm 1026 (request, mapping, moduleConfig, null); 1027 assertNotNull("ActionForm returned", form); 1028 assertTrue("ActionForm of correct type", 1029 form instanceof DynaActionForm); 1030 1031 } 1032 1033 1034 public void testCreateActionForm3b() { 1036 1037 request.setPathElements("/myapp", "/2/dynamic2.do", null, null); 1038 ActionMapping mapping = (ActionMapping) 1039 moduleConfig2.findActionConfig("/dynamic2"); 1040 assertNotNull("Found /dynamic2 mapping", mapping); 1041 assertNotNull("Mapping has non-null name", 1042 mapping.getName()); 1043 assertEquals("Mapping has correct name", 1044 "dynamic2", 1045 mapping.getName()); 1046 assertNotNull("AppConfig has form bean " + mapping.getName(), 1047 moduleConfig2.findFormBeanConfig(mapping.getName())); 1048 ActionForm form = RequestUtils.createActionForm 1049 (request, mapping, moduleConfig2, null); 1050 assertNotNull("ActionForm returned", form); 1051 assertTrue("ActionForm of correct type", 1052 form instanceof DynaActionForm); 1053 1054 } 1055 1056 1057 public void testCreateActionForm4a() { 1059 1060 request.setPathElements("/myapp", "/dynamic0.do", null, null); 1062 ActionMapping mapping = (ActionMapping) 1063 moduleConfig.findActionConfig("/dynamic0"); 1064 assertNotNull("Found /dynamic0 mapping", mapping); 1065 assertNotNull("Mapping has non-null name", 1066 mapping.getName()); 1067 assertEquals("Mapping has correct name", 1068 "dynamic0", 1069 mapping.getName()); 1070 assertNotNull("AppConfig has form bean " + mapping.getName(), 1071 moduleConfig.findFormBeanConfig(mapping.getName())); 1072 ActionForm form = RequestUtils.createActionForm 1073 (request, mapping, moduleConfig, null); 1074 assertNotNull("ActionForm returned", form); 1075 assertTrue("ActionForm of correct type", 1076 form instanceof DynaActionForm); 1077 1078 DynaActionForm dform = (DynaActionForm) form; 1080 Boolean booleanProperty = (Boolean ) dform.get("booleanProperty"); 1081 assertTrue("booleanProperty is true", booleanProperty.booleanValue()); 1082 String stringProperty = (String ) dform.get("stringProperty"); 1083 assertEquals("stringProperty is correct", 1084 "String Property", 1085 stringProperty); 1086 Object value = null; 1087 1088 value = dform.get("intArray1"); 1089 assertNotNull("intArray1 exists", value); 1090 assertTrue("intArray1 is int[]", value instanceof int[]); 1091 int intArray1[] = (int[]) value; 1092 assertEquals("intArray1 length is correct", 3, intArray1.length); 1093 assertEquals("intArray1[0] value is correct", 1, intArray1[0]); 1094 assertEquals("intArray1[1] value is correct", 2, intArray1[1]); 1095 assertEquals("intArray1[2] value is correct", 3, intArray1[2]); 1096 1097 value = dform.get("intArray2"); 1098 assertNotNull("intArray2 exists", value); 1099 assertTrue("intArray2 is int[]", value instanceof int[]); 1100 int intArray2[] = (int[]) value; 1101 assertEquals("intArray2 length is correct", 5, intArray2.length); 1102 assertEquals("intArray2[0] value is correct", 0, intArray2[0]); 1103 assertEquals("intArray2[1] value is correct", 0, intArray2[1]); 1104 assertEquals("intArray2[2] value is correct", 0, intArray2[2]); 1105 assertEquals("intArray2[3] value is correct", 0, intArray2[3]); 1106 assertEquals("intArray2[4] value is correct", 0, intArray2[4]); 1107 1108 value = dform.get("principal"); 1109 assertNotNull("principal exists", value); 1110 assertTrue("principal is MockPrincipal", 1111 value instanceof MockPrincipal); 1112 1113 value = dform.get("stringArray1"); 1114 assertNotNull("stringArray1 exists", value); 1115 assertTrue("stringArray1 is int[]", value instanceof String []); 1116 String stringArray1[] = (String []) value; 1117 assertEquals("stringArray1 length is correct", 3, stringArray1.length); 1118 assertEquals("stringArray1[0] value is correct", 1119 "aaa", stringArray1[0]); 1120 assertEquals("stringArray1[1] value is correct", 1121 "bbb", stringArray1[1]); 1122 assertEquals("stringArray1[2] value is correct", 1123 "ccc", stringArray1[2]); 1124 1125 value = dform.get("stringArray2"); 1126 assertNotNull("stringArray2 exists", value); 1127 assertTrue("stringArray2 is int[]", value instanceof String []); 1128 String stringArray2[] = (String []) value; 1129 assertEquals("stringArray2 length is correct", 3, stringArray2.length); 1130 assertEquals("stringArray2[0] value is correct", 1131 new String (), stringArray2[0]); 1132 assertEquals("stringArray2[1] value is correct", 1133 new String (), stringArray2[1]); 1134 assertEquals("stringArray2[2] value is correct", 1135 new String (), stringArray2[2]); 1136 1137 Object value1 = null; 1139 DynaActionForm dform1 = (DynaActionForm) 1140 RequestUtils.createActionForm(request, mapping, moduleConfig, null); 1141 1142 value = dform.get("principal"); 1143 value1 = dform1.get("principal"); 1144 assertEquals("Different form beans get equal instance values", 1145 value, value1); 1146 assertTrue("Different form beans get different instances 1", 1147 value != value1); 1148 1149 value = dform.get("stringArray1"); 1150 value1 = dform1.get("stringArray1"); 1151 assertTrue("Different form beans get different instances 2", 1152 value != value1); 1153 1154 dform1.set("stringProperty", "Different stringProperty value"); 1155 value = dform.get("stringProperty"); 1156 value1 = dform1.get("stringProperty"); 1157 assertTrue("Different form beans get different instances 3", 1158 value != value1); 1159 1160 } 1161 1162 1163 1164 1166 1167 public void testForwardURL1() { 1169 1170 request.setAttribute(Globals.MODULE_KEY, moduleConfig); 1171 request.setPathElements("/myapp", "/action.do", null, null); 1172 ForwardConfig forward = null; 1173 String result = null; 1174 1175 forward = moduleConfig.findForwardConfig("moduleForward"); 1177 assertNotNull("moduleForward found", forward); 1178 result = RequestUtils.forwardURL(request, forward, null); 1179 assertNotNull("moduleForward computed", result); 1180 assertEquals("moduleForward value", 1181 "/module/forward", 1182 result); 1183 1184 forward = moduleConfig.findForwardConfig("moduleRedirect"); 1186 assertNotNull("moduleRedirect found", forward); 1187 result = RequestUtils.forwardURL(request, forward, null); 1188 assertNotNull("moduleRedirect computed", result); 1189 assertEquals("moduleRedirect value", 1190 "/module/redirect", 1191 result); 1192 1193 forward = moduleConfig.findForwardConfig("contextForward"); 1195 assertNotNull("contextForward found", forward); 1196 result = RequestUtils.forwardURL(request, forward, null); 1197 assertNotNull("contextForward computed", result); 1198 assertEquals("contextForward value", 1199 "/context/forward", 1200 result); 1201 1202 forward = moduleConfig.findForwardConfig("contextRedirect"); 1204 assertNotNull("contextRedirect found", forward); 1205 result = RequestUtils.forwardURL(request, forward, null); 1206 assertNotNull("contextRedirect computed", result); 1207 assertEquals("contextRedirct value", 1208 "/context/redirect", 1209 result); 1210 1211 forward = moduleConfig.findForwardConfig("moduleNoslash"); 1213 assertNotNull("moduleNoslash found", forward); 1214 result = RequestUtils.forwardURL(request, forward, null); 1215 assertNotNull("moduleNoslash computed", result); 1216 assertEquals("moduleNoslash value", 1217 "/module/noslash", 1218 result); 1219 1220 forward = moduleConfig.findForwardConfig("contextNoslash"); 1222 assertNotNull("contextNoslash found", forward); 1223 result = RequestUtils.forwardURL(request, forward, null); 1224 assertNotNull("contextNoslash computed", result); 1225 assertEquals("contextNoslash value", 1226 "/context/noslash", 1227 result); 1228 1229 } 1230 1231 1232 public void testForwardURL2() { 1234 1235 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 1236 request.setPathElements("/myapp", "/2/action.do", null, null); 1237 ForwardConfig forward = null; 1238 String result = null; 1239 1240 forward = moduleConfig2.findForwardConfig("moduleForward"); 1242 assertNotNull("moduleForward found", forward); 1243 result = RequestUtils.forwardURL(request, forward, null); 1244 assertNotNull("moduleForward computed", result); 1245 assertEquals("moduleForward value", 1246 "/2/module/forward", 1247 result); 1248 1249 forward = moduleConfig2.findForwardConfig("moduleRedirect"); 1251 assertNotNull("moduleRedirect found", forward); 1252 result = RequestUtils.forwardURL(request, forward, null); 1253 assertNotNull("moduleRedirect computed", result); 1254 assertEquals("moduleRedirect value", 1255 "/2/module/redirect", 1256 result); 1257 1258 forward = moduleConfig2.findForwardConfig("contextForward"); 1260 assertNotNull("contextForward found", forward); 1261 result = RequestUtils.forwardURL(request, forward, null); 1262 assertNotNull("contextForward computed", result); 1263 assertEquals("contextForward value", 1264 "/context/forward", 1265 result); 1266 1267 forward = moduleConfig2.findForwardConfig("contextRedirect"); 1269 assertNotNull("contextRedirect found", forward); 1270 result = RequestUtils.forwardURL(request, forward, null); 1271 assertNotNull("contextRedirect computed", result); 1272 assertEquals("contextRedirct value", 1273 "/context/redirect", 1274 result); 1275 1276 forward = moduleConfig2.findForwardConfig("moduleNoslash"); 1278 assertNotNull("moduleNoslash found", forward); 1279 result = RequestUtils.forwardURL(request, forward, null); 1280 assertNotNull("moduleNoslash computed", result); 1281 assertEquals("moduleNoslash value", 1282 "/2/module/noslash", 1283 result); 1284 1285 forward = moduleConfig2.findForwardConfig("contextNoslash"); 1287 assertNotNull("contextNoslash found", forward); 1288 result = RequestUtils.forwardURL(request, forward, null); 1289 assertNotNull("contextNoslash computed", result); 1290 assertEquals("contextNoslash value", 1291 "/context/noslash", 1292 result); 1293 1294 } 1295 1296 1297 public void testForwardURL3() { 1299 1300 request.setAttribute(Globals.MODULE_KEY, moduleConfig3); 1301 request.setPathElements("/myapp", "/3/action.do", null, null); 1302 ForwardConfig forward = null; 1303 String result = null; 1304 1305 forward = moduleConfig3.findForwardConfig("moduleForward"); 1307 assertNotNull("moduleForward found", forward); 1308 result = RequestUtils.forwardURL(request, forward, null); 1309 assertNotNull("moduleForward computed", result); 1310 assertEquals("moduleForward value", 1311 "/forwarding/3/module/forward", 1312 result); 1313 1314 forward = moduleConfig3.findForwardConfig("moduleRedirect"); 1316 assertNotNull("moduleRedirect found", forward); 1317 result = RequestUtils.forwardURL(request, forward, null); 1318 assertNotNull("moduleRedirect computed", result); 1319 assertEquals("moduleRedirect value", 1320 "/forwarding/3/module/redirect", 1321 result); 1322 1323 forward = moduleConfig3.findForwardConfig("contextForward"); 1325 assertNotNull("contextForward found", forward); 1326 result = RequestUtils.forwardURL(request, forward, null); 1327 assertNotNull("contextForward computed", result); 1328 assertEquals("contextForward value", 1329 "/context/forward", 1330 result); 1331 1332 forward = moduleConfig3.findForwardConfig("contextRedirect"); 1334 assertNotNull("contextRedirect found", forward); 1335 result = RequestUtils.forwardURL(request, forward, null); 1336 assertNotNull("contextRedirect computed", result); 1337 assertEquals("contextRedirct value", 1338 "/context/redirect", 1339 result); 1340 1341 forward = moduleConfig3.findForwardConfig("moduleNoslash"); 1343 assertNotNull("moduleNoslash found", forward); 1344 result = RequestUtils.forwardURL(request, forward, null); 1345 assertNotNull("moduleNoslash computed", result); 1346 assertEquals("moduleNoslash value", 1347 "/forwarding/3/module/noslash", 1348 result); 1349 1350 forward = moduleConfig3.findForwardConfig("contextNoslash"); 1352 assertNotNull("contextNoslash found", forward); 1353 result = RequestUtils.forwardURL(request, forward, null); 1354 assertNotNull("contextNoslash computed", result); 1355 assertEquals("contextNoslash value", 1356 "/context/noslash", 1357 result); 1358 1359 } 1360 1361 1362 public void testForwardURLa() { 1364 1365 request.setAttribute(Globals.MODULE_KEY, moduleConfig); 1366 request.setPathElements("/myapp", "/action.do", null, null); 1367 ForwardConfig forward = null; 1368 String result = null; 1369 1370 forward = moduleConfig3.findForwardConfig("moduleForward"); 1372 assertNotNull("moduleForward found", forward); 1373 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1374 assertNotNull("moduleForward computed", result); 1375 assertEquals("moduleForward value", 1376 "/forwarding/3/module/forward", 1377 result); 1378 1379 forward = moduleConfig3.findForwardConfig("moduleRedirect"); 1381 assertNotNull("moduleRedirect found", forward); 1382 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1383 assertNotNull("moduleRedirect computed", result); 1384 assertEquals("moduleRedirect value", 1385 "/forwarding/3/module/redirect", 1386 result); 1387 1388 forward = moduleConfig3.findForwardConfig("contextForward"); 1390 assertNotNull("contextForward found", forward); 1391 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1392 assertNotNull("contextForward computed", result); 1393 assertEquals("contextForward value", 1394 "/context/forward", 1395 result); 1396 1397 forward = moduleConfig3.findForwardConfig("contextRedirect"); 1399 assertNotNull("contextRedirect found", forward); 1400 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1401 assertNotNull("contextRedirect computed", result); 1402 assertEquals("contextRedirct value", 1403 "/context/redirect", 1404 result); 1405 1406 forward = moduleConfig3.findForwardConfig("moduleNoslash"); 1408 assertNotNull("moduleNoslash found", forward); 1409 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1410 assertNotNull("moduleNoslash computed", result); 1411 assertEquals("moduleNoslash value", 1412 "/forwarding/3/module/noslash", 1413 result); 1414 1415 forward = moduleConfig3.findForwardConfig("contextNoslash"); 1417 assertNotNull("contextNoslash found", forward); 1418 result = RequestUtils.forwardURL(request, forward, moduleConfig3); 1419 assertNotNull("contextNoslash computed", result); 1420 assertEquals("contextNoslash value", 1421 "/context/noslash", 1422 result); 1423 1424 } 1425 1426 1427 1429 1430 public void testPageURL1() { 1432 1433 request.setAttribute(Globals.MODULE_KEY, moduleConfig); 1434 request.setPathElements("/myapp", "/action.do", null, null); 1435 String page = null; 1436 String result = null; 1437 1438 page = "/mypages/index.jsp"; 1440 result = RequestUtils.pageURL(request, page); 1441 assertNotNull("straight sub found", result); 1442 assertEquals("straight sub value", 1443 "/mypages/index.jsp", result); 1444 1445 1446 } 1447 1448 1449 public void testPageURL2() { 1451 1452 request.setAttribute(Globals.MODULE_KEY, moduleConfig2); 1453 request.setPathElements("/myapp", "/2/action.do", null, null); 1454 String page = null; 1455 String result = null; 1456 1457 page = "/mypages/index.jsp"; 1459 result = RequestUtils.pageURL(request, page); 1460 assertNotNull("straight sub found", result); 1461 assertEquals("straight sub value", 1462 "/2/mypages/index.jsp", result); 1463 1464 1465 } 1466 1467 1468 public void testPageURL3() { 1470 1471 request.setAttribute(Globals.MODULE_KEY, moduleConfig3); 1472 request.setPathElements("/myapp", "/3/action.do", null, null); 1473 String page = null; 1474 String result = null; 1475 1476 page = "/mypages/index.jsp"; 1478 result = RequestUtils.pageURL(request, page); 1479 assertNotNull("straight sub found", result); 1480 assertEquals("straight sub value", 1481 "/paging/3/mypages/index.jsp", result); 1482 1483 1484 } 1485 1486 1487 1489 1490 public void testRequestURL() { 1491 1492 request.setPathElements("/myapp", "/foo.do", null, null); 1493 String url = null; 1494 try { 1495 url = RequestUtils.requestURL(request).toString(); 1496 } catch (MalformedURLException e) { 1497 fail("MalformedURLException: " + e); 1498 } 1499 assertNotNull("URL was returned", url); 1500 assertEquals("URL value", 1501 "http://localhost:8080/myapp/foo.do", 1502 url); 1503 1504 } 1505 1506 1507 1509 1510 public void testSelectApplication1a() { 1512 1513 request.setPathElements("/myapp", "/noform.do", null, null); 1514 ModuleUtils.getInstance().selectModule(request, context); 1515 ModuleConfig moduleConfig = (ModuleConfig) 1516 request.getAttribute(Globals.MODULE_KEY); 1517 assertNotNull("Selected a module", moduleConfig); 1518 assertEquals("Selected correct module", 1519 "", moduleConfig.getPrefix()); 1520 1522 } 1523 1524 1525 public void testSelectApplication1b() { 1527 String [] prefixes = { "/1", "/2" }; 1528 context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes); 1529 request.setPathElements("/myapp", "/2/noform.do", null, null); 1530 1531 ModuleUtils.getInstance().selectModule(request, context); 1532 ModuleConfig moduleConfig = (ModuleConfig) 1533 request.getAttribute(Globals.MODULE_KEY); 1534 assertNotNull("Selected a module", moduleConfig); 1535 assertEquals("Selected correct module", 1536 "/2", moduleConfig.getPrefix()); 1537 1539 } 1540 1541 1542 public void testSelectApplication2a() { 1544 1545 request.setPathElements("/myapp", "/2/noform.do", null, null); 1546 request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH, 1547 "/noform.do"); 1548 ModuleUtils.getInstance().selectModule(request, context); 1549 ModuleConfig moduleConfig = (ModuleConfig) 1550 request.getAttribute(Globals.MODULE_KEY); 1551 assertNotNull("Selected an application", moduleConfig); 1552 assertEquals("Selected correct application", 1553 "", moduleConfig.getPrefix()); 1554 1556 } 1557 1558 1559 public void testSelectApplication2b() { 1561 String [] prefixes = { "/1", "/2" }; 1562 context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes); 1563 request.setPathElements("/myapp", "/noform.do", null, null); 1564 request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH, 1565 "/2/noform.do"); 1566 ModuleUtils.getInstance().selectModule(request, context); 1567 ModuleConfig moduleConfig = (ModuleConfig) 1568 request.getAttribute(Globals.MODULE_KEY); 1569 assertNotNull("Selected a module", moduleConfig); 1570 assertEquals("Selected correct module", 1571 "/2", moduleConfig.getPrefix()); 1572 1574 } 1575 1576 1577 1579 1580 public void testServerURL() { 1582 1583 String url = null; 1584 try { 1585 url = RequestUtils.serverURL(request).toString(); 1586 } catch (MalformedURLException e) { 1587 fail("Threw MalformedURLException: " + e); 1588 } 1589 assertNotNull("serverURL is present", url); 1590 assertEquals("serverURL value", 1591 "http://localhost:8080", 1592 url); 1593 1594 } 1595 1596 1597} 1598 | Popular Tags |