Section 28.1. Flow-Style Actions with DoFixture


28.1. Flow-Style Actions with DoFixture

The first test for the chat server from Section 10.1 on p. 71 is shown again in Figure 28.1. Fit interprets the sequence of tables in the following steps.

Figure 28.1. TestDisconnect


1.

For the first table a fixture object of class ChatStart is created, as usual. This class is shown in Listing 28.1. As this class is a DoFixture, this first fixture object is involved in the interpretation of the rest of the tables in the file or page. We call this fixture object the flow fixture object.

2.

The action in the second table results in a call to the method connectUser() of the flow fixture object. This in turn calls a method in the system under test. As the method returns true, the action is colored green.

3.

The action in the third table results in a call to the method userCreatesRoom() of the flow fixture object.

4.

The action in the fourth table results in a call to the method userEntersRoom() of the flow fixture object.

5.

The first action in the fifth table, users in room results in a call to the method usersInRoom() of the flow fixture object. This returns a Fixture, a RowFixture, that Fit uses to interpret the rest of the table. This is discussed in more detail later in this chapter.

6.

The action in the sixth table results in a call to the method disconnectUser() of the flow fixture object. As this returns TRue, the action is colored green.

7.

The action in the final table results in a call to the method occupantCount() of the flow fixture object, with the argument lotr. The result of this call is then checked against the expected value, 0, in the last cell of the row. As this value is correct, the cell containing the expected value is colored green.

As we've seen, the method usersInRoom() in Listing 28.1 returns a Fixture, which Fit then uses to interpret the rest of that table. Because it is called in the flow fixture object, this method has access to ChatRoom, the system under test, as an instance variable,[1] as well as the parameter, roomName. Together, these are used to create a copy of the users for RowFixture to use through the class ParamRowFixture. The copy is made to provide name as a public instance variable so that RowFixture can access it.

[1] So the flow fixture object avoids the need to use public static variables as global variables to share access to the system under test among fixture objects.

The class UserCopy is shown in Listing 28.2. This class simply makes the user name available to RowFixture as a public instance variable, which is not the case in the class User in the system under test.

The class ParamRowFixture is shown in Listing 28.3. This class simply stores the target class and collection and provides them through the two methods required by RowFixture.

Listing 28.1. ChatStart.java
 public class ChatStart extends DoFixture {    private ChatRoom chat = new ChatRoom();    public boolean connectUser(String userName) {       return chat.connectUser(userName);    }    public boolean userCreatesRoom(String userName, String roomName) {       return chat.userCreatesRoom(userName,roomName);    }    public boolean userEntersRoom(String userName, String roomName) {       return chat.userEntersRoom(userName,roomName);    }    public Fixture usersInRoom(String roomName) {       Set users = chat.usersInRoom(roomName);       Object[] collection = new Object[users.size()];       int i = 0;       for (Iterator it = users.iterator(); it.hasNext(); ) {          User user = (User)it.next();          collection[i++] = new UserCopy(user.getName());       }       return new ParamRowFixture(collection,UserCopy.class);    }    public boolean disconnectUser(String userName) {       return chat.disconnectUser(userName);    }    public int occupantCount(String roomName) {       return chat.occupants(roomName);     } } 

Listing 28.2. UserCopy.java
 public class UserCopy {    public String name;    public UserCopy(String name) {       this.name = name;    } } 

Listing 28.3. ParamRowFixture.java
 public class ParamRowFixture extends fit.RowFixture {    private Object[] collection;    private Class targetClass;    public ParamRowFixture(Object[] collection, Class targetClass) {       this.collection = collection;       this.targetClass = targetClass;    }    public Object[] query() throws Exception {       return collection;    }    public Class getTargetClass() {       return targetClass;    } } 



    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