org.unitils.orm.common
Class OrmModule<ORM_PERSISTENCE_UNIT,ORM_PERSISTENCE_CONTEXT,PROVIDER_CONFIGURATION_OBJECT,PERSISTENCE_UNIT_CONFIG_ANNOTATION extends Annotation,ORM_CONFIG extends OrmConfig,ORM_PERSISTENCE_UNIT_CONFIG_LOADER extends ResourceConfigLoader<ORM_CONFIG>>

java.lang.Object
  extended by org.unitils.orm.common.OrmModule<ORM_PERSISTENCE_UNIT,ORM_PERSISTENCE_CONTEXT,PROVIDER_CONFIGURATION_OBJECT,PERSISTENCE_UNIT_CONFIG_ANNOTATION,ORM_CONFIG,ORM_PERSISTENCE_UNIT_CONFIG_LOADER>
Type Parameters:
ORM_PERSISTENCE_UNIT - Type of the ORM persistence unit
ORM_PERSISTENCE_CONTEXT - Type of the ORM persistence context
PROVIDER_CONFIGURATION_OBJECT - Type of the implementation specific configuration object
PERSISTENCE_UNIT_CONFIG_ANNOTATION - Type of the annotation used for configuring and injecting the persistence unit
ORM_CONFIG - Type of the value object extending OrmConfig that contains all unitils specific persitence unit configuration
ORM_PERSISTENCE_UNIT_CONFIG_LOADER - Subtype of OrmPersistenceUnitLoader that loads the persistence unit based on the ORM_CONFIG.
All Implemented Interfaces:
Module, Flushable
Direct Known Subclasses:
HibernateModule, JpaModule

public abstract class OrmModule<ORM_PERSISTENCE_UNIT,ORM_PERSISTENCE_CONTEXT,PROVIDER_CONFIGURATION_OBJECT,PERSISTENCE_UNIT_CONFIG_ANNOTATION extends Annotation,ORM_CONFIG extends OrmConfig,ORM_PERSISTENCE_UNIT_CONFIG_LOADER extends ResourceConfigLoader<ORM_CONFIG>>
extends Object
implements Module, Flushable

Base module defining common behavior for a module that provides object relational mapping support for tests. This abstract module takes into account loading and caching of persistence units. A persistence unit can be configured using unitils annotations or in a spring ApplicationContext. The persistence unit is injected into the test object's annotated fields. This module also supports flushing of the active persistence context.

Author:
Filip Neven, Tim Ducheyne

Nested Class Summary
protected  class OrmModule.OrmTestListener
          The TestListener for this module
 
Field Summary
protected  Map<ORM_CONFIG,ConfiguredOrmPersistenceUnit<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT>> configuredOrmPersistenceUnitCache
          Cache for persistence units and its configuration.
protected  OrmPersistenceUnitLoader<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT,ORM_CONFIG> ormPersistenceUnitLoader
          Class that loads the persistence unit, given an object extending OrmConfig
protected  OrmSpringSupport<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT> ormSpringSupport
          Support class that enables getting a configured persistence unit from a spring ApplicationContext configured in unitils.
protected  ORM_PERSISTENCE_UNIT_CONFIG_LOADER persistenceUnitConfigLoader
          Class that loads the persistence unit configuration
 
Constructor Summary
OrmModule()
           
 
Method Summary
 void afterInit()
          Gives the module the opportunity to performs initialization that can only work after all other modules have been initialized
protected abstract  ORM_PERSISTENCE_UNIT_CONFIG_LOADER createOrmConfigLoader()
           
protected abstract  OrmPersistenceUnitLoader<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT,ORM_CONFIG> createOrmPersistenceUnitLoader()
           
protected abstract  ORM_PERSISTENCE_CONTEXT doGetActivePersistenceContext(Object testObject)
          Implementations of this method must return the persistence context object that is associated with the current transaction, if any.
protected abstract  ORM_PERSISTENCE_CONTEXT doGetPersistenceContext(Object testObject)
          Implementations of this method must return a persistence context object, and must be associated with the current transaction active in unitils.
 void flushDatabaseUpdates(Object testObject)
          Flushes all pending updates to the database.
protected abstract  void flushOrmPersistenceContext(ORM_PERSISTENCE_CONTEXT activePersistenceContext)
          Flushes all pending update, using the given active persistence context
