org.unitils.orm.jpa
Class JpaModule

java.lang.Object
  extended by org.unitils.orm.common.OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
      extended by org.unitils.orm.jpa.JpaModule
All Implemented Interfaces:
Module, Flushable

public class JpaModule
extends OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>

Module providing support for unit tests for code that uses JPA. It offers an easy way of loading a EntityManagerFactory and having it injected into the test. The configured EntityManagerFactory will connect to the unitils configured test datasource, and join in unitils test-bound transactions.

EntityManagerFactory instances are cached, to make sure that for any two tests that share the same configuration, the same instance of this object is used.

An EntityManagerFactory is injected into all fields or methods of the test object annotated with JpaEntityManagerFactory or javax.persistence.PersistenceUnit. An EntityManager is injected into all fields or methods of the test annotated with javax.persistence.PersistenceContext. todo injection into other objects

This module also offers a test to check whether the mapping of all entities is consistent with the structure of the database. It is highly recommended to write a unit test that invokes JpaUnitils.assertMappingWithDatabaseConsistent(), This is a very useful test that verifies whether the mapping of all your objects entities still corresponds with the actual structure of the database. Currently, it's only available when the persistence provider is hibernate.

Author:
Filip Neven, Tim Ducheyne

Nested Class Summary
protected  class JpaModule.JpaTestListener
          The TestListener for this module
 
Nested classes/interfaces inherited from class org.unitils.orm.common.OrmModule
OrmModule.OrmTestListener
 
Field Summary
protected  JpaProviderSupport jpaProviderSupport
          Implements provider specific operations
static String PROPKEY_PERSISTENCE_PROVIDER
           
 
Fields inherited from class org.unitils.orm.common.OrmModule
configuredOrmPersistenceUnitCache, ormPersistenceUnitLoader, ormSpringSupport, persistenceUnitConfigLoader
 
Constructor Summary
JpaModule()
          Constructor for JpaModule.
 
Method Summary
 void afterInit()
          Gives the module the opportunity to performs initialization that can only work after all other modules have been initialized
 void assertMappingWithDatabaseConsistent(Object testObject)
          Checks if the mapping of the managed objects with the database is still correct.
protected  JpaAnnotationConfigLoader createOrmConfigLoader()
           
protected  OrmPersistenceUnitLoader<javax.persistence.EntityManagerFactory,Object,JpaConfig> createOrmPersistenceUnitLoader()
           
protected  javax.persistence.EntityManager doGetActivePersistenceContext(Object testObject)
          Implementations of this method must return the persistence context object that is associated with the current transaction, if any.
protected  javax.persistence.EntityManager doGetPersistenceContext(Object testObject)
          Implementations of this method must return a persistence context object, and must be associated with the current transaction active in unitils.
protected  void flushOrmPersistenceContext(javax.persistence.EntityManager activeEntityManager)
          Flushes all pending update, using the given active persistence context
protected  DataSource getDataSource()
           
 JpaProviderSupport getJpaProviderSupport()
           
protected  String getOrmSpringSupportImplClassName()
           
protected  Class<javax.persistence.EntityManagerFactory> getPersistenceUnitClass()
           
protected  Class<JpaEntityManagerFactory> getPersistenceUnitConfigAnnotationClass()
           
 TestListener getTestListener()
          Creates the test listener for this module.
 void init(Properties configuration)
          Initializes the module with the given configuration settings.
 void injectEntityManager(Object testObject, Object target)
          Injects the currently active JPA EntityManager into all fields and methods that are annotated with javax.persistence.PersistenceContext
 void injectEntityManagerFactory(Object testObject, Object target)
          Injects the JPA EntityManagerFactory into all fields and methods that are annotated with javax.persistence.PersistenceUnit
 void injectJpaResourcesInto(Object testObject, Object target)
          Injects the EntityManagerFactory and currently active EntityManager into fields or methods of the given target object annotated with javax.persistence.PersistenceUnit or javax.persistence.PersistenceContext, respectively.
 void injectJpaResourcesIntoTestObject(Object testObject)
          Injects the currently configured EntityManagerFactory and currently active EntityManager into fields or methods or the test object annotated with javax.persistence.PersistenceUnit or javax.persistence.PersistenceContext, respectively.
 
Methods inherited from class org.unitils.orm.common.OrmModule
flushDatabaseUpdates, getActivePersistenceContext, getConfigurationObject, getConfiguredPersistenceUnit, getDatabaseModule, getPersistenceContext, getPersistenceUnit, getPersistenceUnitConfig, initOrmSpringSupport, injectOrmPersistenceUnitIntoTestObject, isPersistenceUnitConfiguredFor, isSpringModuleEnabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_PERSISTENCE_PROVIDER

public static final String PROPKEY_PERSISTENCE_PROVIDER
See Also:
Constant Field Values

jpaProviderSupport

protected JpaProviderSupport jpaProviderSupport
Implements provider specific operations

