001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet;
021:
022: import javax.servlet.jsp.tagext.Tag;
023:
024: import org.apache.cactus.JspTestCase;
025: import org.apache.cactus.WebResponse;
026:
027: /**
028: * Tests of the <code>SampleTag</code> class.
029: *
030: * @version $Id: TestSampleTag.java 238816 2004-02-29 16:36:46Z vmassol $
031: */
032: public class TestSampleTag extends JspTestCase {
033: /**
034: * Our tag instance to unit test
035: */
036: private SampleTag tag;
037:
038: /**
039: * @see TestCase#setUp()
040: */
041: public void setUp() {
042: this .tag = new SampleTag();
043: this .tag.setPageContext(this .pageContext);
044: }
045:
046: //-------------------------------------------------------------------------
047:
048: /**
049: * Tests whether doStartTag() will skip the body if the corresponding tag
050: * attribute is set. Also tests whether an attribute put into page scope
051: * before the tag executes will be output to the response.
052: *
053: * @exception Exception if the test fails for an unexpected reason
054: */
055: public void testDoStartTag() throws Exception {
056: //put something in page scope to see if it shows up in the response...
057: this .pageContext.setAttribute("test-key", "test-value");
058:
059: this .tag.setShowBody("false");
060:
061: int result = this .tag.doStartTag();
062:
063: //body should not show up
064: assertEquals(Tag.SKIP_BODY, result);
065: }
066:
067: /**
068: * Verifies that the output includes the output from doStartTag (a message
069: * from the tag and the attribute set into page scope).
070: *
071: * @param theResponse the response from the server side.
072: */
073: public void endDoStartTag(WebResponse theResponse) {
074: // check that two of the lines output by the tag showed up in
075: // the response
076: assertContains(theResponse,
077: "The following attributes exist in page scope: <BR>");
078:
079: assertContains(theResponse, "test-key = test-value <BR>");
080: }
081:
082: //-------------------------------------------------------------------------
083:
084: /**
085: * Test whether the tag's body will be shown if the corresponding attribute
086: * is set.
087: *
088: * @exception Exception if the test fails for an unexpected reason
089: */
090: public void testDoStartTagInclude() throws Exception {
091: this .tag.setShowBody("true");
092:
093: int result = this .tag.doStartTag();
094:
095: //body should show up
096: assertEquals(Tag.EVAL_BODY_INCLUDE, result);
097: }
098:
099: /**
100: * The tag prints a message before the body is included, here we check that
101: * the message shows up.
102: *
103: * @param theResponse the response from the server side.
104: */
105: public void endDoStartTagInclude(WebResponse theResponse) {
106: // check that the pre-body message printed by the tag shows up
107: assertContains(theResponse, "Body Content Follows: <BR>");
108: }
109:
110: //-------------------------------------------------------------------------
111:
112: /**
113: * Checks if the tag will continue the page correctly if its stopPage
114: * property is set to false.
115: *
116: * @exception Exception if the test fails for an unexpected reason
117: */
118: public void testDoEndTagContinue() throws Exception {
119: this .tag.setParent(new SampleTag());
120: this .tag.setStopPage("false");
121:
122: int result = this .tag.doEndTag();
123:
124: assertEquals(Tag.EVAL_PAGE, result);
125: }
126:
127: /**
128: * Checks whether the tag has printed a message indicating that it has a
129: * parent tag.
130: *
131: * @param theResponse the response from the server side.
132: */
133: public void endDoEndTagContinue(WebResponse theResponse) {
134: assertContains(theResponse, "This tag has a parent. <BR>");
135: }
136:
137: //-------------------------------------------------------------------------
138:
139: /**
140: * Checks if the tag will signal that page processing should stop if
141: * stopPage is set to "true"
142: *
143: * @exception Exception if the test fails for an unexpected reason
144: */
145: public void testDoEndTagStop() throws Exception {
146: //no parent set
147: this .tag.setStopPage("true");
148:
149: int result = this .tag.doEndTag();
150:
151: assertEquals(Tag.SKIP_PAGE, result);
152: }
153:
154: /**
155: * Checks whether the tag has printed a message indicating that it has a
156: * parent tag. (In this case it should not.)
157: *
158: * @param theResponse the response from the server side.
159: */
160: public void endDoEndTagStop(WebResponse theResponse) {
161: String target = theResponse.getText();
162: boolean containsMessage = target
163: .indexOf("This tag has a parent. <BR>") > 0;
164: assertTrue(!containsMessage);
165: }
166:
167: //--------------------------------------------------------------------------
168:
169: /**
170: * Convenience function that asserts that a substring can be found in a
171: * the returned HTTP response body.
172: *
173: * @param theResponse the response from the server side.
174: * @param theSubstring the substring to look for
175: */
176: public void assertContains(WebResponse theResponse,
177: String theSubstring) {
178: String target = theResponse.getText();
179:
180: if (target.indexOf(theSubstring) < 0) {
181: fail("Response did not contain the substring: ["
182: + theSubstring + "]");
183: }
184: }
185: }
|