From 66acff81b9c9e76b817b69678b33578baa81413b Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 29 May 2015 12:46:34 +0200 Subject: [PATCH] Removed unused and redundant code from test player base classes. --- .../mage/test/multiplayer/PrimordialTest.java | 2 +- .../base/CardTestMultiPlayerBase.java | 271 +--------------- .../serverside/base/CardTestPlayerBase.java | 307 +----------------- .../base/impl/CardTestPlayerAPIImpl.java | 158 ++++++++- 4 files changed, 167 insertions(+), 571 deletions(-) diff --git a/Mage.Tests/src/test/java/org/mage/test/multiplayer/PrimordialTest.java b/Mage.Tests/src/test/java/org/mage/test/multiplayer/PrimordialTest.java index 6cd0342685..fec0313fc0 100644 --- a/Mage.Tests/src/test/java/org/mage/test/multiplayer/PrimordialTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/multiplayer/PrimordialTest.java @@ -29,7 +29,6 @@ package org.mage.test.multiplayer; import mage.constants.PhaseStep; import mage.constants.Zone; -import org.junit.Assert; import org.junit.Test; import org.mage.test.serverside.base.CardTestMultiPlayerBase; @@ -65,6 +64,7 @@ public class PrimordialTest extends CardTestMultiPlayerBase { assertPermanentCount(playerA, "Walking Corpse", 0); assertPermanentCount(playerA, "Pillarfield Ox", 1); assertGraveyardCount(playerC, "Walking Corpse", 1); + assertGraveyardCount(playerD, "Pillarfield Ox", 0); } } \ No newline at end of file diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestMultiPlayerBase.java b/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestMultiPlayerBase.java index e891527b51..36ededfe4a 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestMultiPlayerBase.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestMultiPlayerBase.java @@ -1,18 +1,14 @@ package org.mage.test.serverside.base; -import mage.constants.PhaseStep; import mage.cards.Card; import mage.cards.decks.Deck; import mage.cards.decks.importer.DeckImporterUtil; import mage.constants.MultiplayerAttackOption; import mage.constants.RangeOfInfluence; -import mage.filter.Filter; import mage.game.*; import mage.game.permanent.Permanent; import mage.players.Player; import org.junit.Assert; -import org.junit.Before; -import org.mage.test.player.TestPlayer; import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl; import java.io.File; @@ -26,274 +22,15 @@ import java.io.FileNotFoundException; */ public abstract class CardTestMultiPlayerBase extends CardTestPlayerAPIImpl { - protected enum ExpectedType { - TURN_NUMBER, - RESULT, - LIFE, - BATTLEFIELD, - GRAVEYARD, - UNKNOWN - } - - protected GameOptions gameOptions; - - @Before - public void reset() throws GameException, FileNotFoundException { - if (currentGame != null) { - logger.debug("Resetting previous game and creating new one!"); - currentGame = null; - System.gc(); - } - + @Override + protected Game createNewGameAndPlayers() throws GameException, FileNotFoundException { Game game = new FreeForAll(MultiplayerAttackOption.LEFT, RangeOfInfluence.ONE, 0, 20); playerA = createPlayer(game, playerA, "PlayerA"); playerB = createPlayer(game, playerB, "PlayerB"); playerC = createPlayer(game, playerC, "PlayerC"); playerD = createPlayer(game, playerD, "PlayerD"); - - activePlayer = playerA; - currentGame = game; - - stopOnTurn = 2; - stopAtStep = PhaseStep.UNTAP; - - for (Player player : currentGame.getPlayers().values()) { - TestPlayer testPlayer = (TestPlayer)player; - getCommands(testPlayer).clear(); - getLibraryCards(testPlayer).clear(); - getHandCards(testPlayer).clear(); - getBattlefieldCards(testPlayer).clear(); - getGraveCards(testPlayer).clear(); - } - - gameOptions = new GameOptions(); - } - - public void load(String path) throws FileNotFoundException, GameException { - String cardPath = TESTS_PATH + path; - File checkFile = new File(cardPath); - if (!checkFile.exists()) { - throw new FileNotFoundException("Couldn't find test file: " + cardPath); - } - if (checkFile.isDirectory()) { - throw new FileNotFoundException("Couldn't find test file: " + cardPath + ". It is directory."); - } - - if (currentGame != null) { - logger.debug("Resetting previous game and creating new one!"); - currentGame = null; - System.gc(); - } - - Game game = new TwoPlayerDuel(MultiplayerAttackOption.LEFT, RangeOfInfluence.ALL, 0, 20); - - playerA = createNewPlayer("ComputerA"); - playerA.setTestMode(true); - - Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - - if (deck.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size()); - } - game.addPlayer(playerA, deck); - game.loadCards(deck.getCards(), playerA.getId()); - - playerB = createNewPlayer("ComputerB"); - playerB.setTestMode(true); - Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - if (deck2.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size()); - } - game.addPlayer(playerB, deck2); - game.loadCards(deck2.getCards(), playerB.getId()); - - parseScenario(cardPath); - - activePlayer = playerA; - currentGame = game; - } - - /** - * Starts testing card by starting current game. - * - * @throws IllegalStateException In case game wasn't created previously. Use {@link #load} method to initialize the game. - */ - public void execute() throws IllegalStateException { - if (currentGame == null || activePlayer == null) { - throw new IllegalStateException("Game is not initialized. Use load method to load a test case and initialize a game."); - } - - for (Player player : currentGame.getPlayers().values()) { - TestPlayer testPlayer = (TestPlayer)player; - currentGame.cheat(player.getId(), getCommands(testPlayer)); - currentGame.cheat(player.getId(), getLibraryCards(testPlayer), getHandCards(testPlayer), - getBattlefieldCards(testPlayer), getGraveCards(testPlayer)); - } - - boolean testMode = true; - long t1 = System.nanoTime(); - - gameOptions.testMode = true; - gameOptions.stopOnTurn = stopOnTurn; - gameOptions.stopAtStep = stopAtStep; - currentGame.start(activePlayer.getId(), gameOptions); - long t2 = System.nanoTime(); - logger.debug("Winner: " + currentGame.getWinner()); - logger.info("Test has been executed. Execution time: " + (t2 - t1) / 1000000 + " ms"); - - assertTheResults(); - } - - /** - * Assert expected and actual results. - */ - private void assertTheResults() { - logger.debug("Matching expected results:"); - for (String line : expectedResults) { - boolean ok = false; - try { - ExpectedType type = getExpectedType(line); - if (type.equals(CardTestMultiPlayerBase.ExpectedType.UNKNOWN)) { - throw new AssertionError("Unknown expected type, check the line in $expected section=" + line); - } - parseType(type, line); - ok = true; - } finally { - logger.info(" " + line + " - " + (ok ? "OK" : "ERROR")); - } - } - } - - private ExpectedType getExpectedType(String line) { - if (line.startsWith("turn:")) { - return CardTestMultiPlayerBase.ExpectedType.TURN_NUMBER; - } - if (line.startsWith("result:")) { - return CardTestMultiPlayerBase.ExpectedType.RESULT; - } - if (line.startsWith("life:")) { - return CardTestMultiPlayerBase.ExpectedType.LIFE; - } - if (line.startsWith("battlefield:")) { - return CardTestMultiPlayerBase.ExpectedType.BATTLEFIELD; - } - if (line.startsWith("graveyard:")) { - return CardTestMultiPlayerBase.ExpectedType.GRAVEYARD; - } - return CardTestMultiPlayerBase.ExpectedType.UNKNOWN; - } - - private void parseType(ExpectedType type, String line) { - if (type.equals(CardTestMultiPlayerBase.ExpectedType.TURN_NUMBER)) { - int turn = getIntParam(line, 1); - Assert.assertEquals("Turn numbers are not equal", turn, currentGame.getTurnNum()); - return; - } - if (type.equals(CardTestMultiPlayerBase.ExpectedType.RESULT)) { - String expected = getStringParam(line, 1); - String actual = "draw"; - if (currentGame.getWinner().equals("Player ComputerA is the winner")) { - actual = "won"; - } else if (currentGame.getWinner().equals("Player ComputerB is the winner")) { - actual = "lost"; - } - Assert.assertEquals("Game results are not equal", expected, actual); - return; - } - if (type.equals(CardTestMultiPlayerBase.ExpectedType.LIFE)) { - String player = getStringParam(line, 1); - int expected = getIntParam(line, 2); - if (player.equals("ComputerA")) { - int actual = currentGame.getPlayer(playerA.getId()).getLife(); - Assert.assertEquals("Life amounts are not equal", expected, actual); - } else if (player.equals("ComputerB")) { - int actual = currentGame.getPlayer(playerB.getId()).getLife(); - Assert.assertEquals("Life amounts are not equal", expected, actual); - } else { - throw new IllegalArgumentException("Wrong player in 'life' line, player=" + player + ", line=" + line); - } - return; - } - if (type.equals(CardTestMultiPlayerBase.ExpectedType.BATTLEFIELD)) { - String playerName = getStringParam(line, 1); - String cardName = getStringParam(line, 2); - int expectedCount = getIntParam(line, 3); - Player player = null; - if (playerName.equals("ComputerA")) { - player = currentGame.getPlayer(playerA.getId()); - } else if (playerName.equals("ComputerB")) { - player = currentGame.getPlayer(playerB.getId()); - } else { - throw new IllegalArgumentException("Wrong player in 'battlefield' line, player=" + player + ", line=" + line); - } - int actualCount = 0; - for (Permanent permanent : currentGame.getBattlefield().getAllPermanents()) { - if (permanent.getControllerId().equals(player.getId())) { - if (permanent.getName().equals(cardName)) { - actualCount++; - } - } - } - Assert.assertEquals("(Battlefield) Card counts are not equal (" + cardName + ")", expectedCount, actualCount); - return; - } - if (type.equals(CardTestMultiPlayerBase.ExpectedType.GRAVEYARD)) { - String playerName = getStringParam(line, 1); - String cardName = getStringParam(line, 2); - int expectedCount = getIntParam(line, 3); - Player player = null; - if (playerName.equals("ComputerA")) { - player = currentGame.getPlayer(playerA.getId()); - } else if (playerName.equals("ComputerB")) { - player = currentGame.getPlayer(playerB.getId()); - } else { - throw new IllegalArgumentException("Wrong player in 'graveyard' line, player=" + player + ", line=" + line); - } - int actualCount = 0; - for (Card card : player.getGraveyard().getCards(currentGame)) { - if (card.getName().equals(cardName)) { - actualCount++; - } - } - Assert.assertEquals("(Graveyard) Card counts are not equal (" + cardName + ")", expectedCount, actualCount); - } - } - - private int getIntParam(String line, int index) { - String[] params = line.split(":"); - if (index > params.length - 1) { - throw new IllegalArgumentException("Not correct line: " + line); - } - return Integer.parseInt(params[index]); - } - - private String getStringParam(String line, int index) { - String[] params = line.split(":"); - if (index > params.length - 1) { - throw new IllegalArgumentException("Not correct line: " + line); - } - return params[index]; - } - - protected void checkPermanentPT(Player player, String cardName, int power, int toughness, Filter.ComparisonScope scope) { - if (currentGame == null) { - throw new IllegalStateException("Current game is null"); - } - if (scope.equals(Filter.ComparisonScope.All)) { - throw new UnsupportedOperationException("ComparisonScope.All is not implemented."); - } - - for (Permanent permanent : currentGame.getBattlefield().getAllActivePermanents(player.getId())) { - if (permanent.getName().equals(cardName)) { - Assert.assertEquals("Power is not the same", power, permanent.getPower().getValue()); - Assert.assertEquals("Toughness is not the same", toughness, permanent.getToughness().getValue()); - break; - } - } - } - - protected void skipInitShuffling() { - gameOptions.skipInitShuffling = true; + return game; } + } diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestPlayerBase.java b/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestPlayerBase.java index 68c08699bc..e3435c3d3f 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestPlayerBase.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/base/CardTestPlayerBase.java @@ -1,27 +1,11 @@ package org.mage.test.serverside.base; -import java.io.File; import java.io.FileNotFoundException; -import mage.cards.Card; -import mage.cards.decks.Deck; -import mage.cards.decks.importer.DeckImporterUtil; import mage.constants.MultiplayerAttackOption; -import mage.constants.PhaseStep; import mage.constants.RangeOfInfluence; -import mage.filter.Filter; import mage.game.Game; import mage.game.GameException; -import mage.game.GameOptions; import mage.game.TwoPlayerDuel; -import mage.game.permanent.Permanent; -import mage.players.Player; -import org.junit.Assert; -import org.junit.Before; -import org.mage.test.player.TestPlayer; -import static org.mage.test.serverside.base.MageTestPlayerBase.TESTS_PATH; -import static org.mage.test.serverside.base.MageTestPlayerBase.activePlayer; -import static org.mage.test.serverside.base.MageTestPlayerBase.currentGame; -import static org.mage.test.serverside.base.MageTestPlayerBase.logger; import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl; /** @@ -30,293 +14,14 @@ import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl; * @author ayratn */ public abstract class CardTestPlayerBase extends CardTestPlayerAPIImpl { - - public static final String NO_TARGET = "NO_TARGET"; - - protected enum ExpectedType { - TURN_NUMBER, - RESULT, - LIFE, - BATTLEFIELD, - GRAVEYARD, - UNKNOWN - } - - protected GameOptions gameOptions; - - public CardTestPlayerBase() { - } - + @Override - protected TestPlayer createNewPlayer(String playerName) { - return createPlayer(playerName); - } - - @Before - public void reset() throws GameException, FileNotFoundException { - if (currentGame != null) { - logger.debug("Resetting previous game and creating new one!"); - currentGame = null; - System.gc(); - } - + protected Game createNewGameAndPlayers() throws GameException, FileNotFoundException { Game game = new TwoPlayerDuel(MultiplayerAttackOption.LEFT, RangeOfInfluence.ONE, 0, 20); - playerA = createNewPlayer("PlayerA"); - playerA.setTestMode(true); - logger.debug("Loading deck..."); - Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - logger.debug("Done!"); - if (deck.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size()); - } - game.loadCards(deck.getCards(), playerA.getId()); - game.addPlayer(playerA, deck); + playerA = createPlayer(game, playerA, "PlayerA"); + playerB = createPlayer(game, playerB, "PlayerB"); + return game; + } - playerB = createNewPlayer("PlayerB"); - playerB.setTestMode(true); - Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - if (deck2.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size()); - } - game.loadCards(deck2.getCards(), playerB.getId()); - game.addPlayer(playerB, deck2); - activePlayer = playerA; - currentGame = game; - - stopOnTurn = 2; - stopAtStep = PhaseStep.UNTAP; - - for (Player player : currentGame.getPlayers().values()) { - TestPlayer testPlayer = (TestPlayer)player; - getCommands(testPlayer).clear(); - getLibraryCards(testPlayer).clear(); - getHandCards(testPlayer).clear(); - getBattlefieldCards(testPlayer).clear(); - getGraveCards(testPlayer).clear(); - } - - gameOptions = new GameOptions(); - } - - public void load(String path) throws FileNotFoundException, GameException { - String cardPath = TESTS_PATH + path; - File checkFile = new File(cardPath); - if (!checkFile.exists()) { - throw new FileNotFoundException("Couldn't find test file: " + cardPath); - } - if (checkFile.isDirectory()) { - throw new FileNotFoundException("Couldn't find test file: " + cardPath + ". It is directory."); - } - - if (currentGame != null) { - logger.debug("Resetting previous game and creating new one!"); - currentGame = null; - System.gc(); - } - - Game game = new TwoPlayerDuel(MultiplayerAttackOption.LEFT, RangeOfInfluence.ALL, 0, 20); - - playerA = createNewPlayer("ComputerA"); - playerA.setTestMode(true); - - Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - - if (deck.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size()); - } - game.addPlayer(playerA, deck); - game.loadCards(deck.getCards(), playerA.getId()); - - playerB = createNewPlayer("ComputerB"); - playerB.setTestMode(true); - Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false); - if (deck2.getCards().size() < 40) { - throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size()); - } - game.addPlayer(playerB, deck2); - game.loadCards(deck2.getCards(), playerB.getId()); - - parseScenario(cardPath); - - activePlayer = playerA; - currentGame = game; - } - - /** - * Starts testing card by starting current game. - * - * @throws IllegalStateException In case game wasn't created previously. Use {@link #load} method to initialize the game. - */ - public void execute() throws IllegalStateException { - if (currentGame == null || activePlayer == null) { - throw new IllegalStateException("Game is not initialized. Use load method to load a test case and initialize a game."); - } - - for (Player player : currentGame.getPlayers().values()) { - TestPlayer testPlayer = (TestPlayer)player; - currentGame.cheat(player.getId(), getCommands(testPlayer)); - currentGame.cheat(player.getId(), getLibraryCards(testPlayer), getHandCards(testPlayer), - getBattlefieldCards(testPlayer), getGraveCards(testPlayer)); - } - - boolean testMode = true; - long t1 = System.nanoTime(); - - gameOptions.testMode = true; - gameOptions.stopOnTurn = stopOnTurn; - gameOptions.stopAtStep = stopAtStep; - currentGame.start(activePlayer.getId(), gameOptions); - long t2 = System.nanoTime(); - logger.debug("Winner: " + currentGame.getWinner()); - logger.info("Test has been executed. Execution time: " + (t2 - t1) / 1000000 + " ms"); - - assertTheResults(); - } - - /** - * Assert expected and actual results. - */ - private void assertTheResults() { - logger.debug("Matching expected results:"); - for (String line : expectedResults) { - boolean ok = false; - try { - ExpectedType type = getExpectedType(line); - if (type.equals(CardTestPlayerBase.ExpectedType.UNKNOWN)) { - throw new AssertionError("Unknown expected type, check the line in $expected section=" + line); - } - parseType(type, line); - ok = true; - } finally { - logger.info(" " + line + " - " + (ok ? "OK" : "ERROR")); - } - } - } - - private ExpectedType getExpectedType(String line) { - if (line.startsWith("turn:")) { - return CardTestPlayerBase.ExpectedType.TURN_NUMBER; - } - if (line.startsWith("result:")) { - return CardTestPlayerBase.ExpectedType.RESULT; - } - if (line.startsWith("life:")) { - return CardTestPlayerBase.ExpectedType.LIFE; - } - if (line.startsWith("battlefield:")) { - return CardTestPlayerBase.ExpectedType.BATTLEFIELD; - } - if (line.startsWith("graveyard:")) { - return CardTestPlayerBase.ExpectedType.GRAVEYARD; - } - return CardTestPlayerBase.ExpectedType.UNKNOWN; - } - - private void parseType(ExpectedType type, String line) { - if (type.equals(CardTestPlayerBase.ExpectedType.TURN_NUMBER)) { - int turn = getIntParam(line, 1); - Assert.assertEquals("Turn numbers are not equal", turn, currentGame.getTurnNum()); - return; - } - if (type.equals(CardTestPlayerBase.ExpectedType.RESULT)) { - String expected = getStringParam(line, 1); - String actual = "draw"; - switch (currentGame.getWinner()) { - case "Player ComputerA is the winner": - actual = "won"; - break; - case "Player ComputerB is the winner": - actual = "lost"; - break; - } - Assert.assertEquals("Game results are not equal", expected, actual); - return; - } - - Player player = null; - String playerName = getStringParam(line, 1); - switch (playerName) { - case "ComputerA": - player = currentGame.getPlayer(playerA.getId()); - break; - case "ComputerB": - player = currentGame.getPlayer(playerB.getId()); - break; - } - if (player == null) { - throw new IllegalArgumentException("Wrong player in 'battlefield' line, player=" + player + ", line=" + line); - } - - if (type.equals(CardTestPlayerBase.ExpectedType.LIFE)) { - int expected = getIntParam(line, 2); - int actual = player.getLife(); - Assert.assertEquals("Life amounts are not equal", expected, actual); - return; - } - - if (type.equals(CardTestPlayerBase.ExpectedType.BATTLEFIELD)) { - String cardName = getStringParam(line, 2); - int expectedCount = getIntParam(line, 3); - int actualCount = 0; - for (Permanent permanent : currentGame.getBattlefield().getAllPermanents()) { - if (permanent.getControllerId().equals(player.getId())) { - if (permanent.getName().equals(cardName)) { - actualCount++; - } - } - } - Assert.assertEquals("(Battlefield) Card counts are not equal (" + cardName + ")", expectedCount, actualCount); - return; - } - - if (type.equals(CardTestPlayerBase.ExpectedType.GRAVEYARD)) { - String cardName = getStringParam(line, 2); - int expectedCount = getIntParam(line, 3); - int actualCount = 0; - for (Card card : player.getGraveyard().getCards(currentGame)) { - if (card.getName().equals(cardName)) { - actualCount++; - } - } - Assert.assertEquals("(Graveyard) Card counts are not equal (" + cardName + ")", expectedCount, actualCount); - } - } - - private int getIntParam(String line, int index) { - String[] params = line.split(":"); - if (index > params.length - 1) { - throw new IllegalArgumentException("Not correct line: " + line); - } - return Integer.parseInt(params[index]); - } - - private String getStringParam(String line, int index) { - String[] params = line.split(":"); - if (index > params.length - 1) { - throw new IllegalArgumentException("Not correct line: " + line); - } - return params[index]; - } - - protected void checkPermanentPT(Player player, String cardName, int power, int toughness, Filter.ComparisonScope scope) { - if (currentGame == null) { - throw new IllegalStateException("Current game is null"); - } - if (scope.equals(Filter.ComparisonScope.All)) { - throw new UnsupportedOperationException("ComparisonScope.All is not implemented."); - } - - for (Permanent permanent : currentGame.getBattlefield().getAllActivePermanents(player.getId())) { - if (permanent.getName().equals(cardName)) { - Assert.assertEquals("Power is not the same", power, permanent.getPower().getValue()); - Assert.assertEquals("Toughness is not the same", toughness, permanent.getToughness().getValue()); - break; - } - } - } - - protected void skipInitShuffling() { - gameOptions.skipInitShuffling = true; - } } diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java b/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java index 1a3774626e..a19f445bce 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java @@ -1,5 +1,6 @@ package org.mage.test.serverside.base.impl; +import java.io.FileNotFoundException; import mage.abilities.Ability; import mage.cards.Card; import mage.cards.decks.Deck; @@ -28,6 +29,8 @@ import org.mage.test.serverside.base.MageTestPlayerBase; import java.util.List; import java.util.UUID; +import mage.game.GameOptions; +import org.junit.Before; /** * API for test initialization and asserting the test results. @@ -36,6 +39,20 @@ import java.util.UUID; */ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implements CardTestAPI { + // Defines the constant if for activate ability is not target but a ability on the stack to define + public static final String NO_TARGET = "NO_TARGET"; + + protected GameOptions gameOptions; + + protected enum ExpectedType { + TURN_NUMBER, + RESULT, + LIFE, + BATTLEFIELD, + GRAVEYARD, + UNKNOWN + } + static { // CardScanner.scanned = true; CardScanner.scan(); @@ -83,7 +100,36 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement removeAllCardsFromLibrary(playerB); addCard(Zone.LIBRARY, playerB, "Plains", 10); } + + @Before + public void reset() throws GameException, FileNotFoundException { + if (currentGame != null) { + logger.debug("Resetting previous game and creating new one!"); + currentGame = null; + System.gc(); + } + currentGame = createNewGameAndPlayers(); + + activePlayer = playerA; + + stopOnTurn = 2; + stopAtStep = PhaseStep.UNTAP; + + for (Player player : currentGame.getPlayers().values()) { + TestPlayer testPlayer = (TestPlayer)player; + getCommands(testPlayer).clear(); + getLibraryCards(testPlayer).clear(); + getHandCards(testPlayer).clear(); + getBattlefieldCards(testPlayer).clear(); + getGraveCards(testPlayer).clear(); + } + + gameOptions = new GameOptions(); + } + + abstract protected Game createNewGameAndPlayers() throws GameException, FileNotFoundException; + protected TestPlayer createPlayer(Game game, TestPlayer player, String name) throws GameException { player = createNewPlayer(name); player.setTestMode(true); @@ -93,16 +139,68 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement if (deck.getCards().size() < 40) { throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size()); } - game.addPlayer(player, deck); game.loadCards(deck.getCards(), player.getId()); + game.addPlayer(player, deck); return player; } + /** + * Starts testing card by starting current game. + * + * @throws IllegalStateException In case game wasn't created previously. Use {@link #load} method to initialize the game. + */ + public void execute() throws IllegalStateException { + if (currentGame == null || activePlayer == null) { + throw new IllegalStateException("Game is not initialized. Use load method to load a test case and initialize a game."); + } + + for (Player player : currentGame.getPlayers().values()) { + TestPlayer testPlayer = (TestPlayer)player; + currentGame.cheat(player.getId(), getCommands(testPlayer)); + currentGame.cheat(player.getId(), getLibraryCards(testPlayer), getHandCards(testPlayer), + getBattlefieldCards(testPlayer), getGraveCards(testPlayer)); + } + + long t1 = System.nanoTime(); + + gameOptions.testMode = true; + gameOptions.stopOnTurn = stopOnTurn; + gameOptions.stopAtStep = stopAtStep; + currentGame.start(activePlayer.getId(), gameOptions); + long t2 = System.nanoTime(); + logger.debug("Winner: " + currentGame.getWinner()); + logger.info("Test has been executed. Execution time: " + (t2 - t1) / 1000000 + " ms"); + + } + + + protected TestPlayer createNewPlayer(String playerName) { return createPlayer(playerName); } - + + protected Player getPlayerFromName(String playerName, String line) { + Player player = null; + switch (playerName) { + case "ComputerA": + player = currentGame.getPlayer(playerA.getId()); + break; + case "ComputerB": + player = currentGame.getPlayer(playerB.getId()); + break; + case "ComputerC": + player = currentGame.getPlayer(playerC.getId()); + break; + case "ComputerD": + player = currentGame.getPlayer(playerD.getId()); + break; + default: + throw new IllegalArgumentException("Wrong player in 'battlefield' line, player=" + player + ", line=" + line); + } + return player; + } + /** * Removes all cards from player's library from the game. * Usually this should be used once before initialization to form the library in certain order. @@ -897,5 +995,61 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement public void addTarget(TestPlayer player, TestPlayer targetPlayer) { player.addTarget("targetPlayer="+targetPlayer.getName()); } + + protected void skipInitShuffling() { + gameOptions.skipInitShuffling = true; + } + + protected ExpectedType getExpectedType(String line) { + if (line.startsWith("turn:")) { + return ExpectedType.TURN_NUMBER; + } + if (line.startsWith("result:")) { + return ExpectedType.RESULT; + } + if (line.startsWith("life:")) { + return ExpectedType.LIFE; + } + if (line.startsWith("battlefield:")) { + return ExpectedType.BATTLEFIELD; + } + if (line.startsWith("graveyard:")) { + return ExpectedType.GRAVEYARD; + } + return ExpectedType.UNKNOWN; + } + + protected String getStringParam(String line, int index) { + String[] params = line.split(":"); + if (index > params.length - 1) { + throw new IllegalArgumentException("Not correct line: " + line); + } + return params[index]; + } + + protected void checkPermanentPT(Player player, String cardName, int power, int toughness, Filter.ComparisonScope scope) { + if (currentGame == null) { + throw new IllegalStateException("Current game is null"); + } + if (scope.equals(Filter.ComparisonScope.All)) { + throw new UnsupportedOperationException("ComparisonScope.All is not implemented."); + } + for (Permanent permanent : currentGame.getBattlefield().getAllActivePermanents(player.getId())) { + if (permanent.getName().equals(cardName)) { + Assert.assertEquals("Power is not the same", power, permanent.getPower().getValue()); + Assert.assertEquals("Toughness is not the same", toughness, permanent.getToughness().getValue()); + break; + } + } + } + + protected int getIntParam(String line, int index) { + String[] params = line.split(":"); + if (index > params.length - 1) { + throw new IllegalArgumentException("Not correct line: " + line); + } + return Integer.parseInt(params[index]); + } + }