org.wamblee.test.persistence
Class JpaBuilder

java.lang.Object
  extended by org.wamblee.test.persistence.JpaBuilder
All Implemented Interfaces:
TransactionResource<javax.persistence.EntityManager>

public class JpaBuilder
extends java.lang.Object
implements TransactionResource<javax.persistence.EntityManager>

Utility for building an appropriately configured EntityManagerFactory. The idea is that a persistence.xml is used unchanged from the production version. This utility will then add the additional properties required for execution in a standalone environment. The other purpose is to to shield dependencies of the test code on a particular JPA provider.


Nested Class Summary
static interface JpaBuilder.JpaUnitOfWork<T>
          Callback interface to execute some JPA code within a transaction with the entitymanager to use provided as input.
 
Constructor Summary
JpaBuilder(java.lang.String aUrl, java.lang.String aUser, java.lang.String aPassword, PersistenceUnitDescription aPersistenceUnit)
          Constructs the builder.
 
Method Summary
 javax.persistence.EntityManager begin()
          Begins a transaction.
 void commit(javax.persistence.EntityManager aEntityManager)
          Commits the transaction.
 javax.persistence.EntityManagerFactory createFactory()
          Creates a new entity manager factory.
<T> T
execute(JpaBuilder.JpaUnitOfWork<T> aWork)
          Executes a unit of work.
 javax.persistence.EntityManager getContextualEntityManager()
          Gets a contextual reference to an entity manager that delegates to the appropriate (current) one which is active for the current transaction.
 void rollback(javax.persistence.EntityManager aEntityManager)
          Rolls back a transaction.
 void start()
          Starts the builder, which in particular, mocks JNDI, binds the datasource the JNDI where the persistence unit expects it, creates the entity manager factory, and forces creation of the database schema.
 void stop()
          Stops the entity manager factory and disables JNDI mocking.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaBuilder

public JpaBuilder(java.lang.String aUrl,
                  java.lang.String aUser,
                  java.lang.String aPassword,
                  PersistenceUnitDescription aPersistenceUnit)
Constructs the builder.

Parameters:
aUrl - JDBC URL
aUser - User name
aPassword - Password.
aPersistenceUnit - Persistence unit.
Method Detail

start

public void start()
           throws java.lang.Exception
Starts the builder, which in particular, mocks JNDI, binds the datasource the JNDI where the persistence unit expects it, creates the entity manager factory, and forces creation of the database schema.

Throws:
java.lang.Exception

stop

public void stop()
Stops the entity manager factory and disables JNDI mocking.


createFactory

public javax.persistence.EntityManagerFactory createFactory()
Creates a new entity manager factory. Typically not used by test code.

Returns:
Entity manager factory.

execute

public <T> T execute(JpaBuilder.JpaUnitOfWork<T> aWork)
          throws java.lang.Exception
Executes a unit of work. This creates an entitymanager and runs the JpaBuilder.JpaUnitOfWork.execute(EntityManager) within a transaction, passing it the entity manager. Use of this method saves a lot of typing for applications.

Parameters:
aWork - Work to execute.
Returns:
The return value of the execute method of the unit of work.
Throws:
java.lang.Exception

begin

public javax.persistence.EntityManager begin()
Description copied from interface: TransactionResource
Begins a transaction.

Specified by:
begin in interface TransactionResource<javax.persistence.EntityManager>
Returns:
Object that manages the transaction for the resource.

commit

public void commit(javax.persistence.EntityManager aEntityManager)
Description copied from interface: TransactionResource
Commits the transaction.

Specified by:
commit in interface TransactionResource<javax.persistence.EntityManager>
Parameters:
aEntityManager - Object that manages the transaction for the resource.

rollback

public void rollback(javax.persistence.EntityManager aEntityManager)
Description copied from interface: TransactionResource
Rolls back a transaction.

Specified by:
rollback in interface TransactionResource<javax.persistence.EntityManager>
Parameters:
aEntityManager - Object that manages the transaction for the resource.

getContextualEntityManager

public javax.persistence.EntityManager getContextualEntityManager()
Gets a contextual reference to an entity manager that delegates to the appropriate (current) one which is active for the current transaction.

Returns:
EntityManager.


Copyright © 2010. All Rights Reserved.