1 7 package com.inversoft.junit; 8 9 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.PageContext ; 12 import javax.servlet.jsp.tagext.Tag ; 13 import javax.servlet.jsp.tagext.BodyTag ; 14 15 import com.inversoft.junit.internal.http.MockBodyContent; 16 import com.inversoft.junit.internal.http.MockPageContext; 17 18 19 33 public class JspTestCase extends WebTestCase { 34 35 public PageContext pageContext; 36 37 38 42 public JspTestCase(String name) { 43 super(name); 44 } 45 46 47 50 public MockBodyContent createBodyContent() { 51 return createBodyContent(null); 52 } 53 54 57 public MockBodyContent createBodyContent(String body) { 58 if (!isLocal()) { 59 throw new IllegalStateException ("You can only call getPageContext" + 60 " when running tests locally"); 61 } 62 return new MockBodyContent(body, pageContext.getOut()); 63 } 64 65 68 public MockPageContext getPageContext() { 69 if (!isLocal()) { 70 throw new IllegalStateException ("You can only call getPageContext" + 71 " when running tests locally"); 72 } 73 return (MockPageContext) pageContext; 74 } 75 76 83 public static int runTag(Tag tag) throws JspException { 84 85 if (tag instanceof BodyTag ) { 87 return runTag((BodyTag ) tag, null); 88 } 89 90 tag.doStartTag(); 91 return tag.doEndTag(); 92 } 93 94 102 public static int runTag(Tag tag, JspTestCallback callback) throws JspException { 103 104 if (tag instanceof BodyTag ) { 106 return runTag((BodyTag ) 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 125 public static int runTag(BodyTag tag, JspTestCallback callback) 126 throws JspException { 127 int ret = tag.doStartTag(); 128 if (ret == BodyTag.EVAL_BODY_INCLUDE || ret == BodyTag.EVAL_BODY_BUFFERED) { 129 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 157 public static int runTag(BodyTag tag, int loops, JspTestCallback callback) 158 throws JspException { 159 int ret = tag.doStartTag(); 160 int count = 0; 161 if (ret == BodyTag.EVAL_BODY_INCLUDE) { 162 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 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 |