Section 40.2. Generate a Simple Sequence


40.2. Generate a Simple Sequence

As with development generally, it makes sense to go step by step. A sensible first step is to generate a simple test and see that it works. We can then extend our test generator as we get clearer about what it needs to do. We may find that a simple approach works fine and there is no point in making it more sophisticated. Or, after some use, we may find ways of improving it that weren't obvious initially. After all, it's just as hard for us to see the future clearly as it is for customers.

To generate a sequence, we will need to have a representation of the clients and rental items in our simulation. Then we can randomly pick a client and some rental items to be rented and later returned.

In this first step, we simply generate a single test that rents and then immediately returns each RentalItem. Listing 40.1 shows the first part of the code for class Generate1, a subclass of CustomRunner. Some initial setup data for clients and rental items is created in static arrays, and this is used in makeSetUp().

The constructor for Generate1 in Listing 40.1 creates the tables and runs the CustomRunner on them to produce a Fit report with title RPS. (See Chapter 39 for an introduction to the use of CustomRunner.)

The remainder of class Generate1 is shown in Listing 40.2. The makeTables() method randomly chooses a RentalItemModel that has some items available for rental. The method generates one table to rent all those items and a second table to immediately return them.

The makeCheck() method in Listing 40.2 generates the final table, which checks that no RentalItems are outstanding. This could be improved by also checking that the rentals are back to their original numbers. (See the exercises at the end of the chapter.)

The method makeSetUp() in Listing 40.1 generates the Fit tables for initializing the setup data in the system under test. This uses the method setUpRow() in both ClientModel and RentalItemModel to return the relevant data to be inserted in the row of the setup table. Class ClientModel is shown in Listing 40.3, and class RentalItemModel is shown in Listing 40.4.

Listing 40.1. Generate1.java (part 1)
 public class Generate1 extends CustomRunner {     private Random random = new Random();     private static final RentalItemModel[] rentalItems = {            new RentalItemModel("coffee urn", 10, 8.20),            new RentalItemModel("hot water urn", 10, 8.00)     };     private static final ClientModel[] clients = {            new ClientModel("Joanna", "3737599"),            new ClientModel("Mistra", "4074707")     };     public Generate1() throws IOException {         super("RPS");         makeSetUp();         makeTables();         makeCheck();         runAndReport();     }     private void makeSetUp() {         addTable("rent.StartApplication");         addTable("set up");         addRow("rental item name | count | $/hour | $/day | $/week | deposit");         for (int i = 0; i < rentalItems.length; i++)            addRow(rentalItems[i].setUpRow());         addTable("set up");         addRow("staff name | phone");         addRow("Bill | 555 9876");         addTable("set up");         addRow("client name | phone");         for (int i = 0; i < clients.length; i++)            addRow(clients[i].setUpRow());         addTable("time is now | 2004/05/06 09:01");      }      // ... } 

Listing 40.2. Generate1.java (part 2)
 public class Generate1 extends CustomRunner {     // ...     private void makeTables() {        RentalItemModel hireItem = getHireItem();        addTable("begin transaction for client | Joanna | staff | Bill");        addRow(hireItem.rentAll(1));        addRow("pay with cash $ | 82.00");        addRow("complete transaction");        addTable("time is now | 2004/05/07 09:01");        addTable("begin transaction for client | Joanna | staff | Bill");        addRow(hireItem.returnAll(1));        addRow("complete transaction");     }     private void makeCheck() {         addTable("rentals for client | Joanna");         addRow("rental item | count | start date | end date");         addTable("fit.Summary");    }    private RentalItemModel getHireItem() {       return rentalItems[random.nextInt(rentalItems.length)];    } } 

Listing 40.3. ClientModel.java
 public class ClientModel {    public String name, phone;    public ClientModel(String name, String phone) {       this.name = name;       this.phone = phone;    }    public String setUpRow() {       return name+" | "+phone;    }     public String getName() {         return name;     } } 

Listing 40.4. RentalItemModel.java (version 1)
 public class RentalItemModel {     private String name;     private int count;     private double dailyRate;     public RentalItemModel(String name, int count, double dailyRate) {         this.name = name;         this.count = count;         this.dailyRate = dailyRate;     }     public String setUpRow() {        return name+" | "+count+" | 0.00, "+dailyRate+"0 | 0.00 | 0.00";     }     public String rentAll(int days) {        return "rent | "+count+" | | "+name+" | for | "+days+" days";     }     public String returnAll(int days) {        return "return items | "+count+" | | "+name;     } } 



    Fit for Developing Software. Framework for Integrated Tests
    Fit for Developing Software: Framework for Integrated Tests
    ISBN: 0321269349
    EAN: 2147483647
    Year: 2005
    Pages: 331

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net