Yes, I have used cactus to test an action class.  Here is some sample code.
I use it to ensure I am properly storing the correct Hostname in a member
session.  (we use virtual host)
This is my first stab at it and it is not very robust,  but it works.  


----------------------------------
</code>
package com.planetu.upons.webserver;

import org.apache.struts.action.*;
import junit.framework.*;

import org.apache.commons.cactus.*;

public class StartActionTest
    extends org.apache.commons.cactus.ServletTestCase {
    private org.apache.struts.action.ActionServlet actionServlet = null;
    
    /**
     * Insert the method's description here.
     * Creation date: (4/10/01 4:03:34 PM)
     * @param theConnection java.net.HttpURLConnection
     */
    public void beginVirtualHost(ServletTestRequest theRequest) {
        theRequest.setURL("virtualhost", "/e2", "/Start", "", "");

    }
    /**
     * Looks the output html to make sure the correct "Host Name is read
from the connection"
     * Creation date: (4/10/01 4:03:34 PM)
     * @param theConnection java.net.HttpURLConnection
     */
    public void endVirtualHost(java.net.HttpURLConnection theConnection) {
        int i = theConnection.toString().indexOf("Virtual Host");
        assert("Hostname displayed as Virtual Host", i == -1);

    }
    public static junit.framework.Test suite() {
        return new TestSuite(StartActionTest.class);
    }
   
    public void testLocalHost()
        throws java.io.IOException, javax.servlet.ServletException {
        actionServlet = new org.apache.struts.action.ActionServlet();
        actionServlet.init(config);

        ActionMapping am = actionServlet.findMapping("/Start");

        StartAction sa = new StartAction();
        ActionForward af = sa.perform(am, null, request, response);
        Visit visit = (Visit) session.getAttribute(SESSION.VISIT);

        assert("StartAcction forwards to success",
af.getName().equals("front"));

        assert("localhost stored in session",
visit.getHostName().equals("localhost"));

    }
   
    public void testVirtualHost()
        throws java.io.IOException, javax.servlet.ServletException {
        actionServlet = new org.apache.struts.action.ActionServlet();
        actionServlet.init(config);

        ActionMapping am = actionServlet.findMapping("/Start");

        StartAction sa = new StartAction();
        ActionForward af = sa.perform(am, null, request, response);
        Visit visit = (Visit) session.getAttribute(SESSION.VISIT);

        assert("StartAcction forwards to success",
af.getName().equals("front"));

        assert(
            "virtualhost stored in session",
            visit.getHostName().equals("virtualhost"));

    }
}

</code>

Reply via email to