Hibernate profile flexible use in unit testing

xiaoxiao2021-03-06  39

Hibernate is a popular open source object relationship mapping tool, the importance of unit testing and continuous integration has also been extensive promotion and identity, how to ensure the automation and persistence of tests in Hibernate projects? This article discusses the process of Hibernate loading its profile hibernate.properties and hibernate.cfg.xml, and how to use the access method of Hibernate's configuration files to unit tests.

1 Introduction Hibernate is a popular open source object relationship mapping tool, the importance of unit testing and continuous integration has also been widely promoted, how to ensure the automation and continuity of test in Hibernate projects? This article discusses the process of Hibernate loading its profile hibernate.properties and hibernate.cfg.xml, and how to use the access method of Hibernate's configuration files to unit tests. Note: This article uses hibernate2.1 as the foundation of the discussion, and does not guarantee the views of this article suitable for other versions.

2 Readers Java developers, ask for familiar with JUnit and master the basics of Hibernate

3 content

3.1. Preparing for the beginning of Hibernate, the experience of using Hibernate for the first time is usually:

1. Install the configuration of Hibernate, we will put% hibernate_home% as a reference to the Hibernate installation directory,

2, start to create your first example, such as the class CAT in the Hibernate manual,

3. Configure HBM mapping files (such as Cat.hbm.xml, this article does not discuss the meaning of this file within the file) and database (such as HSQLDB),

