Java Reference
In-Depth Information
Listing 17.4
XmlDataSet
representation of users table
<?xml version="1.0"?>
<!DOCTYPE dataset SYSTEM "dataset.dtd">
<dataset>
<table name="users">
<column>id</column>
<column>username</column>
<column>first_name</column>
<column>last_name</column>
<row>
<value>1</value>
<value>ElDuderino</value>
<value>Jeffrey</value>
<value>Lebowsky</value>
</row>
</table>
</dataset>
The
XmlDataSet
format is self-described, but it has two problems. First, it's verbose. As
you can see in the previous example, a simple row in a table required 16 lines of
XML
code. The advantage of this format is that it follows a well-defined
DTD
(available
inside DbUnit's
JAR
), which could avoid problems caused by bad
XML
syntax. But that
brings up the second issue: DbUnit doesn't validate the
DTD
(that
DOCTYPE
line could
be removed or even changed to any garbage, and the result would be the same).
Although the lack of
XML
validation is a DbUnit bug, the verboseness of the for-
mat is a design option. A much simpler option is to use
FlatXmlDataSet
, where each
line describes a row in the database. Listing 17.5 shows the same dataset using the flat
XML
format.
Listing 17.5
FlatXmlDataSet
representation of users table (user.xml)
<?xml version="1.0"?>
<dataset>
<users id="1" username="ElDuderino"
first_name="Jeffrey" last_name="Lebowsky" />
</dataset>
The
FlatXmlDataSet
format is much clearer and easier to maintain,
4
so we use it in
our examples. Listing 17.6 shows our first test case.
Listing 17.6
Initial test case for UserDaoJdbcImpl (UserDaoJdbcImplTest)
[...]
public class
UserDaoJdbcImplTest {
B
private static
UserDaoJdbcImpl dao =
new
UserDaoJdbcImpl();
private static
Connection connection;
private static
HsqldbConnection dbunitConnection;
4
This format has its issues as well, which we will cover later in the chapter.