| 1 18 package org.apache.struts.taglib.logic; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.PageContext ; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import org.apache.cactus.WebResponse; 33 import org.apache.struts.taglib.SimpleBeanForTesting; 34 import org.apache.struts.taglib.TaglibTestBase; 35 36 37 38 43 public class TestIterateTag extends TaglibTestBase { 44 45 private int iterations = 2; 46 47 52 public TestIterateTag(String theName) { 53 super(theName); 54 } 55 56 61 public static void main(String [] theArgs) { 62 junit.awtui.TestRunner.main(new String [] {TestIterateTag.class.getName()}); 63 } 64 65 69 public static Test suite() { 70 return new TestSuite(TestIterateTag.class); 72 } 73 74 75 84 85 public void testApplicationScopeNameIterateList() 87 throws ServletException , JspException , IOException { 88 89 String testKey = "testApplicationScopeNameIterateList"; 90 91 ArrayList lst = new ArrayList (); 92 for (int i = 0; i < iterations; i++) { 93 lst.add("test" + i); 94 } 95 96 pageContext.setAttribute(testKey, lst, 97 PageContext.APPLICATION_SCOPE); 98 99 IterateTag tag = new IterateTag(); 100 tag.setPageContext(pageContext); 101 tag.setId("theId"); 102 tag.setName(testKey); 103 tag.setScope("application"); 104 105 int iteration = 0; 106 tag.doStartTag(); 107 tag.doInitBody(); 108 do 109 { 110 out.print((String )pageContext.getAttribute("theId")); 111 iteration++; 112 113 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 114 tag.doEndTag(); 115 assertEquals(iterations, iteration); 116 } 117 118 public void endApplicationScopeNameIterateList (WebResponse response){ 119 String output = response.getText(); 120 String compare = ""; 121 for (int i = 0; i < iterations; i++) { 122 compare += "test" + i; 123 } 124 125 output = replace(output,"\r",""); 127 output = replace(output,"\n",""); 128 129 assertEquals(compare, output); 130 } 131 132 public void testSessionScopeNameIterateList() 134 throws ServletException , JspException , IOException { 135 136 String testKey = "testSessionScopeNameIterateList"; 137 138 ArrayList lst = new ArrayList (); 139 for (int i = 0; i < iterations; i++) { 140 lst.add("test" + i); 141 } 142 143 pageContext.setAttribute(testKey, lst, 144 PageContext.SESSION_SCOPE); 145 146 IterateTag tag = new IterateTag(); 147 tag.setPageContext(pageContext); 148 tag.setId("theId"); 149 tag.setName(testKey); 150 tag.setScope("session"); 151 152 int iteration = 0; 153 tag.doStartTag(); 154 tag.doInitBody(); 155 do 156 { 157 out.print((String )pageContext.getAttribute("theId")); 158 iteration++; 159 160 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 161 tag.doEndTag(); 162 assertEquals(iterations, iteration); 163 } 164 165 public void endSessionScopeNameIterateList (WebResponse response){ 166 String output = response.getText(); 167 String compare = ""; 168 for (int i = 0; i < iterations; i++) { 169 compare += "test" + i; 170 } 171 172 output = replace(compare,"\r",""); 174 output = replace(output,"\n",""); 175 176 assertEquals(compare, output); 177 } 178 179 public void testRequestScopeNameIterateList() 181 throws ServletException , JspException , IOException { 182 183 String testKey = "testRequestScopeNameIterateList"; 184 185 ArrayList lst = new ArrayList (); 186 for (int i = 0; i < iterations; i++) { 187 lst.add("test" + i); 188 } 189 190 pageContext.setAttribute(testKey, lst, 191 PageContext.REQUEST_SCOPE); 192 193 IterateTag tag = new IterateTag(); 194 tag.setPageContext(pageContext); 195 tag.setId("theId"); 196 tag.setName(testKey); 197 tag.setScope("request"); 198 199 int iteration = 0; 200 tag.doStartTag(); 201 tag.doInitBody(); 202 do 203 { 204 out.print((String )pageContext.getAttribute("theId")); 205 iteration++; 206 207 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 208 tag.doEndTag(); 209 assertEquals(iterations, iteration); 210 } 211 212 public void endRequestScopeNameIterateList (WebResponse response){ 213 String output = response.getText(); 214 String compare = ""; 215 for (int i = 0; i < iterations; i++) { 216 compare += "test" + i; 217 } 218 219 output = replace(compare,"\r",""); 221 output = replace(output,"\r",""); 222 223 assertEquals(compare, output); 224 } 225 226 227 236 237 public void testApplicationScopePropertyIterateList() 239 throws ServletException , JspException , IOException { 240 241 242 String testKey = "testApplicationScopePropertyIterate"; 243 244 ArrayList lst = new ArrayList (); 245 for (int i = 0; i < iterations; i++) { 246 lst.add("test" + i); 247 } 248 249 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 250 sbft.setList(lst); 251 252 pageContext.setAttribute(testKey, sbft, 253 PageContext.APPLICATION_SCOPE); 254 255 IterateTag tag = new IterateTag(); 256 tag.setPageContext(pageContext); 257 262 tag.setId("theId"); 263 tag.setName(testKey); 264 tag.setScope("application"); 265 tag.setProperty("list"); 266 267 int iteration = 0; 268 tag.doStartTag(); 269 tag.doInitBody(); 270 do 271 { 272 out.print((String )pageContext.getAttribute("theId")); 273 iteration++; 274 275 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 276 tag.doEndTag(); 277 assertEquals(iterations, iteration); 278 } 279 280 public void endApplicationScopePropertyIterateList (WebResponse response){ 281 String output = response.getText(); 282 String compare = ""; 283 for (int i = 0; i < iterations; i++) { 284 compare += "test" + i; 285 } 286 287 output = replace(compare,"\r",""); 289 output = replace(output,"\n",""); 290 291 assertEquals(compare, output); 292 } 293 294 295 public void testSessionScopePropertyIteratesList() 297 throws ServletException , JspException , IOException { 298 299 300 String testKey = "testSessionScopePropertyIterate"; 301 302 ArrayList lst = new ArrayList (); 303 for (int i = 0; i < iterations; i++) { 304 lst.add("test" + i); 305 } 306 307 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 308 sbft.setList(lst); 309 310 pageContext.setAttribute(testKey, sbft, 311 PageContext.SESSION_SCOPE); 312 313 IterateTag tag = new IterateTag(); 314 tag.setPageContext(pageContext); 315 320 tag.setId("theId"); 321 tag.setName(testKey); 322 tag.setScope("session"); 323 tag.setProperty("list"); 324 325 int iteration = 0; 326 tag.doStartTag(); 327 tag.doInitBody(); 328 do 329 { 330 out.print((String )pageContext.getAttribute("theId")); 331 iteration++; 332 333 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 334 tag.doEndTag(); 335 assertEquals(iterations, iteration); 336 } 337 338 public void endSessionScopePropertyIterateList (WebResponse response){ 339 String output = response.getText(); 340 String compare = ""; 341 for (int i = 0; i < iterations; i++) { 342 compare += "test" + i; 343 } 344 345 output = replace(compare,"\r",""); 347 output = replace(output,"\n",""); 348 349 assertEquals(compare, output); 350 } 351 352 353 public void testRequestScopePropertyIteratesList() 355 throws ServletException , JspException , IOException { 356 357 358 String testKey = "testRequestScopePropertyIterate"; 359 360 ArrayList lst = new ArrayList (); 361 for (int i = 0; i < iterations; i++) { 362 lst.add("test" + i); 363 } 364 365 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 366 sbft.setList(lst); 367 368 pageContext.setAttribute(testKey, sbft, 369 PageContext.REQUEST_SCOPE); 370 371 IterateTag tag = new IterateTag(); 372 tag.setPageContext(pageContext); 373 378 tag.setId("theId"); 379 tag.setName(testKey); 380 tag.setScope("request"); 381 tag.setProperty("list"); 382 383 int iteration = 0; 384 tag.doStartTag(); 385 tag.doInitBody(); 386 do 387 { 388 out.print((String )pageContext.getAttribute("theId")); 389 iteration++; 390 391 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 392 tag.doEndTag(); 393 assertEquals(iterations, iteration); 394 } 395 396 public void endRequestScopePropertyIterateList (WebResponse response){ 397 String output = response.getText(); 398 String compare = ""; 399 for (int i = 0; i < iterations; i++) { 400 compare += "test" + i; 401 } 402 403 output = replace(compare,"\r",""); 405 output = replace(output,"\n",""); 406 407 assertEquals(compare, output); 408 } 409 410 411 412 421 422 public void testApplicationScopeNameIterateEnumeration() 424 throws ServletException , JspException , IOException { 425 426 String testKey = "testApplicationScopeNameIterateEnumeration"; 427 428 StringTokenizer st = new StringTokenizer ("Application Scope Name Iterate Enumeration"); 429 430 pageContext.setAttribute(testKey, st, 431 PageContext.APPLICATION_SCOPE); 432 433 IterateTag tag = new IterateTag(); 434 tag.setPageContext(pageContext); 435 tag.setId("theId"); 436 tag.setName(testKey); 437 tag.setScope("application"); 438 439 tag.doStartTag(); 440 tag.doInitBody(); 441 do 442 { 443 out.print((String )pageContext.getAttribute("theId")); 444 445 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 446 tag.doEndTag(); 447 448 } 449 450 public void endApplicationScopeNameIterateEnumeration (WebResponse response){ 451 String output = response.getText(); 452 StringTokenizer st = new StringTokenizer ("Application Scope Name Iterate Enumeration"); 453 String compare = ""; 454 455 while (st.hasMoreTokens()) { 456 compare += st.nextToken(); 457 } 458 459 output = replace(compare,"\r",""); 461 output = replace(output,"\n",""); 462 463 assertEquals(compare, output); 464 } 465 466 public void testSessionScopeNameIterateEnumeration() 468 throws ServletException , JspException , IOException { 469 470 String testKey = "testSessionScopeNameIterateEnumeration"; 471 472 StringTokenizer st = new StringTokenizer ("Session Scope Name Iterate Enumeration"); 473 474 pageContext.setAttribute(testKey, st, 475 PageContext.SESSION_SCOPE); 476 477 IterateTag tag = new IterateTag(); 478 tag.setPageContext(pageContext); 479 tag.setId("theId"); 480 tag.setName(testKey); 481 tag.setScope("session"); 482 483 tag.doStartTag(); 484 tag.doInitBody(); 485 do 486 { 487 out.print((String )pageContext.getAttribute("theId")); 488 489 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 490 tag.doEndTag(); 491 492 } 493 494 public void endSessionScopeNameIterateEnumeration (WebResponse response){ 495 String output = response.getText(); 496 StringTokenizer st = new StringTokenizer ("Session Scope Name Iterate Enumeration"); 497 String compare = ""; 498 499 while (st.hasMoreTokens()) { 500 compare += st.nextToken(); 501 } 502 503 output = replace(compare,"\r",""); 505 output = replace(output,"\n",""); 506 507 assertEquals(compare, output); 508 } 509 510 public void testRequestScopeNameIterateEnumeration() 512 throws ServletException , JspException , IOException { 513 514 String testKey = "testRequestScopeNameIterateEnumeration"; 515 516 StringTokenizer st = new StringTokenizer ("Request Scope Name Iterate Enumeration"); 517 518 pageContext.setAttribute(testKey, st, 519 PageContext.REQUEST_SCOPE); 520 521 IterateTag tag = new IterateTag(); 522 tag.setPageContext(pageContext); 523 tag.setId("theId"); 524 tag.setName(testKey); 525 tag.setScope("request"); 526 527 tag.doStartTag(); 528 tag.doInitBody(); 529 do 530 { 531 out.print((String )pageContext.getAttribute("theId")); 532 533 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 534 tag.doEndTag(); 535 536 } 537 538 public void endRequestScopeNameIterateEnumeration (WebResponse response){ 539 String output = response.getText(); 540 StringTokenizer st = new StringTokenizer ("Request Scope Name Iterate Enumeration"); 541 String compare = ""; 542 543 while (st.hasMoreTokens()) { 544 compare += st.nextToken(); 545 } 546 547 output = replace(compare,"\r",""); 549 output = replace(output,"\n",""); 550 551 assertEquals(compare, output); 552 } 553 554 555 564 565 public void testApplicationScopePropertyIterateEnumeration() 567 throws ServletException , JspException , IOException { 568 569 String testKey = "testApplicationScopePropertyIterateEnumeration"; 570 571 StringTokenizer st = new StringTokenizer ("Application Scope Property Iterate Enumeration"); 572 573 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 574 sbft.setEnumeration(st); 575 576 pageContext.setAttribute(testKey, sbft, 577 PageContext.APPLICATION_SCOPE); 578 579 IterateTag tag = new IterateTag(); 580 tag.setPageContext(pageContext); 581 tag.setId("theId"); 582 tag.setName(testKey); 583 tag.setScope("application"); 584 tag.setProperty("enumeration"); 585 586 tag.doStartTag(); 587 tag.doInitBody(); 588 do 589 { 590 out.print((String )pageContext.getAttribute("theId")); 591 592 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 593 tag.doEndTag(); 594 595 } 596 597 public void endApplicationScopePropertyIterateEnumeration (WebResponse response){ 598 String output = response.getText(); 599 StringTokenizer st = new StringTokenizer ("Application Scope Property Iterate Enumeration"); 600 String compare = ""; 601 602 while (st.hasMoreTokens()) { 603 compare += st.nextToken(); 604 } 605 606 output = replace(compare,"\r",""); 608 output = replace(output,"\n",""); 609 610 assertEquals(compare, output); 611 } 612 613 public void testSessionScopePropertyIterateEnumeration() 615 throws ServletException , JspException , IOException { 616 617 String testKey = "testSessionScopePropertyIterateEnumeration"; 618 619 StringTokenizer st = new StringTokenizer ("Session Scope Property Iterate Enumeration"); 620 621 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 622 sbft.setEnumeration(st); 623 624 pageContext.setAttribute(testKey, sbft, 625 PageContext.SESSION_SCOPE); 626 627 IterateTag tag = new IterateTag(); 628 tag.setPageContext(pageContext); 629 tag.setId("theId"); 630 tag.setName(testKey); 631 tag.setScope("session"); 632 tag.setProperty("enumeration"); 633 634 tag.doStartTag(); 635 tag.doInitBody(); 636 do 637 { 638 out.print((String )pageContext.getAttribute("theId")); 639 640 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 641 tag.doEndTag(); 642 643 } 644 645 public void endSessionScopePropertyIterateEnumeration (WebResponse response){ 646 String output = response.getText(); 647 StringTokenizer st = new StringTokenizer ("Session Scope Property Iterate Enumeration"); 648 String compare = ""; 649 650 while (st.hasMoreTokens()) { 651 compare += st.nextToken(); 652 } 653 654 output = replace(compare,"\r",""); 656 output = replace(output,"\n",""); 657 658 assertEquals(compare, output); 659 } 660 661 public void testRequestScopePropertyIterateEnumeration() 663 throws ServletException , JspException , IOException { 664 665 String testKey = "testRequestScopePropertyIterateEnumeration"; 666 667 StringTokenizer st = new StringTokenizer ("Request Scope Property Iterate Enumeration"); 668 669 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 670 sbft.setEnumeration(st); 671 672 pageContext.setAttribute(testKey, sbft, 673 PageContext.REQUEST_SCOPE); 674 675 IterateTag tag = new IterateTag(); 676 tag.setPageContext(pageContext); 677 tag.setId("theId"); 678 tag.setName(testKey); 679 tag.setScope("request"); 680 tag.setProperty("enumeration"); 681 682 tag.doStartTag(); 683 tag.doInitBody(); 684 do 685 { 686 out.print((String )pageContext.getAttribute("theId")); 687 688 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 689 tag.doEndTag(); 690 691 } 692 693 public void endRequestScopePropertyIterateEnumeration (WebResponse response){ 694 String output = response.getText(); 695 StringTokenizer st = new StringTokenizer ("Request Scope Property Iterate Enumeration"); 696 String compare = ""; 697 698 while (st.hasMoreTokens()) { 699 compare += st.nextToken(); 700 } 701 702 output = replace(compare,"\r",""); 704 output = replace(output,"\n",""); 705 706 assertEquals(compare, output); 707 } 708 709 710 711 712 713 722 723 public void testApplicationScopeNameIterateMap() 725 throws ServletException , JspException , IOException { 726 727 String testKey = "testApplicationScopeNameIterateMap"; 728 729 HashMap map = new HashMap (); 730 for (int i = 0; i < iterations; i++) { 731 map.put("test" + i,"test" + i); 732 } 733 734 pageContext.setAttribute(testKey, map, 735 PageContext.APPLICATION_SCOPE); 736 737 IterateTag tag = new IterateTag(); 738 tag.setPageContext(pageContext); 739 tag.setId("theId"); 740 tag.setName(testKey); 741 tag.setScope("application"); 742 743 int iteration = 0; 744 tag.doStartTag(); 745 tag.doInitBody(); 746 do 747 { 748 out.print(pageContext.getAttribute("theId")); 749 iteration++; 750 751 } while (tag.doAfterBody() == IterateTag.EVAL_BODY_TAG); 752 tag.doEndTag(); 753 assertEquals(iterations, iteration); 754 } 755 756 public void endApplicationScopeNameIterateMap (WebResponse response){ 757 String output = response.getText(); 758 String compare = ""; 759 for (int i = 0; i < iterations; i++) { 760 compare += "test" + i; 761 } 762 763 output = replace(compare,"\r",""); 765 output = replace(output,"\n",""); 766 767 assertEquals(compare, output); 768 } 769 770 public void testSessionScopeNameIterateMap() 772 throws ServletException , JspException , IOException { 773 774 String testKey = "testSessionScopeNameIterateMap"; 775 776 HashMap map = new HashMap (); 777 for (int i = 0; i < iterations; i++) { 778 map.put("test" + i,"test" + i); 779 } 780 781 pageContext.setAttribute(testKey, map, 782 PageContext.SESSION_SCOPE); 783 784 IterateTag tag = new IterateTag(); 785 tag.setPageContext(pageContext); 786 tag.setId("theId"); 787 tag.setName(testKey); 788 tag.setScope("session"); 789 790 int iteration = 0; 791 tag.doStartTag(); 792 tag.doInitBody(); 793 do 794 &n
|