KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > helpers > TagTestHelper


1 package com.mockobjects.helpers;
2
3 import com.mockobjects.servlet.MockBodyContent;
4 import com.mockobjects.servlet.MockJspWriter;
5 import com.mockobjects.servlet.MockPageContext;
6 import junit.framework.Assert;
7
8 import javax.servlet.jsp.JspException JavaDoc;
9 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
10 import javax.servlet.jsp.tagext.IterationTag JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13 /**
14  * Sets up mock tag objects in a common configuration.
15  * MockHttpServletRequest, MockServletContext and MockHttpSession are attached to MockPageContext
16  * @see com.mockobjects.servlet.MockPageContext#setRequest();
17  * @see com.mockobjects.servlet.MockPageContext#setServletContext();
18  * @see com.mockobjects.servlet.MockPageContext#setSession();
19  */

20 public class TagTestHelper extends AbstractServletTestHelper {
21     private final MockPageContext pageContext = new MockPageContext();
22     private final MockBodyContent bodyContent = new MockBodyContent();
23     private final MockJspWriter outWriter = new MockJspWriter();
24     private final MockJspWriter enclosingWriter = new MockJspWriter();
25     private final Tag JavaDoc testSubject;
26
27     private final String JavaDoc getReturnValueName(int returnValue) {
28         switch (returnValue) {
29             case BodyTag.EVAL_BODY_INCLUDE:
30                 return "EVAL_BODY_INCLUDE";
31             case BodyTag.EVAL_PAGE:
32                 return "EVAL_PAGE";
33             case BodyTag.SKIP_BODY:
34                 return "SKIP_BODY";
35             case BodyTag.SKIP_PAGE:
36                 return "SKIP_PAGE";
37             case BodyTag.EVAL_BODY_BUFFERED:
38                 return "EVAL_BODY_BUFFERED|EVAL_BODY_AGAIN";
39             default:
40                 return "Unknown return value (" + returnValue + ")";
41         }
42     }
43
44
45     /**
46      * @param testSubject The Tag to be tested
47      */

48     public TagTestHelper(Tag JavaDoc testSubject) {
49         this.testSubject = testSubject;
50
51         pageContext.setRequest(getRequest());
52         pageContext.setServletContext(getServletContext());
53         pageContext.setSession(getHttpSession());
54         pageContext.setJspWriter(outWriter);
55         bodyContent.setupGetEnclosingWriter(enclosingWriter);
56     }
57
58     /**
59      * @return The writer use when making calls to PageContext.getOut
60      */

61     public MockJspWriter getOutWriter() {
62         return outWriter;
63     }
64
65     public MockPageContext getPageContext() {
66         return pageContext;
67     }
68
69     /**
70      * Assert that the return value of doStartTag is equal to an expectedValue
71      * @param expectedValue value to check against doStartTag
72      */

73     public void assertDoStartTag(final int expectedValue) throws JspException JavaDoc {
74         testSubject.setPageContext(pageContext);
75
76         checkReturnValue("doStartTag", expectedValue, testSubject.doStartTag());
77     }
78
79     private final void checkReturnValue(final String JavaDoc methodName, final int expectedValue, final int returnValue) {
80         Assert.assertEquals(methodName + " expected value " + getReturnValueName(expectedValue) +
81             " but was " + getReturnValueName(returnValue),
82             expectedValue, returnValue);
83     }
84
85     /**
86      * Invoke doInitBody on the test subject
87      */

88     public void testDoInitBody() throws JspException JavaDoc {
89         Assert.assertTrue("doInitBody should not be called as test subject not an instance of BodyTag",
90             testSubject instanceof BodyTag JavaDoc);
91
92         ((BodyTag JavaDoc) testSubject).setBodyContent(bodyContent);
93         ((BodyTag JavaDoc) testSubject).doInitBody();
94     }
95
96     /**
97      * Assert that the return value of doAfterBody is equal to an expectedValue
98      * @param expectedValue value to check against doAfterBody
99      */

100     public void assertDoAfterBody(int expectedValue) throws JspException JavaDoc {
101         Assert.assertTrue("doAfterTag should not be called as test subject not an instance of IterationTag",
102             testSubject instanceof IterationTag JavaDoc);
103
104         checkReturnValue("doAfterTag", expectedValue, ((IterationTag JavaDoc) testSubject).doAfterBody());
105     }
106
107     /**
108      * Assert that the return value of doEndTag is equal to an expectedValue
109      * @param expectedValue value to check against doEndTag
110      */

111     public void assertDoEndTag(int expectedValue) throws JspException JavaDoc {
112         Assert.assertEquals("doEndTag returned unexpected value" + getReturnValueName(expectedValue),
113             expectedValue, testSubject.doEndTag());
114     }
115 }
116
Popular Tags