protected  ORM_PERSISTENCE_CONTEXT getActivePersistenceContext(Object testObject)
          The currently active persistence context, if any.
 PROVIDER_CONFIGURATION_OBJECT getConfigurationObject(Object testObject)
          Returns the ORM implementation specific configuration object.
protected  ConfiguredOrmPersistenceUnit<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT> getConfiguredPersistenceUnit(Object testObject)
          Returns a wrapper for the persistence unit and any implementation specific configuration object for the given test object.
protected  DatabaseModule getDatabaseModule()
           
protected abstract  String getOrmSpringSupportImplClassName()
           
 ORM_PERSISTENCE_CONTEXT getPersistenceContext(Object testObject)
          Returns an implementation specific persistence context, which is associated with the current transaction.
 ORM_PERSISTENCE_UNIT getPersistenceUnit(Object testObject)
          Returns a configured ORM persistence unit for the given test object.
protected abstract  Class<ORM_PERSISTENCE_UNIT> getPersistenceUnitClass()
           
protected  ORM_CONFIG getPersistenceUnitConfig(Object testObject)
           
protected abstract  Class<PERSISTENCE_UNIT_CONFIG_ANNOTATION> getPersistenceUnitConfigAnnotationClass()
           
 void init(Properties configuration)
          Initializes the module with the given configuration settings.
protected  void initOrmSpringSupport()
          Creates an instance of OrmSpringSupport, that implements the dependency to the SpringModule.
 void injectOrmPersistenceUnitIntoTestObject(Object testObject)
          Injects the persistence unit object into all fields and methods that are annotated with the annotation defined by getPersistenceUnitConfigAnnotationClass()
 boolean isPersistenceUnitConfiguredFor(Object testObject)
          Indicates whether an ORM persistence unit has been configured for the given testObject.
protected  boolean isSpringModuleEnabled()
          Verifies whether the SpringModule is enabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.unitils.core.Module
getTestListener
 

Field Detail

persistenceUnitConfigLoader

protected ORM_PERSISTENCE_UNIT_CONFIG_LOADER extends ResourceConfigLoader<ORM_CONFIG> persistenceUnitConfigLoader
Class that loads the persistence unit configuration


ormPersistenceUnitLoader

protected OrmPersistenceUnitLoader<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT,ORM_CONFIG extends OrmConfig> ormPersistenceUnitLoader
Class that loads the persistence unit, given an object extending OrmConfig


configuredOrmPersistenceUnitCache

protected Map<ORM_CONFIG extends OrmConfig,ConfiguredOrmPersistenceUnit<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT>> configuredOrmPersistenceUnitCache
Cache for persistence units and its configuration. We use this to make sure that for tests that use the same persistence unit configuration, the same persistence unit instance is reused


ormSpringSupport

protected OrmSpringSupport<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT> ormSpringSupport
Support class that enables getting a configured persistence unit from a spring ApplicationContext configured in unitils. If the spring module is not enabled, this object is null.

Constructor Detail

OrmModule

public OrmModule()
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
Parameters:
configuration - The config, 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

createOrmConfigLoader

protected abstract ORM_PERSISTENCE_UNIT_CONFIG_LOADER createOrmConfigLoader()
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 abstract Class<PERSISTENCE_UNIT_CONFIG_ANNOTATION> getPersistenceUnitConfigAnnotationClass()
Returns:
The class of the annotation that is used for configuring and requesting injection of the persistence unit

getPersistenceUnitClass

protected abstract Class<ORM_PERSISTENCE_UNIT> getPersistenceUnitClass()
Returns:
The type of the persistence unit

createOrmPersistenceUnitLoader

protected abstract OrmPersistenceUnitLoader<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT,ORM_CONFIG> createOrmPersistenceUnitLoader()
Returns:
A new instance of OrmPersistenceUnitLoader that can create a new persistence unit based on an OrmConfig object

getOrmSpringSupportImplClassName

protected abstract String getOrmSpringSupportImplClassName()
Returns:
The fully qualified classname of the concrete implementation of OrmSpringSupport that is used by the ORM module implementation

getPersistenceUnit

public ORM_PERSISTENCE_UNIT getPersistenceUnit(Object testObject)
Returns a configured ORM persistence unit for the given test object. This persistence unit can be either configured in a Spring ApplicationContext or by using the annotation that is applicable for the ORM implementation. An exception is thrown if no persistence unit is configured. If possible, a cached instance is returned that was created during a previous test.

