(no commit message)
authorErik Brakkee <erik@brakkee.org>
Tue, 27 Jul 2010 20:32:12 +0000 (20:32 +0000)
committerErik Brakkee <erik@brakkee.org>
Tue, 27 Jul 2010 20:32:12 +0000 (20:32 +0000)
test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java

index d8e80440530cd495762fe227d86a8809cc6991cd..bc4e147f61253277c098729720ea0fc7f0da6e6a 100644 (file)
@@ -16,6 +16,7 @@
 package org.wamblee.test.persistence;
 
 import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
 
 import javax.persistence.EntityManager;
 import javax.transaction.UserTransaction;
@@ -28,6 +29,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.
@@ -177,4 +179,68 @@ 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);
+    }
+    
+    
 }