Thank you for you repidly replies!
code snippets of build.xml
Code:
<project name="Gameshop" default="test" basedir=".">
<property name="webapp.dir" value="D:/tomcat/webapps/GameShop"/>
<property name="lib.dir" value="${webapp.dir}/WEB-INF/lib"/>
<property name="build.dir" value="${webapp.dir}/WEB-INF/classes"/>
<property name="src.dir" value="./src"/>
<property name="test.dir" value="./test"/>
<path id="classpath">
<pathelement location="${build.dir}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="classpathTest">
<fileset dir="d:/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="all" depends="isJUnitAvailable, test"/>
<target name="isJUnitAvailable">
<available property="junit.present" classname="junit.framework.TestCase"/>
</target>
<target name="clean">
<delete dir="${build.dir}">
<exclude name="**/*.properties"/>
<exclude name="**/*.xml"/>
</delete>
</target>
<target name="init" depends="clean">
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpathTest"/>
</target>
<target name="compileTester" depends="compile">
<javac srcdir="${test.dir}" destdir="${build.dir}" classpathref="classpathTest"/>
</target>
<target name="test" depends="compileTester">
<junit printsummary="true">
<classpath>
<path refid="classpath"/>
<path refid="classpathTest"/>
</classpath>
<formatter type="xml" usefile="true"/>
<test name="gameshop.framework.dao.BaseDAOTest"/>
<test name="gameshop.framework.dao.impl.BaseDAOIbatisImplTest"/>
</junit>
</target>
<!-- otherthings omitted -->
</project>
the whole Config File
Code:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>gameshop/framework/testmodel/Entity.hbm.xml</value>
<value>gameshop/framework/testmodel/EntityChild.hbm.xml</value>
</list>
</property>
</bean>
<bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="baseDAO"
class="org.springframework.transactinterceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="target">
<ref local="baseDAOHibernate"/>
</property>
<property name="transactionManager">
<ref local="myTransactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="baseDAOHibernate" class="gameshop.framework.dao.impl.BaseDAOHibernateImpl">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="baseDAOIbatis" class="gameshop.framework.dao.impl.BaseDAOIbatisImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sqlMapClient">
<ref local="sqlMap"/>
</property>
</bean>
<!-- dbunit和Ibatis需要用到下边的dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/Gameshop?useUnicode=true&characterEncoding=GBK</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>ibatis/SqlmapConfig.xml</value>
</property>
</bean>
</beans>
I load bean with my custom class: BeanLocator
Is theare any problem in this Class?
Code:
public class BeanLocator
{
public static ClassPathXmlApplicationContext context;
static
{
try
{
init();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private BeanLocator()
{
}
public static Object getBean(final String beanName)
{
return context.getBean(beanName);
}
private static void init()
{
String keyConfigFileName = "/spring/Context.xml";
String path = BeanLocator.class.getResource(keyConfigFileName).getPath();
File file = new File(path);
File dir = file.getParentFile();
String fileNames[] = dir.list();
for (int i = 0; i < fileNames.length; ++i)
{
fileNames[i] = "/spring/" + fileNames[i];
}
context = new ClassPathXmlApplicationContext(fileNames);
}
}
My test class:
Code:
public class BaseDAOTest extends DatabaseBaseTestCase
{
IBaseDAO baseDAO = (IBaseDAO) BeanLocator.getBean("baseDAO");
Entity entity = null;
Entity anotherEntity = null;
private static final String entityName = "entity";
private static final String anotherEntityName = "anotherEntity";
protected SessionFactory sessionFactory = null;
public BaseDAOTest(String s)
{
super(s);
}
protected void setUp() throws Exception
{
super.setUp();
emptyAllTables();
entity = new Entity();
entity.setId("1");
entity.setName(entityName);
anotherEntity = new Entity();
anotherEntity.setId("2");
anotherEntity.setName(anotherEntityName);
sessionFactory = (SessionFactory) BeanLocator.getBean("mySessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
protected void tearDown() throws Exception
{
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(sessionHolder.getSession(), sessionFactory);
super.tearDown();
}
public void testSave() throws SQLException, DataSetException, HibernateException
{
ITable table = connection.createQueryTable("ExceptedData", QUERY);
assertEquals(0, table.getRowCount());
baseDAO.save(entity);
table = connection.createQueryTable("ExceptedData", QUERY);
assertEquals(1, table.getRowCount());
}
}
Every thing goes well excpet that spring can't aware of my changes of the config files when I run the test in the build file with ant. It seems that spring has been slept in ant' build file!
In order to wake the spring up. All must I do is only to run such a dumb thing like this:
Code:
public static void main(String[] args)
{
IBaseDAO baseDAO = (IBaseDAO) BeanLocator.getBean("baseDAOIbatis");
}
And then the dear MR.spring wake up!The changes of the config files will be known by MR.spring.
Any suggestion ? more details?
Thanx in advance!
Bow
Yoshiyan