All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.connector.GenericFailureInteraction Maven / Gradle / Ivy

Go to download

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.connector;

import javax.resource.ResourceException;
import javax.resource.cci.InteractionSpec;
import javax.resource.cci.Record;

/**
 * This interaction implementor can be used to simulate failures. By default
 * it simply accepts every request and throws a ResourceException
 * for all execute calls. It can be disabled and it can be configured
 * to return false for {@link #execute(InteractionSpec, Record, Record)}
 * and null for {@link #execute(InteractionSpec, Record)} instead of
 * throwing an exception.
 */
public class GenericFailureInteraction implements InteractionImplementor
{
    private boolean enabled;
    private boolean throwException;
    private ResourceException exception;
    
    /**
     * Sets the default values, i.e. throwing a ResourceException.
     */
    public GenericFailureInteraction()
    {
        this(true);
    }
    
    /**
     * Sets if failure values should be returned instead of throwing a
     * ResourceException.
     * @param throwException true thrown an exception,
     *                       false return failure values for execute
     */
    public GenericFailureInteraction(boolean throwException)
    {
        this(throwException, new ResourceException("Simulated test exception"));
    }
    
    /**
     * Sets if failure values should be returned instead of throwing a
     * ResourceException and allows to set the exception
     * that will be thrown if exceptions are enabled.
     * @param throwException true thrown an exception,
     *                       false return failure values for execute
     * @param exception the exception to be thrown
     */
    public GenericFailureInteraction(boolean throwException, ResourceException exception)
    {
        this.enabled = true;
        this.throwException = throwException;
        this.exception = exception;
    }

    /**
     * Enables this implementor. {@link #canHandle(InteractionSpec, Record, Record)}
     * returns true, if this implementor is enabled.
     */
    public void enable()
    {
        this.enabled = true;
    }
    
    /**
     * Disables this implementor. {@link #canHandle(InteractionSpec, Record, Record)}
     * returns false, if this implementor is disabled.
     */
    public void disable()
    {
        this.enabled = false;
    }
    
    /**
     * Sets if failure values should be returned instead of throwing a
     * ResourceException.
     * @param throwException true thrown an exception,
     *                       false return failure values for execute
     */
    public void setThrowException(boolean throwException)
    {
        this.throwException = throwException;
    }
    
    /**
     * Sets the exception that will be thrown if exceptions are enabled.
     * @param exception the exception to be thrown
     */
    public void setException(ResourceException exception)
    {
        this.exception = exception;
    }
    
    /**
     * Returns true if this implementor is enabled and
     * false otherwise.
     */
    public boolean canHandle(InteractionSpec interactionSpec, Record actualRequest, Record actualResponse)
    {
        return enabled;
    }

    /**
     * Throws a ResourceException or returns false.
     * You can use {@link #setThrowException(boolean)} to configure this
     * behaviour.
     */
    public boolean execute(InteractionSpec interactionSpec, Record actualRequest, Record actualResponse) throws ResourceException
    {
        if(throwException)
        {
            throw exception;
        }
        return false;
    }

    /**
     * Throws a ResourceException or returns null.
     * You can use {@link #setThrowException(boolean)} to configure this
     * behaviour.
     */
    public Record execute(InteractionSpec interactionSpec, Record actualRequest) throws ResourceException
    {
        if(throwException)
        {
            throw exception;
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy