@Test public void executeAndReturnDoesNotExecuteTheAfterSuccessTaskExecutorAfterTheTransactionIsRolledBack() { AfterSuccessTaskExecutor afterSuccessTaskExecutor = mock(AfterSuccessTaskExecutor.class); TimbuctooActions actions = mock(TimbuctooActions.class); TransactionEnforcer instance = TransactionEnforcerStubs .withAfterSuccessExecutorAndTimbuctooActions(afterSuccessTaskExecutor, actions); instance.executeAndReturn(timbuctooActions -> TransactionStateAndResult.rollbackAndReturn("")); verify(actions).rollback(); verifyZeroInteractions(afterSuccessTaskExecutor); }
public <T> T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action) { boolean success = false; TimbuctooActions timbuctooActions = timbuctooActionsFactory.create(afterSuccessTaskExecutor); try { TransactionStateAndResult<T> result = action.apply(timbuctooActions); if (result.wasCommitted()) { success = true; timbuctooActions.success(); } else { success = false; timbuctooActions.rollback(); } return result.getValue(); } catch (RuntimeException e) { success = false; timbuctooActions.rollback(); throw e; } finally { timbuctooActions.close(); if (success) { afterSuccessTaskExecutor.executeTasks(); } } }
TransactionEnforcer { public <T> T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action) { boolean success = false; TimbuctooActions timbuctooActions = timbuctooActionsFactory.create(afterSuccessTaskExecutor); try { TransactionStateAndResult<T> result = action.apply(timbuctooActions); if (result.wasCommitted()) { success = true; timbuctooActions.success(); } else { success = false; timbuctooActions.rollback(); } return result.getValue(); } catch (RuntimeException e) { success = false; timbuctooActions.rollback(); throw e; } finally { timbuctooActions.close(); if (success) { afterSuccessTaskExecutor.executeTasks(); } } } }
TransactionEnforcer { public <T> T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action) { boolean success = false; TimbuctooActions timbuctooActions = timbuctooActionsFactory.create(afterSuccessTaskExecutor); try { TransactionStateAndResult<T> result = action.apply(timbuctooActions); if (result.wasCommitted()) { success = true; timbuctooActions.success(); } else { success = false; timbuctooActions.rollback(); } return result.getValue(); } catch (RuntimeException e) { success = false; timbuctooActions.rollback(); throw e; } finally { timbuctooActions.close(); if (success) { afterSuccessTaskExecutor.executeTasks(); } } } TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory);   TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory,
                      AfterSuccessTaskExecutor afterSuccessTaskExecutor); }
TransactionEnforcer { public <T> T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action) { boolean success = false; TimbuctooActions timbuctooActions = timbuctooActionsFactory.create(afterSuccessTaskExecutor); try { TransactionStateAndResult<T> result = action.apply(timbuctooActions); if (result.wasCommitted()) { success = true; timbuctooActions.success(); } else { success = false; timbuctooActions.rollback(); } return result.getValue(); } catch (RuntimeException e) { success = false; timbuctooActions.rollback(); throw e; } finally { timbuctooActions.close(); if (success) { afterSuccessTaskExecutor.executeTasks(); } } } TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory);   TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory,
                      AfterSuccessTaskExecutor afterSuccessTaskExecutor); T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action); void execute(Function<TimbuctooActions, TransactionState> action); }
TransactionEnforcer { public <T> T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action) { boolean success = false; TimbuctooActions timbuctooActions = timbuctooActionsFactory.create(afterSuccessTaskExecutor); try { TransactionStateAndResult<T> result = action.apply(timbuctooActions); if (result.wasCommitted()) { success = true; timbuctooActions.success(); } else { success = false; timbuctooActions.rollback(); } return result.getValue(); } catch (RuntimeException e) { success = false; timbuctooActions.rollback(); throw e; } finally { timbuctooActions.close(); if (success) { afterSuccessTaskExecutor.executeTasks(); } } } TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory);   TransactionEnforcer(TimbuctooActions.TimbuctooActionsFactory timbuctooActionsFactory,
                      AfterSuccessTaskExecutor afterSuccessTaskExecutor); T executeAndReturn(Function<TimbuctooActions, TransactionStateAndResult<T>> action); void execute(Function<TimbuctooActions, TransactionState> action);  }