7.5 Generating the cactus.properties File Automatically

7.5.1 Problem

You want to automatically generate the cactus.properties file to match the current environment.

7.5.2 Solution

Create a target within your Ant buildfile to generate the cactus.properties file each time Cactus tests are run.

7.5.3 Discussion

Writing Cactus tests is pretty straightforward, but configuring your environment can be cumbersome, especially if your environment changes over time. Automatically generating configuration files eases the burden of keeping your testing environment in sync with your development environment.

Ant is the obvious choice for generating the cactus.properties file. The first step is to ensure that the following Ant properties are defined in your buildfile:[7]

[7] Your test environment may require different property values for the host, port, web application context name, etc. than shown here.

<property name="dir.build" value="build"/>
<property name="host" value="http://localhost"/>
<property name="port" value="8080"/>
<property name="webapp.context.name" value="xptest"/>
<property name="servlet.redirector" value="ServletRedirector"/>
<property name="jsp.redirector" value="JspRedirector"/>
<property name="filter.redirector" value="FilterRedirector"/>

By setting up global properties, you ensure that a single change ripples through the rest of the buildfile. Next, your buildfile should execute the propertyfile task:

<target name="prepare">
  <mkdir dir="${dir.build}"/>

  <propertyfile file="${dir.build}/cactus.properties">
    <entry key="cactus.contextURL"
           value="${host}:${port}/${webapp.context.name}"/>
    <entry key="cactus.servletRedirectorName"
          value="${servlet.redirector}"/>
    <entry key="cactus.jspRedirectorName"
          value="${jsp.redirector}"/>
    <entry key="cactus.filterRedirectorName"
          value="${filter.redirector}"/>
  </propertyfile>
</target>

The propertyfile task is an optional Ant task. If you are using Ant 1.4 then you must include the optional Ant JAR file in Ant's classpath. The simplest way to do this is to copy the optional Ant JAR file to Ant's lib directory. If you are using Ant 1.5 or greater you do not have to do anything.

The best place to generate the cactus.properties file is within a target that executes each time the Cactus test suite runs. In this recipe, the property file is generated within the prepare target, which is the first target executed when running the test suite. Each time you run the Cactus test suite the cactus.properties file is generated by Ant to reflect any changes to the host, port, web context name, or redirector values.

7.5.4 See Also

Recipe 7.4 describes the details of the cactus.properties file. Recipe 7.3 describes how to setup a stable build environment for server-side testing.