Tested yet another way to manage transactions through the user transaction.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 17 Jul 2010 21:21:46 +0000 (21:21 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 17 Jul 2010 21:21:46 +0000 (21:21 +0000)
test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java

index 7f19633386378dae98c107d1bbdc687044c73bbe..d8e80440530cd495762fe227d86a8809cc6991cd 100644 (file)
@@ -18,16 +18,16 @@ package org.wamblee.test.persistence;
 import static junit.framework.Assert.*;
 
 import javax.persistence.EntityManager;
+import javax.transaction.UserTransaction;
 
 import org.dbunit.IDatabaseTester;
 import org.dbunit.dataset.ITable;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.wamblee.test.persistence.DatabaseUtils;
-import org.wamblee.test.persistence.JpaBuilder;
-import org.wamblee.test.persistence.JpaTester;
 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
+import org.wamblee.test.transactions.DefaultUserTransactionFactory;
+import org.wamblee.test.transactions.SimpleTransactionManager;
 
 /**
  * This class shows an example of how to test an entity using jpa.
@@ -150,4 +150,31 @@ public class MyEntityExampleTestBase {
         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
 
     }
+    
+    @Test
+    public void testEntityPersistenceWithUserTransactions() throws Exception {
+
+        SimpleTransactionManager manager = new SimpleTransactionManager(new DefaultUserTransactionFactory());
+        manager.addResource(jpaTester.getJpaBuilder());
+        
+        UserTransaction transaction = manager.getTransaction();
+        transaction.begin();
+
+        EntityManager em = builder.getContextualEntityManager(); 
+        MyEntity entity = new MyEntity("a", "b");
+        em.persist(entity);
+
+        transaction.commit();
+
+        // Verify one row is written (using Db unit)
+        ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
+        assertEquals(1, table.getRowCount());
+
+        assertEquals("a", table.getValue(0, "SLEUTELTJE"));
+        assertEquals("b", table.getValue(0, "VALUE"));
+
+        // For this simple test, it can also be done through DatabaseUtils
+        assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
+
+    }
 }