28.1. Flow-Style Actions with DoFixtureThe 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
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.
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.javapublic 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.javapublic class UserCopy { public String name; public UserCopy(String name) { this.name = name; } } Listing 28.3. ParamRowFixture.javapublic 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; } } |