Cactus(03)-Tomcat快速开始

来源:互联网 发布:无线扫描器 软件 编辑:程序博客网 时间:2024/06/10 09:39

Tomcat Quickstart forewords

本指南适用于Cactus1.4以及更高版本、Tomcat4.0以及其更高版本

本文档演示了如何在10分钟内(当然不包括下载时间^_^)在Tomcat上面安装和运行Cactus测试。

有两种方法可以使你在你的应用程序上执行Cactus测试:

  • 把所有的Cactus Jar包放到你的 WEB-INF/lib目录下面,具体描述见 Classpath Tutorial,
  • 把Cactus Jar包放到你的容器的classpath中,这样Cactus就会使用容器的Context类加载器加载它们了。本指南将会描述这个策略,这也是一种仅用比较的干扰就可以使很多Web应用程度共用Cactus的方法。

另外,有很多种途径可以触发Cactus测试的执行(请看TestRunner Howto指南)。在本指南中,我们将要描述一种最简单的方式在浏览器上安装Cactus。

第1步到第3步是一个保证Cactus能够在Tomcat上运行的步骤,仅需安装一次。

第1步:安装Tomcat

下载Tomcat4.0或更高版本,将其解压到一个目录,我们称这个目录为[tomcat-root]

第2步:拷贝Cactus Jar包

从 Cactus download page下载Cactus的Jar包,这些Jar放在压缩文件的 lib/ 目录下。

将下面的Jar包拷贝到 [tomcat-root]/common/lib 下面:

  • cactus.jar
  • commons-httpclient.jar
  • commons-logging.jar
  • junit.jar
  • aspectjrt.jar
这是运行Cactus所需要Jar的最小集合,如果稍后你想要使用Cactus进行HttpUnit integration,你还需要拷贝httpunit.jar包。

第3步:修改 Tomcat web.xml文件

编辑[tomcat-root]/conf/web.xml 文件,将下面的内容放到文件开头的 <webapp>标签下面。

<servlet>  <servlet-name>ServletRedirector</servlet-name>  <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>  <init-param>    <param-name>param1</param-name>    <param-value>value1 used for testing</param-value>  </init-param></servlet><servlet>  <servlet-name>ServletTestRunner</servlet-name>  <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class></servlet>

然后,在最后一个<servlet>定义(除了我们上面的两个<servlet>,Tomcat还提供了一些。)后面加上下面的内容:

<servlet-mapping>    <servlet-name>ServletRedirector</servlet-name>    <url-pattern>/ServletRedirector</url-pattern></servlet-mapping><servlet-mapping>    <servlet-name>ServletTestRunner</servlet-name>    <url-pattern>/ServletTestRunner</url-pattern></servlet-mapping>
警告: Be careful when you modify the global Tomcat web.xml file. If later on you wish to use the Cactus Ant integration and more specifically if you use the <cactifywar> Ant task, you may run into problems. The <cactifywar> task automatically adds the needed Cactus redirectors (thus they'll be added twice leading to an error.

第4步:建立一个简单的测试用的应用程序

现在,我们在服务器上建立一个非常非常简单的应用程序,以便于我们来对它进行单元测试。

首先,建立下面的目录结构:

[tomcat-root]/webapps  |_ test    |_ WEB-INF      |_ classes

接着,建立下面的SampleServlet.java Java源文件,编译并将编译后的.class文件拷贝到 [tomcat-root]/webapps/test/WEB-INF/classes目录下面。

import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;public class SampleServlet extends HttpServlet{    public void saveToSession(HttpServletRequest request)    {    String testparam = request.getParameter("testparam");    request.getSession().setAttribute("testAttribute", testparam);    }}
You'll notice that this isn't even a finished servlet ! However, this shows that you can start testing your code with Cactus even before you have finished writing it completely. Extreme Programmers should like this :-)

We're now read to create our first Cactus test case. Create the following TestSampleServlet.java java source file, compile it and copy the resulting .class file in [tomcat-root]/webapps/test/WEB-INF/classes.

import junit.framework.Test;import junit.framework.TestSuite;import org.apache.cactus.ServletTestCase;import org.apache.cactus.WebRequest;public class TestSampleServlet extends ServletTestCase{    public TestSampleServlet(String theName)    {        super(theName);    }    public static Test suite()    {        return new TestSuite(TestSampleServlet.class);    }    public void beginSaveToSessionOK(WebRequest webRequest)    {        webRequest.addParameter("testparam", "it works!");    }    public void testSaveToSessionOK()    {        SampleServlet servlet = new SampleServlet();        servlet.saveToSession(request);        assertEquals("it works!", session.getAttribute("testAttribute"));    }}

第5步:运行测试

Time to enjoy our hard work ! Start Tomcat by running [tomcat-root]/bin/startup.bat (for windows) or [tomcat-root]/bin/startup.sh (for unix).

Open a browser and point it at http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet

You should see:

XML output of ServletTestRunner

第6步:更多有趣的东西!

很好,但是我怎样才能想看HTML而不是XML?别急,这里有一个方法。Grab the following XSLT stylesheet (based on the stylesheet used by the <junitreport> Ant task) and drop it in [tomcat-root]/webapps/test. 然后,打开浏览器,在地址栏输入: http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet&xsl=cactus-report.xsl  。现在,你应该看到下面的界面了:

HTML output of ServletTestRunner
This will work with any browser that supports client-slide XSLT transformations (both Internet Explorer and Mozilla do, for example).