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_configuration.properties
  • test001_medewlist.txt
  • test002_configuration.properties
  • test002_medewlist.txt
Then for every test case I wanted to copy the test???_configuration.properties from the test directory to 'conf/configuration.properties'. 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">
<classpath>
<pathelement location="c:/repository/generic/support/common/lib/ant-contrib.jar"/>
</classpath>
</taskdef>

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

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

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

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

2 comments :

Unknown said...

Thanks Martien

This Blog helpful for me & works fine.

I want to know how to SHOW/CATCH Runtime Exception in ANTCONTRIB ?

In Our Build process we want notify to Users in group about Build Result with reason.

reason like in Java: (exception.getMessage() or e.getCause() or e.getPrintStackTrace()).

Please reply ASAP

Martien van den Akker said...

Hi,

It's been a while ago that I've done this. So it would take me to dive in to it. Maybe some of our other readers could help. Or you should post on a ant-forum.

Sorry I can't help you further at the moment.

Regqrds,
Martien