Hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了 Hibernate 的项目中如何保证测试的自动化和持续性呢?本文讨论了 Hibernate 加载其配置文件 hibernate.properties 和 hibernate.cfg.xml 的过程,以及怎么样将 hibernate 提供的配置文件的访问方法灵活运用到单元测试中。 注意:本文以 hibernate2.1 作为讨论的基础,不保证本文的观点适合于其他版本。
Java 开发人员,要求熟悉 JUnit 和掌握 Hibernate 的基础知识
对于 hibernate 的初学者来说,第一次使用 hibernate 的经验通常是:
1, 安装配置好 Hibernate,我们后面将 %HIBERNATE_HOME% 作为对 Hibernate 安装目录的引用,
2, 开始创建好自己的第一个例子,例如 hibernate 手册里面的类 Cat,
3, 配置好 hbm 映射文件(例如 Cat.hbm.xml,本文不讨论这个文件内配置项的含义)和数据库(如 hsqldb),
4, 在项目的 classpath 路径下添加一个 hibernate.cfg.xml 文件,如下 ( 第一次使用 hibernate 最常见的配置内容 ):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">false</property>
<mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
|
5, 然后还需要提供一个类来测试一下创建,更新,删除和查询 Cat,对于熟悉 JUnit 的开发人员,可以创建一个单元测试类来进行测试,如下:
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class CatTest extends TestCase {
private Session session;
private Transaction tx;
protected void setUp() throws Exception {
Configuration cfg = new Configuration().configure();//// 注意这一行,这是本文重点讨论研究的地方。
session = cfg.buildSessionFactory().openSession();
tx = session.beginTransaction();
}
protected void tearDown() throws Exception {
tx.commit();
session.close();
}
public void testCreate() {
// 请在此方法内添加相关的代码,本文不讨论怎么样使用 Hibernate API。
}
public void testUpdate() {
// 请在此方法内添加相关的代码,本文不讨论怎么样使用 Hibernate API。
}
public void testDelete() {
// 请在此方法内添加相关的代码,本文不讨论怎么样使用 Hibernate API。
}
public void testQuery() {
// 请在此方法内添加相关的代码,本文不讨论怎么样使用 Hibernate API。
}
}
|
3.2 new Configuration() 都做了什么?
对于第一次使用 hibernate 的新手来说,下面的这段代码可以说是最常见的使用 Configuration 方式。
Configuration cfg = new Configuration().configure(); |
Configuration 是 hibernate 的入口,在新建一个 Configuration 的实例的时候,hibernate 会在 classpath 里面查找 hibernate.properties 文件,如果该文件存在,则将该文件的内容加载到一个 Properties 的实例 GLOBAL_PROPERTIES 里面,如果不存在,将打印信息
hibernate.properties not found |
然后是将所有系统环境变量(System.getProperties())也添加到 GLOBAL_PROPERTIES 里面( 注 1)。如果 hibernate.properties 文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印警告信息。
new Configuration() 讨论至此,下面讨论 configure() 方法。
configure() 方法默认会在 classpath 下面寻找 hibernate.cfg.xml 文件,如果没有找到该文件,系统会打印如下信息并抛出 HibernateException 异常。
hibernate.cfg.xml not found |
如果找到该文件,configure() 方法会首先访问 < session-factory >,并获取该元素的 name 属性,如果非空,将用这个配置的值来覆盖 hibernate.properties 的 hibernate.session_factory_name 的配置的值,从这里我们可以看出,hibernate.cfg.xml 里面的配置信息可以覆盖 hibernate.properties 的配置信息。
接着 configure() 方法访问 <session-factory> 的子元素,首先将使用所有的 <property> 元素配置的信息( 注 2),如前面我们使用的配置文件
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
|
会覆盖 hibernate.properties 里面对应的配置,hibernate2.1 发布包里面自带的 hibernate.properties 文件(位于 %HIBERNATE_HOME%/etc 下面)里面的值,如下:
hibernate.dialect net.sf.hibernate.dialect.HSQLDialect hibernate.connection.driver_class org.hsqldb.jdbcDriver hibernate.connection.username sa hibernate.connection.password hibernate.connection.url jdbc:hsqldb:hsql://localhost |
然后 configure() 会顺序访问以下几个元素的内容
<mapping> <jcs-class-cache> <jcs-collection-cache> <collection-cache> |
其中 <mapping> 是必不可少的,必须通过配置 <mapping>,configure() 才能访问到我们定义的 java 对象和关系数据库表的映射文件(hbm.xml),例如:
<mapping resource="Cat.hbm.xml"/> |
通过以上的分析,我们对 hibernate 配置文件 hibernate.properties 和 hibernate.cfg.xml 的默认的加载过程就比较清楚了。
Configuration 的 configure () 方法还支持带参数的访问方式,你可以指定 hbm.xml 文件的位置,而不是使用默认的 classpath 下面的 hibernate.cfg.xml 这种方式,例如:
Configuration cfg = new Configuration().configure("myexample.xml");
|
同时 Configuration 还提供了一系列方法用来定制 hibernate 的加载配置文件的过程,让你的应用更加灵活,常用的是以下几种:
addProperties(Element) addProperties(Properties) setProperties(Properties) setProperty(String, String) |
通过以上几个方法,除了使用默认的 hibernate.properties 文件,你还可以提供多个 .properties 配置文件,使用 Hibernate 的时候根据不同的情况使用不同的配置文件,例如:
Properties properties = Properties.load("my.properties");
Configuration config = new Configuration().setProperties(properties).configure();
|
除了指定 .properties 文件之外,还可以指定 .hbm.xml 文件,下面列出几个常用的方法:
addClass(Class) addFile(File) addFile(String) addURL(URL) |
前面我们已经讲了,configure() 方法默认是通过访问 hibernate.cfg.xml 的 <mapping> 元素来加载我们提供的 .hbm.xml 文件,上面列出的方法可以直接指定 hbm.xml 文件,例如 addClass() 方法可以直接通过指定 class 来加载对应的映射文件,hibernate 会将提供的 class 的全名(包括 package)自动转化为文件路径,如 net.sf.hibernate.examples.quickstart.Cat.class 对应了 net/sf/hibernate/examples/quickstart/Cat.hbm.xml,还可以用 addFile 方法直接指定映射文件。
例一:
Configuration config = new Configuration().addClass(Cat.class); |
例二:
Configuration config =
new Configuration().addURL(Configuration.class.getResource ("Cat.hbm.xml"));
|
例三:
Configuration config = new Configuration().addFile("Cat.hbm.xml");
|
Configuration 提供的这些方法的好处如下:
1 . 一个应用中往往有很多 .hbm.xml 映射文件,开发的过程中如果只是为了测试某个或几个 Java PO(Persistence Object),我们没有必要把所有的 .hbm.xml 都加载到内存,这样可以通过 addClass 或者 addFile 直接,显得非常灵活。
2 . 学习 Hibernate 的过程中,往往需要通过练习来体会 Hibernate 提供的各种特征,而很多特征是需要修改配置文件的,如果要观察相同的代码在不同的特征下的表现,就需要手工改配置文件,这样太麻烦了,而且容易出错,我们可以提供多个配置文件,每个配置文件针对需要的特征而配置,这样我们在调用程序的时候,把不同的配置文件作为参数传递进去,而程序代码里面使用 setProperties 和 addFile 指定传入的配置文件参数就可以了。
3 . 在单元测试中,特别是在集成测试里面,整个过程是自动化的,我们不能手工干预测试过程,往往需要准备多个配置文件针对不同的测试案例,这个时候 setProperties 和 addFile 方法就显得特别有用了,在不同的测试案例中用这些方法来指定相应的配置文件,这样就可以做到自动化测试,保证了持续性。
在刚开始学习 hibernate 的时候,对于 hibernate 的 hbm 映射文件里的各种配置参数没有一个感性的认识,例如 inverse="true",lazy="true"这样的配置参数,不通过实践是无法体会到其作用的,传统的方法就是每需要测试一种参数的效果就更改相应的配置文件,然后运行测试来观察结果,如果能够灵活的运用 Configuration 提供的定制配置的方法,我们可以提供多个配置文件,每个配置文件里面有不同的配置参数,配合相应的测试案例就方便多了。
例如针对 ono-to-many 和 many-to-one 的双向关联的映射关系,我们想测试在 one-to-many 一方使用 inverse="false"和 inverse="true"的不同效果,假设已经正确的配置好了 hibernate.properties,那么还需要提供两个不同的 hbm.xml 文件,假设分别名为 bidirect.inverse.false.hbm.xml 和 bidirect.inverse.true.hbm.xml。
然后需要写两个不同的测试案例,分别针对两个不同的配置文件进行测试就可以了,这样的好处是,不用针对不同的测试案例修改配置文件,特别是在集成测试的时候,一切都是自动化的,如果每测试一个案例就需要手工去更改配置文件,这肯定是一个失败的测试。
代码模板如下:
FalseInverseTest.java 文件
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
/**
* test false inverse
*/
public class FalseInverseTest extends TestCase {
private Session session;
private Transaction tx;
protected void setUp() throws Exception {
Configuration cfg = new Configuration().addFile("bidirect.inverse.false.hbm.xml");
session = cfg.buildSessionFactory().openSession();
tx = session.beginTransaction();
}
protected void tearDown() throws Exception {
tx.commit();
session.close();
}
public void testLogic() {
// 在此编写测试代码
}
}
|
TrueInverseTest.java 文件
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
/**
* test true inverse
*/
public class TrueInverseTest extends TestCase {
private Session session;
private Transaction tx;
protected void setUp() throws Exception {
Configuration cfg = new Configuration().addFile("bidirect.inverse.true.hbm.xml");
session = cfg.buildSessionFactory().openSession();
tx = session.beginTransaction();
}
protected void tearDown() throws Exception {
tx.commit();
session.close();
}
public void testLogic() {
// 在此编写测试代码
}
}
|
通过对 Hibernate 默认的配置文件的加载顺序和 Hibernate 提供的加载配置文件的方法的讨论,我们对在使用到 Hibernate 的项目的单元测试中使用多个 Hibernate 配置文件有了比较清楚的认识。
持续集成中的测试的特征是自动化和持续性,不能手工干预其过程,在使用到 Hibernate 的项目如果要实现持续集成,就要为不同的测试案例提供不同的配置文件,而不是针对不同的测试案例进行手工调整,因此,在使用到 Hibernate 的项目中灵活的运用多配置文件,可以提高测试的效率,保证自动化和持续性。
注 1:有关的代码请参考 Environment 类的 static{}。
注 2:如果在 hibernate.cfg.xml 的 <property/> 配置的 name 没有以 hibernate 开头,那么 configure() 内部会自动在前面添加 hibernate,例如 connection.url,hibernate 会自动将其转化为 hibernate.connection.url。
- 官方 hibernate reference 地址:
http://hibernate.org
张辰雪,主要从事于系统分析和架构设计,擅长J2EE和各类设计模式,热衷开源项目,有4年的JAVA经验。 联系方式: melthaw@gmail.com