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でのテスト方法(超簡単版)

  1. test-exampleをローカルにインストールしたTomcatにデプロイする。
  2. Tomcatを起動する。
  3. Eclipseで「HelloWorldTagTest.java」を開き、「右クリック」→「実行」→「JUnit」でテストを実行する。

ソースのサンプル

  • jspRedirector.jsp は jakarta-cactus-13-1.7.2/web/jspRedirector.jsp をそのまま使用した。

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>
scratch/java/cactus-test.txt · 最終更新: 2007/08/10 01:41 (外部編集)
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0