29.2. Testing AssociationsAs we saw in Section 11.2 on p. 82, graphics can aid in the expressiveness of some tests. Figure 29.2 shows again the test from Section 11.2 for the occupancy of various users in various chat rooms (see also Plate 6). Figure 29.2. A Table Containing an Image
The fixture class UsersAndRooms is shown in Listing 29.2. The method users() in Listing 29.2 returns a DotGraphic with a String containing a series of Dot commands corresponding to the expected graph. Because this method returns a DotGraphic, DoFixture expects that
The image file referenced in the last cell of the second row of the Fit table is http://files/ChatGraph.gif, so the file http://files/ChatGraph.dot is also expected in that folder. The method users() returns a DotGraphic with a String in Dot format, as follows: digraph G { lotr->luke; lotr->anna; } This string defines three nodeslotr, luke, and annawith links from lotr to the other two nodes. Listing 29.1. TaxInvoice.javapublic class TaxInvoice extends fitnesse.fixtures.TableFixture { private Invoice invoice = new Invoice(); protected void doStaticTable(int rows) { check(3,3,invoice.getCustomer()); checkAddress(); checkDelivery(); check(0,3,invoice.getAccountNumber()); check(1,3,invoice.getDateAsString()); check(2,3,invoice.getOrderNo()); Order[] orderItems = invoice.getOrders(); checkOrderItems(orderItems); checkSpecialDelivery(orderItems.length); checkTotal(orderItems.length); } private void checkOrderItems(Order[] orderItems) { int firstRow = 5; for (int row = 0; row < orderItems.length; row++) { Order item = orderItems [row]; check(firstRow+row,0,""+item.getQuantity()); check(firstRow+row,1,item.getPart()); check(firstRow+row,2,""+item.getDescription()); check(firstRow+row,3,""+item.getDispatched()); check(firstRow+row,4,item.getPrice()); check(firstRow+row,5,item.getTotal()); } } private void check(int row, int column, String expected) { if (expected.equals(getText(row,column))) right(row,column); else wrong(row,column,expected); } private void check(int row, int column, double expected) { double actual = Double.valueOf(getText(row,column)).doubleValue(); if (expected == actual) right(row,column); else wrong(row,column,""+expected); } // ... } Listing 29.2. UsersAndRooms.javapublic class UsersAndRooms extends DoFixture { private ChatServer chat = new ChatServer(); public DotGraphic users() { String dot = "digraph G {\n"; for (Iterator itRoom = chat.getRooms(); itRoom.hasNext(); ) { Room room = (Room)itRoom.next(); for (Iterator itUser = room.users(); itUser.hasNext(); ) { User user = (User)itUser.next(); dot += room.getName()+"->"+user.getName()+";\n"; } } return new DotGraphic(dot+"}\n"); } } The String in Dot format, as returned by the method users(), is compared against the contents of the ChatGraph.dot file. If they are the same, the test passes. Otherwise, the actual String returned by the users() method is written to a file in the same folder and Dot is run to generate a corresponding GIF file. The reported table then includes a reference to this new GIF file as the actual result. Note Dot is an application for laying out and producing graphsmade up of nodes and arcsfrom a text file of commands that describe the graph. Dot is available free from [Dot], including a user manual. |