Thanks very much Nick,

It is simple and worked really well.

A similar implementation is available in Expresso Framework.

Thanks again.

Shri

-----Original Message-----
From: Nick Chalko [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 25 October 2001 12:02 PM
To: '[EMAIL PROTECTED]'
Subject: RE: Cactus : Struts


Here is the extension I wrote.  I am still trying to get this released Open
Source.

Note that I have a internal class to wrap the action servlet, to return the
action forward,
instead of the response.
This also allows me to use the actual struts-congif.xml.

Try it with

<---sample test --->

package com.planetu.upons.actions;

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

import org.apache.cactus.*;

/**
 * Cactus test for the Login Action.
 * @author: $Author: nick $
 * @version: $Revision: 1.2 $
 */
public class MyActionTest extends UponsActionTest {
public MyActionTest(String arg1) {
        super(arg1);

}
public void beginLoginButtonNew(WebRequest theRequest) {

theRequest.setURL("www.myhost.com","/mycontext","/MyAction.do","","");
        theRequest.addParameter("new.x","55");
        theRequest.addParameter("new.y","14");


        }
public void testLoginButtonNew()
    throws
        java.io.IOException,
        javax.servlet.ServletException,
   {

    ActionForward af= processCleanAction();

    assertActionForward("new", af);

}
}



<---------- StrutsActionTest --->

package com.planetu.upons.strutsext;

/*
 *$Header:
/usr/cvsroot/upons/src/com/planetu/upons/strutsext/StrutsActionTest.java,v
1.12 2001/10/24 03:11:51 nick Exp $
 */

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

import org.apache.cactus.*;
import org.apache.struts.upload.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.cactus.util.AssertUtils;
import org.apache.cactus.Cookie;

/**
 * Cactus test for the Start Action.
 * @author: $Author: nick $
 * @version: $Revision: 1.12 $
 * Copyright 2001 PlanetU
 */
public abstract class StrutsActionTest extends ServletTestCase {
    ActionServletWrapper actionServlet= null;

    /**
    * Insert the type's description here.
    * @author: $Author: nick $
    * @version: $Revision: 1.12 $
    */
    public class ActionServletWrapper extends ActionServlet {
        /**
         * Process an HTTP request for a cactus test.
         *
         * @param request The servlet request we are processing
         * @param response The servlet response we are creating
         *
         * @exception IOException if an input/output error occurs
         * @exception ServletException if a servlet exception occurs
         */
        public ActionForward processCatus(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {

            String contentType= request.getContentType();
            String method= request.getMethod();

            //if this is a multipart request, wrap the HttpServletRequest
object
            //with a MultipartRequestWrapper to keep the process sub-methods
            //from failing when checking for certain request parameters
            //for command tokens and cancel button detection
            if ((contentType != null) &&
(contentType.startsWith("multipart/form-data")) && (method.equals("POST")))
{
                request= new MultipartRequestWrapper(request);
            }

            // Identify the path component we will use to select a mapping
            String path= processPath(request);
            if (path == null) {
                if (debug >= 1)
                    log(" No path available for request URI " +
request.getRequestURI());
                throw new
ServletException(internal.getMessage("processPath"));

            }
            if (debug >= 1)
                log("Processing a " + request.getMethod() + " for " + path);

            // Automatically select a locale for this user if requested
            processLocale(request);

            // Set the content type and no-caching headers if requested
            processContent(response);
            processNoCache(response);

            // General purpose preprocessing hook
            if (!processPreprocess(request, response))
                throw new ServletException("Preprocess failed");

            // Look up the corresponding mapping
            ActionMapping mapping= processMapping(path, request);
            if (mapping == null) {
                if (debug >= 1)
                    log(" No mapping available for path " + path);
                throw new
ServletException(internal.getMessage("processInvalid", path));

            }

            // Process any ActionForm bean related to this request
            ActionForm formInstance= processActionForm(mapping, request);
            processPopulate(formInstance, mapping, request);
            if (!processValidate(mapping, formInstance, request, response))
                throw new ServletException("ProcesssValidate failed");

            // Execute a forward if specified by this mapping
            if (!processForward(mapping, request, response))
                throw new ServletException("Do not test forwards with
this");

            // Execute an include if specified by this mapping
            if (!processInclude(mapping, request, response))
                throw new ServletException("processInclude failed");
            ;

            // Acquire the Action instance to process this request
            Action actionInstance= processActionCreate(mapping, request);
            if (actionInstance == null) {
                throw new
ServletException(internal.getMessage("actionCreate", mapping.getPath()));

            }

            // Call the Action instance itself
            ActionForward forward= processActionPerform(actionInstance,
mapping, formInstance, request, response);
            //set the request back to it's normal state if it's currently
wrapped,
            //to avoid ClassCastExceptions from ServletContainers if
forwarding
            if (request instanceof MultipartRequestWrapper) {
                request= ((MultipartRequestWrapper) request).getRequest();
            }

            // Process the returned ActionForward (if any)
            return forward;

        }
    }

/** @link dependency
 * @stereotype tests*/
/*#StartAction lnkStartAction;*/

/**
 * StartActionTest constructor comment.
 * @param arg1 java.lang.String
 */
public StrutsActionTest(String arg1) {
        super(arg1);


}
/**
 * Insert the method's description here.
 *
 * @param errors org.apache.struts.action.ActionErrors
 */
public void assertActionForward(String message, String forwardName,
ActionForward af) {
   assertEquals(message, forwardName, af != null ? af.getName() : null);
}
/**
 * Insert the method's description here.
 *
 */
public void assertActionForward(String forwardName, ActionForward af) {
   assertActionForward("Checking ActionForward",forwardName,af);
}
/**
 * Insert the method's description here.
 *
 */
public void assertCookieExpired(String message, WebResponse response, String
name, String domain) {
   Cookie[] cookies = response.getCookies();
   Cookie c = null;
    for (int i = 0; i < cookies.length; i++) {
        c =  cookies[i];
        if (domain == null || c.getDomain() == null ||
c.getDomain().endsWith(domain)) {
            if (c.getName().equals(name)) {
                break;
            }

        }
        c = null;
    }

    assertNotNull(message + " Expected "+ name+" but found " +
java.util.Arrays.asList(cookies), c);

    assertEquals(message + " cookie " + c + " isExpired  ", true,
c.isExpired());

}
/**
 * Insert the method's description here.
 *
 */
public void assertCookieExpired(WebResponse response, String name) {
        assertCookieExpired("",response,name,null);

}
/**
 * Insert the method's description here.
 *
 */
public void assertCookieNotExpired(String message, WebResponse response,
String name, String domain) {
   Cookie[] cookies = response.getCookies();
   Cookie c = null;
    for (int i = 0; i < cookies.length; i++) {
        c =  cookies[i];
        if (domain == null || c.getDomain() == null ||
c.getDomain().endsWith(domain)) {
            if (c.getName().equals(name)) {
                break;
            }

        }
        c = null;
    }

    assertNotNull(message + " Expected "+ name+" but found " +
java.util.Arrays.asList(cookies), c);

    assertEquals(message + " cookie " + c + " isExpired  ", false,
c.isExpired());

}
/**
 * Insert the method's description here.
 *
 */
public void assertCookieNotExpired(WebResponse response, String name) {
        assertCookieNotExpired("",response,name,null);

}
/**
 * Insert the method's description here.
 *
 */
public void assertErrorCount(int count) {
        assertErrorCount("",count);

}
/**
 * Insert the method's description here.
 *
 */
public void assertErrorCount(String message, int count) {
    message = (message == null ? "" : message + " ") + "Expected " + count +
" errors ";
    ActionErrors errors = (ActionErrors)
request.getAttribute(Action.ERROR_KEY);
    assertNotNull(message + " but ActionErrors is to be null ", errors);
    if (errors.size() != count) {
        ActionError theError = null;
        StringBuffer otherErrors = new StringBuffer();
        otherErrors.append("{");
        for (java.util.Iterator i = errors.get(); i.hasNext();) {
            ActionError error = (ActionError) i.next();
            otherErrors.append(error.getKey());
            if (i.hasNext()) {
                otherErrors.append(", ");
            }

        }
        otherErrors.append("}");

        fail(message + "but found " + errors.size() + " errors in " +
otherErrors);
    }
}
/**
 * Insert the method's description here.
 *
 */
public void assertHasCookie(String message, WebResponse response, String
name, String value, String domain) {
    org.apache.cactus.Cookie[] cookies= response.getCookies();
    org.apache.cactus.Cookie c= null;
    for (int i= 0; i < cookies.length; i++) {
        c= cookies[i];
        if (domain == null || c.getDomain() == null ||
c.getDomain().endsWith(domain)) {
            if (c.getName().equals(name)) {
                break;
            }

        }
        c= null;
    }

    assertNotNull(message + " but found "+ java.util.Arrays.asList(cookies)
, c);
    assertEquals(message, value, c.getValue());
}
/**
 * Insert the method's description here.
 *
 */
public void assertHasCookie(WebResponse response, String name, String value)
{
    assertHasCookie("Looking for cookie \"" + name + "\" with the value  \""
+ value + "\"", response, name, value, null);

}
/**
 * Insert the method's description here.
 *
 */
public void assertHasCookie(WebResponse response, String name, String value,
String domain) {
    assertHasCookie("Looking for cookie \"" + name + "\" in domain
\""+domain+"\" with the value  \"" + value + "\"", response, name, value,
domain);

}
/**
 * Insert the method's description here.
 *
 */
public void assertHasError(String fieldKey, String messageKey) {
        assertHasError("",fieldKey,messageKey);

}
/**
 * Insert the method's description here.
 *
 */
public void assertHasError(String message, String fieldKey, String
messageKey) {
    message= (message == null ? "" : message + " ") + "Expected the error "
+ messageKey + " for field " + fieldKey + "  but found ";
    ActionErrors errors= (ActionErrors)
request.getAttribute(Action.ERROR_KEY);
    assertNotNull(message + "ActionErrors to be null ", errors);
    ActionError theError= null;
    StringBuffer otherErrors= new StringBuffer();
    otherErrors.append("{");
    for (java.util.Iterator i= errors.get(fieldKey); i.hasNext();) {
        ActionError error= (ActionError) i.next();
        otherErrors.append(error.getKey());
        if (i.hasNext()) {
            otherErrors.append(", ");
        }
        if (error.getKey().equals(messageKey)) {
            theError= error;
        }
    }
    otherErrors.append("}");
    assertNotNull(message + otherErrors.toString() , theError);
    assertEquals(message + " the error " + theError.getKey(), messageKey,
theError.getKey());
}
/**
 * Insert the method's description here.
 *
 * @param errors org.apache.struts.action.ActionErrors
 */
public void assertNoErrors() {
    assertNoErrors("");
}
/**
 * Insert the method's description here.
 *
 * @param errors org.apache.struts.action.ActionErrors
 */
public void assertNoErrors(String message) {
    ActionErrors errors= (ActionErrors)
request.getAttribute(Action.ERROR_KEY);

    if (errors != null && errors.size() != 0) {
        StringBuffer sb= new StringBuffer();
        sb.append("{");
        for (java.util.Iterator i= errors.get(); i.hasNext();) {
            ActionError error= (ActionError) i.next();
            sb.append(error.getKey());
            if (i.hasNext()) {
                sb.append(", ");
            }
        }

        sb.append("}");
        fail(message + " Expected no errors but was " + sb.toString());
    }

}
/**
 * Insert the method's description here.
 *
 * @param errors org.apache.struts.action.ActionErrors
 */
public  void assertNoException() {
assertNoException("");
}
/**
 * Insert the method's description here.
 *
 * @param errors org.apache.struts.action.ActionErrors
 */
public void assertNoException(String message) {
    Exception e= (Exception) request.getAttribute(Action.EXCEPTION_KEY);

    if (e != null) {
        java.io.StringWriter sw= new java.io.StringWriter();
        java.io.PrintWriter pw= new java.io.PrintWriter(sw);
        while (e != null) {
            e.printStackTrace(pw);
            if (e.getClass().equals(javax.ejb.EJBException.class)) {
                e= ((javax.ejb.EJBException) e).getCausedByException();
            } else {
                e= null;
            }
            this.fail(message + " Expected no exceptions but had " +
sw.toString());
        }

    }    }
/**
 * Insert the method's description here.
 * Creation date: (4/10/01 10:36:51 AM)
 */
public void finishProcess2Blank() throws java.io.IOException,
javax.servlet.ServletException {
        PrintWriter out   =response.getWriter();
        out.println("Finished");
}
/**
 * Insert the method's description here.
 * Creation date: (4/10/01 10:36:51 AM)
 */
public ActionForward processAction()
        throws java.io.IOException, javax.servlet.ServletException {

         actionServlet = new ActionServletWrapper();
        actionServlet.init(config);


        return actionServlet.processCatus(request, response);

}
/**
 * Process an Action expecting Form Validation to fail.
 * Creation date: (4/10/01 10:36:51 AM)
 */
public ActionErrors processActionFormValidateFailure()
    throws java.io.IOException, javax.servlet.ServletException {

    try {
        ActionForward af= processAction();
        fail("Should have thrown Servlet Exception");
    } catch (javax.servlet.ServletException e) {
        if (!e.getMessage().equals("ProcesssValidate failed")) {
            throw e;
        }
    }

    return (ActionErrors) request.getAttribute(Action.ERROR_KEY);

}
/**
 * Insert the method's description here.
 * Creation date: (4/10/01 10:36:51 AM)
 */
public ActionForward processCleanAction() throws java.io.IOException,
javax.servlet.ServletException {
    ActionForward af=processAction();

    assertNoErrors();
    assertNoException();
    return af;
}
}


Reply via email to