TouK OpenSource Projects

pl.touk.ormtest
Class IbatisSpringTxTestRule

java.lang.Object
  extended by pl.touk.ormtest.SpringTxTestRule
      extended by pl.touk.ormtest.IbatisSpringTxTestRule
All Implemented Interfaces:
org.junit.rules.TestRule
Direct Known Subclasses:
MysqlIbatisSpringTxTestRule

public class IbatisSpringTxTestRule
extends SpringTxTestRule

Class for JUnit testing of Spring-based iBATIS DAOs.

By default, this class uses H2 in-memory database and searches sqlmap-config.xml file on the classpath to configure iBATIS.

Tests using this class are very fast because they don't load spring application context although they can be used to test spring DAOs.

This class is very simple to use. An example is presented below. Although the example doesn't use spring DAOs it would be very similar if it did. The spring DAOs, which extend Spring's SqlMapClientDaoSupport, need an SqlMapClientTemplate which is in fact referenced in the example below (bold fragment in method before).

 public class TransactionalTest {
    @Rule
    public IbatisSpringTxTestRule txContext = new IbatisSpringTxTestRule();
@Before public void prepareEnvironmentForEveryTest() { // Transaction (new for every test) has already been open. SimpleJdbcTestUtils.executeSqlScript( new SimpleJdbcTemplate(txContext.getSqlMapClientTemplate().getDataSource()), new ClassPathResource("some-script-creating-database.sql"), false); txContext.getSqlMapClientTemplate().insert("insert", new ExampleEntity(1, "some name")); }
@After public void cleanUpAfterEveryTest() { // Transaction for the last executed test has not yet been closed if it is needed. txContext.getSqlMapClientTemplate().insert("insert", new ExampleEntity(1, "some other name")); }
@Test public void shoudPersistEntityA() throws Exception { txContext.getSqlMapClientTemplate().insert("insert", new ExampleEntity(2, "name")); }
@Test public void shoudPersistEntityB() throws Exception { txContext.getSqlMapClientTemplate().insert("insert", new ExampleEntity(2, "name")); } }
In above example, if the two tests are executed in parallel then each of them will be executed on different, completely independent in-memory H2 databases. For the above example to work a file sqlmap-config.xml must be on the classpath. This file can look for example like this:
 <sqlMapConfig>
   <sqlMap resource="example-entity.xml"/>
 </sqlMapConfig>
 
The above sqlmap configuration references one sql map file, example-entity.xml, which can look for example like this:
 <sqlMap namespace="exampleEntity">
   <resultMap class="pl.touk.ormtest.ExampleEntity" id="exampleEntityResult">
     <result property="id" column="id" />
     <result property="name" column="name" />
   </resultMap>
<select id="selectAll" resultMap="exampleEntity.exampleEntityResult"> SELECT * FROM EXAMPLEENTITIES </select>
<select id="select" resultMap="exampleEntity.exampleEntityResult"> SELECT * FROM EXAMPLEENTITIES WHERE id = #id# </select>
<insert id="insert" parameterClass="pl.touk.ormtest.ExampleEntity"> INSERT INTO EXAMPLEENTITIES (name) VALUES (#name#) <selectKey keyProperty="id" resultClass="int"> SELECT LAST_INSERT_ID(); </selectKey> </insert> </sqlMap>
And of course an ExampleEntity plain old java bean (POJO) with id and name properties would be needed for the above example to work.

Author:
Michał Sokołowski

Field Summary
 
Fields inherited from class pl.touk.ormtest.SpringTxTestRule
threadsPerTestClass, txManagers, txStatuses
 
Constructor Summary
IbatisSpringTxTestRule()
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the default location i.e. from the classpath resource "/sqlmap-config.xml".
IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig)
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given Resource.
IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig, String h2CompatibilityMode)
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given Resource and sets the H2 compatibility mode to the provided one.
IbatisSpringTxTestRule(String sqlMapConfigPath)
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path.
IbatisSpringTxTestRule(String sqlMapConfigPath, String h2CompatibilityMode)
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path and sets the H2 compatibility mode to the provided one.
IbatisSpringTxTestRule(String fileSystemSqlMapConfigPath, String ancestorDirectory, String h2CompatibilityMode)
          Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path (fileSystemSqlMapConfigPath) that is a descendant of ancestorDirectory and sets the H2 compatibility mode to the provided one.
 
Method Summary
protected  void ensureTemplateInitialized()
           
 org.springframework.orm.ibatis.SqlMapClientTemplate getSqlMapClientTemplate()
           
static Object[] getSqlMapConfig()
           
static void resetThreadsForCurrentTestClass()
           
protected static void resetThreadsForCurrentTestClass(boolean hardReset)
           
static void setSqlMapConfig(Object... sqlMapConfigs)
           
protected  com.ibatis.sqlmap.client.SqlMapClient sqlMapClient()
          Can be overriden in subclasses and should return an SqlMapClient that will be used to create an SqlMapClientTemplate.
protected  org.springframework.orm.ibatis.SqlMapClientFactoryBean sqlMapClientFactoryBean()
          Can be overriden in subclasses and should return an SqlMapClientFactoryBean.
 
Methods inherited from class pl.touk.ormtest.SpringTxTestRule
apply, commitTransactionAndBeginNewOne, dataSource, findInvokingTestClass, getThreads, rollBackTransactionAndBeginNewOne
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IbatisSpringTxTestRule

public IbatisSpringTxTestRule()
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the default location i.e. from the classpath resource "/sqlmap-config.xml".


IbatisSpringTxTestRule

public IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig)
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given Resource.

