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 InteractionImplementor 016 { 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 { 026 this(true); 027 } 028 029 /** 030 * Sets if failure values should be returned instead of throwing a 031 * <code>ResourceException</code>. 032 * @param throwException <code>true</code> thrown an exception, 033 * <code>false</code> return failure values for <code>execute</code> 034 */ 035 public GenericFailureInteraction(boolean throwException) 036 { 037 this(throwException, new ResourceException("Simulated test exception")); 038 } 039 040 /** 041 * Sets if failure values should be returned instead of throwing a 042 * <code>ResourceException</code> and allows to set the exception 043 * that will be thrown if exceptions are enabled. 044 * @param throwException <code>true</code> thrown an exception, 045 * <code>false</code> return failure values for <code>execute</code> 046 * @param exception the exception to be thrown 047 */ 048 public GenericFailureInteraction(boolean throwException, ResourceException exception) 049 { 050 this.enabled = true; 051 this.throwException = throwException; 052 this.exception = exception; 053 } 054 055 /** 056 * Enables this implementor. {@link #canHandle(InteractionSpec, Record, Record)} 057 * returns <code>true</code>, if this implementor is enabled. 058 */ 059 public void enable() 060 { 061 this.enabled = true; 062 } 063 064 /** 065 * Disables this implementor. {@link #canHandle(InteractionSpec, Record, Record)} 066 * returns <code>false</code>, if this implementor is disabled. 067 */ 068 public void disable() 069 { 070 this.enabled = false; 071 } 072 073 /** 074 * Sets if failure values should be returned instead of throwing a 075 * <code>ResourceException</code>. 076 * @param throwException <code>true</code> thrown an exception, 077 * <code>false</code> return failure values for <code>execute</code> 078 */ 079 public void setThrowException(boolean throwException) 080 { 081 this.throwException = throwException; 082 } 083 084 /** 085 * Sets the exception that will be thrown if exceptions are enabled. 086 * @param exception the exception to be thrown 087 */ 088 public void setException(ResourceException exception) 089 { 090 this.exception = exception; 091 } 092 093 /** 094 * Returns <code>true</code> if this implementor is enabled and 095 * <code>false</code> otherwise. 096 */ 097 public boolean canHandle(InteractionSpec interactionSpec, Record actualRequest, Record actualResponse) 098 { 099 return enabled; 100 } 101 102 /** 103 * Throws a <code>ResourceException</code> or returns <code>false</code>. 104 * You can use {@link #setThrowException(boolean)} to configure this 105 * behaviour. 106 */ 107 public boolean execute(InteractionSpec interactionSpec, Record actualRequest, Record actualResponse) throws ResourceException 108 { 109 if(throwException) 110 { 111 throw exception; 112 } 113 return false; 114 } 115 116 /** 117 * Throws a <code>ResourceException</code> or returns <code>null</code>. 118 * You can use {@link #setThrowException(boolean)} to configure this 119 * behaviour. 120 */ 121 public Record execute(InteractionSpec interactionSpec, Record actualRequest) throws ResourceException 122 { 123 if(throwException) 124 { 125 throw exception; 126 } 127 return null; 128 } 129 }