KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestJspTagLifecycle


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet.unit;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
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 /**
41  * Tests for the <code>JspTagLifecycle</code> extension.
42  *
43  * <p>
44  * The lifecycle helper is tested here by testing the core tags of the
45  * reference implementation of the JSP standard tag library (JSTL), available
46  * at <a HREF="http://jakarta.apache.org/taglibs/">.
47  * </p>
48  *
49  * @version $Id: TestJspTagLifecycle.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
50  */

51 public class TestJspTagLifecycle extends JspTestCase
52 {
53     // Test Methods ------------------------------------------------------------
54

55     /**
56      * Tests whether the constructor throws a <code>NullPointerException</code>
57      * when passed a <code>null</code> <code>PageContext</code> reference.
58      */

59     public void testConstructorWithNullPageContext()
60     {
61         try
62         {
63             new JspTagLifecycle(null, new OutTag());
64             fail("Expected NullPointerException");
65         }
66         catch (NullPointerException JavaDoc expected)
67         {
68             // expected
69
}
70     }
71     
72     /**
73      * Tests whether the constructor throws a <code>NullPointerException</code>
74      * when passed a <code>null</code> <code>Tag</code> reference.
75      */

76     public void testConstructorWithNullTag()
77     {
78         try
79         {
80             new JspTagLifecycle(pageContext, null);
81             fail("Expected NullPointerException");
82         }
83         catch (NullPointerException JavaDoc expected)
84         {
85             // expected
86
}
87     }
88     
89     /**
90      * Tests whether the <code>addInterceptor()</code> method throws a
91      * <code>NullPointerException</code> when passed a <code>null</code>
92      * <code>Interceptor</code> reference.
93      */

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 JavaDoc expected)
104         {
105             // expected
106
}
107     }
108     
109     /**
110      * Tests whether the <code>addNestedTag()</code> method throws a
111      * <code>NullPointerException</code> when passed a <code>null</code>
112      * <code>Interceptor</code> reference.
113      */

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 JavaDoc expected)
124         {
125             // expected
126
}
127     }
128     
129     /**
130      * Tests whether the <code>addNestedText()</code> method throws a
131      * <code>NullPointerException</code> when passed a <code>null</code>
132      * <code>Interceptor</code> reference.
133      */

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 JavaDoc expected)
144         {
145             // expected
146
}
147     }
148     
149     /**
150      * Tests whether the <code>assertScopedVariableExposed()</code> method
151      * throws a <code>NullPointerException</code> when passed a
152      * <code>null</code> name.
153      */

154     public void testAssertScopedVariableExposedWithNullName()
155     {
156         try
157         {
158             JspTagLifecycle lifecycle =
159                 new JspTagLifecycle(pageContext, new OutTag());
160             lifecycle.expectScopedVariableExposed(null, new Object JavaDoc[] {"value"});
161             fail("Expected NullPointerException");
162         }
163         catch (NullPointerException JavaDoc expected)
164         {
165             // expected
166
}
167     }
168     
169     /**
170      * Tests whether the <code>assertScopedVariableExposed()</code> method
171      * throws a <code>NullPointerException</code> when passed a
172      * <code>null</code> reference as expected values array.
173      */

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 JavaDoc expected)
184         {
185             // expected
186
}
187     }
188     
189     /**
190      * Tests whether the <code>assertScopedVariableExposed()</code> method
191      * throws a <code>IllegalArgumentException</code> when passed an empty
192      * expected values array.
193      */

194     public void testAssertScopedVariableExposedWithEmptyExpectedValues()
195     {
196         try
197         {
198             JspTagLifecycle lifecycle =
199                 new JspTagLifecycle(pageContext, new OutTag());
200             lifecycle.expectScopedVariableExposed("name", new Object JavaDoc[0]);
201             fail("Expected IllegalArgumentException");
202         }
203         catch (IllegalArgumentException JavaDoc expected)
204         {
205             // expected
206
}
207     }
208     
209     /**
210      * Tests whether the <code>assertScopedVariableExposed()</code> method
211      * throws a <code>IllegalArgumentException</code> when passed an invalid
212      * scope identifier.
213      */

214     public void testAssertScopedVariableExposedWithIllegalScope()
215     {
216         try
217         {
218             JspTagLifecycle lifecycle =
219                 new JspTagLifecycle(pageContext, new OutTag());
220             lifecycle.expectScopedVariableExposed(
221                 "name", new Object JavaDoc[]{"value"}, 0);
222             fail("Expected IllegalArgumentException");
223         }
224         catch (IllegalArgumentException JavaDoc expected)
225         {
226             // expected
227
}
228     }
229     
230     /**
231      * Tests the <code>&lt;c:out&gt;</code>-tag with a proper, literal value for
232      * it's <code>value</code> attribute.
233      *
234      * @throws JspException If the tag throws a JSPException
235      * @throws IOException If the tag throws an IOException
236      */

237     public void testOutTag()
238         throws JspException JavaDoc, IOException JavaDoc
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     /**
248      * Verifies that the response has been correctly rendered by the
249      * <code>&lt;c:out&gt;</code>-tag.
250      *
251      * @param theResponse The HTTP response
252      */

