From 0640d57928bc0681227c2e513b501e437201fdf8 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Mon, 15 May 2017 20:48:57 +0200 Subject: Add test for Lobby.removePlayer() --- .../java/org/luxons/sevenwonders/lobby/Lobby.java | 23 ++++++++++++++-------- .../org/luxons/sevenwonders/lobby/LobbyTest.java | 21 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 10 deletions(-) (limited to 'backend') diff --git a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java index 0b8ef0b2..b143538a 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java +++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Lobby.java @@ -62,7 +62,7 @@ public class Lobby { throw new GameAlreadyStartedException(); } if (maxPlayersReached()) { - throw new PlayerOverflowException(); + throw new PlayerOverflowException(gameDefinition.getMaxPlayers()); } if (playerNameAlreadyUsed(player.getDisplayName())) { throw new PlayerNameAlreadyUsedException(player.getDisplayName()); @@ -85,7 +85,7 @@ public class Lobby { public synchronized Game startGame() throws PlayerUnderflowException { if (!hasEnoughPlayers()) { - throw new PlayerUnderflowException(); + throw new PlayerUnderflowException(gameDefinition.getMinPlayers()); } state = State.PLAYING; return gameDefinition.initGame(id, settings, players.size()); @@ -95,7 +95,7 @@ public class Lobby { return players.size() >= gameDefinition.getMinPlayers(); } - public void reorderPlayers(List orderedUsernames) { + public void reorderPlayers(List orderedUsernames) throws UnknownPlayerException { List formerList = new ArrayList<>(players); players.clear(); for (int i = 0; i < orderedUsernames.size(); i++) { @@ -105,7 +105,7 @@ public class Lobby { } } - private static Player getPlayer(List players, String username) { + private static Player getPlayer(List players, String username) throws UnknownPlayerException { return players.stream() .filter(p -> p.getUsername().equals(username)) .findAny() @@ -120,26 +120,33 @@ public class Lobby { return players.stream().anyMatch(p -> p.getUsername().equals(username)); } - public void removePlayer(String username) { - players.removeIf(p -> p.getUsername().equals(username)); + public void removePlayer(String username) throws UnknownPlayerException { + Player player = getPlayer(players, username); + players.remove(player); } static class GameAlreadyStartedException extends IllegalStateException { } static class PlayerOverflowException extends IllegalStateException { + PlayerOverflowException(int max) { + super(String.format("maximum %d players allowed", max)); + } } static class PlayerUnderflowException extends IllegalStateException { + PlayerUnderflowException(int min) { + super(String.format("minimum %d players required to start a game", min)); + } } - static class PlayerNameAlreadyUsedException extends RuntimeException { + static class PlayerNameAlreadyUsedException extends IllegalStateException { PlayerNameAlreadyUsedException(String name) { super(name); } } - static class UnknownPlayerException extends IllegalArgumentException { + static class UnknownPlayerException extends IllegalStateException { UnknownPlayerException(String username) { super(username); } diff --git a/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java index c47e30b4..9ce4ce0c 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java @@ -128,7 +128,21 @@ public class LobbyTest { } @Test - public void reorderPlayers_failsOnSameName() { + public void removePlayer_failsWhenNotPresent() { + thrown.expect(UnknownPlayerException.class); + lobby.removePlayer("anyname"); + } + + @Test + public void removePlayer_success() { + Player player = new Player("testuser", "Test User"); + lobby.addPlayer(player); + lobby.removePlayer("testuser"); + assertFalse(lobby.containsUser("testuser")); + } + + @Test + public void reorderPlayers_success() { Player player1 = new Player("testuser1", "Test User 1"); Player player2 = new Player("testuser2", "Test User 2"); Player player3 = new Player("testuser3", "Test User 3"); @@ -139,6 +153,9 @@ public class LobbyTest { assertEquals("testuser3", lobby.getPlayers().get(0).getUsername()); assertEquals("testuser1", lobby.getPlayers().get(1).getUsername()); assertEquals("testuser2", lobby.getPlayers().get(2).getUsername()); + assertEquals(0, lobby.getPlayers().get(0).getIndex()); + assertEquals(1, lobby.getPlayers().get(1).getIndex()); + assertEquals(2, lobby.getPlayers().get(2).getIndex()); } @Test(expected = UnknownPlayerException.class) @@ -164,7 +181,7 @@ public class LobbyTest { @Theory public void startGame_succeedsAboveMinPlayers(int nbPlayers) { assumeTrue(nbPlayers >= gameDefinition.getMinPlayers()); - assumeTrue(nbPlayers < gameDefinition.getMaxPlayers()); + assumeTrue(nbPlayers <= gameDefinition.getMaxPlayers()); // there is already the owner addPlayers(nbPlayers - 1); lobby.startGame(); -- cgit