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