@Test public void testAddThenRemoveSuccess() throws TransactionFailureException { TransactionContext context = newTransactionContext(); context.start(); Assert.assertTrue(context.addTransactionAware(ds1)); ds1.addChange(A); try { context.removeTransactionAware(ds1); Assert.fail("Removal of TransactionAware should fails when there is active transaction."); } catch (IllegalStateException e) { // Expected } context.finish(); Assert.assertTrue(context.removeTransactionAware(ds1)); // Removing a TransactionAware not added before should returns false Assert.assertFalse(context.removeTransactionAware(ds2)); // Verify ds1 is committed and post-committed Assert.assertTrue(ds1.started); Assert.assertTrue(ds1.checked); Assert.assertTrue(ds1.committed); Assert.assertTrue(ds1.postCommitted); Assert.assertFalse(ds1.rolledBack); // Verify nothing happen to ds2 Assert.assertFalse(ds2.started); Assert.assertFalse(ds2.checked); Assert.assertFalse(ds2.committed); Assert.assertFalse(ds2.postCommitted); Assert.assertFalse(ds2.rolledBack); Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed); }
@Test public void testAndThenRemoveOnFailure() throws TransactionFailureException { ds1.failCommitTxOnce = InduceFailure.ThrowException; TransactionContext context = newTransactionContext(); context.start(); Assert.assertTrue(context.addTransactionAware(ds1)); ds1.addChange(A); try { context.finish(); Assert.fail("Persist should have failed - exception should be thrown"); } catch (TransactionFailureException e) { Assert.assertEquals("persist failure", e.getCause().getMessage()); } Assert.assertTrue(context.removeTransactionAware(ds1)); // Verify ds1 is rolled back Assert.assertTrue(ds1.started); Assert.assertTrue(ds1.checked); Assert.assertTrue(ds1.committed); Assert.assertFalse(ds1.postCommitted); Assert.assertTrue(ds1.rolledBack); Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted); }