Constructor Detail

JpaModule

public JpaModule()
Constructor for JpaModule.

Method Detail

init

public void init(Properties configuration)
Description copied from interface: Module
Initializes the module with the given configuration settings.

Specified by:
init in interface Module
Overrides:
init in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Parameters:
configuration - The Unitils configuration, not null

afterInit

public void afterInit()
Description copied from interface: Module
Gives the module the opportunity to performs initialization that can only work after all other modules have been initialized

Specified by:
afterInit in interface Module
Overrides:
afterInit in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>

createOrmConfigLoader

protected JpaAnnotationConfigLoader createOrmConfigLoader()
Specified by:
createOrmConfigLoader in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Returns:
A new instance of the ResourceConfigLoader that scans a test object for a persistence unit configuration, and returns a specific subtype of OrmConfig that wraps this configuration

getPersistenceUnitConfigAnnotationClass

protected Class<JpaEntityManagerFactory> getPersistenceUnitConfigAnnotationClass()
Specified by:
getPersistenceUnitConfigAnnotationClass in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Returns:
The class of the annotation that is used for configuring and requesting injection of the persistence unit

getPersistenceUnitClass

protected Class<javax.persistence.EntityManagerFactory> getPersistenceUnitClass()
Specified by:
getPersistenceUnitClass in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Returns:
The type of the persistence unit

createOrmPersistenceUnitLoader

protected OrmPersistenceUnitLoader<javax.persistence.EntityManagerFactory,Object,JpaConfig> createOrmPersistenceUnitLoader()
Specified by:
createOrmPersistenceUnitLoader in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Returns:
A new instance of OrmPersistenceUnitLoader that can create a new persistence unit based on an OrmConfig object

getOrmSpringSupportImplClassName

protected String getOrmSpringSupportImplClassName()
Specified by:
getOrmSpringSupportImplClassName in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Returns:
The fully qualified classname of the concrete implementation of OrmSpringSupport that is used by the ORM module implementation

doGetPersistenceContext

protected javax.persistence.EntityManager doGetPersistenceContext(Object testObject)
Description copied from class: OrmModule
Implementations of this method must return a persistence context object, and must be associated with the current transaction active in unitils. The implementation can presume that a persistence unit is available, so the method may not return null;

Specified by:
doGetPersistenceContext in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Parameters:
testObject - The test instance, not null
Returns:
An implementation specific persistence context, not null

doGetActivePersistenceContext

protected javax.persistence.EntityManager doGetActivePersistenceContext(Object testObject)
Description copied from class: OrmModule
Implementations of this method must return the persistence context object that is associated with the current transaction, if any. The implementation can presume that a persistence unit is available. If no persistence context is currently active, null is returned.

Specified by:
doGetActivePersistenceContext in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Parameters:
testObject - The test instance, not null
Returns:
The currently active persistence context, if any

flushOrmPersistenceContext

protected void flushOrmPersistenceContext(javax.persistence.EntityManager activeEntityManager)
Description copied from class: OrmModule
Flushes all pending update, using the given active persistence context

Specified by:
flushOrmPersistenceContext in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
Parameters:
activeEntityManager - Active persistence context, associated with the current transaction, not null

assertMappingWithDatabaseConsistent

public void assertMappingWithDatabaseConsistent(Object testObject)
Checks if the mapping of the managed objects with the database is still correct.

Parameters:
testObject - The test instance, not null

injectJpaResourcesIntoTestObject

public void injectJpaResourcesIntoTestObject(Object testObject)
Injects the currently configured EntityManagerFactory and currently active EntityManager into fields or methods or the test object annotated with javax.persistence.PersistenceUnit or javax.persistence.PersistenceContext, respectively.

Parameters:
testObject - The test instance, not null

injectJpaResourcesInto

public void injectJpaResourcesInto(Object testObject,
                                   Object target)
Injects the EntityManagerFactory and currently active EntityManager into fields or methods of the given target object annotated with javax.persistence.PersistenceUnit or javax.persistence.PersistenceContext, respectively.

Parameters:
testObject - The test object, not null
target - The target object to inject the resources into, not null

injectEntityManagerFactory

public void injectEntityManagerFactory(Object testObject,
                                       Object target)
Injects the JPA EntityManagerFactory into all fields and methods that are annotated with javax.persistence.PersistenceUnit

Parameters:
testObject - The test object, not null

injectEntityManager

public void injectEntityManager(Object testObject,
                                Object target)
Injects the currently active JPA EntityManager into all fields and methods that are annotated with javax.persistence.PersistenceContext

Parameters:
testObject - The test object, not null

getDataSource

protected DataSource getDataSource()

getJpaProviderSupport

public JpaProviderSupport getJpaProviderSupport()

getTestListener

public TestListener getTestListener()
Description copied from interface: Module
Creates the test listener for this module.

Returns:
The TestListener associated with this module


Copyright © 2011. All Rights Reserved.