Cactus
Cactus is an open source unit-testing framework for server side Java code. Specifically, Cactus allows you to test servlets, JSPs, and servlet filters. Cactus may also been used to test Enterprise Java Classes.
Cactus extends JUnit to provide three specific junit.framework.TestCase subclasses:
org.apache.cactus. ServletTestCase
org.apache.cactus.JspTestCase
org.apache.cactus. FilterTestCase
Cactus tests execute on both client and server.
It is important to know that you have to deploy your Cactus tests to the server. Specifically, you must create a web-application WAR file containing a valid web.xml file, all Cactus tests, and all support classes needed for your tests to execute. This is necessary because Cactus tests are executed on both the client and server.
Implicit Objects
Here are the implicit objects defined by each test case:
org.apache.cactus. ServletTestCase
HttpServletRequestWrappe r request
HttpServletResponse response
HttpSession session
ServletConfigWrapper config
org.apache.cactus.JspTestCase
PageContextWrapper pageContext
JspWriter out
org.apache.cactus. FilterTestCase
HttpServletRequestWrappe r request
HttpServletResponse response
FilterConfigWrapper config
FilterChain filterChain
Configuring Cactus
To configure cactus we have to add following jars in our project.
(1) aspectjrt-1.5.3
(2) cactus.core.framework.uberjar. javaEE.14-1.8.1
(3) cactus.integration.ant-1.8.1
(4) cactus.integration.shared.api- 1.8.1
(5) commons-httpclient-3.1 - @ Client Side
(6) commons-logging-1.1
(7) httpunit-1.6 - @ Client Side
(8) junit-3.8.2 (or upper)
(9) commons-codec-1.2
Creating the cactus.properties File
It must be placed at client side.
Here's an example of the cactus.properties file:
cactus.contextURL= http://localhost:8080/xptest
cactus. servletRedirectorName= ServletRedirector
cactus. jspRedirectorName= JspRedirector
cactus. filterRedirectorName= FilterRedirector
If you are using the JSP Redirector, you must add the jspRedirector.jsp file to your web application. This file is located under the CACTUS_HOME/sample-servlet/web directory.
****************************** ****************************** ****************************** ***********************
Example of Web.xml entry
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<web-app>
<filter>
<filter-name> FilterRedirector</filter-name>
<filter-class>
org.apache.cactus.server. FilterTestRedirector
</filter-class>
</filter>
<filter-mapping>
<filter-name> FilterRedirector</filter-name>
<url-pattern>/ FilterRedirector</url-pattern>
</filter-mapping>
<servlet>
<servlet-name> ServletRedirector</servlet- name>
<servlet-class>
org.apache.cactus.server. ServletTestRedirector
</servlet-class>
</servlet>
<servlet>
<servlet-name>JspRedirector</ servlet-name>
<jsp-file>/jspRedirector.jsp</ jsp-file>
</servlet>
<servlet>
<servlet-name> CustomerServlet</servlet-name>
<servlet-class>
com.oreilly.javaxp.cactus. servlet.CustomerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> JspRedirector</servlet-name>
<url-pattern>/ JspRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name> ServletRedirector</servlet- name>
<url-pattern>/ ServletRedirector</url- pattern>
</servlet-mapping>
</web-app>
****************************** ****************************** ****************************** **************************
One more option is to generate cactus.properties automatically.
To test an EJB, you need to prepare a war to contain the cactus setting and test cases, and then prepare a jar of your ejb. You need to match the JNDI name in the ejb jar and the war.
- Is there an plug in for Cactus ?
It's under construction .... very soon we will use it ....
Here are the steps to use cactus (To deploy EJB jar)
EJB Jar
Step 1: Prepare the Sample EJB
EJB Remote
EJB Home
EJB Bean
Step 2: Prepare EJB Deployment Descriptor (ejb-jar.xml)
Prepare the standard EJB Deployment Descriptor for the Session EJB:
- Write test cases -
package org.apache.cactus.sample.ejb3;
import java.util.Properties;
import javax.naming.InitialContext;
import junit.framework.TestCase;
import org.apache.cactus. ServletTestCase;
/**
* Sample Cactus test for a session bean.
*
* @version $Id: TestConverterEJB.java 238816 2004-02-29 16:36:46Z vmassol $
*/
public class TestConverterEJB extends ServletTestCase
{
/**
* @see TestCase#setUp()
*/
public void setUp() throws Exception
{
.
.
.
}
/**
* Verify yen to dollars conversion works.
* @throws Exception on error
*/
public void testMethodXXXX() throws Exception
{
.
.
}
}
5. Entries in web.xml
<ejb-ref-name>ejb/Converter</ ejb-ref-name>
<ejb-ref-type>Session</ejb- ref-type>
<home>org.apache.cactus. sample.ejb.ConverterHome</ home>
<remote>org.apache.cactus. sample.ejb.Converter</remote>
</ejb-ref>
6. Package Web Application
Package the web application into a war file.
In web projects it is required to write “cactus.xml”.
Deploy
You need to package the war file with all the additional jar files in an ear file and then deploy the ear file based on the deployment procedure of your servlet container and your ejb container.
No comments:
Post a Comment