Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 292   Methods: 12
NCLOC: 167   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DummyTransaction.java 75% 60% 66.7% 63.3%
coverage coverage
 1    package org.jboss.cache.transaction;
 2   
 3    import org.apache.commons.logging.Log;
 4    import org.apache.commons.logging.LogFactory;
 5   
 6    import javax.transaction.HeuristicMixedException;
 7    import javax.transaction.HeuristicRollbackException;
 8    import javax.transaction.RollbackException;
 9    import javax.transaction.Status;
 10    import javax.transaction.Synchronization;
 11    import javax.transaction.SystemException;
 12    import javax.transaction.Transaction;
 13    import javax.transaction.xa.XAResource;
 14    import java.util.Set;
 15    import java.util.concurrent.CopyOnWriteArraySet;
 16   
 17    /**
 18    * @author bela
 19    * @version $Revision: 1.12 $
 20    * Date: May 15, 2003
 21    * Time: 4:20:17 PM
 22    */
 23    public class DummyTransaction implements Transaction
 24    {
 25    private int status = Status.STATUS_UNKNOWN;
 26    private static final Log log = LogFactory.getLog(DummyTransaction.class);
 27    DummyBaseTransactionManager tm_;
 28   
 29    private final Set<Synchronization> participants = new CopyOnWriteArraySet<Synchronization>();
 30   
 31  1121984 public DummyTransaction(DummyBaseTransactionManager tm)
 32    {
 33  1121984 tm_ = tm;
 34  1121984 status = Status.STATUS_ACTIVE;
 35    }
 36   
 37    /**
 38    * Attempt to commit this transaction.
 39    *
 40    * @throws RollbackException If the transaction was marked for rollback
 41    * only, the transaction is rolled back and this exception is
 42    * thrown.
 43    * @throws SystemException If the transaction service fails in an
 44    * unexpected way.
 45    * @throws HeuristicMixedException If a heuristic decision was made and
 46    * some some parts of the transaction have been committed while
 47    * other parts have been rolled back.
 48    * @throws HeuristicRollbackException If a heuristic decision to roll
 49    * back the transaction was made.
 50    * @throws SecurityException If the caller is not allowed to commit this
 51    * transaction.
 52    */
 53  1121767 public void commit()
 54    throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
 55    SecurityException, SystemException
 56    {
 57  1121767 boolean doCommit;
 58  1121767 status = Status.STATUS_PREPARING;
 59  1121767 try
 60    {
 61  1121767 boolean outcome = notifyBeforeCompletion();
 62    // status=Status.STATUS_PREPARED;
 63  1121761 if (outcome && status != Status.STATUS_MARKED_ROLLBACK)
 64    {
 65  1121698 status = Status.STATUS_COMMITTING;
 66  1121698 doCommit = true;
 67    }
 68    else
 69    {
 70  63 status = Status.STATUS_ROLLING_BACK;
 71  63 doCommit = false;
 72    }
 73  1121761 notifyAfterCompletion(doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK);
 74  1121750 status = doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK;
 75  63 if (!doCommit) throw new RollbackException("outcome is " + outcome + " status: " + status);
 76    }
 77    finally
 78    {
 79    // Disassociate tx from thread.
 80  1121750 tm_.setTransaction(null);
 81    }
 82    }
 83   
 84    /**
 85    * Rolls back this transaction.
 86    *
 87    * @throws IllegalStateException If the transaction is in a state
 88    * where it cannot be rolled back. This could be because the
 89    * transaction is no longer active, or because it is in the
 90    * {@link Status#STATUS_PREPARED prepared state}.
 91    * @throws SystemException If the transaction service fails in an
 92    * unexpected way.
 93    */
 94  270 public void rollback() throws IllegalStateException, SystemException
 95    {
 96  270 try
 97    {
 98    // JBCACHE-360 -- to match JBossTM (and presumable the spec) a
 99    // rollback transaction should have status ROLLEDBACK before
 100    // calling afterCompletion().
 101    //status=Status.STATUS_ROLLING_BACK;
 102  270 status = Status.STATUS_ROLLEDBACK;
 103  270 notifyAfterCompletion(Status.STATUS_ROLLEDBACK);
 104    }
 105    catch (Throwable t)
 106    {
 107    }
 108  270 status = Status.STATUS_ROLLEDBACK;
 109   
 110    // Disassociate tx from thread.
 111  270 tm_.setTransaction(null);
 112    }
 113   
 114    /**
 115    * Mark the transaction so that the only possible outcome is a rollback.
 116    *
 117    * @throws IllegalStateException If the transaction is not in an active
 118    * state.
 119    * @throws SystemException If the transaction service fails in an
 120    * unexpected way.
 121    */
 122  102 public void setRollbackOnly() throws IllegalStateException, SystemException
 123    {
 124  102 status = Status.STATUS_MARKED_ROLLBACK;
 125    }
 126   
 127    /**
 128    * Get the status of the transaction.
 129    *
 130    * @return The status of the transaction. This is one of the
 131    * {@link Status} constants.
 132    * @throws SystemException If the transaction service fails in an
 133    * unexpected way.
 134    */
 135  5910338 public int getStatus() throws SystemException
 136    {
 137  5910080 return status;
 138    }
 139   
 140    /**
 141    * Change the transaction timeout for transactions started by the calling
 142    * thread with the {@link DummyTransactionManager#begin()} method.
 143    *
 144    * @param seconds The new timeout value, in seconds. If this parameter
 145    * is <code>0</code>, the timeout value is reset to the default
 146    * value.
 147    * @throws SystemException If the transaction service fails in an
 148    * unexpected way.
 149    */
 150  0 public void setTransactionTimeout(int seconds) throws SystemException
 151    {
 152  0 throw new SystemException("not supported");
 153    }
 154   
 155    /**
 156    * Enlist an XA resource with this transaction.
 157    *
 158    * @return <code>true</code> if the resource could be enlisted with
 159    * this transaction, otherwise <code>false</code>.
 160    * @throws RollbackException If the transaction is marked for rollback
 161    * only.
 162    * @throws IllegalStateException If the transaction is in a state
 163    * where resources cannot be enlisted. This could be because the
 164    * transaction is no longer active, or because it is in the
 165    * {@link Status#STATUS_PREPARED prepared state}.
 166    * @throws SystemException If the transaction service fails in an
 167    * unexpected way.
 168    */
 169  0 public boolean enlistResource(XAResource xaRes)
 170    throws RollbackException, IllegalStateException, SystemException
 171    {
 172  0 throw new SystemException("not supported");
 173    }
 174   
 175    /**
 176    * Delist an XA resource from this transaction.
 177    *
 178    * @return <code>true</code> if the resource could be delisted from
 179    * this transaction, otherwise <code>false</code>.
 180    * @throws IllegalStateException If the transaction is in a state
 181    * where resources cannot be delisted. This could be because the
 182    * transaction is no longer active.
 183    * @throws SystemException If the transaction service fails in an
 184    * unexpected way.
 185    */
 186  0 public boolean delistResource(XAResource xaRes, int flag)
 187    throws IllegalStateException, SystemException
 188    {
 189  0 throw new SystemException("not supported");
 190    }
 191   
 192    /**
 193    * Register a {@link Synchronization} callback with this transaction.
 194    *
 195    * @throws RollbackException If the transaction is marked for rollback
 196    * only.
 197    * @throws IllegalStateException If the transaction is in a state
 198    * where {@link Synchronization} callbacks cannot be registered.
 199    * This could be because the transaction is no longer active,
 200    * or because it is in the
 201    * {@link Status#STATUS_PREPARED prepared state}.
 202    * @throws SystemException If the transaction service fails in an
 203    * unexpected way.
 204    */
 205  1125029 public void registerSynchronization(Synchronization sync)
 206    throws RollbackException, IllegalStateException, SystemException
 207    {
 208  1125029 if (sync == null)
 209  0 throw new IllegalArgumentException("null synchronization " + this);
 210   
 211  1125029 switch (status)
 212    {
 213  1125029 case Status.STATUS_ACTIVE:
 214  0 case Status.STATUS_PREPARING:
 215  1125029 break;
 216  0 case Status.STATUS_PREPARED:
 217  0 throw new IllegalStateException("already prepared. " + this);
 218  0 case Status.STATUS_COMMITTING:
 219  0 throw new IllegalStateException("already started committing. " + this);
 220  0 case Status.STATUS_COMMITTED:
 221  0 throw new IllegalStateException("already committed. " + this);
 222  0 case Status.STATUS_MARKED_ROLLBACK:
 223  0 throw new RollbackException("already marked for rollback " + this);
 224  0 case Status.STATUS_ROLLING_BACK:
 225  0 throw new RollbackException("already started rolling back. " + this);
 226  0 case Status.STATUS_ROLLEDBACK:
 227  0 throw new RollbackException("already rolled back. " + this);
 228  0 case Status.STATUS_NO_TRANSACTION:
 229  0 throw new IllegalStateException("no transaction. " + this);
 230  0 case Status.STATUS_UNKNOWN:
 231  0 throw new IllegalStateException("unknown state " + this);
 232  0 default:
 233  0 throw new IllegalStateException("illegal status: " + status + " tx=" + this);
 234    }
 235   
 236  1125029 if (log.isDebugEnabled())
 237    {
 238  0 log.debug("registering synchronization handler " + sync);
 239    }
 240  1125029 participants.add(sync);
 241   
 242    }
 243   
 244  0 void setStatus(int new_status)
 245    {
 246  0 status = new_status;
 247    }
 248   
 249  1121767 boolean notifyBeforeCompletion()
 250    {
 251  1121767 boolean retval = true;
 252   
 253  1121767 for (Synchronization s : participants)
 254    {
 255  1124690 if (log.isDebugEnabled())
 256    {
 257  0 log.debug("processing beforeCompletion for " + s);
 258    }
 259  1124690 try
 260    {
 261  1124690 s.beforeCompletion();
 262    }
 263    catch (Throwable t)
 264    {
 265  63 retval = false;
 266  63 log.error("beforeCompletion() failed for " + s, t);
 267    }
 268    }
 269  1121761 return retval;
 270    }
 271   
 272  1122031 void notifyAfterCompletion(int status)
 273    {
 274  1122031 for (Synchronization s : participants)
 275    {
 276  1124965 if (log.isDebugEnabled())
 277    {
 278  0 log.debug("processing afterCompletion for " + s);
 279    }
 280  1124965 try
 281    {
 282  1124965 s.afterCompletion(status);
 283    }
 284    catch (Throwable t)
 285    {
 286  0 log.error("afterCompletion() failed for " + s, t);
 287    }
 288    }
 289  1122020 participants.clear();
 290    }
 291   
 292    }