Introduction

The next paragraphs explain the usage of the TestSetGenerator with a number of examples. These examples can be found in src/samples/testsetgenerator_demo.xml .

To run these examples make sure that the AntTestSetGen jar file is in your classpath. Then run the examples from the src/samples directory:

ant -f testsetgenerator_demo.xml            

HSQLDB

All the examples make use of HSQLDB : a relational database engine written in Java, with a JDBC driver, supporting a subset of ANSI-92 SQL. It offers a small (about 100k), fast database engine which offers both in memory and disk based tables.

The database directory contains the customer table of the HSQLDB testset:

CUST_ID CUST_FIRSTNAME CUST_LASTNAME CUST_STREET     CUST_CITY
------- -------------- ------------- --------------- -------------
0       Laura          Steel         429 Seventh Av. Dallas
1       Susanne        King          366 - 20th Ave. Olten
2       Anne           Miller        20 Upland Pl.   Lyon
3       Michael        Clancy        542 Upland Pl.  San Francisco
4       Sylvia         Ringer        365 College Av. Dallas
5       Laura          Miller        294 Seventh Av. Paris
6       Laura          White         506 Upland Pl.  Palo Alto
7       James          Peterson      231 Upland Pl.  San Francisco
8       Andrew         Miller        288 - 20th Ave. Seattle
9       James          Schneider     277 Seventh Av. Berne
10      Anne           Fuller        135 Upland Pl.  Dallas
11      Julia          White         412 Upland Pl.  Chicago
12      George         Ott           381 Upland Pl.  Palo Alto
13      Laura          Ringer        38 College Av.  New York
14      Bill           Karsen        53 College Av.  Oslo
15      Bill           Clancy        319 Upland Pl.  Seattle
16      John           Fuller        195 Seventh Av. New York
17      Laura          Ott           443 Seventh Av. Lyon
18      Sylvia         Fuller        158 - 20th Ave. Paris
19      Susanne        Heiniger      86 - 20th Ave.  Dallas
20      Janet          Schneider     309 - 20th Ave. Oslo
21      Julia          Clancy        18 Seventh Av.  Seattle
22      Bill           Ott           250 - 20th Ave. Berne
23      Julia          Heiniger      358 College Av. Boston
24      James          Sommer        333 Upland Pl.  Olten
25      Sylvia         Steel         269 College Av. Paris
26      James          Clancy        195 Upland Pl.  Oslo
27      Bob            Sommer        509 College Av. Seattle
28      Susanne        White         74 - 20th Ave.  Lyon
29      Andrew         Smith         254 College Av. New York
30      Bill           Sommer        362 - 20th Ave. Olten
31      Bob            Ringer        371 College Av. Olten
32      Michael        Ott           339 College Av. Boston
33      Mary           King          491 College Av. Oslo
34      Julia          May           33 Upland Pl.   Seattle
35      George         Karsen        412 College Av. Chicago
36      John           Steel         276 Upland Pl.  Dallas
37      Michael        Clancy        19 Seventh Av.  Dallas
38      Andrew         Heiniger      347 College Av. Lyon
39      Mary           Karsen        202 College Av. Chicago
40      Susanne        Miller        440 - 20th Ave. Dallas
41      Bill           King          546 College Av. New York
42      Robert         Ott           503 Seventh Av. Oslo
43      Susanne        Smith         2 Upland Pl.    Dallas
44      Sylvia         Ott           361 College Av. New York
45      Janet          May           396 Seventh Av. Oslo
46      Andrew         May           172 Seventh Av. New York
47      Janet          Fuller        445 Upland Pl.  Dallas
48      Robert         White         549 Seventh Av. San Francisco
49      George         Fuller        534 - 20th Ave. Olten

Example 1: Generate a property file

In this example a property file is created with information about customers from the customer table. The first test set contains information about the first customer in the table that is living in San Francisco. The second test set contains information about all customers in the table that are living in San Francisco. The name of the generated property file is example1.properties .

The following build.xml sniplet shows how the TestSetGenerator task is configured.

<target name="example1" description="Generate a property file">

  <testsetgenerator
    description="TestSetGenerator Example 1: Generate a property file"
    driver="${JDBC.DRIVER}" url="${JDBC.URL}" userid="${JDBC.USERNAME}" password="${JDBC.PASSWORD}" classpath="${JDBC.CLASSPATH}">

    <testset name="Customer in San Francisco" rowtoselect="first">
      <query>
        select *
        from customer
        where CUST_CITY = 'San Francisco'
      </query>
      <entry key="CUSTOMER_FIRST_NAME" column="CUST_FIRSTNAME"/>
      <entry key="CUSTOMER_LAST_NAME" column="CUST_LASTNAME"/>
    </testset>

    <testset name="Customer in San Francisco" rowtoselect="all">
      <query>
        select *
        from customer
        where CUST_CITY = 'San Francisco'
      </query>
      <entry key="CUSTOMER_FIRST_NAMES" column="CUST_FIRSTNAME"/>
      <entry key="CUSTOMER_LAST_NAMES" column="CUST_LASTNAME"/>
    </testset>

    <output type="PropertyFile" file="example1.properties"/>

  </testsetgenerator>
