服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将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()都做了什么?

  对于第一次使用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),例如:

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

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

  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");

  5、总结

  configuration提供的这些方法的好处如下:

  1) 一个应用中往往有很多.hbm.xml映射文件,开发的过程中如果只是为了测试某个或几个java po(persistence object),我们没有必要把所有的.hbm.xml都加载到内存,这样可以通过addclass或者addfile直接,显得非常灵活。

  2) 学习hibernate的过程中,往往需要通过练习来体会hibernate提供的各种特征,而很多特征是需要修改配置文件的,如果要观察相同的代码在不同的特征下的表现,就需要手工改配置文件,这样太麻烦了,而且容易出错,我们可以提供多个配置文件,每个配置文件针对需要的特征而配置,这样我们在调用程序的时候,把不同的配置文件作为参数传递进去,而程序代码里面使用setproperties和addfile指定传入的配置文件参数就可以了。

  3) 在单元测试中,特别是在集成测试里面,整个过程是自动化的,我们不能手工干预测试过程,往往需要准备多个配置文件针对不同的测试案例,这个时候setproperties和addfile方法就显得特别有用了,在不同的测试案例中用这些方法来指定相应的配置文件,这样就可以做到自动化测试,保证了持续性。

  6、应用举例

  在刚开始学习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的项目中灵活的运用多配置文件,可以提高测试的效率,保证自动化和持续性。

扫描关注微信公众号