public class TestTransactionManagement {
@Test
public void testTransactionInCurrentThread() throws Exception {
ITransactionManager txMnger = new TransactionManager();
assertNull(txMnger.getTransactionContext());
// create, begin, commit, end
ITransaction tx = txMnger.getNewTransaction();
assertNotNull(tx);
// Not transaction id until the transaction has begun
assertNull(tx.getTransactionId());
// Not transaction context until the transaction has begun
assertNull(txMnger.getTransactionContext());
tx.begin();
assertNotNull (tx.getTransactionId());
assertNotNull (txMnger.getTransactionContext().getTransactionId());
assertEquals (txMnger.getTransactionContext().getTransactionId(), tx.getTransactionId());
tx.commit();
assertNotNull (tx.getTransactionId());
assertNotNull (txMnger.getTransactionContext().getTransactionId());
assertEquals (txMnger.getTransactionContext().getTransactionId(), tx.getTransactionId());
tx.end();
assertNull(tx.getTransactionId());
assertNull(txMnger.getTransactionContext());
// create, begin, rollback
tx = txMnger.getNewTransaction();
assertNull(tx.getTransactionId());
assertNull(txMnger.getTransactionContext());
tx.begin();
assertNotNull (tx.getTransactionId());
assertNotNull (txMnger.getTransactionContext().getTransactionId());
assertEquals (txMnger.getTransactionContext().getTransactionId(), tx.getTransactionId());
tx.rollback();
assertNull(tx.getTransactionId());
assertNull(txMnger.getTransactionContext());
// begin, commit, rollback
tx = txMnger.getNewTransaction();
assertNull(tx.getTransactionId());
assertNull(txMnger.getTransactionContext());
tx.begin();
assertNotNull (tx.getTransactionId());
assertNotNull (txMnger.getTransactionContext().getTransactionId());
assertEquals (txMnger.getTransactionContext().getTransactionId(), tx.getTransactionId());
tx.commit();
assertNotNull (tx.getTransactionId());
assertNotNull (txMnger.getTransactionContext().getTransactionId());
assertEquals (txMnger.getTransactionContext().getTransactionId(), tx.getTransactionId());
tx.rollback();
assertNull(tx.getTransactionId());
assertNull(txMnger.getTransactionContext());
}