</target>

After running the ant task the generated property file looks like:

#=====================================================
# TestSetGenerator Example 1: Generate a property file
#=====================================================
# Generated by: Ant TestSetGenerator
# Generated on: 4-1-04 12:53
# URL: jdbc:hsqldb:../../database/ant_test_db
# Userid: sa

#--------------------------
# Customer in San Francisco
#--------------------------
# Row: First
CUSTOMER_FIRST_NAME = Michael
CUSTOMER_LAST_NAME = Clancy

#--------------------------
# Customer in San Francisco
#--------------------------
# Row: All
CUSTOMER_FIRST_NAMES.1 = Michael
CUSTOMER_FIRST_NAMES.2 = James
CUSTOMER_FIRST_NAMES.3 = Robert
CUSTOMER_LAST_NAMES.1 = Clancy
CUSTOMER_LAST_NAMES.2 = Peterson
CUSTOMER_LAST_NAMES.3 = White
This property file can then be used by your tests to retrieve the test constants.

Example 2: Generate a Java Constants Class

To make it easier to integrate with your test suite it is also possible to generate a Java constants class. This class can then be used by your test classes. The Java constants class makes use of an XML file with the test data. This XML file is also generated by the TestSetGenerator. This is done because over time the test data can change. You then only have to regenerate the XML-file and you don't have to recompile your test constants class.

<target name="example2" description="Generate a Java constants class">

  <testsetgenerator
    description="TestSetGenerator Example 2: Generate a Java constants class"
    driver="${JDBC.DRIVER}" url="${JDBC.URL}" userid="${JDBC.USERNAME}" password="${JDBC.PASSWORD}" classpath="${JDBC.CLASSPATH}">

    <testset name="Customer in San Francisco" rowtoselect="first">
      <query>
        select *
        from customer
        where CUST_CITY = 'San Francisco'
      </query>
      <entry key="CUSTOMER_ID" column="CUST_ID" type="long"/>
      <entry key="CUSTOMER_FIRST_NAME" column="CUST_FIRSTNAME"/>
      <entry key="CUSTOMER_LAST_NAME" column="CUST_LASTNAME"/>
    </testset>

    <testset name="Customers in San Francisco" rowtoselect="all">
      <query>
        select *
        from customer
        where CUST_CITY = 'San Francisco'
      </query>
      <entry key="CUSTOMER_IDS" column="CUST_ID" type="long"/>
      <entry key="CUSTOMER_FIRST_NAMES" column="CUST_FIRSTNAME"/>
      <entry key="CUSTOMER_LAST_NAMES" column="CUST_LASTNAME"/>
    </testset>

    <output type="Java" destination="." packagename="net.sourceforge.test" classname="TestConstants"/>

  </testsetgenerator>
</target>

In the destination directory an xml-file is generated with the name of the class. In this case TestConstants.xml .

<TestConstants>
  <CUSTOMER_ID>3</CUSTOMER_ID>
  <CUSTOMER_FIRST_NAME>Michael</CUSTOMER_FIRST_NAME>
  <CUSTOMER_LAST_NAME>Clancy</CUSTOMER_LAST_NAME>
  <CUSTOMER_IDS>
    <long>3</long>
    <long>7</long>
    <long>48</long>
  </CUSTOMER_IDS>
  <CUSTOMER_FIRST_NAMES>
    <string>Michael</string>
    <string>James</string>
    <string>Robert</string>
  </CUSTOMER_FIRST_NAMES>
  <CUSTOMER_LAST_NAMES>
    <string>Clancy</string>
    <string>Peterson</string>
    <string>White</string>
  </CUSTOMER_LAST_NAMES>
</TestConstants>

The generated Java class is generated in a directory based on the packagename and the destination. So in this case ./net/sourceforge/test/TestConstants.java

/**
 * Generated by: Ant TestSetGenerator
 *
 * Generated on: 4-1-04 11:43
 * URL: jdbc:hsqldb:../../database/ant_test_db
 * Userid: sa
 */

package net.sourceforge.test;

import com.thoughtworks.xstream.XStream;
import net.sourceforge.anttestsetgen.AntTestSetGenUtil;

public class TestConstants {

  // Customer in San Francisco
  public static Long CUSTOMER_ID;
  public static String CUSTOMER_FIRST_NAME;
  public static String CUSTOMER_LAST_NAME;

  // Customers in San Francisco
  public static Long[] CUSTOMER_IDS;
  public static String[] CUSTOMER_FIRST_NAMES;
  public static String[] CUSTOMER_LAST_NAMES;

  static {
    XStream xstream = new XStream();
    xstream.alias("TestConstants", TestConstants.class);
    xstream.fromXML(AntTestSetGenUtil.readFile("TestConstants.xml"));
  }

}

This class makes use of XStream : a simple library to serialize objects to XML and back again.

Now in order to use the testconstants, all you have to do is to include the generated java file in your test suite, add the AntTestSetGen and xstream library to your classpath and use the generated class. Here's a simple example.

