From 07f6ea1de0dc2bf4a6ca02b0666c1d40f2adeaaa Mon Sep 17 00:00:00 2001 From: TimonRey <31767369+TimonRey@users.noreply.github.com> Date: Tue, 10 Jul 2018 14:52:55 +0200 Subject: [PATCH] Refactoring + renaming modal classes + new method for tests --- .../controller/PlayerListController.java | 4 +- .../commercetools/pingpong/model/Game.java | 94 +++++----- .../commercetools/pingpong/model/Player.java | 40 ++-- .../service/impl/GameServiceImpl.java | 12 +- .../controller/GameControllerTest.java | 5 +- .../pingpong/model/GameTest.java | 174 +++++++++--------- .../pingpong/model/PlayerTest.java | 20 +- 7 files changed, 179 insertions(+), 170 deletions(-) diff --git a/src/main/java/com/commercetools/pingpong/controller/PlayerListController.java b/src/main/java/com/commercetools/pingpong/controller/PlayerListController.java index 67b1f69..962ebe8 100644 --- a/src/main/java/com/commercetools/pingpong/controller/PlayerListController.java +++ b/src/main/java/com/commercetools/pingpong/controller/PlayerListController.java @@ -15,7 +15,7 @@ @RequestMapping("/list") public class PlayerListController { - @RequestMapping(path = "", method = RequestMethod.GET) + /**@RequestMapping(path = "", method = RequestMethod.GET) public String get(@RequestParam(value="id", required = false) String id, Model model) { final Player player1 = new Player("Andi", 12, 265); final Player player2 = new Player("Roman", 12, 264); @@ -32,6 +32,6 @@ public String get(@RequestParam(value="id", required = false) String id, Model m model.addAttribute("players", players.get(playerIndex)); } return "leaderboard"; - } + }**/ } diff --git a/src/main/java/com/commercetools/pingpong/model/Game.java b/src/main/java/com/commercetools/pingpong/model/Game.java index 686f5ea..a40518b 100644 --- a/src/main/java/com/commercetools/pingpong/model/Game.java +++ b/src/main/java/com/commercetools/pingpong/model/Game.java @@ -2,22 +2,22 @@ public class Game { - private Player playerOne; - private Player playerTwo; + private Player leftPlayer; + private Player rightPlayer; public Game(Player firstPlayer, Player secondPlayer) { - playerOne = firstPlayer; - playerTwo = secondPlayer; + leftPlayer = firstPlayer; + rightPlayer = secondPlayer; } public void updateScoreOfPlayer(Player player, int actionType) { if (actionType == 1 && !hasSomebodyWonSet()) { - player.addPoint(); + player.addSetPoint(); decideWhoServes(); } else if (actionType == 2 && player.getSetScore() > 0) { - player.subPoint(); + player.subSetPoint(); whoServesAfterDoubleClick(); } else if (actionType == 3) { @@ -31,13 +31,13 @@ public void updateScoreOfPlayer(Player player, int actionType) { } public void updateMatchScoreOfPlayers() { - if (hasPlayerWonSet(playerOne, playerTwo) && !isMatchOver()) { - playerOne.addMatchScore(); + if (hasPlayerWonSet(leftPlayer, rightPlayer) && !isMatchOver()) { + leftPlayer.addMatchPoint(); resetSetScores(); whoBeginsServing(); - } else if (hasPlayerWonSet(playerTwo, playerOne) && !isMatchOver()) { - playerTwo.addMatchScore(); + } else if (hasPlayerWonSet(rightPlayer, leftPlayer) && !isMatchOver()) { + rightPlayer.addMatchPoint(); resetSetScores(); whoBeginsServing(); } @@ -52,7 +52,7 @@ public void gameOver() { } public boolean hasSomebodyWonSet() { - return (hasPlayerWonSet(playerOne, playerTwo) || hasPlayerWonSet(playerTwo, playerOne)); + return (hasPlayerWonSet(leftPlayer, rightPlayer) || hasPlayerWonSet(rightPlayer, leftPlayer)); } public boolean hasPlayerWonSet(Player playerWhoIsWinning, Player playerWhoIsLoosing) { @@ -68,7 +68,7 @@ public boolean hasPlayerWonSet(Player playerWhoIsWinning, Player playerWhoIsLoos } public boolean isMatchOver() { - return (playerOne.getMatchScore() == 2 || playerTwo.getMatchScore() == 2); + return (leftPlayer.getMatchScore() == 2 || rightPlayer.getMatchScore() == 2); } public boolean isTwoPointDifference(Player winningPlayer, Player losingPlayer) { @@ -76,59 +76,59 @@ public boolean isTwoPointDifference(Player winningPlayer, Player losingPlayer) { } public void resetSetScores() { - playerOne.resetSet(); - playerTwo.resetSet(); + leftPlayer.resetSetScore(); + rightPlayer.resetSetScore(); } public void resetMatchScores() { - playerOne.resetMatchScore(); - playerTwo.resetMatchScore(); + leftPlayer.resetMatchScore(); + rightPlayer.resetMatchScore(); } public void resetServe() { - playerOne.unsetServe(); - playerTwo.unsetServe(); + leftPlayer.unsetServe(); + rightPlayer.unsetServe(); } public void resetBeginningServe() { - playerOne.unsetBeginningServe(); - playerTwo.unsetBeginningServe(); + leftPlayer.unsetBeginningServe(); + rightPlayer.unsetBeginningServe(); } public boolean areBothSetScoresZero() { - return (playerOne.getSetScore() == 0 && playerTwo.getSetScore() == 0); + return (leftPlayer.getSetScore() == 0 && rightPlayer.getSetScore() == 0); } public boolean areBothMatchScoresZero() { - return (playerOne.getMatchScore() == 0 && playerTwo.getMatchScore() == 0); + return (leftPlayer.getMatchScore() == 0 && rightPlayer.getMatchScore() == 0); } public boolean isSomeoneServing() { - return (playerOne.amIServing() || playerTwo.amIServing()); + return (leftPlayer.amIServing() || rightPlayer.amIServing()); } public boolean isSetScoreEven() { - return ((playerOne.getSetScore() + playerTwo.getSetScore()) % 2 == 0); + return ((leftPlayer.getSetScore() + rightPlayer.getSetScore()) % 2 == 0); } public boolean isMatchScoreEven() { - return ((playerOne.getMatchScore() + playerTwo.getMatchScore()) % 2 == 0); + return ((leftPlayer.getMatchScore() + rightPlayer.getMatchScore()) % 2 == 0); } - public Player getPlayerOne() { - return playerOne; + public Player getLeftPlayer() { + return leftPlayer; } - public Player getPlayerTwo() { - return playerTwo; + public Player getRightPlayer() { + return rightPlayer; } public boolean getPlayerOneServe() { - return playerOne.amIServing(); + return leftPlayer.amIServing(); } public boolean getPlayerTwoServe() { - return playerTwo.amIServing(); + return rightPlayer.amIServing(); } public void changeServingPlayer(Player nextServingPlayer, Player lastServingPlayer) { @@ -149,11 +149,11 @@ public boolean isOvertime(Player playerOne, Player playerTwo) { public void whoServesAfterDoubleClick() { if (!isSetScoreEven()) { - if (playerOne.amIServing()) { - changeServingPlayer(playerTwo, playerOne); + if (leftPlayer.amIServing()) { + changeServingPlayer(rightPlayer, leftPlayer); - } else if (playerTwo.amIServing()) { - changeServingPlayer(playerOne, playerTwo); + } else if (rightPlayer.amIServing()) { + changeServingPlayer(leftPlayer, rightPlayer); } } } @@ -165,11 +165,11 @@ public void setFirstServe(Player actingPlayer) { public void whoBeginsServing() { - if (playerOne.didIServeAtBeginning()) { - changeBeginningServingPlayer(playerTwo, playerOne); + if (leftPlayer.didIServeAtBeginning()) { + changeBeginningServingPlayer(rightPlayer, leftPlayer); - } else if (playerTwo.didIServeAtBeginning()) { - changeBeginningServingPlayer(playerOne, playerTwo); + } else if (rightPlayer.didIServeAtBeginning()) { + changeBeginningServingPlayer(leftPlayer, rightPlayer); } } @@ -177,17 +177,17 @@ public void decideWhoServes() { if (areBothSetScoresZero() && areBothMatchScoresZero()) { return; - } else if (isOvertime(playerOne, playerTwo) && playerOne.amIServing()) { - changeServingPlayer(playerTwo, playerOne); + } else if (isOvertime(leftPlayer, rightPlayer) && leftPlayer.amIServing()) { + changeServingPlayer(rightPlayer, leftPlayer); - } else if (isOvertime(playerOne, playerTwo) && playerTwo.amIServing()) { - changeServingPlayer(playerOne, playerTwo); + } else if (isOvertime(leftPlayer, rightPlayer) && rightPlayer.amIServing()) { + changeServingPlayer(leftPlayer, rightPlayer); - } else if (playerOne.amIServing() && isSetScoreEven()) { - changeServingPlayer(playerTwo, playerOne); + } else if (leftPlayer.amIServing() && isSetScoreEven()) { + changeServingPlayer(rightPlayer, leftPlayer); - } else if (playerTwo.amIServing() && isSetScoreEven()) { - changeServingPlayer(playerOne, playerTwo); + } else if (rightPlayer.amIServing() && isSetScoreEven()) { + changeServingPlayer(leftPlayer, rightPlayer); } } } diff --git a/src/main/java/com/commercetools/pingpong/model/Player.java b/src/main/java/com/commercetools/pingpong/model/Player.java index a0928ca..c22fbc6 100644 --- a/src/main/java/com/commercetools/pingpong/model/Player.java +++ b/src/main/java/com/commercetools/pingpong/model/Player.java @@ -8,9 +8,6 @@ public class Player { private boolean serve; private boolean beginningServe; - public Player(String name, int numberOfGamesWon, int totalPoints) { - } - public Player(String name) { this.name = name; } @@ -18,24 +15,33 @@ public Player(String name) { public Player() { } - public void addPoint() { + public void customizeSetScore(int newPoints) { + setScore = setScore + newPoints; + } + + public void customizeMatchScore(int newPoints) { + matchScore = matchScore + newPoints; + } + + + public void addSetPoint() { setScore++; } - public void subPoint() { + public void subSetPoint() { setScore--; } - public void resetSet() { - setScore = 0; + public void addMatchPoint() { + matchScore++; } - public void setBeginningServe() { - beginningServe = true; + public void resetSetScore() { + setScore = 0; } - public void unsetBeginningServe() { - beginningServe = false; + public void resetMatchScore() { + matchScore = 0; } public void setServe() { @@ -46,15 +52,17 @@ public void unsetServe() { serve = false; } - public void addMatchScore() { - matchScore++; + public void setBeginningServe() { + beginningServe = true; } - public void resetMatchScore() { - matchScore = 0; + public void unsetBeginningServe() { + beginningServe = false; } + + public String getName() { return name; } @@ -75,4 +83,4 @@ public boolean amIServing() { public boolean didIServeAtBeginning() { return beginningServe; } -} +} \ No newline at end of file diff --git a/src/main/java/com/commercetools/pingpong/service/impl/GameServiceImpl.java b/src/main/java/com/commercetools/pingpong/service/impl/GameServiceImpl.java index 31c6f7a..a90078d 100644 --- a/src/main/java/com/commercetools/pingpong/service/impl/GameServiceImpl.java +++ b/src/main/java/com/commercetools/pingpong/service/impl/GameServiceImpl.java @@ -36,24 +36,24 @@ public void updateScore(Message message) { private Player getPlayer(Message message) { if (message.getButtonId().equals("1") && match.isMatchScoreEven()) { - return match.getPlayerOne(); + return match.getLeftPlayer(); } else if (message.getButtonId().equals("2") && match.isMatchScoreEven()) { - return match.getPlayerTwo(); + return match.getRightPlayer(); } else if (message.getButtonId().equals("1")) { - return match.getPlayerTwo(); + return match.getRightPlayer(); } else { - return match.getPlayerOne(); + return match.getLeftPlayer(); } } @Override public Player getLeftPlayer() { - return match.isMatchScoreEven() ? match.getPlayerOne() : match.getPlayerTwo(); + return match.isMatchScoreEven() ? match.getLeftPlayer() : match.getRightPlayer(); } @Override public Player getRightPlayer() { - return match.isMatchScoreEven() ? match.getPlayerTwo() : match.getPlayerOne(); + return match.isMatchScoreEven() ? match.getRightPlayer() : match.getLeftPlayer(); } @Override diff --git a/src/test/java/com/commercetools/pingpong/controller/GameControllerTest.java b/src/test/java/com/commercetools/pingpong/controller/GameControllerTest.java index e6c52b7..268387e 100644 --- a/src/test/java/com/commercetools/pingpong/controller/GameControllerTest.java +++ b/src/test/java/com/commercetools/pingpong/controller/GameControllerTest.java @@ -3,6 +3,8 @@ import com.commercetools.pingpong.model.Game; import com.commercetools.pingpong.model.Player; +import com.commercetools.pingpong.service.GameService; +import com.commercetools.pingpong.service.impl.GameServiceImpl; import org.junit.Test; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,8 +20,9 @@ public void shouldReturnBubbleMessage() { Player playerTestTwo = new Player("Kelso"); + GameService testGameService = new GameServiceImpl(); Game matchTest = new Game(playerTestOne, playerTestTwo); - GameController gameControllerTest = new GameController(); + GameController gameControllerTest = new GameController(testGameService); } } diff --git a/src/test/java/com/commercetools/pingpong/model/GameTest.java b/src/test/java/com/commercetools/pingpong/model/GameTest.java index a8ce6c0..d160740 100644 --- a/src/test/java/com/commercetools/pingpong/model/GameTest.java +++ b/src/test/java/com/commercetools/pingpong/model/GameTest.java @@ -12,27 +12,19 @@ public class GameTest { - public Player getLeftDummy(int setScore, int matchScore, boolean serve, boolean beginningServe) { + public Player getDummies(int setScore, int matchScore, boolean serve, boolean beginningServe) { Player leftDummyPlayer = new Player(); - for (leftDummyPlayer.getSetScore(); leftDummyPlayer.getSetScore() < setScore;) { - leftDummyPlayer.addPoint(); - } + leftDummyPlayer.customizeSetScore(setScore); - for (leftDummyPlayer.getMatchScore(); leftDummyPlayer.getMatchScore() < matchScore;) { - leftDummyPlayer.addMatchScore(); - } + leftDummyPlayer.customizeMatchScore(matchScore); if (serve) { leftDummyPlayer.setServe(); - } else { - leftDummyPlayer.unsetServe(); } if (beginningServe) { leftDummyPlayer.setBeginningServe(); - } else { - leftDummyPlayer.unsetBeginningServe(); } return leftDummyPlayer; @@ -42,24 +34,16 @@ public Player getLeftDummy(int setScore, int matchScore, boolean serve, boolean public Player getRightDummy(int setScore, int matchScore, boolean serve, boolean beginningServe) { Player rightDummyPlayer = new Player(); - for (rightDummyPlayer.getSetScore(); rightDummyPlayer.getSetScore() < setScore;) { - rightDummyPlayer.addPoint(); - } + rightDummyPlayer.customizeSetScore(setScore); - for (rightDummyPlayer.getMatchScore(); rightDummyPlayer.getMatchScore() < matchScore;) { - rightDummyPlayer.addMatchScore(); - } + rightDummyPlayer.customizeMatchScore(matchScore); if (serve) { rightDummyPlayer.setServe(); - } else { - rightDummyPlayer.unsetServe(); } if (beginningServe) { rightDummyPlayer.setBeginningServe(); - } else { - rightDummyPlayer.unsetBeginningServe(); } return rightDummyPlayer; @@ -75,8 +59,8 @@ public void shouldReturnNewGame() { Game testGame = new Game(leftTestPlayer, rightTestPlayer); - assertThat("It should return the name of the leftPlayer", testGame.getPlayerOne().getName(), is("Hugo")); - assertThat("It should return the name of the rightPlayer", testGame.getPlayerTwo().getName(), is("Boss")); + assertThat("It should return the name of the leftPlayer", testGame.getLeftPlayer().getName(), is("Hugo")); + assertThat("It should return the name of the rightPlayer", testGame.getRightPlayer().getName(), is("Boss")); } @@ -109,24 +93,24 @@ public void shouldSubSetScoreOfPlayer() { @Test public void shouldAddMatchPointOfLeftPlayer() { - Game testMatch = new Game(getLeftDummy(11, 0, true, false), getRightDummy(7, 0, false, true)); + Game testMatch = new Game(getDummies(11, 0, true, false), getRightDummy(7, 0, false, true)); testMatch.updateMatchScoreOfPlayers(); - assertThat("It should remove one matchpoint only for the left player", testMatch.getPlayerOne().getMatchScore(), is(1)); + assertThat("It should remove one matchpoint only for the left player", testMatch.getLeftPlayer().getMatchScore(), is(1)); } @Test public void shouldAddMatchPointOfRightPlayer() { - Game testMatch = new Game(getLeftDummy(7, 0, false, true), getRightDummy(11, 0, true, false)); + Game testMatch = new Game(getDummies(7, 0, false, true), getRightDummy(11, 0, true, false)); testMatch.updateMatchScoreOfPlayers(); - assertThat("It should remove one matchpoint only for the right player", testMatch.getPlayerTwo().getMatchScore(), is(1)); + assertThat("It should remove one matchpoint only for the right player", testMatch.getRightPlayer().getMatchScore(), is(1)); } @Test public void shouldChangeServingPlayer() { - Game testMatch = new Game(getLeftDummy(8,0,true,true), getRightDummy(6,0,false,false)); - testMatch.changeServingPlayer(testMatch.getPlayerTwo(),testMatch.getPlayerOne()); + Game testMatch = new Game(getDummies(8,0,true,true), getRightDummy(6,0,false,false)); + testMatch.changeServingPlayer(testMatch.getRightPlayer(),testMatch.getLeftPlayer()); assertThat("It should change the serving value of the leftPlayer into false", testMatch.getPlayerOneServe(), is(false)); assertThat("It should change the serving value of the rightPlayer into true", testMatch.getPlayerTwoServe(), is(true)); @@ -134,27 +118,27 @@ public void shouldChangeServingPlayer() { @Test public void shouldSetBeginningServingPlayer() { - Game testMatch = new Game(getLeftDummy(0,0,false,false), getRightDummy(0,0,false,false)); - testMatch.changeBeginningServingPlayer(testMatch.getPlayerOne(),testMatch.getPlayerTwo()); + Game testMatch = new Game(getDummies(0,0,false,false), getRightDummy(0,0,false,false)); + testMatch.changeBeginningServingPlayer(testMatch.getLeftPlayer(),testMatch.getRightPlayer()); - assertThat("It should set the very first serve for the leftPlayer into true", testMatch.getPlayerOne().didIServeAtBeginning(), is(true)); - assertThat("It should set the very first serve for the rightPlayer into true", testMatch.getPlayerTwo().didIServeAtBeginning(), is(false)); + assertThat("It should set the very first serve for the leftPlayer into true", testMatch.getLeftPlayer().didIServeAtBeginning(), is(true)); + assertThat("It should set the very first serve for the rightPlayer into true", testMatch.getRightPlayer().didIServeAtBeginning(), is(false)); - assertThat("It should set the normal serve value of the leftPlayer into the same value like above", testMatch.getPlayerOne().amIServing(), is(true)); - assertThat("It should set the normal serve value of the rightPlayer into the same value like above", testMatch.getPlayerTwo().amIServing(), is(false)); + assertThat("It should set the normal serve value of the leftPlayer into the same value like above", testMatch.getLeftPlayer().amIServing(), is(true)); + assertThat("It should set the normal serve value of the rightPlayer into the same value like above", testMatch.getRightPlayer().amIServing(), is(false)); } @Test public void shouldResetGameLeftPlayer() { Message testMessage = new Message("1", 3); - Game testMatch = new Game(getLeftDummy(5,1,true,false), getRightDummy(10,1,false,true)); + Game testMatch = new Game(getDummies(5,1,true,false), getRightDummy(10,1,false,true)); - testMatch.updateScoreOfPlayer(testMatch.getPlayerOne(), testMessage.getActionType()); + testMatch.updateScoreOfPlayer(testMatch.getLeftPlayer(), testMessage.getActionType()); - assertThat("", testMatch.getPlayerOne().getSetScore(), is(0)); - assertThat("", testMatch.getPlayerOne().getMatchScore(), is(0)); - assertThat("", testMatch.getPlayerOne().amIServing(), is(false)); - assertThat("", testMatch.getPlayerOne().didIServeAtBeginning(), is(false)); + assertThat("", testMatch.getLeftPlayer().getSetScore(), is(0)); + assertThat("", testMatch.getLeftPlayer().getMatchScore(), is(0)); + assertThat("", testMatch.getLeftPlayer().amIServing(), is(false)); + assertThat("", testMatch.getLeftPlayer().didIServeAtBeginning(), is(false)); // After the reset, variables of the leftPlayer should be default } @@ -162,111 +146,111 @@ public void shouldResetGameLeftPlayer() { @Test public void shouldResetGameRightPlayer() { Message testMessage = new Message("1", 3); - Game testMatch = new Game(getLeftDummy(5,1,true,false), getRightDummy(10,1,false,true)); + Game testMatch = new Game(getDummies(5,1,true,false), getRightDummy(10,1,false,true)); - testMatch.updateScoreOfPlayer(testMatch.getPlayerTwo(), testMessage.getActionType()); + testMatch.updateScoreOfPlayer(testMatch.getRightPlayer(), testMessage.getActionType()); - assertThat("", testMatch.getPlayerTwo().getSetScore(), is(0)); - assertThat("", testMatch.getPlayerTwo().getMatchScore(), is(0)); - assertThat("", testMatch.getPlayerTwo().amIServing(), is(false)); - assertThat("", testMatch.getPlayerTwo().didIServeAtBeginning(), is(false)); + assertThat("", testMatch.getRightPlayer().getSetScore(), is(0)); + assertThat("", testMatch.getRightPlayer().getMatchScore(), is(0)); + assertThat("", testMatch.getRightPlayer().amIServing(), is(false)); + assertThat("", testMatch.getRightPlayer().didIServeAtBeginning(), is(false)); // After the reset, variables of the rightPlayer should be default } @Test public void shouldResetEveryValue() { - Game testMatch = new Game(getLeftDummy(11,1,true,true), getRightDummy(6,1,false,false)); + Game testMatch = new Game(getDummies(11,1,true,true), getRightDummy(6,1,false,false)); testMatch.gameOver(); - assertThat("", testMatch.getPlayerOne().getSetScore(), is(0)); - assertThat("", testMatch.getPlayerOne().getMatchScore(), is(0)); - assertThat("", testMatch.getPlayerOne().amIServing(), is(false)); - assertThat("", testMatch.getPlayerOne().didIServeAtBeginning(), is(false)); + assertThat("", testMatch.getLeftPlayer().getSetScore(), is(0)); + assertThat("", testMatch.getLeftPlayer().getMatchScore(), is(0)); + assertThat("", testMatch.getLeftPlayer().amIServing(), is(false)); + assertThat("", testMatch.getLeftPlayer().didIServeAtBeginning(), is(false)); - assertThat("", testMatch.getPlayerTwo().getSetScore(), is(0)); - assertThat("", testMatch.getPlayerTwo().getMatchScore(), is(0)); - assertThat("", testMatch.getPlayerTwo().amIServing(), is(false)); - assertThat("", testMatch.getPlayerTwo().didIServeAtBeginning(), is(false)); + assertThat("", testMatch.getRightPlayer().getSetScore(), is(0)); + assertThat("", testMatch.getRightPlayer().getMatchScore(), is(0)); + assertThat("", testMatch.getRightPlayer().amIServing(), is(false)); + assertThat("", testMatch.getRightPlayer().didIServeAtBeginning(), is(false)); // After a game ends, each value should be reset automatically } @Test public void shouldCheckIfTheSetScoreDifferenceIsTwo() { - Game testMatch = new Game(getLeftDummy(14,1,true,false), getRightDummy(12,1,false,true)); + Game testMatch = new Game(getDummies(14,1,true,false), getRightDummy(12,1,false,true)); - assertThat("It checks if the leftPlayer has two setPoints more than the rightPlayer", testMatch.isTwoPointDifference(testMatch.getPlayerOne(), testMatch.getPlayerTwo()), is(true)); - assertThat("It checks if the rightPlayer has two setPoints more than the leftPlayer", testMatch.isTwoPointDifference(testMatch.getPlayerTwo(), testMatch.getPlayerOne()), is(false)); + assertThat("It checks if the leftPlayer has two setPoints more than the rightPlayer", testMatch.isTwoPointDifference(testMatch.getLeftPlayer(), testMatch.getRightPlayer()), is(true)); + assertThat("It checks if the rightPlayer has two setPoints more than the leftPlayer", testMatch.isTwoPointDifference(testMatch.getRightPlayer(), testMatch.getLeftPlayer()), is(false)); } @Test public void shouldFindOutWhoServesAfterDoubleClickCaseOne() { - Game scenarioOne = new Game(getLeftDummy(8,0,true,true), getRightDummy(6,0,false,false)); + Game scenarioOne = new Game(getDummies(8,0,true,true), getRightDummy(6,0,false,false)); scenarioOne.whoServesAfterDoubleClick(); - assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioOne.getPlayerOne().amIServing(), is(true)); - assertThat("It should find out if the rightPlayer is serving after a setPoint was removed", scenarioOne.getPlayerTwo().amIServing(), is(false)); + assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioOne.getLeftPlayer().amIServing(), is(true)); + assertThat("It should find out if the rightPlayer is serving after a setPoint was removed", scenarioOne.getRightPlayer().amIServing(), is(false)); } @Test public void shouldFindOutWhoServesAfterDoubleClickCaseTwo() { - Game scenarioTwo = new Game(getLeftDummy(7,0,true,true), getRightDummy(6,0,false,false)); + Game scenarioTwo = new Game(getDummies(7,0,true,true), getRightDummy(6,0,false,false)); scenarioTwo.whoServesAfterDoubleClick(); - assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioTwo.getPlayerOne().amIServing(), is(false)); - assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioTwo.getPlayerTwo().amIServing(), is(true)); + assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioTwo.getLeftPlayer().amIServing(), is(false)); + assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioTwo.getRightPlayer().amIServing(), is(true)); } @Test public void shouldFindOutWhoServesAfterDoubleClickCaseThree() { - Game scenarioThree = new Game(getLeftDummy(7,0,false,true), getRightDummy(6,0,true,false)); + Game scenarioThree = new Game(getDummies(7,0,false,true), getRightDummy(6,0,true,false)); scenarioThree.whoServesAfterDoubleClick(); - assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioThree.getPlayerOne().amIServing(), is(true)); - assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioThree.getPlayerTwo().amIServing(), is(false)); + assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioThree.getLeftPlayer().amIServing(), is(true)); + assertThat("It should find out if the leftPlayer is serving after a setPoint was removed", scenarioThree.getRightPlayer().amIServing(), is(false)); } @Test public void shouldSetTheFirstServe() { - Game testMatch = new Game(getLeftDummy(0,0,false,false), getRightDummy(0,0,false,false)); - testMatch.setFirstServe(testMatch.getPlayerOne()); + Game testMatch = new Game(getDummies(0,0,false,false), getRightDummy(0,0,false,false)); + testMatch.setFirstServe(testMatch.getLeftPlayer()); - assertThat("", testMatch.getPlayerOne().didIServeAtBeginning(), is(true)); - assertThat("", testMatch.getPlayerOne().amIServing(), is(true)); + assertThat("", testMatch.getLeftPlayer().didIServeAtBeginning(), is(true)); + assertThat("", testMatch.getLeftPlayer().amIServing(), is(true)); // After a button was pressed the first time in a game, the acting player should change both serving values into 'true' } @Test public void shouldDecideWhoWillBeginWithServingCaseOne() { - Game scenarioOne = new Game(getLeftDummy(0,1,false,false), getRightDummy(0,1,true,true)); + Game scenarioOne = new Game(getDummies(0,1,false,false), getRightDummy(0,1,true,true)); scenarioOne.whoBeginsServing(); - assertThat("", scenarioOne.getPlayerOne().amIServing(), is(true)); - assertThat("", scenarioOne.getPlayerOne().didIServeAtBeginning(), is(true)); + assertThat("", scenarioOne.getLeftPlayer().amIServing(), is(true)); + assertThat("", scenarioOne.getLeftPlayer().didIServeAtBeginning(), is(true)); - assertThat("", scenarioOne.getPlayerTwo().amIServing(), is(false)); - assertThat("", scenarioOne.getPlayerTwo().didIServeAtBeginning(), is(false)); + assertThat("", scenarioOne.getRightPlayer().amIServing(), is(false)); + assertThat("", scenarioOne.getRightPlayer().didIServeAtBeginning(), is(false)); // After a match was played, the method should decide who will start the next match and set all serving values into the correct boolean // CASE: the beginningServe of the rightPlayer is 'true' } @Test public void shouldDecideWhoWillBeginWithServingCaseTwo() { - Game scenarioTwo = new Game(getLeftDummy(0,1,true,true), getRightDummy(0,1,false,false)); + Game scenarioTwo = new Game(getDummies(0,1,true,true), getRightDummy(0,1,false,false)); scenarioTwo.whoBeginsServing(); - assertThat("", scenarioTwo.getPlayerOne().amIServing(), is(false)); - assertThat("", scenarioTwo.getPlayerOne().didIServeAtBeginning(), is(false)); + assertThat("", scenarioTwo.getLeftPlayer().amIServing(), is(false)); + assertThat("", scenarioTwo.getLeftPlayer().didIServeAtBeginning(), is(false)); - assertThat("", scenarioTwo.getPlayerTwo().amIServing(), is(true)); - assertThat("", scenarioTwo.getPlayerTwo().didIServeAtBeginning(), is(true)); + assertThat("", scenarioTwo.getRightPlayer().amIServing(), is(true)); + assertThat("", scenarioTwo.getRightPlayer().didIServeAtBeginning(), is(true)); // After a match was played, the method should decide who will start the next match and set all serving values into the correct boolean // CASE: the beginningServe of the leftPlayer is 'true' } @Test public void shouldDecideWhoServesCaseOne() { - Game scenarioOne = new Game(getLeftDummy(0,0,false,false), getRightDummy(0,0,false,false)); + Game scenarioOne = new Game(getDummies(0,0,false,false), getRightDummy(0,0,false,false)); scenarioOne.decideWhoServes(); assertThat("", scenarioOne.getPlayerOneServe(), is(false)); @@ -278,7 +262,7 @@ public void shouldDecideWhoServesCaseOne() { @Test public void shouldDecideWhoServesCaseTwo() { - Game scenarioTwo = new Game(getLeftDummy(12, 0, true, true), getRightDummy(11, 1, false, false)); + Game scenarioTwo = new Game(getDummies(12, 0, true, true), getRightDummy(11, 1, false, false)); scenarioTwo.decideWhoServes(); assertThat("", scenarioTwo.getPlayerOneServe(), is(false)); @@ -289,7 +273,7 @@ public void shouldDecideWhoServesCaseTwo() { @Test public void shouldDecideWhoServesCaseThree() { - Game scenarioThree = new Game(getLeftDummy(12, 0, false, false), getRightDummy(11, 1, true, true)); + Game scenarioThree = new Game(getDummies(12, 0, false, false), getRightDummy(11, 1, true, true)); scenarioThree.decideWhoServes(); assertThat("", scenarioThree.getPlayerOneServe(), is(true)); @@ -300,7 +284,7 @@ public void shouldDecideWhoServesCaseThree() { @Test public void shouldDecideWhoServesCaseFour() { - Game scenarioFour = new Game(getLeftDummy(8, 1, true, false), getRightDummy(6, 1, false, true)); + Game scenarioFour = new Game(getDummies(8, 1, true, false), getRightDummy(6, 1, false, true)); scenarioFour.decideWhoServes(); assertThat("", scenarioFour.getPlayerOneServe(), is(false)); @@ -311,7 +295,7 @@ public void shouldDecideWhoServesCaseFour() { @Test public void shouldDecideWhoServesCaseFive() { - Game scenarioFive = new Game(getLeftDummy(8, 1, false, true), getRightDummy(10, 1, true, false)); + Game scenarioFive = new Game(getDummies(8, 1, false, true), getRightDummy(10, 1, true, false)); scenarioFive.decideWhoServes(); assertThat("", scenarioFive.getPlayerOneServe(), is(true)); @@ -320,4 +304,20 @@ public void shouldDecideWhoServesCaseFive() { // CASE: The match is taking its normal way and the rightPlayer had the last serve } + @Test + public void shouldCoustomizeSetScore() { + Game gameTest = new Game(getDummies(4,1,true,false), getRightDummy(2,0,false,true)); + gameTest.getLeftPlayer().customizeSetScore(6); + + assertThat("", gameTest.getLeftPlayer().getSetScore(), is(10)); + } + + @Test + public void shouldCoustomizeMatchScore() { + Game gameTest = new Game(getDummies(4,0,true,false), getRightDummy(2,0,false,true)); + gameTest.getLeftPlayer().customizeMatchScore(1); + + assertThat("", gameTest.getLeftPlayer().getMatchScore(), is(1)); + } + } diff --git a/src/test/java/com/commercetools/pingpong/model/PlayerTest.java b/src/test/java/com/commercetools/pingpong/model/PlayerTest.java index b5baf63..64e273b 100644 --- a/src/test/java/com/commercetools/pingpong/model/PlayerTest.java +++ b/src/test/java/com/commercetools/pingpong/model/PlayerTest.java @@ -1,7 +1,5 @@ package com.commercetools.pingpong.model; -import org.assertj.core.error.ShouldBe; -import org.assertj.core.error.ShouldBeEqual; import org.junit.Before; import org.junit.Test; @@ -23,7 +21,7 @@ public void shouldReturnNewPlayer() { @Test public void shouldAddSetPoint() { Player testPlayer = new Player(); - testPlayer.addPoint(); + testPlayer.addSetPoint(); assertThat("It should add one SetPoint", testPlayer.getSetScore(), is(1)); } @@ -31,16 +29,16 @@ public void shouldAddSetPoint() { @Test public void shouldAddMatchPoint() { Player testPlayer = new Player(); - testPlayer.addMatchScore(); + testPlayer.addMatchPoint(); assertThat("It should add one MatchPoint", testPlayer.getMatchScore(), is(1)); } @Test public void shouldSubSetPoint() { Player testPlayer = new Player(); - testPlayer.addPoint(); - testPlayer.addPoint(); - testPlayer.subPoint(); + testPlayer.addSetPoint(); + testPlayer.addSetPoint(); + testPlayer.subSetPoint(); assertThat("Should remove one SetPoint", testPlayer.getSetScore(), is(1)); } @@ -48,9 +46,9 @@ public void shouldSubSetPoint() { @Test public void shouldResetSetPoints() { Player testPlayer = new Player(); - testPlayer.addPoint(); - testPlayer.addPoint(); - testPlayer.resetSet(); + testPlayer.addSetPoint(); + testPlayer.addSetPoint(); + testPlayer.resetSetScore(); assertThat("Should reset all SetPoints", testPlayer.getSetScore(), is(0)); } @@ -59,7 +57,7 @@ public void shouldResetSetPoints() { public void shouldResetMatchPoints() { Player testPlayer = new Player(); - testPlayer.addMatchScore(); + testPlayer.addMatchPoint(); testPlayer.resetMatchScore(); assertThat("Should reset all MatchPoints", testPlayer.getMatchScore(), is(0));