package org.wamblee.test.persistence;
import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
import org.wamblee.test.transactions.DefaultUserTransactionFactory;
import org.wamblee.test.transactions.SimpleTransactionManager;
+import org.wamblee.test.transactions.TransactionResult;
/**
* This class shows an example of how to test an entity using jpa.
assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
}
+
+ @Test
+ public void testTransactionCommittedCallback() throws Exception {
+ TransactionResultCallback callback = mock(TransactionResultCallback.class);
+ builder.execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ MyEntity entity = new MyEntity("a", "b");
+ aEm.persist(entity);
+ return null;
+ }
+ }, callback);
+ verify(callback).status(TransactionResult.COMMIT);
+ verifyNoMoreInteractions(callback);
+ }
+
+ @Test
+ public void testTransactionMarkedForRollback() throws Exception {
+ TransactionResultCallback callback = mock(TransactionResultCallback.class);
+
+ builder.execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ MyEntity entity = new MyEntity("a", "b");
+ aEm.persist(entity);
+ aEm.getTransaction().setRollbackOnly();
+ return null;
+ }
+ }, callback);
+ verify(callback).status(TransactionResult.ROLLBACK);
+ verifyNoMoreInteractions(callback);
+ }
+
+ @Test
+ public void testTransactionRolledBack() throws Exception {
+ TransactionResultCallback callback = mock(TransactionResultCallback.class);
+
+ builder.execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ MyEntity entity = new MyEntity("a", "b");
+ aEm.persist(entity);
+ aEm.getTransaction().rollback();
+ return null;
+ }
+ }, callback);
+ verify(callback).status(TransactionResult.UNKNOWN);
+ verifyNoMoreInteractions(callback);
+ }
+
+ @Test
+ public void testTransactionCommitted() throws Exception {
+ TransactionResultCallback callback = mock(TransactionResultCallback.class);
+
+ builder.execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ MyEntity entity = new MyEntity("a", "b");
+ aEm.persist(entity);
+ aEm.getTransaction().commit();
+ return null;
+ }
+ }, callback);
+ verify(callback).status(TransactionResult.UNKNOWN);
+ verifyNoMoreInteractions(callback);
+ }
+
+
}