(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / persistence / MyEntityExampleTestBase.java
index d8e80440530cd495762fe227d86a8809cc6991cd..0ee5b38a37cf77c655fe6674d9e4b4f23e98954b 100644 (file)
 package org.wamblee.test.persistence;
 
 import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
 
 import javax.persistence.EntityManager;
 import javax.transaction.UserTransaction;
 
+import junit.framework.AssertionFailedError;
+
 import org.dbunit.IDatabaseTester;
 import org.dbunit.dataset.ITable;
 import org.junit.After;
@@ -28,6 +31,7 @@ import org.junit.Test;
 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.
@@ -124,16 +128,17 @@ public class MyEntityExampleTestBase {
         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
 
     }
-    
+
     @Test
-    public void testEntityPersistenceWithContextualEntityManager() throws Exception {
+    public void testEntityPersistenceWithContextualEntityManager()
+        throws Exception {
 
         // Use the JPA builder to create a transaction scoped entity manager for
         // as and execute the
         // unit of work.
         builder.begin();
 
-        EntityManager em = builder.getContextualEntityManager(); 
+        EntityManager em = builder.getContextualEntityManager();
         MyEntity entity = new MyEntity("a", "b");
         em.persist(entity);
 
@@ -150,17 +155,18 @@ public class MyEntityExampleTestBase {
         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
 
     }
-    
+
     @Test
     public void testEntityPersistenceWithUserTransactions() throws Exception {
 
-        SimpleTransactionManager manager = new SimpleTransactionManager(new DefaultUserTransactionFactory());
+        SimpleTransactionManager manager = new SimpleTransactionManager(
+            new DefaultUserTransactionFactory());
         manager.addResource(jpaTester.getJpaBuilder());
-        
+
         UserTransaction transaction = manager.getTransaction();
         transaction.begin();
 
-        EntityManager em = builder.getContextualEntityManager(); 
+        EntityManager em = builder.getContextualEntityManager();
         MyEntity entity = new MyEntity("a", "b");
         em.persist(entity);
 
@@ -177,4 +183,79 @@ public class MyEntityExampleTestBase {
         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);
+    }
+
+    @Test(expected = AssertionFailedError.class)
+    public void testTransactionNotCommittedUnexpectedly() throws Exception {
+        builder.execute(new JpaUnitOfWork<Void>() {
+            public Void execute(EntityManager aEm) {
+                MyEntity entity = new MyEntity("a", "b");
+                aEm.persist(entity);
+                aEm.getTransaction().rollback();
+                return null;
+            }
+        });
+    }
+
 }