001 package com.mockrunner.mock.ejb;
002
003 import javax.transaction.HeuristicMixedException;
004 import javax.transaction.HeuristicRollbackException;
005 import javax.transaction.NotSupportedException;
006 import javax.transaction.RollbackException;
007 import javax.transaction.Status;
008 import javax.transaction.SystemException;
009 import javax.transaction.UserTransaction;
010
011 /**
012 * Mock implementation of <code>UserTransaction</code>.
013 */
014 public class MockUserTransaction implements UserTransaction
015 {
016 private boolean beginCalled;
017 private boolean commitCalled;
018 private boolean rollbackCalled;
019 private boolean rollbackOnlyCalled;
020 private int transactionTimeout;
021 private int beginCalls;
022 private int commitCalls;
023 private int rollbackCalls;
024 private int rollbackOnlyCalls;
025
026 public MockUserTransaction()
027 {
028 reset();
029 }
030
031 /**
032 * Resets the state, i.e. sets the status to
033 * <code>Status.STATUS_NO_TRANSACTION</code>
034 * and the number of calls to 0.
035 */
036 public void reset()
037 {
038 beginCalled = false;
039 commitCalled = false;
040 rollbackCalled = false;
041 rollbackOnlyCalled = false;
042 transactionTimeout = 0;
043 beginCalls = 0;
044 commitCalls = 0;
045 rollbackCalls = 0;
046 rollbackOnlyCalls = 0;
047 }
048
049 /**
050 * Returns if {@link #begin} was called.
051 * @return was {@link #begin} called
052 */
053 public boolean wasBeginCalled()
054 {
055 return beginCalled;
056 }
057
058 /**
059 * Returns if {@link #commit} was called.
060 * @return was {@link #commit} called
061 */
062 public boolean wasCommitCalled()
063 {
064 return commitCalled;
065 }
066
067 /**
068 * Returns if {@link #rollback} was called.
069 * @return was {@link #rollback} called
070 */
071 public boolean wasRollbackCalled()
072 {
073 return rollbackCalled;
074 }
075
076 /**
077 * Returns if {@link #setRollbackOnly} was called.
078 * @return was {@link #setRollbackOnly} called
079 */
080 public boolean wasRollbackOnlyCalled()
081 {
082 return rollbackOnlyCalled;
083 }
084
085 /**
086 * Returns the transaction timeout.
087 * @return the transaction timeout
088 */
089 public int getTransactionTimeout()
090 {
091 return transactionTimeout;
092 }
093
094 /**
095 * Returns the number of overall {@link #begin} calls.
096 * @return the number of overall {@link #begin} calls
097 */
098 public int getNumberBeginCalls()
099 {
100 return beginCalls;
101 }
102
103 /**
104 * Returns the number of overall {@link #commit} calls.
105 * @return the number of overall {@link #commit} calls
106 */
107 public int getNumberCommitCalls()
108 {
109 return commitCalls;
110 }
111
112 /**
113 * Returns the number of overall {@link #rollback} calls.
114 * @return the number of overall {@link #rollback} calls
115 */
116 public int getNumberRollbackCalls()
117 {
118 return rollbackCalls;
119 }
120
121 /**
122 * Returns the number of overall {@link #setRollbackOnly} calls.
123 * @return the number of overall {@link #setRollbackOnly} calls
124 */
125 public int getNumberRollbackOnlyCalls()
126 {
127 return rollbackOnlyCalls;
128 }
129
130 /**
131 * Returns the status of this transaction.
132 * @return the status of this transaction
133 */
134 public int getStatus() throws SystemException
135 {
136 if(rollbackCalled) return Status.STATUS_ROLLEDBACK;
137 if(commitCalled) return Status.STATUS_COMMITTED;
138 if(rollbackOnlyCalled) return Status.STATUS_MARKED_ROLLBACK;
139 if(beginCalled) return Status.STATUS_ACTIVE;
140 return Status.STATUS_NO_TRANSACTION;
141 }
142
143 /**
144 * Starts the transaction. The status will be
145 * <code>Status.STATUS_ACTIVE</code> and the
146 * flags regarding <code>wasXYZCalled</code>
147 * are reset to <code>false</code>. This method
148 * does not reset the number of overall calls.
149 */
150 public void begin() throws NotSupportedException, SystemException
151 {
152 beginCalled = true;
153 commitCalled = false;
154 rollbackCalled = false;
155 rollbackOnlyCalled = false;
156 beginCalls++;
157 }
158
159 /**
160 * Commits the transaction.
161 */
162 public void commit() throws RollbackException,
163 HeuristicMixedException,
164 HeuristicRollbackException,
165 SecurityException,
166 IllegalStateException,
167 SystemException
168 {
169
170 commitCalled = true;
171 commitCalls++;
172 }
173
174 /**
175 * Rolls back the transaction.
176 */
177 public void rollback() throws IllegalStateException, SecurityException, SystemException
178 {
179 rollbackCalled = true;
180 rollbackCalls++;
181 }
182
183 /**
184 * Sets the rollback only flag.
185 */
186 public void setRollbackOnly() throws IllegalStateException, SystemException
187 {
188 rollbackOnlyCalled = true;
189 rollbackOnlyCalls++;
190 }
191
192 /**
193 * Sets the transaction timeout.
194 * @param timeout the transaction timeout
195 */
196 public void setTransactionTimeout(int timeout) throws SystemException
197 {
198 transactionTimeout = timeout;
199 }
200 }