diff options
Diffstat (limited to 'src/test/java/org')
6 files changed, 235 insertions, 14 deletions
diff --git a/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java index d8a06575..4a12592e 100644 --- a/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java @@ -78,15 +78,15 @@ public class LobbyTest { } @Test - public void isOwner_trueWhenOwnerUserName() { - assertTrue(lobby.isOwner(gameOwner.getUserName())); + public void isOwner_trueWhenOwnerUsername() { + assertTrue(lobby.isOwner(gameOwner.getUsername())); } @Test public void isOwner_falseWhenOtherPlayerName() { Player player = new Player("testuser", "Test User"); lobby.addPlayer(player); - assertFalse(lobby.isOwner(player.getUserName())); + assertFalse(lobby.isOwner(player.getUsername())); } @Test @@ -134,9 +134,9 @@ public class LobbyTest { lobby.addPlayer(player2); lobby.addPlayer(player3); lobby.reorderPlayers(Arrays.asList("testuser3", "testuser1", "testuser2")); - assertEquals("testuser3", lobby.getPlayers().get(0).getUserName()); - assertEquals("testuser1", lobby.getPlayers().get(1).getUserName()); - assertEquals("testuser2", lobby.getPlayers().get(2).getUserName()); + assertEquals("testuser3", lobby.getPlayers().get(0).getUsername()); + assertEquals("testuser1", lobby.getPlayers().get(1).getUsername()); + assertEquals("testuser2", lobby.getPlayers().get(2).getUsername()); } @Test(expected = UnknownPlayerException.class) diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index e0b2ccd7..b5ddb7b1 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -3,16 +3,21 @@ package org.luxons.sevenwonders.game.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.Science; import org.luxons.sevenwonders.game.boards.ScienceType; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.cards.Decks; import org.luxons.sevenwonders.game.cards.Requirements; import org.luxons.sevenwonders.game.effects.Effect; import org.luxons.sevenwonders.game.effects.ScienceProgress; @@ -26,6 +31,16 @@ import org.luxons.sevenwonders.game.wonders.WonderStage; public class TestUtils { + public static Game createGame(int id, int nbPlayers) { + Settings settings = new Settings(nbPlayers, new CustomizableSettings()); + List<Player> players = TestUtils.createPlayers(nbPlayers); + List<Board> boards = TestUtils.createBoards(nbPlayers); + List<Card> cards = TestUtils.createSampleCards(0, nbPlayers * 7); + Map<Integer, List<Card>> cardsPerAge = new HashMap<>(); + cardsPerAge.put(1, cards); + return new Game(id, settings, players, boards, new Decks(cardsPerAge)); + } + public static Table createTable(int nbPlayers) { return new Table(createBoards(nbPlayers)); } @@ -41,9 +56,9 @@ public class TestUtils { public static List<Player> createPlayers(int count) { List<Player> players = new ArrayList<>(count); for (int i = 0; i < count; i++) { - String userName = "testUser" + i; + String username = "testUser" + i; String displayName = "Test User " + i; - Player player = new Player(userName, displayName); + Player player = new Player(username, displayName); players.add(player); } return players; @@ -53,9 +68,9 @@ public class TestUtils { Settings settings = new Settings(5); Wonder wonder = createWonder(initialResource); - String userName = "testUser" + initialResource.getSymbol(); + String username = "testUser" + initialResource.getSymbol(); String displayName = "Test User " + initialResource.getSymbol(); - Player player = new Player(userName, displayName); + Player player = new Player(username, displayName); return new Board(wonder, player, settings); } diff --git a/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java new file mode 100644 index 00000000..5d7d558b --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java @@ -0,0 +1,61 @@ +package org.luxons.sevenwonders.repositories; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Game; +import org.luxons.sevenwonders.game.test.TestUtils; +import org.luxons.sevenwonders.repositories.GameRepository.GameAlreadyExistsException; +import org.luxons.sevenwonders.repositories.GameRepository.GameNotFoundException; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +public class GameRepositoryTest { + + private GameRepository repository; + + @Before + public void setUp() { + repository = new GameRepository(); + } + + @Test(expected = GameAlreadyExistsException.class) + public void add_failsOnExistingId() { + Game game1 = TestUtils.createGame(0, 5); + repository.add(game1); + Game game2 = TestUtils.createGame(0, 7); + repository.add(game2); + } + + @Test(expected = GameNotFoundException.class) + public void find_failsOnUnknownId() { + repository.find(123); + } + + @Test + public void find_returnsTheSameObject() { + Game game = TestUtils.createGame(0, 5); + repository.add(game); + assertSame(game, repository.find(0)); + } + + @Test(expected = GameNotFoundException.class) + public void remove_failsOnUnknownId() { + repository.remove(123); + } + + @Test + public void remove_succeeds() { + Game game = TestUtils.createGame(0, 5); + repository.add(game); + assertNotNull(repository.find(0)); + repository.remove(0); + try { + repository.find(0); + fail(); // the call to find() should have failed + } catch (GameNotFoundException e) { + // the game has been properly removed + } + } +}
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java new file mode 100644 index 00000000..f5a8d800 --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java @@ -0,0 +1,77 @@ +package org.luxons.sevenwonders.repositories; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Lobby; +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.game.data.GameDefinitionLoader; +import org.luxons.sevenwonders.repositories.LobbyRepository.LobbyNotFoundException; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class LobbyRepositoryTest { + + private LobbyRepository repository; + + @Before + public void setUp() { + repository = new LobbyRepository(new GameDefinitionLoader()); + } + + @Test + public void list_initiallyEmpty() { + assertTrue(repository.list().isEmpty()); + } + + @Test + public void list_returnsAllLobbies() { + Player owner = new Player("owner", "The Owner"); + Lobby lobby1 = repository.create("Test Name 1", owner); + Lobby lobby2 = repository.create("Test Name 2", owner); + assertTrue(repository.list().contains(lobby1)); + assertTrue(repository.list().contains(lobby2)); + } + + @Test + public void create_withCorrectOwner() { + Player owner = new Player("owner", "The Owner"); + Lobby lobby = repository.create("Test Name", owner); + assertTrue(lobby.isOwner(owner.getUsername())); + } + + @Test(expected = LobbyNotFoundException.class) + public void find_failsOnUnknownId() { + repository.find(123); + } + + @Test + public void find_returnsTheSameObject() { + Player owner = new Player("owner", "The Owner"); + Lobby lobby1 = repository.create("Test Name 1", owner); + Lobby lobby2 = repository.create("Test Name 2", owner); + assertSame(lobby1, repository.find(lobby1.getId())); + assertSame(lobby2, repository.find(lobby2.getId())); + } + + @Test(expected = LobbyNotFoundException.class) + public void remove_failsOnUnknownId() { + repository.remove(123); + } + + @Test + public void remove_succeeds() { + Player owner = new Player("owner", "The Owner"); + Lobby lobby1 = repository.create("Test Name 1", owner); + assertNotNull(repository.find(lobby1.getId())); + repository.remove(lobby1.getId()); + try { + repository.find(lobby1.getId()); + fail(); // the call to find() should have failed + } catch (LobbyNotFoundException e) { + // the lobby has been properly removed + } + } +}
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java index a8a7643c..d9e07b3f 100644 --- a/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java +++ b/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java @@ -1,5 +1,73 @@ package org.luxons.sevenwonders.repositories; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + public class PlayerRepositoryTest { + private PlayerRepository repository; + + @Before + public void setUp() { + repository = new PlayerRepository(); + } + + @Test + public void contains_falseIfNoUserAdded() { + assertFalse(repository.contains("anyUsername")); + } + + @Test + public void contains_trueForCreatedPlayer() { + repository.createOrUpdate("player1", "Player 1"); + assertTrue(repository.contains("player1")); + } + + @Test + public void createOrUpdate_createsProperly() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + assertEquals("player1", player1.getUsername()); + assertEquals("Player 1", player1.getDisplayName()); + } + + @Test + public void createOrUpdate_updatesDisplayName() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + Player player1Updated = repository.createOrUpdate("player1", "Much Better Name"); + assertSame(player1, player1Updated); + assertEquals("Much Better Name", player1Updated.getDisplayName()); + } + + @Test(expected = PlayerNotFoundException.class) + public void find_failsOnUnknownUsername() { + repository.find("anyUsername"); + } + + @Test + public void find_returnsTheSameObject() { + Player player1 = repository.createOrUpdate("player1", "Player 1"); + Player player2 = repository.createOrUpdate("player2", "Player 2"); + assertSame(player1, repository.find("player1")); + assertSame(player2, repository.find("player2")); + } + + @Test(expected = PlayerNotFoundException.class) + public void remove_failsOnUnknownUsername() { + repository.remove("anyUsername"); + } + + @Test + public void remove_succeeds() { + repository.createOrUpdate("player1", "Player 1"); + assertTrue(repository.contains("player1")); + repository.remove("player1"); + assertFalse(repository.contains("player1")); + } }
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java b/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java index 35b9f7c0..1a799ff3 100644 --- a/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java +++ b/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java @@ -28,8 +28,8 @@ public class DestinationAccessValidatorTest { destinationAccessValidator = new DestinationAccessValidator(lobbyRepository, gameRepository); } - private Lobby createLobby(String gameName, String ownerUserName, String... otherPlayers) { - Player owner = new Player(ownerUserName, ownerUserName); + private Lobby createLobby(String gameName, String ownerUsername, String... otherPlayers) { + Player owner = new Player(ownerUsername, ownerUsername); Lobby lobby = lobbyRepository.create(gameName, owner); for (String playerName : otherPlayers) { Player player = new Player(playerName, playerName); @@ -38,8 +38,8 @@ public class DestinationAccessValidatorTest { return lobby; } - private void createGame(String gameName, String ownerUserName, String... otherPlayers) { - Lobby lobby = createLobby(gameName, ownerUserName, otherPlayers); + private void createGame(String gameName, String ownerUsername, String... otherPlayers) { + Lobby lobby = createLobby(gameName, ownerUsername, otherPlayers); Game game = lobby.startGame(); gameRepository.add(game); } |