KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > JspTestCase


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12 import javax.servlet.jsp.tagext.Tag JavaDoc;
13 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
14
15 import com.inversoft.junit.internal.http.MockBodyContent;
16 import com.inversoft.junit.internal.http.MockPageContext;
17
18
19 /**
20  * <p>
21  * This class is the parent of all Jsp test case classes.
22  * This provides the ability to create mock PageContext
23  * object so that tag libraries can be called locally. It
24  * also provides the functionality of calling the proxy JSP
25  * on the server in order to test JSP pages, tag libs, etc.
26  * on the server.
27  * </p>
28  *
29  * @author Brian Pontarelli
30  * @since 2.0
31  * @version 2.0
32  */

33 public class JspTestCase extends WebTestCase {
34
35     public PageContext JavaDoc pageContext;
36
37
38     /**
39      * Constructor for JspTestCase.
40      * @param name
41      */

42     public JspTestCase(String JavaDoc name) {
43         super(name);
44     }
45
46
47     /**
48      * Returns the instance a new instance of the MockBodyContent
49      */

50     public MockBodyContent createBodyContent() {
51         return createBodyContent(null);
52     }
53
54     /**
55      * Returns the instance a new instance of the MockBodyContent
56      */

57     public MockBodyContent createBodyContent(String JavaDoc body) {
58         if (!isLocal()) {
59             throw new IllegalStateException JavaDoc("You can only call getPageContext" +
60                 " when running tests locally");
61         }
62         return new MockBodyContent(body, pageContext.getOut());
63     }
64
65     /**
66      * Returns the instance of the PageContext
67      */

68     public MockPageContext getPageContext() {
69         if (!isLocal()) {
70             throw new IllegalStateException JavaDoc("You can only call getPageContext" +
71                 " when running tests locally");
72         }
73         return (MockPageContext) pageContext;
74     }
75
76     /**
77      * Runs the tag as the container would. This uses the rules from the J2EE JavaDoc
78      * version 1.3.1
79      *
80      * @param tag The Tag to run
81      * @return The result from a call to tag.doEndTag()
82      */

83     public static int runTag(Tag JavaDoc tag) throws JspException JavaDoc {
84
85         // Handle boy tags
86
if (tag instanceof BodyTag JavaDoc) {
87             return runTag((BodyTag JavaDoc) tag, null);
88         }
89
90         tag.doStartTag();
91         return tag.doEndTag();
92     }
93
94     /**
95      * Runs the tag as the container would. This uses the rules from the J2EE JavaDoc
96      * version 1.3.1. This calls the callback if the tag returns EVAL_BODY_INCLUDE
97      *
98      * @param tag The Tag to run
99      * @param callback (Optional) A Callback handler for testing body handling
100      * @return The result from a call to tag.doEndTag()
101      */

102     public static int runTag(Tag JavaDoc tag, JspTestCallback callback) throws JspException JavaDoc {
103
104         // Handle boy tags
105
if (tag instanceof BodyTag JavaDoc) {
106             return runTag((BodyTag JavaDoc) tag, callback);
107         }
108
109         int ret = tag.doStartTag();
110         if (ret == Tag.EVAL_BODY_INCLUDE) {
111             callback.callback();
112         }
113
114         return tag.doEndTag();
115     }
116
117     /**
118      * Runs the tag as the container would. This uses the rules from the J2EE JavaDoc
119      * version 1.3.1
120      *
121      * @param tag The Tag to run
122      * @param callback (Optional) A Callback handler for testing iterations
123      * @return The result from a call to tag.doEndTag()
124      */

125     public static int runTag(BodyTag JavaDoc tag, JspTestCallback callback)
126     throws JspException JavaDoc {
127         int ret = tag.doStartTag();
128         if (ret == BodyTag.EVAL_BODY_INCLUDE || ret == BodyTag.EVAL_BODY_BUFFERED) {
129             // Call body ???
130
do {
131                 tag.doInitBody();
132
133                 if (callback != null) {
134                     callback.callback();
135                 }
136
137                 ret = tag.doAfterBody();
138             } while (ret != Tag.SKIP_BODY);
139         }
140
141         ret = tag.doEndTag();
142
143         return ret;
144     }
145
146     /**
147      * Runs the tag as the container would. This uses the rules from the J2EE JavaDoc
148      * version 1.3.1
149      *
150      * @param tag The Tag to run
151      * @param loops The number of times the body should be evaluated. If the
152      * times that this method calls doAfterBody is not equal to this,
153      * this method asserts
154      * @param callback (Optional) A Callback handler for testing iterations
155      * @return The result from a call to tag.doEndTag()
156      */

157     public static int runTag(BodyTag JavaDoc tag, int loops, JspTestCallback callback)
158     throws JspException JavaDoc {
159         int ret = tag.doStartTag();
160         int count = 0;
161         if (ret == BodyTag.EVAL_BODY_INCLUDE) {
162             // Call body ???
163
do {
164                 tag.doInitBody();
165
166                 if (callback != null) {
167                     callback.callback();
168                 }
169
170                 ret = tag.doAfterBody();
171                 count++;
172             } while (ret != Tag.SKIP_BODY);
173         }
174
175         if (count != loops) {
176             fail("Expected to loop: " + loops + " but only looped: " + count);
177         }
178
179         return tag.doEndTag();
180     }
181
182     /**
183      * Copies all the local variables from this TestCase to the given TestCase.
184      * This allows the given TestCase to be called as though it was being called
185      * by the JUnit system.
186      *
187      * @param testCase The JspTestCase to setup
188      */

189     public void setupJspTestCase(JspTestCase testCase) {
190         testCase.request = request;
191         testCase.response = response;
192         testCase.context = context;
193         testCase.pageContext = pageContext;
194     }
195 }
Popular Tags