Parameters:
sqlMapConfig - a Resource containing Ibatis configuration

IbatisSpringTxTestRule

public IbatisSpringTxTestRule(String sqlMapConfigPath)
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path. If the given path is an Ant pattern (i.e. AntPathMatcher.isPattern(String) returns true for this path) then it is resolved by PathMatchingResourcePatternResolver.getResources(String). Otherwise it is resolved by PathMatchingResourcePatternResolver.getResource(String).

Parameters:
sqlMapConfigPath - a path pointing to an Ibatis configuration

IbatisSpringTxTestRule

public IbatisSpringTxTestRule(String fileSystemSqlMapConfigPath,
                              String ancestorDirectory,
                              String h2CompatibilityMode)
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path (fileSystemSqlMapConfigPath) that is a descendant of ancestorDirectory and sets the H2 compatibility mode to the provided one.

If the given path is an Ant pattern (i.e. AntPathMatcher.isPattern(String) returns true for this path) then it is resolved by PathMatchingResourcePatternResolver.getResources(String). Otherwise it is resolved by PathMatchingResourcePatternResolver.getResource(String). After the above resolution only one resource should be a descendant of the given ancestorDirectory (here "descendant" means that absolute path of Resource.getFile() contains ancestorDirectory) and if this is the case it will be used as the Ibatis configuration. Otherwise a RuntimeException is thrown.

Parameters:
fileSystemSqlMapConfigPath - a path pointing to an Ibatis configuration
ancestorDirectory - file which is descendant of this directory will be used as Ibatis configuration
h2CompatibilityMode - H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)

IbatisSpringTxTestRule

public IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig,
                              String h2CompatibilityMode)
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given Resource and sets the H2 compatibility mode to the provided one.

Parameters:
sqlMapConfig - a Resource containing Ibatis configuration
h2CompatibilityMode - H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)

IbatisSpringTxTestRule

public IbatisSpringTxTestRule(String sqlMapConfigPath,
                              String h2CompatibilityMode)
Constructs an IbatisSpringTxTestRule that reads the Ibatis configuration from the given path and sets the H2 compatibility mode to the provided one. If the given path is an Ant pattern (i.e. AntPathMatcher.isPattern(String) returns true for this path) then it is resolved by PathMatchingResourcePatternResolver.getResources(String). Otherwise it is resolved by PathMatchingResourcePatternResolver.getResource(String).

Parameters:
sqlMapConfigPath - a path pointing to an Ibatis configuration
h2CompatibilityMode - H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)
Method Detail

setSqlMapConfig

public static void setSqlMapConfig(Object... sqlMapConfigs)

getSqlMapConfig

public static Object[] getSqlMapConfig()

sqlMapClientFactoryBean

protected org.springframework.orm.ibatis.SqlMapClientFactoryBean sqlMapClientFactoryBean()
Can be overriden in subclasses and should return an SqlMapClientFactoryBean. The returned factory bean is used in the default implementation of sqlMapClient(). The default implementation of this method returns an SqlMapClientFactoryBean initialized in the following manner.
  1. The dataSource property is assigned the value returned by SpringTxTestRule.dataSource().
  2. The configLocation property is assigned the value "/sqlmap-config.xml".
  3. The SqlMapClientFactoryBean.afterPropertiesSet() is invoked.

Returns:
an SqlMapClientFactoryBean

sqlMapClient

protected com.ibatis.sqlmap.client.SqlMapClient sqlMapClient()
Can be overriden in subclasses and should return an SqlMapClient that will be used to create an SqlMapClientTemplate. The default implementation returns SqlMapClient created by invoking sqlMapClientFactoryBean().getObject().

Returns:
sqlMapClient that will be used during tests

getSqlMapClientTemplate

public org.springframework.orm.ibatis.SqlMapClientTemplate getSqlMapClientTemplate()

ensureTemplateInitialized

protected void ensureTemplateInitialized()
Specified by:
ensureTemplateInitialized in class SpringTxTestRule

resetThreadsForCurrentTestClass

public static void resetThreadsForCurrentTestClass()

resetThreadsForCurrentTestClass

protected static void resetThreadsForCurrentTestClass(boolean hardReset)

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

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