test-example
│ jspRedirector.jsp
│
└─WEB-INF
│ web.xml
│
├─classes
│ │ cactus.properties
│ │
│ ├─taglib
│ │ HelloWorldTag.class
│ │
│ └─test
│ └─taglib
│ HelloWorldTagTest.class
│
└─lib
aspectjrt-1.2.1.jar
cactus-1.7.2.jar
commons-httpclient-2.0.2.jar
commons-logging-1.0.4.jar
junit-3.8.1.jar
HelloWorldTag.java
package taglib; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class HelloWorldTag extends TagSupport { public int doStartTag() throws JspException { try { pageContext.getOut().print("Hello World Start"); } catch (Exception e) { throw new JspException(e.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException { try { pageContext.getOut().print("Hello World End"); } catch (Exception e) { throw new JspException(e.getMessage()); } return EVAL_PAGE; } }
HelloWorldTagTest.java
package test.taglib; import javax.servlet.jsp.tagext.Tag; import org.apache.cactus.*; import taglib.HelloWorldTag; public class HelloWorldTagTest extends JspTestCase { private HelloWorldTag tag; public HelloWorldTagTest(String theName) { super(theName); } public void setUp() { this.tag = new HelloWorldTag(); this.tag.setPageContext(this.pageContext); } public void testDoStartTag() throws Exception { int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); } public void testDoEndTag() throws Exception { int result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); } public void endDoStartTag(WebResponse response) { assertContains(response, "Hello World Start"); } public void endDoEndTag(WebResponse response) { assertContains(response, "Hello World End"); } private void assertContains(WebResponse response, String str) { String text = response.getText(); if (text.indexOf(str) < 0) { fail("指定された文字列がレスポンスに含まれていません。: [" + str + "]"); } } }
cactus.properties
cactus.contextURL = http://localhost:8080/test-example cactus.servletRedirectorName = ServletRedirector cactus.jspRedirectorName = JspRedirector cactus.filterRedirectorName = test/filterRedirector.jsp cactus.enableLogging = true
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>test-esample</display-name> <servlet> <servlet-name>JspRedirector</servlet-name> <jsp-file>/jspRedirector.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>JspRedirector</servlet-name> <url-pattern>/JspRedirector</url-pattern> </servlet-mapping> </web-app>