public class JpaModule extends OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>
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.| Modifier and Type | Class and Description |
|---|---|
protected class |
JpaModule.JpaTestListener
The
TestListener for this module |
OrmModule.OrmTestListener| Modifier and Type | Field and Description |
|---|---|
protected JpaProviderSupport |
jpaProviderSupport
Implements provider specific operations
|
static String |
PROPKEY_PERSISTENCE_PROVIDER |
configuredOrmPersistenceUnitCache, databaseName, ormPersistenceUnitLoader, ormSpringSupport, persistenceUnitConfigLoader, wrappers| Constructor and Description |
|---|
JpaModule()
Constructor for JpaModule.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterInit() |
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 void |
getDatabaseName(Object testObject,
Method testMethod) |
protected DataSource |
getDataSource() |
JpaProviderSupport |
getJpaProviderSupport() |
protected String |
getOrmSpringSupportImplClassName() |
protected Class<javax.persistence.EntityManagerFactory> |
getPersistenceUnitClass() |
protected Class<JpaEntityManagerFactory> |
getPersistenceUnitConfigAnnotationClass() |
TestListener |
getTestListener() |
void |
init(Properties configuration) |
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. |
void |
registerTransactionManagementConfiguration() |
flushDatabaseUpdates, getActivePersistenceContext, getConfigurationObject, getConfiguredPersistenceUnit, getDatabaseModule, getPersistenceContext, getPersistenceUnit, getPersistenceUnitConfig, initOrmSpringSupport, injectOrmPersistenceUnitIntoTestObject, isPersistenceUnitConfiguredFor, isSpringModuleEnabledpublic static final String PROPKEY_PERSISTENCE_PROVIDER
protected JpaProviderSupport jpaProviderSupport
public void init(Properties configuration)
init in interface Moduleinit in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>configuration - The Unitils configuration, not nullpublic void afterInit()
afterInit in interface ModuleafterInit in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>public void registerTransactionManagementConfiguration()
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)
OrmModuledoGetPersistenceContext in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>testObject - The test instance, not nullprotected javax.persistence.EntityManager doGetActivePersistenceContext(Object testObject)
OrmModuledoGetActivePersistenceContext in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>testObject - The test instance, not nullprotected void flushOrmPersistenceContext(javax.persistence.EntityManager activeEntityManager)
OrmModuleflushOrmPersistenceContext 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.PersistenceUnittestObject - The test object, not nullpublic void injectEntityManager(Object testObject, Object target)
EntityManager into all fields and methods that are
annotated with javax.persistence.PersistenceContexttestObject - The test object, not nullprotected DataSource getDataSource()
public JpaProviderSupport getJpaProviderSupport()
public TestListener getTestListener()
protected void getDatabaseName(Object testObject, Method testMethod)
getDatabaseName in class OrmModule<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager,Object,JpaEntityManagerFactory,JpaConfig,JpaAnnotationConfigLoader>OrmModule.getDatabaseName(java.lang.Object, java.lang.reflect.Method)Copyright © 2016. All Rights Reserved.