253     public void endOutTag(WebResponse theResponse)
254     {
255         String JavaDoc output = theResponse.getText();
256         assertEquals("Value", output);
257     }
258     
259     /**
260      * Tests the <code>&lt;c:out&gt;</code>-tag with a proper, literal value for
261      * it's <code>value</code> attribute that contains special XML characters
262      * that need to be escaped.
263      *
264      * @throws JspException If the tag throws a JSPException
265      * @throws IOException If the tag throws an IOException
266      */

267     public void testOutTagEscapeXml()
268         throws JspException JavaDoc, IOException JavaDoc
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     /**
278      * Verifies that the response has been correctly rendered by the
279      * <code>&lt;c:out&gt;</code>-tag.
280      *
281      * @param theResponse The HTTP response
282      */

283     public void endOutTagEscapeXml(WebResponse theResponse)
284     {
285         String JavaDoc output = theResponse.getText();
286         assertEquals("&lt;value/&gt;", output);
287     }
288     
289     /**
290      * Tests the <code>&lt;c:out&gt;</code>-tag with a proper, literal value for
291      * it's <code>value</code> attribute that contains special XML characters
292      * that need to be escaped.
293      *
294      * @throws JspException If the tag throws a JSPException
295      * @throws IOException If the tag throws an IOException
296      */

297     public void testOutTagNoEscapeXml()
298         throws JspException JavaDoc, IOException JavaDoc
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     /**
309      * Verifies that the response has been correctly rendered by the
310      * <code>&lt;c:out&gt;</code>-tag.
311      *
312      * @param theResponse The HTTP response
313      */

314     public void endOutTagNoEscapeXml(WebResponse theResponse)
315     {
316         String JavaDoc output = theResponse.getText();
317         assertEquals("<value/>", output);
318     }
319     
320     /**
321      * Tests the <code>&lt;c:out&gt;</code>-tag with <code>null</code> for
322      * it's <code>value</code> attribute, and a proper, literal value for it's
323      * <code>default</code> attribute.
324      *
325      * @throws JspException If the tag throws a JSPException
326      * @throws IOException If the tag throws an IOException
327      */

328     public void testOutTagDefaultAttribute()
329         throws JspException JavaDoc, IOException JavaDoc
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     /**
340      * Verifies that the response has been correctly rendered by the
341      * <code>&lt;c:out&gt;</code>-tag.
342      *
343      * @param theResponse The HTTP response
344      */

345     public void endOutTagWithDefaultAttribute(WebResponse theResponse)
346     {
347         String JavaDoc output = theResponse.getText();
348         assertEquals("Default", output);
349     }
350     
351     /**
352      * Tests the <code>&lt;c:out&gt;</code>-tag with a proper, literal value
353      * for it's <code>value</code> attribute, as well as a proper, literal
354      * value for it's <code>default</code> attribute. In this case, the value
355      * of the <code>default</code> attribute should be ignored, which is
356      * asserted.
357      *
358      * @throws JspException If the tag throws a JSPException
359      * @throws IOException If the tag throws an IOException
360      */

361     public void testOutTagDefaultAttributeIgnored()
362         throws JspException JavaDoc, IOException JavaDoc
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     /**
373      * Verifies that the response has been correctly rendered by the
374      * <code>&lt;c:out&gt;</code>-tag.
375      *
376      * @param theResponse The HTTP response
377      */

378     public void endOutTagWithDefaultAttributeIgnored(WebResponse theResponse)
379     {
380         String JavaDoc output = theResponse.getText();
381         assertEquals("Value", output);
382     }
383     
384     /**
385      * Tests the &lt;c:out&gt;-Tag with a value that evaluates to
386      * <code>null</code>, and the default value specified in the tag's body.
387      *
388      * @throws JspException If the tag throws a JSPException
389      * @throws IOException If the tag throws an IOException
390      */

391     public void testOutTagDefaultBody()
392         throws JspException JavaDoc, IOException JavaDoc
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     /**
403      * Verifies that the response has been correctly rendered by the
404      * <code>&lt;c:out&gt;</code>-tag.
405      *
406      * @param theResponse The HTTP response
407      */

408     public void endOutTagDefaultBody(WebResponse theResponse)
409     {
410         String JavaDoc output = theResponse.getText();
411         assertEquals("Default", output);
412     }
413     
414     /**
415      * Tests the <code>&lt;c:out&gt;</code>-tag with a proper, literal value
416      * for it's <code>value</code> attribute, as well the default value
417      * specified in the tag's body. In this case, the tag's body content
418      * should be ignored, which is asserted.
419      *
420      * @throws JspException If the tag throws a JSPException
421      * @throws IOException If the tag throws an IOException
422      */

423     public void testOutTagDefaultBodyIgnored()
424         throws JspException JavaDoc, IOException JavaDoc
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     /**
435      * Verifies that the response has been correctly rendered by the
436      * <code>&lt;c:out&gt;</code>-tag.
437      *
438      * @param theResponse The HTTP response
439      */