import net.sourceforge.test.TestConstants;

public class Example2 {

  public static void main(String[] args) {
    for (int i = 0; i < TestConstants.CUSTOMER_LAST_NAMES.length; i++) {
      String lastName = TestConstants.CUSTOMER_LAST_NAMES[i];
      System.out.println("Last name: " + lastName);
    }
  }
}

And the result is:

Last name: Clancy
Last name: Peterson
Last name: White

Regenerating the XML-file with testdata

When at a certain time the data in the database is changed there are two options to regenerate the xml-file with test data. The first option is to use the onlyxml flag:

<output type="Java" destination="." packagename="net.sourceforge.test" classname="TestConstants" onlyxml="true"/>

Or you can use the special xml output type. Make then sure that the name of the root element is the same as the name of the generated Java class.

<output type="xml" destination="./TestConstants.xml" root="TestConstants"/>

Example 3: Generate Ant properties

In this example a testset is used to select the customer id from a customer how lives in San Francisco. Based on the outcome an Ant property is created.

This Ant property is then used in t he next testset to parameterize a query.

<target name="example3" description="Creating ant properties">

  <testsetgenerator
    description="TestSetGenerator Example 3"
    driver="${JDBC.DRIVER}" url="${JDBC.URL}" userid="${JDBC.USERNAME}" password="${JDBC.PASSWORD}" classpath="${JDBC.CLASSPATH}">

    <testset name="Customer in San Francisco" rowtoselect="first">
      <query>
        select *
        from customer
        where CUST_CITY = 'San Francisco'
      </query>
      <entry key="CUSTOMER_ID" column="CUST_ID" createproperty="true"/>
    </testset>

    <testset name="Details of Customer in San Francisco" rowtoselect="first">
      <query>
        select *
        from customer
        where CUST_ID= ${CUSTOMER_ID}
      </query>
      <entry key="CUSTOMER_FIRSTNAME" column="CUST_FIRSTNAME"/>
      <entry key="CUSTOMER_LASTNAME" column="CUST_LASTNAME"/>
      <entry key="CUSTOMER_STREET" column="CUST_STREET"/>
    </testset>

    <output type="PropertyFile" file="example3.properties"/>

  </testsetgenerator>
</target>

The property file looks like:

#===========================
# TestSetGenerator Example 3
#===========================
# Generated by: Ant TestSetGenerator
# Generated on: 4-1-04 13:18
# URL: jdbc:hsqldb:../../database/ant_test_db
# Userid: sa

#--------------------------
# Customer in San Francisco
#--------------------------
# Row: First
CUSTOMER_ID = 3

#-------------------------------------
# Details of Customer in San Francisco
#-------------------------------------
# Row: First
CUSTOMER_FIRSTNAME = Michael
CUSTOMER_LASTNAME = Clancy
CUSTOMER_STREET = 542 Upland Pl.

Example 4: Using a custom validator

Sometimes you also need to incorporate information from external systems to get a correct test data. In that case you can use a custom validator.

In this example we are looking for a customer in Dallas (that informati on is stored in our customer table) that can order BroadBand access (that information is found in an external system). That validate test data against the external system the sample BroadBandValidator is used. This validator only knows one valid address where you can get BroadBand access: at 86 - 20th Ave. in Dallas.

Depending on the rowtoselect setting, either the first valid row from the resultset will be selected, or all valid rows from the resulset.

<target name="example4" description="Using a custom validator">

  <testsetgenerator
    description="TestSetGenerator Example 4"
    driver="${JDBC.DRIVER}" url="${JDBC.URL}" userid="${JDBC.USERNAME}" password="${JDBC.PASSWORD}" classpath="${JDBC.CLASSPATH}">

    <testset name="Address in Dallas with BroadBand access" rowtoselect="first">
      <query>
        select *
        from customer
        where CUST_CITY = 'Dallas'
      </query>
      <entry key="DSL_ID" column="cust_id"/>
      <entry key="DSL_FIRSTNAME" column="cust_firstname"/>
      <entry key="DSL_LASTNAME" column="cust_lastname"/>
      <entry key="DSL_STREET" column="cust_street"/>
      <entry key="DSL_CITY" column="cust_city"/>

      <validation className="net.sourceforge.anttestsetgen.sample.BroadBandValidator">
        <parameter name="street" column="cust_street"/>
        <parameter name="city" column="cust_city"/>
      </validation>
    </testset>

    <output type="PropertyFile" file="example4.properties"/>
  </testsetgenerator>

</target>

The property file looks like:

#===========================
# TestSetGenerator Example 4
#===========================
# Generated by: Ant TestSetGenerator
# Generated on: 4-1-04 13:32
# URL: jdbc:hsqldb:../../database/ant_test_db
# Userid: sa

#----------------------------------------
# Address in Dallas with BroadBand access
#----------------------------------------
# Row: First
DSL_ID = 19
DSL_FIRSTNAME = Susanne
DSL_LASTNAME = Heiniger
DSL_STREET = 86 - 20th Ave.
DSL_CITY = Dallas