|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.unitils.orm.common.OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
org.unitils.orm.jpa.JpaModule
public class JpaModule
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.
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 |
---|
public static final String PROPKEY_PERSISTENCE_PROVIDER
protected JpaProviderSupport jpaProviderSupport
Constructor Detail |
---|
public JpaModule()
Method Detail |
---|
public void init(Properties configuration)
Module
init
in interface Module
init
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
configuration
- The Unitils configuration, not nullpublic void afterInit()
Module
afterInit
in interface Module
afterInit
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
protected JpaAnnotationConfigLoader createOrmConfigLoader()
createOrmConfigLoader
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
ResourceConfigLoader
that scans a test object for a persistence
unit configuration, and returns a specific subtype of OrmConfig
that wraps this configurationprotected Class<JpaEntityManagerFactory> getPersistenceUnitConfigAnnotationClass()
getPersistenceUnitConfigAnnotationClass
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
protected Class<javax.persistence.EntityManagerFactory> getPersistenceUnitClass()
getPersistenceUnitClass
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
protected OrmPersistenceUnitLoader<javax.persistence.EntityManagerFactory,Object,JpaConfig> createOrmPersistenceUnitLoader()
createOrmPersistenceUnitLoader
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
OrmPersistenceUnitLoader
that can create a new persistence unit
based on an OrmConfig
objectprotected String getOrmSpringSupportImplClassName()
getOrmSpringSupportImplClassName
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
OrmSpringSupport
that
is used by the ORM module implementationprotected javax.persistence.EntityManager doGetPersistenceContext(Object testObject)
OrmModule
doGetPersistenceContext
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
testObject
- The test instance, not null
protected javax.persistence.EntityManager doGetActivePersistenceContext(Object testObject)
OrmModule
doGetActivePersistenceContext
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
testObject
- The test instance, not null
protected void flushOrmPersistenceContext(javax.persistence.EntityManager activeEntityManager)
OrmModule
flushOrmPersistenceContext
in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
activeEntityManager
- Active persistence context, associated with the current transaction, not nullpublic void assertMappingWithDatabaseConsistent(Object testObject)
testObject
- The test instance, not nullpublic void injectJpaResourcesIntoTestObject(Object testObject)
EntityManagerFactory
and currently active
EntityManager
into fields or methods or the test object annotated with
javax.persistence.PersistenceUnit
or javax.persistence.PersistenceContext
,
respectively.
testObject
- The test instance, not nullpublic void injectJpaResourcesInto(Object testObject, Object target)
EntityManagerFactory
and currently active EntityManager
into fields
or methods of the given target object annotated with javax.persistence.PersistenceUnit
or
javax.persistence.PersistenceContext
, respectively.
testObject
- The test object, not nulltarget
- The target object to inject the resources into, not nullpublic void injectEntityManagerFactory(Object testObject, Object target)
EntityManagerFactory
into all fields and methods that are
annotated with javax.persistence.PersistenceUnit
testObject
- The test object, not nullpublic void injectEntityManager(Object testObject, Object target)
EntityManager
into all fields and methods that are
annotated with javax.persistence.PersistenceContext
testObject
- The test object, not nullprotected DataSource getDataSource()
public JpaProviderSupport getJpaProviderSupport()
public TestListener getTestListener()
Module
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |