summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Lobby.java8
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/LobbyTest.java130
2 files changed, 134 insertions, 4 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Lobby.java b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
index 35f72f0f..b205f269 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Lobby.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
@@ -90,16 +90,16 @@ public class Lobby {
return players.stream().anyMatch(p -> p.getUserName().equals(userName));
}
- private static class GameAlreadyStartedException extends IllegalStateException {
+ static class GameAlreadyStartedException extends IllegalStateException {
}
- private static class PlayerOverflowException extends IllegalStateException {
+ static class PlayerOverflowException extends IllegalStateException {
}
- private static class PlayerUnderflowException extends IllegalStateException {
+ static class PlayerUnderflowException extends IllegalStateException {
}
- private static class PlayerNameAlreadyUsedException extends UniqueIdAlreadyUsedException {
+ static class PlayerNameAlreadyUsedException extends UniqueIdAlreadyUsedException {
PlayerNameAlreadyUsedException(String name) {
super(name);
}
diff --git a/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java
new file mode 100644
index 00000000..8e59dea1
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java
@@ -0,0 +1,130 @@
+package org.luxons.sevenwonders.game;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.luxons.sevenwonders.game.Lobby.GameAlreadyStartedException;
+import org.luxons.sevenwonders.game.Lobby.PlayerNameAlreadyUsedException;
+import org.luxons.sevenwonders.game.Lobby.PlayerOverflowException;
+import org.luxons.sevenwonders.game.Lobby.PlayerUnderflowException;
+import org.luxons.sevenwonders.game.data.GameDefinition;
+import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
+
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+@RunWith(Theories.class)
+public class LobbyTest {
+
+ @DataPoints
+ public static int[] nbPlayers() {
+ return new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ }
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private static GameDefinition gameDefinition;
+
+ private Player gameOwner;
+
+ private Lobby lobby;
+
+ @BeforeClass
+ public static void loadDefinition() {
+ gameDefinition = new GameDefinitionLoader().getGameDefinition();
+ }
+
+ @Before
+ public void setUp() {
+ gameOwner = new Player("Game owner", "gameowner");
+ lobby = new Lobby(0, "Test Game", gameOwner, gameDefinition);
+ }
+
+ @Test
+ public void isOwner_falseWhenNull() {
+ assertFalse(lobby.isOwner(null));
+ }
+
+ @Test
+ public void isOwner_falseWhenEmptyString() {
+ assertFalse(lobby.isOwner(""));
+ }
+
+ @Test
+ public void isOwner_falseWhenGarbageString() {
+ assertFalse(lobby.isOwner("this is garbage"));
+ }
+
+ @Test
+ public void isOwner_trueWhenOwnerUserName() {
+ assertTrue(lobby.isOwner(gameOwner.getUserName()));
+ }
+
+ @Test
+ public void isOwner_falseWhenOtherPlayerName() {
+ Player player = new Player("Test User", "testuser");
+ lobby.addPlayer(player);
+ assertFalse(lobby.isOwner(player.getUserName()));
+ }
+
+ @Test
+ public void addPlayer_success() {
+ Player player = new Player("Test User", "testuser");
+ lobby.addPlayer(player);
+ assertTrue(lobby.containsUser("testuser"));
+ }
+
+ @Test(expected = PlayerNameAlreadyUsedException.class)
+ public void addPlayer_failsOnSameName() {
+ Player player = new Player("Test User", "testuser");
+ Player player2 = new Player("Test User", "testuser2");
+ lobby.addPlayer(player);
+ lobby.addPlayer(player2);
+ }
+
+ @Test(expected = PlayerOverflowException.class)
+ public void addPlayer_playerOverflowWhenTooMany() {
+ // the owner + the max number gives an overflow
+ addPlayers(gameDefinition.getMaxPlayers());
+ }
+
+ @Test(expected = GameAlreadyStartedException.class)
+ public void addPlayer_failWhenGameStarted() {
+ // total with owner is the minimum
+ addPlayers(gameDefinition.getMinPlayers() - 1);
+ lobby.startGame(new Settings());
+ lobby.addPlayer(new Player("The Late Guy", "soonerNextTime"));
+ }
+
+ private void addPlayers(int nbPlayers) {
+ for (int i = 0; i < nbPlayers; i++) {
+ Player player = new Player("Test User " + i, "testuser" + i);
+ lobby.addPlayer(player);
+ }
+ }
+
+ @Theory
+ public void startGame_failsBelowMinPlayers(int nbPlayers) {
+ assumeTrue(nbPlayers < gameDefinition.getMinPlayers());
+ thrown.expect(PlayerUnderflowException.class);
+ // there is already the owner
+ addPlayers(nbPlayers - 1);
+ lobby.startGame(new Settings());
+ }
+
+ @Theory
+ public void startGame_succeedsAboveMinPlayers(int nbPlayers) {
+ assumeTrue(nbPlayers >= gameDefinition.getMinPlayers());
+ assumeTrue(nbPlayers < gameDefinition.getMaxPlayers());
+ // there is already the owner
+ addPlayers(nbPlayers - 1);
+ lobby.startGame(new Settings());
+ }
+} \ No newline at end of file
bgstack15