|
TouK OpenSource Projects | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object pl.touk.ormtest.HibernateSpringTxTestRule
public class HibernateSpringTxTestRule
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();In above example, if the two tests are executed in parallel then each of them will be executed on a different in-memory database.
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")); } }
By default HibernateSpringTxTestRule
scans for entity classes so every
class marked with @Entity
will be available during tests.
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 |
---|
public HibernateSpringTxTestRule()
Method Detail |
---|
protected DataSource dataSource()
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).
protected Properties hibernateProperties()
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.
protected org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean annotationSessionFactoryBean()
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.
dataSource
property is assigned the value returned by dataSource()
.hibernateProperties
property is assigned the value returned by
hibernateProperties()
.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()
.afterPropertiesSet()
is invoked.
Can be overridden in subclasses.
AnnotationSessionFactoryBean
protected Class[] annotatedClasses()
annotationSessionFactoryBean()
.
The default implementation of this method returns null
.
Can be overridden in subclasses.
protected String packageWithAnnotatedClasses()
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.
protected org.hibernate.SessionFactory sessionFactory()
The default implementation returns the session factory created by invoking
annotationSessionFactoryBean()
.getObject()
.
Can be overridden in subclasses.
public void beginTransaction()
This method is idempotent - it will create only one transaction even if invoked more than once.
public void rollback()
This method can rollback the transaction started by this
rule. It can also rollback any transaction started manually through beginTransaction()
.
public void commit()
This method can commit the transaction started by this
rule. It can also commit any transaction started manually through beginTransaction()
.
public void flush()
Some Hibernate mapping errors can be detected only after a flush, i.e. after actual database operations are invoked.
public void close()
From now on it will be impossible to begin a new transaction within the current test.
public void rollbackAndClose()
public void commitAndClose()
public org.springframework.orm.hibernate3.HibernateTemplate getHibernateTemplate()
public org.hibernate.SessionFactory getSessionFactory()
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
|
TouK sp. z o.o. s.k.a. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |