From 59b7e714a1ccf08b603bab171d122a0b41505164 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sat, 17 Jul 2010 20:53:53 +0000 Subject: [PATCH] --- .../wamblee/test/persistence/JpaBuilder.java | 79 ++++++++++++++----- .../persistence/MyEntityExampleTestBase.java | 52 +++++++++++- 2 files changed, 111 insertions(+), 20 deletions(-) diff --git a/test/enterprise/src/main/java/org/wamblee/test/persistence/JpaBuilder.java b/test/enterprise/src/main/java/org/wamblee/test/persistence/JpaBuilder.java index 331ea9a1..dbc88dc0 100644 --- a/test/enterprise/src/main/java/org/wamblee/test/persistence/JpaBuilder.java +++ b/test/enterprise/src/main/java/org/wamblee/test/persistence/JpaBuilder.java @@ -26,7 +26,9 @@ import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.PersistenceException; +import org.wamblee.general.ThreadSpecificProxyFactory; import org.wamblee.test.jndi.StubInitialContextFactory; +import org.wamblee.test.transactions.TransactionResource; /** * Utility for building an appropriately configured EntityManagerFactory. The @@ -37,7 +39,7 @@ import org.wamblee.test.jndi.StubInitialContextFactory; * The other purpose is to to shield dependencies of the test code on a * particular JPA provider. */ -public class JpaBuilder { +public class JpaBuilder implements TransactionResource { private static final Logger LOGGER = Logger.getLogger(JpaBuilder.class .getName()); @@ -60,26 +62,31 @@ public class JpaBuilder { } private PersistenceUnitDescription persistenceUnit; - private String url; - private String user; - private String password; + private String url; + private String user; + private String password; private EntityManagerFactory factory; - + private ThreadSpecificProxyFactory entityManager; + /** * Constructs the builder. * - * @param aUrl JDBC URL - * @param aUser User name - * @param aPassword Password. + * @param aUrl + * JDBC URL + * @param aUser + * User name + * @param aPassword + * Password. * @param aPersistenceUnit * Persistence unit. */ public JpaBuilder(String aUrl, String aUser, String aPassword, PersistenceUnitDescription aPersistenceUnit) { persistenceUnit = aPersistenceUnit; - url = aUrl; - user = aUser; - password = aPassword; + url = aUrl; + user = aUser; + password = aPassword; + entityManager = new ThreadSpecificProxyFactory(EntityManager.class); } /** @@ -117,7 +124,7 @@ public class JpaBuilder { */ public EntityManagerFactory createFactory() { Map jpaProps = new TreeMap(); - + jpaProps.put("javax.persistence.jtaDataSource", null); jpaProps.put("javax.persistence.transactionType", "RESOURCE_LOCAL"); jpaProps.put("javax.persistence.jdbc.url", url); @@ -126,7 +133,7 @@ public class JpaBuilder { JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit, jpaProps); - + // jpaProps.put("javax.persistence.provider", // HibernatePersistence.class.getName()); EntityManagerFactory emf = Persistence.createEntityManagerFactory( @@ -147,19 +154,53 @@ public class JpaBuilder { * @return The return value of the execute method of the unit of work. */ public T execute(JpaUnitOfWork aWork) throws Exception { - EntityManager em = factory.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); + EntityManager em = begin(); try { T value = aWork.execute(em); - transaction.commit(); + commit(em); return value; } catch (Exception e) { LOGGER.log(Level.WARNING, "Exception occured", e); - transaction.rollback(); + rollback(em); throw e; + } + } + + @Override + public EntityManager begin() { + EntityManager em = factory.createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + transaction.begin(); + entityManager.set(em); + return em; + } + + @Override + public void commit(EntityManager aEntityManager) { + try { + aEntityManager.getTransaction().commit(); } finally { - em.close(); + aEntityManager.close(); + entityManager.set(null); } } + + @Override + public void rollback(EntityManager aEntityManager) { + try { + aEntityManager.getTransaction().rollback(); + } finally { + aEntityManager.close(); + entityManager.set(null); + } + } + + /** + * Gets a contextual reference to an entity manager that delegates to the + * appropriate (current) one which is active for the current transaction. + * @return EntityManager. + */ + public EntityManager getContextualEntityManager() { + return entityManager.getProxy(); + } } 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 737455ce..7f196333 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 @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.test.persistence; import static junit.framework.Assert.*; @@ -100,4 +100,54 @@ public class MyEntityExampleTestBase { } + @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")); + + } } -- 2.31.1