首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室软件测试频道
Google
首页 资讯动态 测试技术 测试工具 行业软件测试 测试管理 测试下载 经验分享 软件质量 其他技术 RSS订阅 博客 论坛
您现在的位置: 中国IT实验室 >> 软件测试 >> 测试技术 >> 单元测试 >> 正文

Hibernate配置文件在单元测试中的应用

介绍

  Hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了Hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了Hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将hibernate提供的配置文件的访问方法灵活运用到单元测试中。注意:本文以hibernate2.1作为讨论的基础,不保证本文的观点适合于其他版本。

  读者 
 
  Java开发人员,要求熟悉JUnit和掌握Hibernate的基础知识

  内容

  1.准备

  对于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。
 }
}

  2、new Configuration()都做了什么?

[NextPage]

  对于第一次使用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文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印警告信息。

  3、configure()在做什么?

  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),例如:

[NextPage]

<mapping resource="Cat.hbm.xml"/>

  通过以上的分析,我们对hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。

【责编:Luzi】

中国IT教育

相关产品和培训
文章评论
 专题推荐

 ·防范Linux病毒 打造没有病毒的乐土…
 ·巧用网络流量 打造健康内网…
 ·带你领略windows系统“另类”安装
 ·无线路由器设置从入门到精通
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·企业网管如何部署你的网络监控系统?
 ·2008年软考官方指定教材及辅导书下载专题
 ·负载均衡技术方案攻略
 ·中国IT实验室2007年技术热点盘点
 最近更新
 博客论点
 频道精选
 软件测试频道导航