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 ++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'backend/src/main/java/org') 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); } -- cgit