From 9ed4408d10a124990e1b9cc6697872ee96c00c1d Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sat, 17 Jul 2010 21:21:46 +0000 Subject: [PATCH] Tested yet another way to manage transactions through the user transaction. --- .../persistence/MyEntityExampleTestBase.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) 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 7f196333..d8e80440 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 @@ -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")); + + } } -- 2.31.1