TouK OpenSource Projects

pl.touk.ormtest
Class HibernateSpringTxTestRule

java.lang.Object
  extended by pl.touk.ormtest.HibernateSpringTxTestRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class HibernateSpringTxTestRule
extends Object
implements org.junit.rules.TestRule

Class for JUnit testing of Spring-based Hibernate DAOs. Such DAOs extend Spring's HibernateDaoSupport which in turn needs a HibernateTemplate. Such template can be obtained through getHibernateTemplate() like in the example below.

 public class ExampleTransactionalTest {
   @Rule
   public HibernateSpringTxTestRule txContext = new HibernateSpringTxTestRule();
private ExampleHibernateDao dao = new ExampleHibernateDao(txContext.getHibernateTemplate());
@Before public void before() { // Transaction (new for every test method) has already been open: dao.save(new ExampleEntity(2, "entity created in before()")); }
@After public void after() { // Transaction for the last executed test has not yet been closed - if it is needed: dao.save(new ExampleEntity(3, "entity created in after()")); }
@Test public void should_persist_entity() throws Exception { dao.save(new ExampleEntity(1, "name")); }
@Test public void should_persist_entity_too() throws Exception { dao.save(new ExampleEntity(1, "name")); } }
In above example, if the two tests are executed in parallel then each of them will be executed on a different in-memory database.

By default HibernateSpringTxTestRule scans for entity classes so every class marked with @Entity will be available during tests.

Author:
Michał Sokołowski

Constructor Summary
HibernateSpringTxTestRule()
           
 
Method Summary
protected  Class[] annotatedClasses()
          Returns an array of annotated classes to be used by Hibernate.
protected  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean annotationSessionFactoryBean()
          Returns an AnnotationSessionFactoryBean.
 org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
           
 void beginTransaction()
          Begins a new transaction.
 void close()
          Closes underlying Hibernate session.
 void commit()
          Commits the current transaction.
 void commitAndClose()
           
protected  DataSource dataSource()
          Returns a data source.
 void flush()
          Flashes the current Hibernate session.
 org.springframework.orm.hibernate3.HibernateTemplate getHibernateTemplate()
           
 org.hibernate.SessionFactory getSessionFactory()
           
protected  Properties hibernateProperties()
          Returns Hibernate properties.
protected  String packageWithAnnotatedClasses()
          Returns a package containing annotated classes to be processed by Hibernate.
 void rollback()
          Rollbacks the current transaction.
 void rollbackAndClose()
           
protected  org.hibernate.SessionFactory sessionFactory()
          Returns a session factory that will be used to create a Hibernate session before every JUnit or TestNG test.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateSpringTxTestRule

public HibernateSpringTxTestRule()
Method Detail

dataSource

protected DataSource dataSource()
Returns a data source. The returned data source is used in the default implementation of annotationSessionFactoryBean().

The default implementation of this method returns a data source for an in-memory HSQL database (with sid being "test" followed by the current thread's hash code, with user name sa and no password).

Returns:
data source to be used during tests

hibernateProperties

protected Properties hibernateProperties()
Returns Hibernate properties. Returned properties are used in the default implementation of annotationSessionFactoryBean().

The default implementation of this method returns following key-value pairs:

hibernate.connection.autocommit
false
hibernate.hbm2ddl.auto
create-drop (if dataSource() returns a data source which has an url property starting with jdbc:hsqldb:mem: or jdbc:h2:mem:)
validate (otherwise)

Can be overridden in subclasses.

Returns:
Hibernate properties to be used during tests

annotationSessionFactoryBean

protected org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean annotationSessionFactoryBean()
Returns an AnnotationSessionFactoryBean. The returned factory is used in the default implementation of sessionFactory().

The default implementation of this method returns an AnnotationSessionFactoryBean initialized in the following manner.

  1. The dataSource property is assigned the value returned by dataSource().
  2. The hibernateProperties property is assigned the value returned by hibernateProperties().
  3. If annotatedClasses() returns a non-null value than it is assigned to annotatedClasses property. Otherwise the packagesToScan property is assigned an one-element array containing the value returned by packageWithAnnotatedClasses().
  4. The afterPropertiesSet() is invoked.

Can be overridden in subclasses.

Returns:
an AnnotationSessionFactoryBean

annotatedClasses

protected Class[] annotatedClasses()
Returns an array of annotated classes to be used by Hibernate. The returned array is used by the default implementation of annotationSessionFactoryBean().

The default implementation of this method returns null.

Can be overridden in subclasses.

Returns:
annotated classes to be used by Hibernate

packageWithAnnotatedClasses

protected String packageWithAnnotatedClasses()
Returns a package containing annotated classes to be processed by Hibernate.

The default implementation returns an empty string which means that all packages will be scanned.

The returned package is used in the default implementation of annotationSessionFactoryBean() as a search location for annotated classes. The default implementation is suitable in most cases but also not optimal in most cases as annotated classes are probably located in some specific package.

Returns:
package containing annotated classes

sessionFactory

protected org.hibernate.SessionFactory sessionFactory()
Returns a session factory that will be used to create a Hibernate session before every JUnit or TestNG test.

The default implementation returns the session factory created by invoking annotationSessionFactoryBean().getObject().

Can be overridden in subclasses.

Returns:
session factory that will be used to create a Hibernate session before every JUnit or TestNG test

beginTransaction

public void beginTransaction()
Begins a new transaction.

This method is idempotent - it will create only one transaction even if invoked more than once.


rollback

public void rollback()
Rollbacks the current transaction.

This method can rollback the transaction started by this rule. It can also rollback any transaction started manually through beginTransaction().


commit

public void commit()
Commits the current transaction.

This method can commit the transaction started by this rule. It can also commit any transaction started manually through beginTransaction().


flush

public void flush()
Flashes the current Hibernate session.

Some Hibernate mapping errors can be detected only after a flush, i.e. after actual database operations are invoked.


close

public void close()
Closes underlying Hibernate session.

From now on it will be impossible to begin a new transaction within the current test.


rollbackAndClose

public void rollbackAndClose()

commitAndClose

public void commitAndClose()

getHibernateTemplate

public org.springframework.orm.hibernate3.HibernateTemplate getHibernateTemplate()

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()

apply

public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement,
                                               org.junit.runner.Description description)
Specified by:
apply in interface org.junit.rules.TestRule

TouK sp. z o.o. s.k.a.

Copyright © 2010–2015 TouK s.k.a.. All rights reserved.