- Adding Behaviors to the View
- Getting the Test to Pass
- What About Players?
- Showing the Hole Cards
- Whats Next?
What About Players?
Our ultimate goal for this session is to deal cards to players. The only problem is that our application doesn’t support adding players yet! We’ll tackle that effort next, but conceivably we could hardcode some players into the application in the interim.
HoldEmApp is a controller class. Its job is to coordinate between the model and the view, in a way that neither knows anything about the other, and neither knows anything about the controller. A better term for the controller in this relationship is presenter.
The job of HoldEmAppTest, then, will be to ensure that both the model and view get updated correctly when we do things in the application. Our first test will show that when we add players to the application, the Game domain class gets updated and the TablePanel captures the new players. Listing 7 shows the test.
Listing 7 Adding players to the application.
public void testAddPlayers() { final String player1 = "one"; final String player2 = "two"; final Map<String,String> seated = new HashMap<String,String>(); TablePanel panel = new TablePanel(Game.CAPACITY) { @Override public void seat(String player, String position) { seated.put(player, position); } }; app.getFrame().setTablePanel(panel); app.addPlayer(player1); app.addPlayer(player2); assertPlayers(seated, player1, player2); } private void assertPlayers(Map<String,String> seated, String... players) { Game game = app.game(); assertEquals(players.length, game.players().size()); assertEquals(players.length, seated.size()); for (int i = 0; i < players.length; i++) { String seat = "" + (i + 1); assertEquals(seat, seated.get(players[i])); assertEquals(players[i], game.players().get(i).getName()); } }
The first part of the test creates an implementation of TablePanel with a faked seat method. The overridden implementation of seat captures whether or not it was called, by stuffing its arguments into a HashMap. The assertPlayers method uses this HashMap to ensure that it contains entries for both players. Listing 8 shows the modified HoldEmApp that makes this test pass.
Listing 8 Adding players in the application class.
public class HoldEmApp { private HoldEmFrame frame = new HoldEmFrame(); private Game game = new Game(); public void run() { frame.display(); } HoldEmFrame getFrame() { return frame; } public void addPlayer(String name) { game.add(new Player(name)); int seat = game.players().size(); frame.getTablePanel().seat(name, String.valueOf(seat)); } Game game() { return game; } }
To get the code in Listing 8 to compile, HoldEmFrame will need to return the TablePanel. Here’s the modified testCreate in HoldEmFrameTest:
public void testCreate() { ... TablePanel panel = frame.getTablePanel(); assertSame(frame.getContentPane().getComponent(0), panel); }
The slightly tricky part is setting the override instance of TablePanel into the application. To do so, we create a method on the HoldEmFrame class that supports replacing the existing panel. Listing 9 shows all of the changes we need to make to HoldEmFrame.
Listing 9 Redesigning HoldEmFrame to support testing.
public class HoldEmFrame extends JFrame { private TablePanel tablePanel; private void initialize() { ... tablePanel = new TablePanel(SEATS); getContentPane().add(tablePanel); } // for testing purposes void setTablePanel(TablePanel panel) { tablePanel = panel; getContentPane().removeAll(); getContentPane().add(tablePanel); } TablePanel getTablePanel() { return tablePanel; } }