TransactionServiceImpl implements TransactionService { protected OpState getActualStateAfterTransition(OpState oldOpState, OpState currentOpState) { if (oldOpState == null) { if (currentOpState == null) { return null; } else { switch (currentOpState) { case UPDATE: case DELETE: throw new IllegalStateException("Can not update/delete a bean that does not exist"); case SAVE: return OpState.SAVE; } } } else { if (currentOpState == null) { throw new IllegalStateException("Can not update/delete a bean that does not exist"); } else { switch (oldOpState) { case SAVE: { switch (currentOpState) { case SAVE: throw new IllegalStateException( "Constraint violation exception, a object with same id has already been saved"); case DELETE: return null; case UPDATE: default: return oldOpState; } } case UPDATE: { switch (currentOpState) { case SAVE: throw new IllegalStateException("Can not save an existing bean"); case DELETE: return currentOpState; case UPDATE: default: return oldOpState; } } case DELETE: { switch (currentOpState) { case SAVE: return OpState.UPDATE; case UPDATE: case DELETE: default: throw new IllegalStateException( "Can not update/delete a bean that has been deleted earlier in the transaction"); } } } } } return currentOpState; } @Inject  TransactionServiceImpl(TransactionInMemoryCache memCache,
                                TransactionFactory factory); String getNextTransactionId(); void commit(String txId); void rollback(String txId); void save(TransactionElement<T> element); void update(TransactionElement<T> element); void delete(TransactionElement<T> element);  }
@Test public void testTransitionFromUpdateToUpdate() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertEquals(OpState.UPDATE, serviceImpl.getActualStateAfterTransition(OpState.UPDATE, OpState.UPDATE)); }
@Test public void testTransitionFromUpdateToDelete() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertEquals(OpState.DELETE, serviceImpl.getActualStateAfterTransition(OpState.UPDATE, OpState.DELETE)); }
@Test(expected = IllegalStateException.class) public void testTransitionFromUpdateToNull() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.UPDATE, null); }
@Test(expected = IllegalStateException.class) public void testTransitionFromDeleteToUpdate() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.DELETE, OpState.UPDATE); }
@Test(expected = IllegalStateException.class) public void testTransitionFromDeleteToDelete() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.DELETE, OpState.DELETE); }
@Test(expected = IllegalStateException.class) public void testTransitionFromDeleteToNull() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.DELETE, null); }
@Test(expected = IllegalStateException.class) public void testTransitionFromSaveToSave() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.SAVE, OpState.SAVE); }
@Test public void testTransitionFromSaveToUpdate() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertEquals(OpState.SAVE, serviceImpl.getActualStateAfterTransition(OpState.SAVE, OpState.UPDATE)); }
@Test public void testTransitionFromSaveToDelete() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertNull(serviceImpl.getActualStateAfterTransition(OpState.SAVE, OpState.DELETE)); }
@Test(expected = IllegalStateException.class) public void testTransitionFromSaveToNull() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.SAVE, null); }
@Test(expected = IllegalStateException.class) public void testTransitionFromNullToUpdate() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(null, OpState.UPDATE); }
@Test public void testTransitionFromNullToSave() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertEquals(OpState.SAVE, serviceImpl.getActualStateAfterTransition(null, OpState.SAVE)); }
@Test public void testTransitionFromNullToNull() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; Assert.assertNull(serviceImpl.getActualStateAfterTransition(null, null)); }
@Test(expected = IllegalStateException.class) public void testTransitionFromNullToDelete() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(null, OpState.DELETE); }
@Test(expected = IllegalStateException.class) public void testTransitionFromUpdateToSave() { final TransactionService service = injector.getInstance(TransactionService.class); final TransactionServiceImpl serviceImpl = (TransactionServiceImpl) service; serviceImpl.getActualStateAfterTransition(OpState.UPDATE, OpState.SAVE); }