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 ; 9 import javax.servlet.jsp.tagext.BodyTag ; 10 import javax.servlet.jsp.tagext.IterationTag ; 11 import javax.servlet.jsp.tagext.Tag ; 12 13 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 testSubject; 26 27 private final String 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 48 public TagTestHelper(Tag 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 61 public MockJspWriter getOutWriter() { 62 return outWriter; 63 } 64 65 public MockPageContext getPageContext() { 66 return pageContext; 67 } 68 69 73 public void assertDoStartTag(final int expectedValue) throws JspException { 74 testSubject.setPageContext(pageContext); 75 76 checkReturnValue("doStartTag", expectedValue, testSubject.doStartTag()); 77 } 78 79 private final void checkReturnValue(final String 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 88 public void testDoInitBody() throws JspException { 89 Assert.assertTrue("doInitBody should not be called as test subject not an instance of BodyTag", 90 testSubject instanceof BodyTag ); 91 92 ((BodyTag ) testSubject).setBodyContent(bodyContent); 93 ((BodyTag ) testSubject).doInitBody(); 94 } 95 96 100 public void assertDoAfterBody(int expectedValue) throws JspException { 101 Assert.assertTrue("doAfterTag should not be called as test subject not an instance of IterationTag", 102 testSubject instanceof IterationTag ); 103 104 checkReturnValue("doAfterTag", expectedValue, ((IterationTag ) testSubject).doAfterBody()); 105 } 106 107 111 public void assertDoEndTag(int expectedValue) throws JspException { 112 Assert.assertEquals("doEndTag returned unexpected value" + getReturnValueName(expectedValue), 113 expectedValue, testSubject.doEndTag()); 114 } 115 } 116 | Popular Tags |