From: erik Date: Tue, 27 Jul 2010 20:47:24 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.5~47 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=0e30bfe98a8f1d3ed666477759c8f675798b89b9;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 bc4e147f..0ee5b38a 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 @@ -21,6 +21,8 @@ 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; @@ -126,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); @@ -152,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); @@ -179,8 +183,8 @@ public class MyEntityExampleTestBase { assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY")); } - - @Test + + @Test public void testTransactionCommittedCallback() throws Exception { TransactionResultCallback callback = mock(TransactionResultCallback.class); builder.execute(new JpaUnitOfWork() { @@ -193,9 +197,9 @@ public class MyEntityExampleTestBase { verify(callback).status(TransactionResult.COMMIT); verifyNoMoreInteractions(callback); } - - @Test - public void testTransactionMarkedForRollback() throws Exception { + + @Test + public void testTransactionMarkedForRollback() throws Exception { TransactionResultCallback callback = mock(TransactionResultCallback.class); builder.execute(new JpaUnitOfWork() { @@ -209,9 +213,9 @@ public class MyEntityExampleTestBase { verify(callback).status(TransactionResult.ROLLBACK); verifyNoMoreInteractions(callback); } - - @Test - public void testTransactionRolledBack() throws Exception { + + @Test + public void testTransactionRolledBack() throws Exception { TransactionResultCallback callback = mock(TransactionResultCallback.class); builder.execute(new JpaUnitOfWork() { @@ -225,9 +229,9 @@ public class MyEntityExampleTestBase { verify(callback).status(TransactionResult.UNKNOWN); verifyNoMoreInteractions(callback); } - - @Test - public void testTransactionCommitted() throws Exception { + + @Test + public void testTransactionCommitted() throws Exception { TransactionResultCallback callback = mock(TransactionResultCallback.class); builder.execute(new JpaUnitOfWork() { @@ -241,6 +245,17 @@ public class MyEntityExampleTestBase { verify(callback).status(TransactionResult.UNKNOWN); verifyNoMoreInteractions(callback); } - - + + @Test(expected = AssertionFailedError.class) + public void testTransactionNotCommittedUnexpectedly() throws Exception { + builder.execute(new JpaUnitOfWork() { + public Void execute(EntityManager aEm) { + MyEntity entity = new MyEntity("a", "b"); + aEm.persist(entity); + aEm.getTransaction().rollback(); + return null; + } + }); + } + }