====== Cactusのテスト方法(カスタムタグのみ) ====== ===== Webプロジェクトディレクトリ構成 ===== 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 ===== Eclipse+Junitでのテスト方法(超簡単版) ===== - test-exampleをローカルにインストールしたTomcatにデプロイする。 - Tomcatを起動する。 - Eclipseで「HelloWorldTagTest.java」を開き、「右クリック」→「実行」→「JUnit」でテストを実行する。 ===== ソースのサンプル ===== * jspRedirector.jsp は jakarta-cactus-13-1.7.2/web/jspRedirector.jsp をそのまま使用した。 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; } } 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.contextURL = http://localhost:8080/test-example cactus.servletRedirectorName = ServletRedirector cactus.jspRedirectorName = JspRedirector cactus.filterRedirectorName = test/filterRedirector.jsp cactus.enableLogging = true test-esample JspRedirector /jspRedirector.jsp JspRedirector /JspRedirector == {{tag>Java scratch}}