KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > cactus > sample > 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.maven.cactus.sample;
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:34:44 vmassol Exp $
32  */

33 public class TestSampleBodyTag extends JspTestCase
34 {
35     /**
36      * Tag to test.
37      */

38     private SampleBodyTag tag;
39
40     /**
41      * Tag body content for the test.
42      */

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

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

61     /**
62      * Sets the replacement target and replacement String on the tag, then calls
63      * doAfterBody(). Most of the assertion work is done in endReplacement().
64      *
65      * @exception Exception on error
66      */

67     public void testReplacement() throws Exception JavaDoc
68     {
69         //set the target and the String to replace it with
70
this.tag.setTarget("@target@");
71         this.tag.setReplacement("replacement");
72
73         //add the tag's body by writing to the BodyContent object created in
74
//setUp()
75
this.tagContent.println("@target@ is now @target@");
76         this.tagContent.println("@target@_@target@");
77
78         //none of the other life cycle methods need to be implemented, so they
79
//do not need to be called.
80
int result = this.tag.doAfterBody();
81
82         assertEquals(BodyTag.SKIP_BODY, result);
83     }
84
85     /**
86      * @see TestCase#tearDown()
87      */

88     public void tearDown()
89     {
90         //necessary for tag to output anything on most servlet engines.
91
this.pageContext.popBody();
92     }
93
94     /**
95      * Verifies that the target String has indeed been replaced in the tag's
96      * body.
97      *
98      * @param theResponse the response from the server side.
99      */

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