+ @Test
+ public void testEntityPersistenceWithBasicApi() throws Exception {
+
+ // Use the JPA builder to create a transaction scoped entity manager for
+ // as and execute the
+ // unit of work.
+ EntityManager em = builder.begin();
+
+ MyEntity entity = new MyEntity("a", "b");
+ em.persist(entity);
+
+ builder.commit(em);
+
+ // 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"));
+
+ }
+
+ @Test
+ 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();
+ MyEntity entity = new MyEntity("a", "b");
+ em.persist(entity);
+
+ builder.commit(em);
+
+ // 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"));
+
+ }
+
+ @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"));
+
+ }
+
+ @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);
+ }
+
+ @Test(expected = AssertionFailedError.class)
+ public void testTransactionNotCommittedUnexpectedly() throws Exception {
+ builder.execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ MyEntity entity = new MyEntity("a", "b");
+ aEm.persist(entity);
+ aEm.getTransaction().rollback();
+ return null;
+ }
+ });
+ }
+