440     public void endOutTagDefaultBodyIgnored(WebResponse theResponse)
441     {
442         String JavaDoc output = theResponse.getText();
443         assertEquals("Value", output);
444     }
445     
446     /**
447      * Tests the &lt;c:set&gt;-tag with a proper, literal values for it's
448      * <code>var</code> and <code>value</code> attributes. Verification is done
449      * by checking the scoped variable stored by the tag.
450      *
451      * @throws JspException If the tag throws a JSPException
452      * @throws IOException If the tag throws an IOException
453      */

454     public void testSetTag()
455         throws JspException JavaDoc, IOException JavaDoc
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     /**
466      * Tests the tag &lt;c:forEach&gt; by providing a comma-delimited list of
467      * string to it's <code>items</code> attributes, and checking the exposed
468      * scoped variable on every iteration step.
469      *
470      * @throws JspException If the tag throws a JSPException
471      * @throws IOException If the tag throws an IOException
472      */

473     public void testForEachTag()
474         throws JspException JavaDoc, IOException JavaDoc
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 JavaDoc[] {"One", "Two", "Three"});
483         lifecycle.invoke();
484     }
485     
486     /**
487      * Tests the tag &lt;c:forEach&gt; by providing a comma-delimited list of
488      * string to it's <code>items</code> attributes, and checking the exposed
489      * scoped variable on every iteration step.
490      *
491      * @throws JspException If the tag throws a JSPException
492      * @throws IOException If the tag throws an IOException
493      */

494     public void testForEachTagStatus()
495         throws JspException JavaDoc, IOException JavaDoc
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 JavaDoc 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     /**
537      * Tests the conditional tag &lt;c:if&gt; by providing a proper, literal
538      * value to it's <code>test</code> attribute that evaluates to
539      * the boolean value <code>true</code>. The test verifies the correct
540      * behaviour by asserting that the tag's body is not skipped.
541      *
542      * @throws JspException If the tag throws a JSPException
543      * @throws IOException If the tag throws an IOException
544      */

545     public void testIfTagTrue()
546         throws JspException JavaDoc, IOException JavaDoc
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     /**
557      * Verifies that the response has been correctly rendered by the
558      * <code>&lt;c:if&gt;</code>-tag.
559      *
560      * @param theResponse The HTTP response
561      */

562     public void endIfTagTrue(WebResponse theResponse)
563     {
564         String JavaDoc output = theResponse.getText();
565         assertEquals("Value", output);
566     }
567     
568     /**
569      * Tests the conditional tag &lt;c:if&gt; by providing a proper, literal
570      * value to it's <code>test</code> attribute that evaluates to
571      * the boolean value <code>false</code>. The test verifies the correct
572      * behaviour by asserting that the tag's body is not evaluated.
573      *
574      * @throws JspException If the tag throws a JSPException
575      * @throws IOException If the tag throws an IOException
576      */

577     public void testIfTagFalse()
578         throws JspException JavaDoc, IOException JavaDoc
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     /**
589      * Verifies that the response has been correctly rendered by the
590      * <code>&lt;c:if&gt;</code>-tag.
591      *
592      * @param theResponse The HTTP response
593      */

594     public void endIfTagFalse(WebResponse theResponse)
595     {
596         String JavaDoc output = theResponse.getText();
597         assertEquals("", output);
598     }
599     
600     /**
601      * Tests the &lt;c:when&gt;-tag correctly nested inside a &lt;c:choose&gt;
602      * tag, and providing a proper, literal value to it's <code>test</code>
603      * attribute that evaluates to the boolean value <code>true</code>. The test
604      * verifies the correct behaviour by asserting that the tag's body is not
605      * skipped.
606      *
607      * @throws JspException If the tag throws a JSPException
608      * @throws IOException If the tag throws an IOException
609      */

610     public void testWhenTag()
611         throws JspException JavaDoc, IOException JavaDoc
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     /**
627      * Tests the &lt;c:when&gt;-tag correctly nested inside a &lt;c:choose&gt;
628      * tag, and providing a proper, literal value to it's <code>test</code>
629      * attribute that evaluates to the boolean value <code>true</code>. However,
630      * an earlier instance of the <code>&lt;c:when&gt;</code> tag nested in the
631      * parent has already succeeded, so this test asserts that the body of the
632      * later <code>&lt;c:when&gt;</code> does not get evaluated.
633      *
634      * @throws JspException If the tag throws a JSPException
635      * @throws IOException If the tag throws an IOException
636      */

637     public void testWhenTagNoPermission()
638         throws JspException JavaDoc, IOException JavaDoc
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     /**
659      * Tests te <code>&lt;c:when&gt;</code> tag not nested inside a
660      * <code>&lt;c:choose&gt;</code> tag. The test expects a
661      * <code>JspException</code> to be thrown.
662      *
663      * @throws JspException If the tag throws a JSPException
664      * @throws IOException If the tag throws an IOException
665      */

666     public void testWhenTagWithoutChooseTag()
667         throws JspException JavaDoc, IOException JavaDoc
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 JavaDoc expected)
678         {
679             // expected
680
}
681     }
682     
683 }
684
Popular Tags