001: package com.mockrunner.connector;
002:
003: import javax.resource.ResourceException;
004: import javax.resource.cci.InteractionSpec;
005: import javax.resource.cci.Record;
006:
007: /**
008: * This interaction implementor can be used to simulate failures. By default
009: * it simply accepts every request and throws a <code>ResourceException</code>
010: * for all <code>execute</code> calls. It can be disabled and it can be configured
011: * to return <code>false</code> for {@link #execute(InteractionSpec, Record, Record)}
012: * and <code>null</code> for {@link #execute(InteractionSpec, Record)} instead of
013: * throwing an exception.
014: */
015: public class GenericFailureInteraction implements
016: InteractionImplementor {
017: private boolean enabled;
018: private boolean throwException;
019: private ResourceException exception;
020:
021: /**
022: * Sets the default values, i.e. throwing a <code>ResourceException</code>.
023: */
024: public GenericFailureInteraction() {
025: this (true);
026: }
027:
028: /**
029: * Sets if failure values should be returned instead of throwing a
030: * <code>ResourceException</code>.
031: * @param throwException <code>true</code> thrown an exception,
032: * <code>false</code> return failure values for <code>execute</code>
033: */
034: public GenericFailureInteraction(boolean throwException) {
035: this (throwException, new ResourceException(
036: "Simulated test exception"));
037: }
038:
039: /**
040: * Sets if failure values should be returned instead of throwing a
041: * <code>ResourceException</code> and allows to set the exception
042: * that will be thrown if exceptions are enabled.
043: * @param throwException <code>true</code> thrown an exception,
044: * <code>false</code> return failure values for <code>execute</code>
045: * @param exception the exception to be thrown
046: */
047: public GenericFailureInteraction(boolean throwException,
048: ResourceException exception) {
049: this .enabled = true;
050: this .throwException = throwException;
051: this .exception = exception;
052: }
053:
054: /**
055: * Enables this implementor. {@link #canHandle(InteractionSpec, Record, Record)}
056: * returns <code>true</code>, if this implementor is enabled.
057: */
058: public void enable() {
059: this .enabled = true;
060: }
061:
062: /**
063: * Disables this implementor. {@link #canHandle(InteractionSpec, Record, Record)}
064: * returns <code>false</code>, if this implementor is disabled.
065: */
066: public void disable() {
067: this .enabled = false;
068: }
069:
070: /**
071: * Sets if failure values should be returned instead of throwing a
072: * <code>ResourceException</code>.
073: * @param throwException <code>true</code> thrown an exception,
074: * <code>false</code> return failure values for <code>execute</code>
075: */
076: public void setThrowException(boolean throwException) {
077: this .throwException = throwException;
078: }
079:
080: /**
081: * Sets the exception that will be thrown if exceptions are enabled.
082: * @param exception the exception to be thrown
083: */
084: public void setException(ResourceException exception) {
085: this .exception = exception;
086: }
087:
088: /**
089: * Returns <code>true</code> if this implementor is enabled and
090: * <code>false</code> otherwise.
091: */
092: public boolean canHandle(InteractionSpec interactionSpec,
093: Record actualRequest, Record actualResponse) {
094: return enabled;
095: }
096:
097: /**
098: * Throws a <code>ResourceException</code> or returns <code>false</code>.
099: * You can use {@link #setThrowException(boolean)} to configure this
100: * behaviour.
101: */
102: public boolean execute(InteractionSpec interactionSpec,
103: Record actualRequest, Record actualResponse)
104: throws ResourceException {
105: if (throwException) {
106: throw exception;
107: }
108: return false;
109: }
110:
111: /**
112: * Throws a <code>ResourceException</code> or returns <code>null</code>.
113: * You can use {@link #setThrowException(boolean)} to configure this
114: * behaviour.
115: */
116: public Record execute(InteractionSpec interactionSpec,
117: Record actualRequest) throws ResourceException {
118: if (throwException) {
119: throw exception;
120: }
121: return null;
122: }
123: }
|