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.
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"));
+
+ }
}