|
Ihr Spezialist für komplexe IT-Systeme
|
|
The Oracle ADF Toystore uses several Database Tables to retrieve data about the products and store information about customers and orders. Therefore the testcases of the next chapters will modify or add data within that tables. To check and handle that changes within our testcases we are going to use DBUnit [DBUnit] which was developt to adress exactly that purpose.
The picture above shows a few important tables of the ADF Toystore schema. We have to handle several different jobs:
DBUnit can also be used as an extension to the popular build tool ant. So we will define several targets to implement this jobs.
<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE project [
<!ATTLIST dbunit
driver CDATA "oracle.jdbc.driver.OracleDriver"
url CDATA "jdbc:oracle:thin:@//192.168.1.70:1521/B02"
userid CDATA "toystore"
password CDATA "toystore"
datatypeFactory CDATA "org.dbunit.ext.oracle.OracleDataTypeFactory"
>
]>
<project name="MyDBUnitSampleTest" basedir="." default="main">
<property name="oracle.home" value="/opt/oracle/ora10g"/>
<path id="task.path">
<pathelement location="${basedir}/dbunit-2.1.jar"/>
<fileset dir="${oracle.home}" includes="jdbc/lib/*.jar"/>
</path>
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"
classpathref="task.path"/>
<target name="main">
</target>
</project>
The above ant file gives you the outline. We will go and insert our targets in the following sections.
Ok, our first job is to prepare an XML-File corrosponding to the initial state of the tables.
<target name="export_tables">
<dbunit>
<export dest="${dataset.filename}">
<table name="ACCOUNT"/>
<table name="ORDERS"/>
<table name="ORDERSTATUS"/>
<table name="LINEITEM"/>
<table name="PROFILE"/>
<table name="SIGNON"/>
</export>
</dbunit>
</target>
Now we can create our first dataset using the follwing command:
[frank@w0001 dbunit-2.1]$ ant -Ddataset.filename=toystore_init_tables.xml \
-f dbtest.xml export_tables
Buildfile: dbtest.xml
export_tables:
[dbunit] Executing export:
[dbunit] in format: flat to datafile: toystore_init_tables.xml
BUILD SUCCESSFUL
Total time: 8 seconds
[frank@w0001 dbunit-2.1]$
Use your favorite editor to adjust the created file so that it represents an initial state. You should also create two more export files. The first after registering an test user with ADF Toystore. And an second one after makeing an order with that test user. We will use these datasets as the reference for the tests.
Next we have to make sure that we can but our tables back to the inital state after running our tests. We will use an additional ant target to do that.
If you have a closer look to the toystore schema you will notice that there is also an Sequence we have to take care about. I am using an normal ANT-SQL statement to handle that. So we get:
<target name="init_tables">
<sql driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@//192.168.1.70:1521/B02"
userid="toystore"
password="toystore"
classpathref="task.path">
DROP SEQUENCE ordernum;
CREATE SEQUENCE ordernum
START WITH 1100
INCREMENT BY 1
MINVALUE 1
NOMAXVALUE
CACHE 20
NOCYCLE
NOORDER;
</sql>
<dbunit>
<operation type="CLEAN_INSERT" src="${dataset.filename}"/>
</dbunit>
</target>