From 2e27a676edd9a6d88fc4a6f4741df7c129564a42 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Mon, 20 Dec 2010 15:01:41 +0300 Subject: [PATCH] [mage-tests] Added synchronization between steps. Updated bdd classes for step state handling. --- Mage.Common/src/mage/interfaces/Server.java | 2 +- .../src/main/java/mage/server/ServerImpl.java | 22 ++++++----------- .../java/mage/server/game/GameController.java | 7 ++++-- .../java/mage/server/game/GameManager.java | 4 ++-- .../src/test/java/org/mage/test/LandTest.java | 7 +++--- .../test/java/org/mage/test/base/MageAPI.java | 23 ++++++++++++++++-- .../java/org/mage/test/base/MageBase.java | 19 +++++++++++++-- .../org/mage/test/bdd/StepController.java | 2 +- .../java/org/mage/test/bdd/StepState.java | 2 +- .../test/java/org/mage/test/bdd/and/And.java | 8 +++++-- .../java/org/mage/test/bdd/and/Phase.java | 24 +++++++++++++------ .../test/java/org/mage/test/bdd/given/A.java | 15 ++++++++---- .../java/org/mage/test/bdd/given/Given.java | 2 +- .../org/mage/test/bdd/then/Battlefield.java | 2 +- .../java/org/mage/test/bdd/then/Then.java | 2 +- .../test/java/org/mage/test/bdd/when/I.java | 3 +-- .../java/org/mage/test/bdd/when/When.java | 2 +- 17 files changed, 96 insertions(+), 50 deletions(-) diff --git a/Mage.Common/src/mage/interfaces/Server.java b/Mage.Common/src/mage/interfaces/Server.java index 7cc8ade3c6..5ce8de4b8a 100644 --- a/Mage.Common/src/mage/interfaces/Server.java +++ b/Mage.Common/src/mage/interfaces/Server.java @@ -95,6 +95,6 @@ public interface Server extends Remote, CallbackServer { //test methods public void cheat(UUID gameId, UUID sessionId, UUID playerId, DeckCardLists deckList) throws RemoteException, MageException; - public void cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) throws RemoteException, MageException; + public boolean cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) throws RemoteException, MageException; public GameView getGameView(UUID gameId, UUID sessionId, UUID playerId) throws RemoteException, MageException; } diff --git a/Mage.Server/src/main/java/mage/server/ServerImpl.java b/Mage.Server/src/main/java/mage/server/ServerImpl.java index 59bc37a5f2..9585c34595 100644 --- a/Mage.Server/src/main/java/mage/server/ServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/ServerImpl.java @@ -56,6 +56,7 @@ import mage.server.game.ReplayManager; import mage.server.game.TableManager; import mage.server.util.ThreadExecutor; import mage.util.Logging; +import mage.view.CardView; import mage.view.ChatMessage.MessageColor; import mage.view.GameView; import mage.view.TableView; @@ -617,21 +618,12 @@ public class ServerImpl extends RemoteServer implements Server { } @Override - public void cheat(final UUID gameId, final UUID sessionId, final UUID playerId, final String cardName) throws MageException { - try { - rmiExecutor.execute( - new Runnable() { - @Override - public void run() { - if (testMode) - GameManager.getInstance().cheat(gameId, sessionId, playerId, cardName); - } - } - ); - } - catch (Exception ex) { - handleException(ex); - } + public boolean cheat(final UUID gameId, final UUID sessionId, final UUID playerId, final String cardName) throws MageException { + if (testMode) { + return GameManager.getInstance().cheat(gameId, sessionId, playerId, cardName); + } else { + return false; + } } public void handleException(Exception ex) throws MageException { diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index 9f4484ed46..8cb0b38f69 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -235,14 +235,17 @@ public class GameController implements GameCallback { updateGame(); } - public void cheat(UUID sessionId, UUID playerId, String cardName) { + public boolean cheat(UUID sessionId, UUID playerId, String cardName) { String clazz = Sets.findCard(cardName); if (clazz != null) { Card card = CardImpl.createCard(clazz); Set cards = new HashSet(); cards.add(card); game.loadCards(cards, playerId); - updateGame(); + card.moveToZone(Zone.HAND, null, game, false); + return true; + } else { + return false; } } diff --git a/Mage.Server/src/main/java/mage/server/game/GameManager.java b/Mage.Server/src/main/java/mage/server/game/GameManager.java index a28983c5f7..14148324d2 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameManager.java +++ b/Mage.Server/src/main/java/mage/server/game/GameManager.java @@ -113,8 +113,8 @@ public class GameManager { gameControllers.get(gameId).cheat(sessionId, playerId, deckList); } - public void cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) { - gameControllers.get(gameId).cheat(sessionId, playerId, cardName); + public boolean cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) { + return gameControllers.get(gameId).cheat(sessionId, playerId, cardName); } void timeout(UUID gameId, UUID sessionId) { diff --git a/Mage.Tests/src/test/java/org/mage/test/LandTest.java b/Mage.Tests/src/test/java/org/mage/test/LandTest.java index 9535a2d0c4..169afa0cc1 100644 --- a/Mage.Tests/src/test/java/org/mage/test/LandTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/LandTest.java @@ -13,11 +13,10 @@ public class LandTest extends MageAPI { @Test public void testPlayingLandInMainPhase() throws Exception { - //TODO: add test framework callback for synchronization and removing Thread.sleep calls - Given.I.have.a.card("Island"); + Given.I.have.a.card("Mountain"); And.phase.is("Precombat Main", mine); - When.I.play("Island"); - Then.battlefield.has("Island"); + When.I.play("Mountain"); + Then.battlefield.has("Mountain"); And.graveyards.empty(); } diff --git a/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java b/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java index 0b88f85fab..cbb0fcc2e7 100644 --- a/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java +++ b/Mage.Tests/src/test/java/org/mage/test/base/MageAPI.java @@ -1,7 +1,8 @@ package org.mage.test.base; -import mage.game.turn.Phase; import org.junit.BeforeClass; +import org.mage.test.bdd.StepController; +import org.mage.test.bdd.StepState; import sun.reflect.generics.reflectiveObjects.NotImplementedException; /** @@ -19,7 +20,6 @@ public class MageAPI { @BeforeClass public static void startServer() throws Exception { MageBase.getInstance().start(); - Thread.sleep(3000); } public void giveme(String card) throws Exception { @@ -50,4 +50,23 @@ public class MageAPI { public boolean checkGraveyardsEmpty() throws Exception { return MageBase.getInstance().checkGraveyardsEmpty(); } + + /** + * Defined step depending on input parameter. + * If step is UNKNOWN, then use previous remember step, otherwise remember it as current. + * + * Used for replacing "And." by "Given", "When", "Then" + * + * @param step + * @return + */ + public static StepState defineStep(StepState step) { + StepState current = step; + if (!step.equals(StepState.UNKNOWN)) { + StepController.currentState = step; + } else { + current = StepController.currentState; + } + return current; + } } diff --git a/Mage.Tests/src/test/java/org/mage/test/base/MageBase.java b/Mage.Tests/src/test/java/org/mage/test/base/MageBase.java index e00b514445..b174d9bbf1 100644 --- a/Mage.Tests/src/test/java/org/mage/test/base/MageBase.java +++ b/Mage.Tests/src/test/java/org/mage/test/base/MageBase.java @@ -16,6 +16,7 @@ import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.UUID; import java.util.logging.Level; @@ -54,6 +55,7 @@ public class MageBase { private static GameView gameView; private static String phaseToWait; private static Object sync = new Object(); + private static Object syncStart = new Object(); public void start() throws Exception { if (server == null) { @@ -70,6 +72,16 @@ public class MageBase { server.joinTable(sessionId, roomId, table.getTableId(), "Human", Sets.loadDeck("UW Control.dck")); server.joinTable(sessionId, roomId, table.getTableId(), "Computer", Sets.loadDeck("UW Control.dck")); server.startGame(sessionId, roomId, table.getTableId()); + + synchronized (syncStart) { + int waitTime = 7000; + Date prev = new Date(); + syncStart.wait(waitTime); + Date intermediate = new Date(); + if (intermediate.getTime() - prev.getTime() > waitTime - 500) { + throw new IllegalStateException("Couldn't start server"); + } + } } } @@ -96,6 +108,9 @@ public class MageBase { logger.info("ASK >> " + message.getMessage()); if (message.getMessage().equals("Do you want to take a mulligan?")) { server.sendPlayerBoolean(gameId, sessionId, false); + } + synchronized (syncStart) { + syncStart.notify(); } } else if (callback.getMethod().equals("gameTarget")) { GameClientMessage message = (GameClientMessage) callback.getData(); @@ -176,8 +191,8 @@ public class MageBase { } - public void giveme(String cardName) throws Exception { - server.cheat(gameId, sessionId, playerId, cardName); + public boolean giveme(String cardName) throws Exception { + return server.cheat(gameId, sessionId, playerId, cardName); } public boolean checkIhave(String cardName) throws Exception { diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/StepController.java b/Mage.Tests/src/test/java/org/mage/test/bdd/StepController.java index 96dad30e15..d9821bb1c1 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/StepController.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/StepController.java @@ -15,5 +15,5 @@ package org.mage.test.bdd; * */ public class StepController { - public static StepState currentState = StepState.NONE; + public static StepState currentState = StepState.UNKNOWN; } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/StepState.java b/Mage.Tests/src/test/java/org/mage/test/bdd/StepState.java index d34e36c5e0..cc264748ea 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/StepState.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/StepState.java @@ -4,5 +4,5 @@ public enum StepState { GIVEN, WHEN, THEN, - NONE + UNKNOWN } \ No newline at end of file diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java b/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java index 3243bd7ea0..4deb044354 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/and/And.java @@ -1,6 +1,10 @@ package org.mage.test.bdd.and; +import org.mage.test.bdd.StepState; +import org.mage.test.bdd.given.I; + public class And { - public static Phase phase; - public static Graveyards graveyards; + public static Phase phase = new Phase(StepState.UNKNOWN); + public static Graveyards graveyards = new Graveyards(); + public static I I = new I(StepState.UNKNOWN); } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/and/Phase.java b/Mage.Tests/src/test/java/org/mage/test/bdd/and/Phase.java index 53a5a6fbee..3854f52b8b 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/and/Phase.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/and/Phase.java @@ -2,19 +2,29 @@ package org.mage.test.bdd.and; import org.mage.test.base.MageAPI; import org.mage.test.base.MageBase; +import org.mage.test.bdd.StepState; +import org.mage.test.bdd.given.Have; import sun.reflect.generics.reflectiveObjects.NotImplementedException; import static org.mage.test.base.MageAPI.*; import static org.mage.test.base.MageAPI.Owner.*; public class Phase { - public static void is(String phase, MageAPI.Owner owner) throws Exception { - if ("Precombat Main".equals(phase) && (owner.equals(mine) || owner.equals(me))) { - MageBase.getInstance().goToPhase("Precombat Main - play spells and sorceries."); - Thread.sleep(3000); - return; + private StepState step; + public Phase(StepState step) { + this.step = step; + } + public void is(String phase, MageAPI.Owner owner) throws Exception { + StepState current = MageAPI.defineStep(this.step); + if (current.equals(StepState.GIVEN)) { + if ("Precombat Main".equals(phase) && (owner.equals(mine) || owner.equals(me))) { + MageBase.getInstance().goToPhase("Precombat Main - play spells and sorceries."); + return; + } + System.err.println("waitForPhase not implemented for phase="+phase+", owner="+owner.name()); + throw new RuntimeException("Not implemented."); + } else { + throw new RuntimeException("Not implemented for step = " + current); } - System.err.println("waitForPhase not implemented for phase="+phase+", owner="+owner.name()); - throw new NotImplementedException(); } } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java b/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java index 3de8bf0cbb..f9b5bbdfef 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/given/A.java @@ -1,5 +1,6 @@ package org.mage.test.bdd.given; +import org.mage.test.base.MageAPI; import org.mage.test.base.MageBase; import org.mage.test.bdd.StepController; import org.mage.test.bdd.StepState; @@ -10,11 +11,15 @@ public class A { this.step = step; } public void card(String cardName) throws Exception { - MageBase.getInstance().giveme(cardName); - Thread.sleep(4000); - if (!MageBase.getInstance().checkIhave(cardName)) { - throw new IllegalStateException("Couldn't find a card in hand: " + cardName); + StepState current = MageAPI.defineStep(this.step); + if (current.equals(StepState.GIVEN)) { + if (!MageBase.getInstance().giveme(cardName)) { + throw new IllegalStateException("Couldn't create card: " + cardName); + } + } else if (current.equals(StepState.THEN)) { + if (!MageBase.getInstance().checkIhave(cardName)) { + throw new IllegalStateException("Couldn't find requested card in hand: " + cardName); + } } - StepController.currentState = this.step; } } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/given/Given.java b/Mage.Tests/src/test/java/org/mage/test/bdd/given/Given.java index cb2fa48d0e..d80bd10d2b 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/given/Given.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/given/Given.java @@ -5,5 +5,5 @@ import org.mage.test.bdd.and.Phase; public class Given { public static I I = new I(StepState.GIVEN); - public static Phase phase; + public static Phase phase = new Phase(StepState.GIVEN); } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Battlefield.java b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Battlefield.java index fe59b11923..b6a2bfdbd9 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Battlefield.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Battlefield.java @@ -3,7 +3,7 @@ package org.mage.test.bdd.then; import org.mage.test.base.MageBase; public class Battlefield { - public static boolean has(String cardName) throws Exception { + public boolean has(String cardName) throws Exception { return MageBase.getInstance().checkBattlefield(cardName); } } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java index 6307c6b5cb..1dc9f269f3 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/then/Then.java @@ -1,5 +1,5 @@ package org.mage.test.bdd.then; public class Then { - public static Battlefield battlefield; + public static Battlefield battlefield = new Battlefield(); } diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/when/I.java b/Mage.Tests/src/test/java/org/mage/test/bdd/when/I.java index 053c5b6913..606b21257a 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/when/I.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/when/I.java @@ -3,8 +3,7 @@ package org.mage.test.bdd.when; import org.mage.test.base.MageBase; public class I { - public static void play(String cardName) throws Exception { + public void play(String cardName) throws Exception { MageBase.getInstance().playCard(cardName); - Thread.sleep(3000); } } \ No newline at end of file diff --git a/Mage.Tests/src/test/java/org/mage/test/bdd/when/When.java b/Mage.Tests/src/test/java/org/mage/test/bdd/when/When.java index bb99532730..41cb10a937 100644 --- a/Mage.Tests/src/test/java/org/mage/test/bdd/when/When.java +++ b/Mage.Tests/src/test/java/org/mage/test/bdd/when/When.java @@ -1,5 +1,5 @@ package org.mage.test.bdd.when; public class When { - public static I I; + public static I I = new I(); }