### Question:
StartableTransactionRunnable extends ProvidingTransactionRunnable<T, T> { public void start() throws TransactionException { if (this.alreadyRun) { throw new IllegalStateException("This TransactionRunnable has already been run and may not " + "be run again."); } this.alreadyRun = true; this.preRun(); this.run(); this.commit(); this.complete(); }  void start();  }### Answer:
@Test(expected = IllegalStateException.class) public void alreadyRunTest() throws Exception { this.testRunnable.start(); this.testRunnable.start(); Assert.fail("exception was not thrown"); }
@Test(expected = TransactionException.class) public void rollbackAfterExceptionTest() throws Exception { new TransactionRunnable() { protected void onRun() throws Exception { throw new Exception(); } protected void onRollback() { itRan(); } } .runIn(this.testRunnable); try { this.testRunnable.start(); } catch (TransactionException e) { Assert.assertEquals("Wrong number of exceptions reported", 1, e.exceptionCount()); Assert.assertTrue("Rollback did not run after exception", hasRun()); throw e; } Assert.fail("exception was not thrown"); }
@Test(expected = TransactionException.class) public void exceptionInRollbackTest() throws Exception { new TransactionRunnable() { protected void onRun() throws Exception { throw new Exception(); } protected void onRollback() throws Exception { throw new Exception(); } protected void onComplete() { itRan(); } } .runIn(this.testRunnable); try { this.testRunnable.start(); } catch (TransactionException e) { Assert.assertEquals("Wrong number of exceptions reported", 2, e.exceptionCount()); Assert.assertTrue("Rollback failed and yet the exception did not warn of possible corruption.", e.isNonRecoverable()); Assert.assertTrue("Complete did not run after exception", hasRun()); throw e; } Assert.fail("exception was not thrown"); }
@Test(expected = TransactionException.class) public void exceptionInCompleteTest() throws Exception { new TransactionRunnable() { protected void onComplete() throws Exception { throw new Exception(); } } .runIn(this.testRunnable); this.testRunnable.start(); Assert.fail("exception was not thrown"); }