diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-02-02 01:02:02 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-02-02 01:02:02 +0100 |
commit | bf099f2ee34a3640b91e99a6d76ff086b77c2cc1 (patch) | |
tree | 45416494e6e88a199864d0e3f4b426ec33e5086c /backend/src/test/java | |
parent | Refactor Military to be independent of Settings (diff) | |
download | seven-wonders-bf099f2ee34a3640b91e99a6d76ff086b77c2cc1.tar.gz seven-wonders-bf099f2ee34a3640b91e99a6d76ff086b77c2cc1.tar.bz2 seven-wonders-bf099f2ee34a3640b91e99a6d76ff086b77c2cc1.zip |
Decouple Lobby and Players from the Game itself
Diffstat (limited to 'backend/src/test/java')
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java | 12 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java | 8 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java | 10 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java | 34 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java (renamed from backend/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java) | 20 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java | 61 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java | 4 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java | 2 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java | 22 |
9 files changed, 35 insertions, 138 deletions
diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java index 858a6e2c..1dbe7dc7 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -53,13 +53,13 @@ public class BoardTest { CustomizableSettings customSettings = TestUtils.createCustomizableSettings(); customSettings.setInitialGold(goldAmountInSettings); Settings settings = new Settings(5, customSettings); - Board board = new Board(TestUtils.createWonder(), null, settings); + Board board = new Board(TestUtils.createWonder(), 0, settings); assertEquals(goldAmountInSettings, board.getGold()); } @Theory public void initialProduction_containsInitialResource(ResourceType type) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); + Board board = new Board(TestUtils.createWonder(type), 0, new Settings(5)); Resources resources = TestUtils.createResources(type); assertTrue(board.getProduction().contains(resources)); assertTrue(board.getPublicProduction().contains(resources)); @@ -70,7 +70,7 @@ public class BoardTest { @FromDataPoints("gold") int goldRemoved) { assumeTrue(goldRemoved >= 0); assumeTrue(initialGold >= goldRemoved); - Board board = new Board(TestUtils.createWonder(), null, new Settings(5)); + Board board = new Board(TestUtils.createWonder(), 0, new Settings(5)); board.setGold(initialGold); board.removeGold(goldRemoved); assertEquals(initialGold - goldRemoved, board.getGold()); @@ -82,7 +82,7 @@ public class BoardTest { assumeTrue(goldRemoved >= 0); assumeTrue(initialGold < goldRemoved); thrown.expect(InsufficientFundsException.class); - Board board = new Board(TestUtils.createWonder(), null, new Settings(5)); + Board board = new Board(TestUtils.createWonder(), 0, new Settings(5)); board.setGold(initialGold); board.removeGold(goldRemoved); } @@ -90,7 +90,7 @@ public class BoardTest { @Theory public void getNbCardsOfColor_properCount_singleColor(ResourceType type, @FromDataPoints("nbCards") int nbCards, @FromDataPoints("nbCards") int nbOtherCards, Color color) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); + Board board = new Board(TestUtils.createWonder(type), 0, new Settings(5)); TestUtils.addCards(board, nbCards, nbOtherCards, color); assertEquals(nbCards, board.getNbCardsOfColor(Collections.singletonList(color))); } @@ -99,7 +99,7 @@ public class BoardTest { public void getNbCardsOfColor_properCount_multiColors(ResourceType type, @FromDataPoints("nbCards") int nbCards1, @FromDataPoints("nbCards") int nbCards2, @FromDataPoints("nbCards") int nbOtherCards, Color color1, Color color2) { - Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); + Board board = new Board(TestUtils.createWonder(type), 0, new Settings(5)); TestUtils.addCards(board, nbCards1, color1); TestUtils.addCards(board, nbCards2, color2); TestUtils.addCards(board, nbOtherCards, TestUtils.getDifferentColorFrom(color1, color2)); diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java index 4a481442..0d40bc9a 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -25,12 +25,12 @@ public class CardTest { @Before public void initBoard() { - Settings settings = new Settings(5); + Settings settings = new Settings(3); List<Board> boards = new ArrayList<>(3); - boards.add(new Board(new Wonder("TestWonder", ResourceType.WOOD), null, settings)); - boards.add(new Board(new Wonder("TestWonder", ResourceType.STONE), null, settings)); - boards.add(new Board(new Wonder("TestWonder", ResourceType.PAPYRUS), null, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.WOOD), 0, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.STONE), 1, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.PAPYRUS), 2, settings)); table = new Table(boards); Requirements treeFarmRequirements = new Requirements(); diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java index 5acc09df..a5b37edd 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java @@ -1,15 +1,10 @@ package org.luxons.sevenwonders.game.data; -import java.util.List; - import org.junit.Test; 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.test.TestUtils; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; public class GameDefinitionTest { @@ -18,8 +13,7 @@ public class GameDefinitionTest { GameDefinition gameDefinition = new GameDefinitionLoader().getGameDefinition(); assertNotNull(gameDefinition); - List<Player> players = TestUtils.createPlayers(7); - Game game = gameDefinition.initGame(0, new CustomizableSettings(), players); + Game game = gameDefinition.initGame(0, new CustomizableSettings(), 7); assertNotNull(game); } }
\ No newline at end of file diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index b8112779..a206cfb8 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -3,12 +3,8 @@ 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; @@ -17,7 +13,6 @@ 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; @@ -43,16 +38,6 @@ public class TestUtils { return new Settings(nbPlayers, createCustomizableSettings()); } - public static Game createGame(int id, int nbPlayers) { - Settings settings = createSettings(nbPlayers); - 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)); } @@ -66,26 +51,9 @@ public class TestUtils { return boards; } - 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 displayName = "Test User " + i; - Player player = new Player(username, displayName); - player.setIndex(i); - players.add(player); - } - return players; - } - private static Board createBoard(Settings settings, ResourceType initialResource) { Wonder wonder = createWonder(initialResource); - - String username = "testUser" + initialResource.getSymbol(); - String displayName = "Test User " + initialResource.getSymbol(); - Player player = new Player(username, displayName); - - return new Board(wonder, player, settings); + return new Board(wonder, 0, settings); } public static Board createBoard(ResourceType initialResource) { diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java index b959ed96..adc37d3d 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/LobbyTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/lobby/LobbyTest.java @@ -1,4 +1,4 @@ -package org.luxons.sevenwonders.game; +package org.luxons.sevenwonders.lobby; import java.util.Arrays; @@ -11,16 +11,18 @@ 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.Lobby.UnknownPlayerException; import org.luxons.sevenwonders.game.data.GameDefinition; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; - -import static org.junit.Assert.*; -import static org.junit.Assume.*; +import org.luxons.sevenwonders.lobby.Lobby.GameAlreadyStartedException; +import org.luxons.sevenwonders.lobby.Lobby.PlayerNameAlreadyUsedException; +import org.luxons.sevenwonders.lobby.Lobby.PlayerOverflowException; +import org.luxons.sevenwonders.lobby.Lobby.PlayerUnderflowException; +import org.luxons.sevenwonders.lobby.Lobby.UnknownPlayerException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; @RunWith(Theories.class) public class LobbyTest { diff --git a/backend/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java b/backend/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java deleted file mode 100644 index 5d7d558b..00000000 --- a/backend/src/test/java/org/luxons/sevenwonders/repositories/GameRepositoryTest.java +++ /dev/null @@ -1,61 +0,0 @@ -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/backend/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java b/backend/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java index f5a8d800..35618d49 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.java @@ -2,8 +2,8 @@ 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.lobby.Lobby; +import org.luxons.sevenwonders.lobby.Player; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; import org.luxons.sevenwonders.repositories.LobbyRepository.LobbyNotFoundException; diff --git a/backend/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java b/backend/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java index d9e07b3f..8a6efe8c 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.java @@ -2,7 +2,7 @@ package org.luxons.sevenwonders.repositories; import org.junit.Before; import org.junit.Test; -import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.lobby.Player; import org.luxons.sevenwonders.repositories.PlayerRepository.PlayerNotFoundException; import static org.junit.Assert.assertEquals; diff --git a/backend/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java b/backend/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java index 1a799ff3..8623ead8 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.java @@ -2,30 +2,25 @@ package org.luxons.sevenwonders.validation; import org.junit.Before; import org.junit.Test; -import org.luxons.sevenwonders.game.Game; -import org.luxons.sevenwonders.game.Lobby; -import org.luxons.sevenwonders.game.Player; +import org.luxons.sevenwonders.lobby.Lobby; +import org.luxons.sevenwonders.lobby.Player; import org.luxons.sevenwonders.game.data.GameDefinitionLoader; -import org.luxons.sevenwonders.repositories.GameRepository; -import org.luxons.sevenwonders.repositories.GameRepository.GameNotFoundException; import org.luxons.sevenwonders.repositories.LobbyRepository; import org.luxons.sevenwonders.repositories.LobbyRepository.LobbyNotFoundException; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class DestinationAccessValidatorTest { private LobbyRepository lobbyRepository; - private GameRepository gameRepository; - private DestinationAccessValidator destinationAccessValidator; @Before public void setup() { - gameRepository = new GameRepository(); lobbyRepository = new LobbyRepository(new GameDefinitionLoader()); - destinationAccessValidator = new DestinationAccessValidator(lobbyRepository, gameRepository); + destinationAccessValidator = new DestinationAccessValidator(lobbyRepository); } private Lobby createLobby(String gameName, String ownerUsername, String... otherPlayers) { @@ -40,8 +35,7 @@ public class DestinationAccessValidatorTest { private void createGame(String gameName, String ownerUsername, String... otherPlayers) { Lobby lobby = createLobby(gameName, ownerUsername, otherPlayers); - Game game = lobby.startGame(); - gameRepository.add(game); + lobby.startGame(); } @Test @@ -81,7 +75,7 @@ public class DestinationAccessValidatorTest { destinationAccessValidator.hasAccess("", "/lobby/0"); } - @Test(expected = GameNotFoundException.class) + @Test(expected = LobbyNotFoundException.class) public void validate_failWhenNoGameExist() { destinationAccessValidator.hasAccess("", "/game/0"); } @@ -93,7 +87,7 @@ public class DestinationAccessValidatorTest { destinationAccessValidator.hasAccess("doesNotMatter", "/lobby/3"); } - @Test(expected = GameNotFoundException.class) + @Test(expected = LobbyNotFoundException.class) public void validate_failWhenReferencedGameDoesNotExist() { createGame("Test Game 1", "user1", "user2", "user3"); createGame("Test Game 2", "user4", "user5", "user6"); |