From: erik Date: Tue, 27 Jul 2010 20:32:12 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.5~51 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=638134d688bcf5a428b7856bddee4aaef19ceb83;p=utils --- diff --git a/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java b/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java index d8e80440..bc4e147f 100644 --- a/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java +++ b/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java @@ -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() { + 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() { + 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() { + 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() { + 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); + } + + }