Wednesday, 26 August 2009

Testing and exception handling with Ant

This week I worked on a project that had to import employees from files into the BPM Directory. Since I changed the algorithm to a balanced-line construction I felt I wanted to have some repeatable test cases to try to hit every balanced-line case. Balanaced-line processing of files is not that hard, but it is easily to mis a record.

The importer used a property file in which it relates to the file with the data to import. So for every test-case I had to create a test-file and a configuration file. I named them like:
  • test001_medewlist.txt
  • test002_medewlist.txt
Then for every test case I wanted to copy the test??? from the test directory to 'conf/'. Then call the importer and finally replace the configuration file again with the original subversioned one.

For the importer a working ant build file was created with a run target. So I thought it would be nice to have a seperate testBuild.xml file that does the job.

So I created this file with first a simple copy-target that copies a backupped original configuration file back to the conf directory. I want to leave the project as it is committed to subversion after my test.

Then I created a macro-definition that gets the test-configuration-file as an attribute.
It copies this test configuration file over the actual configuration file. Then it tries to call the run-target of the main build file. And then it copies the original one back.

I found that the run-target of the original file could fail and in that case it should also restore the original configuration file, in stead of failing my test-script. With ant-contrib you can use a try-catch-finally the same way as in Java.

Here's my resulting testBuild.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project name="TestDirectoryServiceSynchronizer" basedir=".">
<!-- Task Definition for ant-contrib.jar -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<pathelement location="c:/repository/generic/support/common/lib/ant-contrib.jar"/>

<target name="copyOrgConfig">
<echo>Copy Original Config-file Back</echo>
<copy toFile="conf/" overwrite="true">
<fileset file="test/" />

<target name="001-AddOneEmployee">
<test-case configFile="" />

<target name="002-RemoveOneEmployee">
<test-case configFile="" />

<macrodef name="test-case">
<attribute name="configFile" default="attribute configFile not set" />
<echo>Copy Config-file @{configFile}</echo>
<copy toFile="conf/" overwrite="true">
<fileset file="test/@{configFile}" />
<!-- call run -->
<echo>Run DirectorySynchronizer</echo>
<ant antfile="${basedir}/build.xml" target="run" />
<echo>Investigate exceptions in the run!</echo>
<antcall target="copyOrgConfig" />