23.1. Testing Unordered ListsLet's now return to the chat room occupant test from Section 5.1 on p. 31, as shown again in Figure 23.1. This is a RowFixture table, with the header row labeling the fields that make up the elements of the list.
The application has a list of rooms, each of which has a list of users occupying that room. The fixture code needs to collect the data from the system under test and create a local collection specifically for RowFixture to use in matching against the rows of the table. Figure 23.2 shows this, with the Room and User objects from the system under test at the bottom and the Occupancy objects created from them by the fixture to the right. Figure 23.2. Fit Runs the Table
The fixture code is shown in Listing 23.1. As a subclass of fit.RowFixture, OccupantList defines the two methods query() and getTargetClass(). The method query() returns an array of elements of the actual collection; the method getTargetClass() returns the class of the elements of the array. The method query() of the fixture (line 4 in Listing 23.1) assembles the elements of the actual list from the List of Room supplied by the ChatServer, the system under test. The appropriate data is assembled into an array of Occupancy in the query() method in Listing 23.1. As shown in Listing 23.2, the target class is Occupancy, which has public instance variables for user and room, corresponding to the labels in the header row in Figure 23.1. Listing 23.1. OccupantList.java1 public class OccupantList extends fit.RowFixture { 2 private ChatServer chat = new ChatServer(); 3 4 public Object[] query() throws Exception { 5 List occupancies = new ArrayList(); 6 for (Iterator it = chat.getRooms(); it.hasNext(); ) { 7 Room room = (Room)it.next(); 8 collectOccupants(occupancies, room); 9 } 10 return occupancies.toArray(); 11 } 12 public Class getTargetClass() { 13 return Occupancy.class; 14 } 15 private void collectOccupants(List occupancies, Room room) { 16 for (Iterator it = room.users(); it.hasNext(); ) { 17 User user = (User)it.next(); 18 Occupancy occupant = new Occupancy(room.getName(), 19 user.getName()); 20 occupancies.add(occupant); 21 } 22 } 23 } Listing 23.2. Occupancy.javapublic class Occupancy { public String room, user; public Occupancy(String roomName, String userName) { this.room = roomName; this.user = userName; } } Inherited by OccupantList, the doTable() code in fit.RowFixture has four major steps to carry out:
Questions & Answers
|