Cactus (03)-Tomcat Quick Start

Source: Internet
Author: User
Tags xsl xslt
Tomcat Quickstart forewords this Guide applies to cactus1.4 and later versions, tomcat4.0 and later versions.

This document demonstrates how to install and run the cactus test on Tomcat within 10 minutes (of course, excluding the download time pai_^.

There are two ways to perform the cactus test on your application:

  • Put all the cactus jar packages under your WEB-INF/lib directory. For details, see classpath tutorial,
  • Put the cactus jar package in the classpath of your container, so that cactus will use the context class loader of the container to load them. This policy will be described in this Guide. This is also a method that allows many web applications to share cactus with each other with only comparative interference.

In addition, there are many ways to trigger the execution of the cactus test (see the testrunner howto guide ). In this guide, we will describe the simplest way to install cactus on a browser.

Steps 1st to 3rd are a step to ensure that cactus can run on Tomcat, which only needs to be installed once. Step 2: install Tomcat

Download tomcat4.0 or later and decompress it to a directory. We call this directory[tomcat-root].

Step 2: copy the cactus jar package

Download the cactus jar package from cactus download page.lib/Directory.

Copy the following jar package[tomcat-root]/common/libBelow:

  • cactus.jar
  • commons-httpclient.jar
  • commons-logging.jar
  • junit.jar
  • aspectjrt.jar
This is the minimum set of jar required to run cactus. If you want to use cactus for httpunit integration later, you also need to copy httpunit.jarPackage. Step 2: Modify the Tomcat web. xml file

Edit[tomcat-root]/conf/web.xmlFile, put the following content at the beginning of the file<webapp>Label.

<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>

Then, in the last<servlet>Definition (in addition to the two <servlet> above, Tomcat also provides some .) Add the following content:

<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>
Warning: Be careful when you modify the global Tomcat web.xmlFile. If later on you wish to use the cactus ant integration and more specifically if you use <cactifywar>Ant task, you may run into problems. <cactifywar>Task automatically adds the needed cactus redirectors (thus they'll be added twice leading to an error. Step 4th: create a simple application for testing

Now, we create a very simple application on the server so that we can perform unit tests on it.

First, create the following directory structure:

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

Next, create the followingSampleServlet.javaJava source file, compile and copy the compiled. Class file[tomcat-root]/webapps/test/WEB-INF/classesDirectory.

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 shold like this

We're re now read to create our first cactus test case. Create the followingTestSampleServlet.javaJava 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"));    }}
Step 2: run the test

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 ithttp://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet

You shoshould see:

Step 2: more interesting things!

Good, but how can I see HTML instead of XML? Don't worry. Here is a method. Grab the following XSLT stylesheet (based on the stylesheet used by<Junitreport>Ant task) and drop it in[tomcat-root]/webapps/test. Then, open the browser and enter the following in the address bar:http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet&xsl=cactus-report.xsl . Now you should see the following interface:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.