Parameters:
testObject - The test instance, not null
Returns:
The ORM persistence unit, not null

getConfigurationObject

public PROVIDER_CONFIGURATION_OBJECT getConfigurationObject(Object testObject)
Returns the ORM implementation specific configuration object. This object cannot be used for configuration of a persistence units anymore, but may be useful for implementing some specific behavior, like the entity-database mapping test

Parameters:
testObject - The test instance, not null
Returns:
The ORM implementation specific configuration object

getConfiguredPersistenceUnit

protected ConfiguredOrmPersistenceUnit<ORM_PERSISTENCE_UNIT,PROVIDER_CONFIGURATION_OBJECT> getConfiguredPersistenceUnit(Object testObject)
Returns a wrapper for the persistence unit and any implementation specific configuration object for the given test object. This persistence unit can be either configured in a Spring ApplicationContext or by using the annotation that is applicable for the ORM implementation. An exception is thrown if no persistence unit is configured. If possible, a cached instance is returned that was created during a previous test.

Parameters:
testObject - The test instance, not null
Returns:
The persistence unit, not null

getPersistenceUnitConfig

protected ORM_CONFIG getPersistenceUnitConfig(Object testObject)
Parameters:
testObject - The test instance, not null
Returns:
The persistence unit configuration for this test class. Null if no configuration is available

isPersistenceUnitConfiguredFor

public boolean isPersistenceUnitConfiguredFor(Object testObject)
Indicates whether an ORM persistence unit has been configured for the given testObject. This persistence unit can be either configured in a Spring ApplicationContext or by using the annotation that is applicable for the ORM implementation.

Parameters:
testObject - The test instance, not null
Returns:
true if a EntityManagerFactory has been configured, false otherwise

getPersistenceContext

public ORM_PERSISTENCE_CONTEXT getPersistenceContext(Object testObject)
Returns an implementation specific persistence context, which is associated with the current transaction.

Parameters:
testObject - The test instance, not null
Returns:
A persistence context, not null

doGetPersistenceContext

protected abstract ORM_PERSISTENCE_CONTEXT doGetPersistenceContext(Object testObject)
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;

Parameters:
testObject - The test instance, not null
Returns:
An implementation specific persistence context, not null

getActivePersistenceContext

protected ORM_PERSISTENCE_CONTEXT getActivePersistenceContext(Object testObject)
The currently active persistence context, if any. This method will not create a new persistence context, it will only return something if a persistence context has previously been requested within the current transaction.

Parameters:
testObject - The test instance, not null
Returns:
The currently active persistence context, if any

doGetActivePersistenceContext

protected abstract ORM_PERSISTENCE_CONTEXT doGetActivePersistenceContext(Object testObject)
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.

Parameters:
testObject - The test instance, not null
Returns:
The currently active persistence context, if any

flushDatabaseUpdates

public void flushDatabaseUpdates(Object testObject)
Flushes all pending updates to the database. This method is useful when the effect of updates needs to be checked directly on the database, without going through the persistence unit

Specified by:
flushDatabaseUpdates in interface Flushable
Parameters:
testObject - The test instance, not null

flushOrmPersistenceContext

protected abstract void flushOrmPersistenceContext(ORM_PERSISTENCE_CONTEXT activePersistenceContext)
Flushes all pending update, using the given active persistence context

Parameters:
activePersistenceContext - Active persistence context, associated with the current transaction, not null

injectOrmPersistenceUnitIntoTestObject

public void injectOrmPersistenceUnitIntoTestObject(Object testObject)
Injects the persistence unit object into all fields and methods that are annotated with the annotation defined by getPersistenceUnitConfigAnnotationClass()

Parameters:
testObject - The test object, not null

initOrmSpringSupport

protected void initOrmSpringSupport()
Creates an instance of OrmSpringSupport, that implements the dependency to the SpringModule. If the SpringModule is not active, or if a dependency of OrmSpringSupport could not be found in the classpath, the instance is not loaded.


isSpringModuleEnabled

protected boolean isSpringModuleEnabled()
Verifies whether the SpringModule is enabled. If not, this means that either the property unitils.modules doesn't include spring, unitils.module.spring.enabled = false, or that the module could not be loaded because spring is not in the classpath.

Returns:
true if the SpringModule is enabled, false otherwise

getDatabaseModule

protected DatabaseModule getDatabaseModule()


Copyright © 2011. All Rights Reserved.