Now that we have code that can handle passing a command to the
system, let's look at one of the commands that we must implement to
have a semi-useful wrapper for Arch. The first thing
that the antlib must be able to do is to
register-archive. Figure 2 shows the format of the
command we need to pass to the system. The archive is in the form
of a URL. We can also see that passing an archive name is an
optional parameter that our task should support.

Figure 2. The format for the register-archive
command

Because we have most of the support code written in
AbstractTlaTask, the RegisterArchive is
fairly straightforward. As you can see in the code below, we
override the execute method and include the parameters
that are required. Before running the command,
validate is called to ensure that the required
parameters are present.

Compiling the Antlib

Now that we have a basic antlib prepared, we need to add the
"special sauce" that makes it an antlib, and then we can get down
to testing our creation! For all antlibs, we must include an XML
document with our code (in the package with the class files).

Since we currently only have one task defined in our antlib, we
only need one entry in the antlib.xml. We don't
include the AbstractTlaTask as it is only a support
class and we don't want the user to be able to access it directly.
Finally, we need to jar the antlib and place it into
our $ANT_HOME/lib directory or in our classpath.

Note although the antlib facility is being used
extensively for Ant 1.7+, the ability to load an antlib is
available in the current release version, Ant 1.6.5.

So we're done right? We've got our new antlib .jar on our
classpath and we can use the defined task just like any other
<taskdef>. But does it really work?

Testing Tasks and Antlibs

Traditionally, Java developers use JUnit to create their unit tests.
However, testing Ant tasks and antlibs is easier if you use the
facilities provided by Ant.

Instead of subclassing TestCase, Ant provides a
wrapper BuildFileTest, which allows us to test an Ant
task using a build file as the driver. This is a much better test
of the code since it is being exercised in a similar way to how it will operate in real life. However, some people may object
to calling these tests unit tests, as they are more like
integration or system tests.

For our RegisterArchive task, we'll just bang out a
quick build file to test it by eye-balling (don't worry, the
sample code does include a real BuildFileTest).
For a quick test and a BuildFileTest, you can use the
same build file. For the tests, I've selected the Arch repository
mentioned in the Arch documentation.

Testing with AntUnit

Another new facility provided by Ant is the AntUnit
antlib. Unlike a JUnit TestCase or a
BuildFileTest, AntUnit allows you to specify your
tests without using any Java code. AntUnit itself is provided as an
antlib, so it must be placed in your $ANT_HOME/lib
directory, or specified on the classpath. In a recursively friendly
way, AntUnit is used to test both Ant and AntUnit!

Since we have a build file that we have already used for ad hoc
testing, let's modify it to use AntUnit and introduce a much more
repeatable test. First, we must declare the AntUnit namespace, so
the header of our build file becomes:

AntUnit works in a similar way to JUnit and looks for targets
which start with test, so we must change our
register target to test-register. Like
JUnit, AntUnit also needs some way of asserting if the test passed.
In this case, we'll use the assertLogContains macro to
check if the output is what we are expecting. Here's what our new
test-register looks like:

Of course, we also want to decouple the optional task from the
main Ant source so that we can ship fixes to the optional tasks
(now antlibs) independently from releasing a full Ant distribution.
In reality, all this requires is a change of package namespace and,
along with the antlib.xml file, the optional task
becomes a decoupled antlib!

Recap

We have seen just two of the main new features of Ant
1.7: antlibs and AntUnit.

The antlib feature allows developers of Ant tasks to ship fixes
and updates independently of the main Ant distribution, and the
AntUnit antlib is a much quicker method of creating unit tests for
Ant tasks.

In this article we've written the start of an antlib for the SCM system Arch,
showing how easy it is to develop new tasks with the antlib
mechanism. We also tested this antlib with an AntUnit test.