| 1 20 package org.apache.cactus.sample.servlet.unit; 21 22 import java.io.IOException ; 23 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspTagException ; 26 import javax.servlet.jsp.tagext.BodyContent ; 27 import javax.servlet.jsp.jstl.core.LoopTagStatus; 28 29 import org.apache.cactus.extension.jsp.JspTagLifecycle; 30 import org.apache.cactus.JspTestCase; 31 import org.apache.cactus.WebResponse; 32 import org.apache.taglibs.standard.tag.common.core.ChooseTag; 33 import org.apache.taglibs.standard.tag.common.core.OtherwiseTag; 34 import org.apache.taglibs.standard.tag.el.core.ForEachTag; 35 import org.apache.taglibs.standard.tag.el.core.IfTag; 36 import org.apache.taglibs.standard.tag.el.core.OutTag; 37 import org.apache.taglibs.standard.tag.el.core.SetTag; 38 import org.apache.taglibs.standard.tag.el.core.WhenTag; 39 40 51 public class TestJspTagLifecycle extends JspTestCase 52 { 53 55 59 public void testConstructorWithNullPageContext() 60 { 61 try 62 { 63 new JspTagLifecycle(null, new OutTag()); 64 fail("Expected NullPointerException"); 65 } 66 catch (NullPointerException expected) 67 { 68 } 70 } 71 72 76 public void testConstructorWithNullTag() 77 { 78 try 79 { 80 new JspTagLifecycle(pageContext, null); 81 fail("Expected NullPointerException"); 82 } 83 catch (NullPointerException expected) 84 { 85 } 87 } 88 89 94 public void testAddInterceptorWithNull() 95 { 96 try 97 { 98 JspTagLifecycle lifecycle = 99 new JspTagLifecycle(pageContext, new OutTag()); 100 lifecycle.addInterceptor(null); 101 fail("Expected NullPointerException"); 102 } 103 catch (NullPointerException expected) 104 { 105 } 107 } 108 109 114 public void testAddNestedTagWithNull() 115 { 116 try 117 { 118 JspTagLifecycle lifecycle = 119 new JspTagLifecycle(pageContext, new OutTag()); 120 lifecycle.addNestedTag(null); 121 fail("Expected NullPointerException"); 122 } 123 catch (NullPointerException expected) 124 { 125 } 127 } 128 129 134 public void testAddNestedTextWithNull() 135 { 136 try 137 { 138 JspTagLifecycle lifecycle = 139 new JspTagLifecycle(pageContext, new OutTag()); 140 lifecycle.addNestedText(null); 141 fail("Expected NullPointerException"); 142 } 143 catch (NullPointerException expected) 144 { 145 } 147 } 148 149 154 public void testAssertScopedVariableExposedWithNullName() 155 { 156 try 157 { 158 JspTagLifecycle lifecycle = 159 new JspTagLifecycle(pageContext, new OutTag()); 160 lifecycle.expectScopedVariableExposed(null, new Object [] {"value"}); 161 fail("Expected NullPointerException"); 162 } 163 catch (NullPointerException expected) 164 { 165 } 167 } 168 169 174 public void testAssertScopedVariableExposedWithNullExpectedValues() 175 { 176 try 177 { 178 JspTagLifecycle lifecycle = 179 new JspTagLifecycle(pageContext, new OutTag()); 180 lifecycle.expectScopedVariableExposed("name", null); 181 fail("Expected NullPointerException"); 182 } 183 catch (NullPointerException expected) 184 { 185 } 187 } 188 189 194 public void testAssertScopedVariableExposedWithEmptyExpectedValues() 195 { 196 try 197 { 198 JspTagLifecycle lifecycle = 199 new JspTagLifecycle(pageContext, new OutTag()); 200 lifecycle.expectScopedVariableExposed("name", new Object [0]); 201 fail("Expected IllegalArgumentException"); 202 } 203 catch (IllegalArgumentException expected) 204 { 205 } 207 } 208 209 214 public void testAssertScopedVariableExposedWithIllegalScope() 215 { 216 try 217 { 218 JspTagLifecycle lifecycle = 219 new JspTagLifecycle(pageContext, new OutTag()); 220 lifecycle.expectScopedVariableExposed( 221 "name", new Object []{"value"}, 0); 222 fail("Expected IllegalArgumentException"); 223 } 224 catch (IllegalArgumentException expected) 225 { 226 } 228 } 229 230 237 public void testOutTag() 238 throws JspException , IOException  239 { 240 OutTag tag = new OutTag(); 241 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 242 tag.setValue("Value"); 243 lifecycle.expectBodySkipped(); 244 lifecycle.invoke(); 245 } 246 247 253 public void endOutTag(WebResponse theResponse) 254 { 255 String output = theResponse.getText(); 256 assertEquals("Value", output); 257 } 258 259 267 public void testOutTagEscapeXml() 268 throws JspException , IOException  269 { 270 OutTag tag = new OutTag(); 271 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 272 tag.setValue("<value/>"); 273 lifecycle.expectBodySkipped(); 274 lifecycle.invoke(); 275 } 276 277 283 public void endOutTagEscapeXml(WebResponse theResponse) 284 { 285 String output = theResponse.getText(); 286 assertEquals("<value/>", output); 287 } 288 289 297 public void testOutTagNoEscapeXml() 298 throws JspException , IOException  299 { 300 OutTag tag = new OutTag(); 301 tag.setEscapeXml("false"); 302 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 303 tag.setValue("<value/>"); 304 lifecycle.expectBodySkipped(); 305 lifecycle.invoke(); 306 } 307 308 314 public void endOutTagNoEscapeXml(WebResponse theResponse) 315 { 316 String output = theResponse.getText(); 317 assertEquals("<value/>", output); 318 } 319 320 328 public void testOutTagDefaultAttribute() 329 throws JspException , IOException  330 { 331 OutTag tag = new OutTag(); 332 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 333 tag.setValue(null); 334 tag.setDefault("Default"); 335 lifecycle.expectBodySkipped(); 336 lifecycle.invoke(); 337 } 338 339 345 public void endOutTagWithDefaultAttribute(WebResponse theResponse) 346 { 347 String output = theResponse.getText(); 348 assertEquals("Default", output); 349 } 350 351 361 public void testOutTagDefaultAttributeIgnored() 362 throws JspException , IOException  363 { 364 OutTag tag = new OutTag(); 365 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 366 tag.setValue("Value"); 367 tag.setDefault("Default"); 368 lifecycle.expectBodySkipped(); 369 lifecycle.invoke(); 370 } 371 372 378 public void endOutTagWithDefaultAttributeIgnored(WebResponse theResponse) 379 { 380 String output = theResponse.getText(); 381 assertEquals("Value", output); 382 } 383 384 391 public void testOutTagDefaultBody() 392 throws JspException , IOException  393 { 394 OutTag tag = new OutTag(); 395 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 396 tag.setValue(null); 397 lifecycle.addNestedText("Default"); 398 lifecycle.expectBodyEvaluated(); 399 lifecycle.invoke(); 400 } 401 402 408 public void endOutTagDefaultBody(WebResponse theResponse) 409 { 410 String output = theResponse.getText(); 411 assertEquals("Default", output); 412 } 413 414 423 public void testOutTagDefaultBodyIgnored() 424 throws JspException , IOException  425 { 426 OutTag tag = new OutTag(); 427 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 428 tag.setValue("Value"); 429 lifecycle.addNestedText("Default"); 430 lifecycle.expectBodySkipped(); 431 lifecycle.invoke(); 432 } 433 434 440 public void endOutTagDefaultBodyIgnored(WebResponse theResponse) 441 { 442 String output = theResponse.getText(); 443 assertEquals("Value", output); 444 } 445 446 454 public void testSetTag() 455 throws JspException , IOException  456 { 457 SetTag tag = new SetTag(); 458 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 459 tag.setVar("Var"); 460 tag.setValue("Value"); 461 lifecycle.invoke(); 462 assertEquals("Value", pageContext.findAttribute("Var")); 463 } 464 465 473 public void testForEachTag() 474 throws JspException , IOException  475 { 476 ForEachTag tag = new ForEachTag(); 477 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 478 tag.setVar("Item"); 479 tag.setItems("One,Two,Three"); 480 lifecycle.expectBodyEvaluated(3); 481 lifecycle.expectScopedVariableExposed( 482 "Item", new Object [] {"One", "Two", "Three"}); 483 lifecycle.invoke(); 484 } 485 486 494 public void testForEachTagStatus() 495 throws JspException , IOException  496 { 497 ForEachTag tag = new ForEachTag(); 498 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 499 tag.setVarStatus("status"); 500 tag.setBegin("0"); 501 tag.setEnd("2"); 502 lifecycle.expectBodyEvaluated(3); 503 lifecycle.addInterceptor(new JspTagLifecycle.Interceptor() 504 { 505 public void evalBody(int theIteration, BodyContent theBody) 506 { 507 LoopTagStatus status = (LoopTagStatus) 508 pageContext.findAttribute("status"); 509 assertNotNull(status); 510 if (theIteration == 0) 511 { 512 assertEquals(0, status.getIndex()); 513 assertEquals(1, status.getCount()); 514 assertTrue(status.isFirst()); 515 assertTrue(!status.isLast()); 516 } 517 else if (theIteration == 1) 518 { 519 assertEquals(1, status.getIndex()); 520 assertEquals(2, status.getCount()); 521 assertTrue(!status.isFirst()); 522 assertTrue(!status.isLast()); 523 } 524 else if (theIteration == 2) 525 { 526 assertEquals(2, status.getIndex()); 527 assertEquals(3, status.getCount()); 528 assertTrue(!status.isFirst()); 529 assertTrue(status.isLast()); 530 } 531 } 532 }); 533 lifecycle.invoke(); 534 } 535 536 545 public void testIfTagTrue() 546 throws JspException , IOException  547 { 548 IfTag tag = new IfTag(); 549 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 550 tag.setTest("true"); 551 lifecycle.addNestedText("Value"); 552 lifecycle.expectBodyEvaluated(); 553 lifecycle.invoke(); 554 } 555 556 562 public void endIfTagTrue(WebResponse theResponse) 563 { 564 String output = theResponse.getText(); 565 assertEquals("Value", output); 566 } 567 568 577 public void testIfTagFalse() 578 throws JspException , IOException  579 { 580 IfTag tag = new IfTag(); 581 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 582 tag.setTest("false"); 583 lifecycle.addNestedText("Value"); 584 lifecycle.expectBodySkipped(); 585 lifecycle.invoke(); 586 } 587 588 594 public void endIfTagFalse(WebResponse theResponse) 595 { 596 String output = theResponse.getText(); 597 assertEquals("", output); 598 } 599 600 610 public void testWhenTag() 611 throws JspException , IOException  612 { 613 ChooseTag chooseTag = new ChooseTag(); 614 JspTagLifecycle chooseLifecycle = 615 new JspTagLifecycle(pageContext, chooseTag); 616 617 WhenTag whenTag = new WhenTag(); 618 JspTagLifecycle whenLifecycle = 619 chooseLifecycle.addNestedTag(whenTag); 620 whenTag.setTest("true"); 621 whenLifecycle.expectBodyEvaluated(); 622 623 chooseLifecycle.invoke(); 624 } 625 626 637 public void testWhenTagNoPermission() 638 throws JspException , IOException  639 { 640 ChooseTag chooseTag = new ChooseTag(); 641 JspTagLifecycle chooseLifecycle = 642 new JspTagLifecycle(pageContext, chooseTag); 643 644 WhenTag whenTag = new WhenTag(); 645 JspTagLifecycle whenLifecycle = 646 chooseLifecycle.addNestedTag(whenTag); 647 whenTag.setTest("false"); 648 whenLifecycle.expectBodySkipped(); 649 650 OtherwiseTag otherwiseTag = new OtherwiseTag(); 651 JspTagLifecycle otherwiseLifecycle = 652 chooseLifecycle.addNestedTag(otherwiseTag); 653 otherwiseLifecycle.expectBodyEvaluated(); 654 655 chooseLifecycle.invoke(); 656 } 657 658 666 public void testWhenTagWithoutChooseTag() 667 throws JspException , IOException  668 { 669 WhenTag tag = new WhenTag(); 670 JspTagLifecycle lifecycle = new JspTagLifecycle(pageContext, tag); 671 tag.setTest("true"); 672 try 673 { 674 lifecycle.invoke(); 675 fail("Expected JSPTagException"); 676 } 677 catch (JspTagException expected) 678 { 679 } 681 } 682 683 } 684 | Popular Tags |