KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tag JavaDoc;
23
24 import org.apache.cactus.JspTestCase;
25 import org.apache.cactus.WebResponse;
26
27 /**
28  * Tests of the <code>SampleTag</code> class.
29  *
30  * @version $Id: TestSampleTag.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
31  */

32 public class TestSampleTag extends JspTestCase
33 {
34     /**
35      * Our tag instance to unit test
36      */

37     private SampleTag tag;
38
39     /**
40      * @see TestCase#setUp()
41      */

42     public void setUp()
43     {
44         this.tag = new SampleTag();
45         this.tag.setPageContext(this.pageContext);
46     }
47
48     //-------------------------------------------------------------------------
49

50     /**
51      * Tests whether doStartTag() will skip the body if the corresponding tag
52      * attribute is set. Also tests whether an attribute put into page scope
53      * before the tag executes will be output to the response.
54      *
55      * @exception Exception if the test fails for an unexpected reason
56      */

57     public void testDoStartTag() throws Exception JavaDoc
58     {
59         //put something in page scope to see if it shows up in the response...
60
this.pageContext.setAttribute("test-key", "test-value");
61
62         this.tag.setShowBody("false");
63
64         int result = this.tag.doStartTag();
65
66         //body should not show up
67
assertEquals(Tag.SKIP_BODY, result);
68     }
69
70     /**
71      * Verifies that the output includes the output from doStartTag (a message
72      * from the tag and the attribute set into page scope).
73      *
74      * @param theResponse the response from the server side.
75      */

76     public void endDoStartTag(WebResponse theResponse)
77     {
78         // check that two of the lines output by the tag showed up in
79
// the response
80
assertContains(theResponse,
81             "The following attributes exist in page scope: <BR>");
82
83         assertContains(theResponse, "test-key = test-value <BR>");
84     }
85
86     //-------------------------------------------------------------------------
87

88     /**
89      * Test whether the tag's body will be shown if the corresponding attribute
90      * is set.
91      *
92      * @exception Exception if the test fails for an unexpected reason
93      */

94     public void testDoStartTagInclude() throws Exception JavaDoc
95     {
96         this.tag.setShowBody("true");
97
98         int result = this.tag.doStartTag();
99
100         //body should show up
101
assertEquals(Tag.EVAL_BODY_INCLUDE, result);
102     }
103
104     /**
105      * The tag prints a message before the body is included, here we check that
106      * the message shows up.
107      *
108      * @param theResponse the response from the server side.
109      */

110     public void endDoStartTagInclude(WebResponse theResponse)
111     {
112         // check that the pre-body message printed by the tag shows up
113
assertContains(theResponse, "Body Content Follows: <BR>");
114     }
115
116     //-------------------------------------------------------------------------
117

118     /**
119      * Checks if the tag will continue the page correctly if its stopPage
120      * property is set to false.
121      *
122      * @exception Exception if the test fails for an unexpected reason
123      */

124     public void testDoEndTagContinue() throws Exception JavaDoc
125     {
126         this.tag.setParent(new SampleTag());
127         this.tag.setStopPage("false");
128
129         int result = this.tag.doEndTag();
130
131         assertEquals(Tag.EVAL_PAGE, result);
132     }
133
134     /**
135      * Checks whether the tag has printed a message indicating that it has a
136      * parent tag.
137      *
138      * @param theResponse the response from the server side.
139      */

140     public void endDoEndTagContinue(WebResponse theResponse)
141     {
142         assertContains(theResponse, "This tag has a parent. <BR>");
143     }
144
145     //-------------------------------------------------------------------------
146

147     /**
148      * Checks if the tag will signal that page processing should stop if
149      * stopPage is set to "true"
150      *
151      * @exception Exception if the test fails for an unexpected reason
152      */

153     public void testDoEndTagStop() throws Exception JavaDoc
154     {
155         //no parent set
156
this.tag.setStopPage("true");
157
158         int result = this.tag.doEndTag();
159
160         assertEquals(Tag.SKIP_PAGE, result);
161     }
162
163     /**
164      * Checks whether the tag has printed a message indicating that it has a
165      * parent tag. (In this case it should not.)
166      *
167      * @param theResponse the response from the server side.
168      */

169     public void endDoEndTagStop(WebResponse theResponse)
170     {
171         String JavaDoc target = theResponse.getText();
172         boolean containsMessage =
173             target.indexOf("This tag has a parent. <BR>") > 0;
174         assertTrue(!containsMessage);
175     }
176
177     //--------------------------------------------------------------------------
178

179     /**
180      * Convenience function that asserts that a substring can be found in a
181      * the returned HTTP response body.
182      *
183      * @param theResponse the response from the server side.
184      * @param theSubstring the substring to look for
185      */

186     public void assertContains(WebResponse theResponse, String JavaDoc theSubstring)
187     {
188         String JavaDoc target = theResponse.getText();
189
190         if (target.indexOf(theSubstring) < 0)
191         {
192             fail("Response did not contain the substring: [" + theSubstring
193                 + "]");
194         }
195     }
196 }
197
Popular Tags