KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > TestSampleBodyTag


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;
21
22 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
24
25 import org.apache.cactus.JspTestCase;
26 import org.apache.cactus.WebResponse;
27
28 /**
29  * Tests of the <code>SampleBodyTag</code> class.
30  *
31  * @version $Id: TestSampleBodyTag.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
32  */

33 public class TestSampleBodyTag extends JspTestCase
34 {
35     /**
36      * Our tag instance being unit tested
37      */

38     private SampleBodyTag tag;
39
40     /**
41      * The tag body content to which we can write to in our unit tests
42      * to simulate a content.
43      */

44     private BodyContent JavaDoc tagContent;
45
46     /**
47      * In addition to creating the tag instance and adding the pageContext to
48      * it, this method creates a BodyContent object and passes it to the tag.
49      */

50     public void setUp()
51     {
52         this.tag = new SampleBodyTag();
53         this.tag.setPageContext(this.pageContext);
54
55         //create the BodyContent object and call the setter on the tag instance
56
this.tagContent = this.pageContext.pushBody();
57         this.tag.setBodyContent(this.tagContent);
58     }
59
60     /**
61      * @see TestCase#tearDown()
62      */

63     public void tearDown()
64     {
65         //necessary for tag to output anything on most servlet engines.
66
this.pageContext.popBody();
67     }
68
69     //-------------------------------------------------------------------------
70

71     /**
72      * Sets the replacement target and replacement String on the tag, then calls
73      * doAfterBody(). Most of the assertion work is done in endReplacement().
74      *
75      * @exception Exception if the test fails for an unexpected reason
76      */

77     public void testReplacement() throws Exception JavaDoc
78     {
79         //set the target and the String to replace it with
80
this.tag.setTarget("@target@");
81         this.tag.setReplacement("replacement");
82
83         //add the tag's body by writing to the BodyContent object created in
84
//setUp()
85
this.tagContent.println("@target@ is now @target@");
86         this.tagContent.println("@target@_@target@");
87
88         //none of the other life cycle methods need to be implemented, so they
89
//do not need to be called.
90
int result = this.tag.doAfterBody();
91
92         assertEquals(BodyTag.SKIP_BODY, result);
93     }
94
95     /**
96      * Verifies that the target String has indeed been replaced in the tag's
97      * body.
98      *
99      * @param theResponse the response from the server side.
100      */

101     public void endReplacement(WebResponse theResponse)
102     {
103         String JavaDoc content = theResponse.getText();
104
105         assertTrue("Response should have contained the ["
106             + "replacement is now replacement] string",
107             content.indexOf("replacement is now replacement") > -1);
108         assertTrue("Response should have contained the ["
109             + "replacement_replacement] string",
110             content.indexOf("replacement") > -1);
111     }
112 }
113
Popular Tags