4, add a hibernate.cfg.xml file under the ClassPath path of the project, as follows (the first time using Hibernate's most common configuration):

PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

JDBC: HSQLDB: HSQL: // localhost

Org.hsqldb.jdbcdriver

SA

net.sf.hibernate.diaalect.hsqldiaAlact

false

5, then you need to provide a class to test CAT, you can create a unit test class for testing for developers who are familiar with JUnit, as follows:

Import junit.framework.testcase;

Import net.sf.hibernate.hibernateException;

Import net.sf.hibernate.Session;

Import net.sf.hibernate.transaction;

Import Net.sf.hibiBernate.cfg.configuration;

Public class cattest extends testcase {

PRIVATE session session;

PRIVATE TX;

Protected void setup () throws exception {

Configuration cfg = new configuration (). Configure (); pay attention to this line, this is where this paper focuses on the research.

SESSION = cfg.buildsessionFactory (). OpenSession ();

TX = session.begintransaction ();

}

protected void teardown () throws exception {

TX.comMit ();

session.close ();

}

Public void testcreate () {

/ / Please add a related code in this method. This article does not discuss how to use the Hibernate API.

}

Public void testupdate () {

/ / Please add a related code in this method. This article does not discuss how to use the Hibernate API.

}

Public void testdelete () {

/ / Please add a related code in this method. This article does not discuss how to use the Hibernate API.

}

Public void testQuery () {

/ / Please add a related code in this method. This article does not discuss how to use the Hibernate API.

}

}

3.2 What did new configuration () do? For newcomers using Hibernate for the first time, the following code can be said to be the most common way of using Configuration.

CONFIGURATION CFG = New Configuration (). Configure ();

Configuration is the entrance to Hibernate. When you create an instance of a Configuration, Hibernate looks up the hibernate.properties file in the classpath. If this file exists, load the contents of the file into a Properties Example Global_Properties, if you don't exist, Will print information

Hibernate.properties NOT FOUND

Then add all system environments (System.getProperties ()) to global_properties (Note 1). If the hibernate.properties file exists, the system also verifies the validity of this file configuration. For some unsupported configuration parameters, the system will print a warning message. 3.3. What is configure () What is it? New configuration () discussed here, discussed the configure () method.

Configure () Method The following is looking for hibernate.cfg.xml files below ClassPath, and if this file is not found, the system will print the following information and throw the HibernateException exception.

Hibernate.cfg.xml Not found

If this file is found, the configure () method will first access , and get the Name property of the element. If it is not empty, use the value of this configuration to override the configuration of the Hibernate.Properties Hibernate.Session_Factory_Name, From here we can see that configuration information in hibernate.cfg.xml can overwrite configuration information for hibernate.properties.

The Configure () method then accesses the child elements of , first use all of the element configuration (Note 2), such as the configuration file we use in front.

JDBC: HSQLDB: HSQL: // localhost

Org.hsqldb.jdbcdriver

SA

net.sf.hibernate.diaalect.hsqldiaAlact

The values ​​inside the corresponding configuration in Hibernate.properties are covered, and the values ​​inside the Hibernate.properties file (under the% hibernate_home / etc below), as follows:

Hibernate.dialev 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

Then Configure () will sequentially access the content of the following elements.

Where is essential, you must access , configure () to access the mapping files (HBM.XML) of our defined Java objects and relational database tables, for example:

Through the above analysis, our default load process for Hibernate configuration files hibernate.properties and hibernate.cfg.xml is more clear.

3.4 Other Usage of Configuration Configuration's configure () method also supports access to parameters, you can specify the location of the HBM.xml file instead of using the Hibernate.cfg.xml under the default classpath, for example:

Configuration cfg = new configuration (). Configure ("myexample.xml);

At the same time, Configuration also provides a range of methods to customize Hibernate loading profiles, making your app more flexible, commonly used:

AddProperties (Element)

AddProperties (Properties)

SetProperties (Properties)

SetProperty (String, String)

Through the above methods, in addition to using the default hibernate.properties file, you can also provide multiple .properties profiles, using different configuration files when using Hibernate, for example:

Properties Properties = Properties.Load ("My.properties");

Configuration config = new configuration (). SetProperties .configure ();

In addition to specifying the .properties file, you can specify .hbm.xml file, which lists several common methods:

AddClass (Class)

AddFile (file)

AddFile (String)

AddURL (URL)

As we have talked, the configure () method defaults to load us with the "Hibernate.cfg.xml" map "element, the method listed above can specify the HBM.xml file directly, such as AddClass () The method can load the corresponding mapping file directly by specifying the class, Hibernate automatically converts the full name (including package) provided to the file path, such as Net.sf.Hibernate.Examples.quickStart.cat.class corresponds to Net / sf / hibernate / examples / quickstart / cat.hbm.xml can also specify a mapping file directly with the AddFile method.

Example 1:

Configuration config = new configuration (); addclass (catch);

Example 2:

Configuration config = new configuration (). AddURL ("Cat.hbm.xml");

Example 3:

Configuration config = new configuration (). Addfile ("Cat.hbm.xml"); 3.5 Summary Configuration provides the benefits of these methods provided by:

1. There are often many. HBM.xml mapping files, if it is only to test one or several Java Po (Persistence Object), we don't have to load all .hbm.xml to memory, this can It is very flexible to use Addclass or AddFile.

2. During the process of learning Hibernate, you often need to experience the various features provided by Hibernate, and many features need to modify the configuration file. If you want to observe the same code, you need to manually change the configuration file, This is too much trouble, and it is easy to make mistakes, we can provide multiple profiles, each configuration file configured for the features required, so that we will pass different profiles as parameters when calling the program, and the program code Use setProperties and addfile to specify that the incoming configuration file parameters can be used.

3. In unit test, especially in the integrated test, the entire process is automated, we cannot manually intervene, often need to prepare multiple profiles for different test cases, this time setProperties and AddFile methods are especially useful, In different test cases, use these methods to designate the corresponding configuration file, so that automation tests can be made to ensure continuity.

3.6 Application Example When you just start learning Hibernate, there is no sensibility for the various configuration parameters of Hibernate's HBM mapping file, such as inverse = "true", lazy = "true", this configuration parameters are not passed. If it is unably, the traditional method is to change the corresponding configuration file everywise to test a parameter, then run the test to observe the result. If we can flexibly use the custom configuration of Configuration, we can provide more A configuration file, there are different configuration parameters in each configuration file, which is more convenient to match the corresponding test case.

For example, for the two-way associated mapping relationship of ONO-TO-MANY and MANY-TO-One, we want to test different effects in One-to-Many-to-Many, and inverse = "false" and inverse = "True", assume that it is correct Hibernate.properties is configured, then two different HBM.XML files are required, assuming that bidirect.inverse.false.hbm.xml and bidirect.inverse.true.hbm.xml are named, respectively.

Then you need to write two different test cases, which are tested for two different profiles. This benefit is that the configuration files are modified for different test cases, especially when integrated testing, everything is Automation, if each test is required to change the configuration file manually, this is definitely a failed test.

The code template is as follows:

FalseinverSetest.java file

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 falseinvetetetest extends testcase {

PRIVATE session session;

PRIVATE 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 () {

/ / Write test code here

}

}

TrueinverSetest.java file

Import junit.framework.testcase;

Import net.sf.hibernate.hibernateException;

Import net.sf.hibernate.Session;

Import net.sf.hibernate.transaction;

Import Net.sf.hibiBernate.cfg.configuration;

/ **

* Test True Inverse

* /

Public Class TrueinverSetest Extends Testcase {

PRIVATE session session;

PRIVATE 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 () {

/ / Write test code here

}

}

Conclusion By discussion of the method of loading the Hibernate default configuration file and the method of loading the configuration file provided by Hibernate, we have a relatively clear understanding of multiple Hibernate profiles in unit tests that use the Hibernate project.

The tests in the continuous integration are automated and continuous, and they cannot be manually intervened. If you use the Hibernate, if you want to achieve continuous integration, you will provide different profiles for different test cases, not for different tests. The case is manually adjusted, so flexible use of multi-configuration files in the project to Hibernate, you can improve the efficiency of the test, ensure automation, and continuity.

Note 1: For the code, please refer to the static {} of the Environment class.

转载请注明原文地址:https://www.9cbs.com/read-57162.html

New Post(0)