|
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.SpringTxTestRule pl.touk.ormtest.IbatisSpringTxTestRule
public class IbatisSpringTxTestRule
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();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:
@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")); } }
<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>And of course an ExampleEntity plain old java bean (POJO) with id and name properties would be needed for the above example to work.
<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>
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 |
---|
public IbatisSpringTxTestRule()
public IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig)
sqlMapConfig
- a Resource containing Ibatis configurationpublic IbatisSpringTxTestRule(String sqlMapConfigPath)
AntPathMatcher.isPattern(String)
returns true for this path)
then it is resolved by PathMatchingResourcePatternResolver.getResources(String)
.
Otherwise it is resolved by PathMatchingResourcePatternResolver.getResource(String)
.
sqlMapConfigPath
- a path pointing to an Ibatis configurationpublic IbatisSpringTxTestRule(String fileSystemSqlMapConfigPath, String ancestorDirectory, String h2CompatibilityMode)
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.
fileSystemSqlMapConfigPath
- a path pointing to an Ibatis configurationancestorDirectory
- file which is descendant of this directory will be used as Ibatis configurationh2CompatibilityMode
- H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)public IbatisSpringTxTestRule(org.springframework.core.io.Resource sqlMapConfig, String h2CompatibilityMode)
sqlMapConfig
- a Resource containing Ibatis configurationh2CompatibilityMode
- H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)public IbatisSpringTxTestRule(String sqlMapConfigPath, String h2CompatibilityMode)
AntPathMatcher.isPattern(String)
returns true for this path)
then it is resolved by PathMatchingResourcePatternResolver.getResources(String)
.
Otherwise it is resolved by PathMatchingResourcePatternResolver.getResource(String)
.
sqlMapConfigPath
- a path pointing to an Ibatis configurationh2CompatibilityMode
- H2 compatibility mode to be used (for example "Oracle", "MySQL" etc.)Method Detail |
---|
public static void setSqlMapConfig(Object... sqlMapConfigs)
public static Object[] getSqlMapConfig()
protected org.springframework.orm.ibatis.SqlMapClientFactoryBean sqlMapClientFactoryBean()
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.
dataSource
property is assigned the value returned by SpringTxTestRule.dataSource()
.configLocation
property is assigned the value "/sqlmap-config.xml".SqlMapClientFactoryBean.afterPropertiesSet()
is invoked.
SqlMapClientFactoryBean
protected com.ibatis.sqlmap.client.SqlMapClient sqlMapClient()
SqlMapClient
that will be used to create an
SqlMapClientTemplate
. The default implementation
returns SqlMapClient
created by invoking
sqlMapClientFactoryBean()
.getObject()
.
public org.springframework.orm.ibatis.SqlMapClientTemplate getSqlMapClientTemplate()
protected void ensureTemplateInitialized()
ensureTemplateInitialized
in class SpringTxTestRule
public static void resetThreadsForCurrentTestClass()
protected static void resetThreadsForCurrentTestClass(boolean hardReset)
|
TouK sp. z o.o. s.k.a. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |