com.mockrunner.struts.DefaultExceptionHandlerConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.struts;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import com.mockrunner.base.NestedApplicationException;
/**
* The default implementation of {@link ExceptionHandlerConfig}. It uses the Struts
* exception handling mechanism. Use the various constructors to provide your subclass
* of ExceptionHandler
or to configure exception handling using an
* instance of ExceptionConfig
. The ExceptionConfig
class
* allows you to set the handler class an exception type.
* Use {@link ActionTestModule#addExceptionHandler} to register an exception handler.
*/
public class DefaultExceptionHandlerConfig implements ExceptionHandlerConfig
{
private ExceptionConfig exceptionConfig;
private ExceptionHandler exceptionHandler;
private Class exceptionClass;
public DefaultExceptionHandlerConfig(ExceptionConfig exceptionConfig)
{
this.exceptionConfig = exceptionConfig;
try
{
Class handlerClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getHandler());
exceptionHandler = (ExceptionHandler)handlerClass.newInstance();
exceptionClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getType());
}
catch(Exception exc)
{
throw new NestedApplicationException(exc);
}
}
public DefaultExceptionHandlerConfig(ExceptionHandler exceptionHandler, ExceptionConfig exceptionConfig)
{
this.exceptionHandler = exceptionHandler;
this.exceptionConfig = exceptionConfig;
this.exceptionConfig.setHandler(exceptionHandler.getClass().getName());
try
{
exceptionClass = exceptionConfig.getClass().getClassLoader().loadClass(exceptionConfig.getType());
}
catch(Exception exc)
{
throw new NestedApplicationException(exc);
}
}
public DefaultExceptionHandlerConfig(ExceptionHandler exceptionHandler, Class exceptionClass)
{
this.exceptionHandler = exceptionHandler;
this.exceptionClass = exceptionClass;
this.exceptionConfig = new ExceptionConfig();
this.exceptionConfig.setHandler(exceptionHandler.getClass().getName());
this.exceptionConfig.setType(exceptionClass.getName());
}
public DefaultExceptionHandlerConfig(Class exceptionClass)
{
this.exceptionHandler = new ExceptionHandler();
this.exceptionClass = exceptionClass;
this.exceptionConfig = new ExceptionConfig();
this.exceptionConfig.setHandler(ExceptionHandler.class.getName());
this.exceptionConfig.setType(exceptionClass.getName());
}
public boolean canHandle(Exception exception)
{
return exceptionClass.isInstance(exception);
}
public Object handle(Exception exception, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException
{
if(!canHandle(exception)) return null;
if(null == exceptionHandler) return null;
return exceptionHandler.execute((Exception)exception, exceptionConfig, mapping, form, request, response);
}
/**
* Get the underlying ExceptionConfig
. If you did not provide
* an instance of ExceptionConfig
, this class will create one
* internally.
* @return the ExceptionConfig
*/
public ExceptionConfig getExceptionConfig()
{
return exceptionConfig;
}
/**
* Get the underlying ExceptionHandler
.
* @return the ExceptionHandler
*/
public ExceptionHandler getExceptionHandler()
{
return exceptionHandler;
}
}