[mage-tests] Added synchronization between steps. Updated bdd classes for step state handling.

This commit is contained in:
magenoxx 2010-12-20 15:01:41 +03:00
parent 49eac2c737
commit 2e27a676ed
17 changed files with 96 additions and 50 deletions

View file

@ -95,6 +95,6 @@ public interface Server extends Remote, CallbackServer {
//test methods //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, 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; public GameView getGameView(UUID gameId, UUID sessionId, UUID playerId) throws RemoteException, MageException;
} }

View file

@ -56,6 +56,7 @@ import mage.server.game.ReplayManager;
import mage.server.game.TableManager; import mage.server.game.TableManager;
import mage.server.util.ThreadExecutor; import mage.server.util.ThreadExecutor;
import mage.util.Logging; import mage.util.Logging;
import mage.view.CardView;
import mage.view.ChatMessage.MessageColor; import mage.view.ChatMessage.MessageColor;
import mage.view.GameView; import mage.view.GameView;
import mage.view.TableView; import mage.view.TableView;
@ -617,21 +618,12 @@ public class ServerImpl extends RemoteServer implements Server {
} }
@Override @Override
public void cheat(final UUID gameId, final UUID sessionId, final UUID playerId, final String cardName) throws MageException { public boolean cheat(final UUID gameId, final UUID sessionId, final UUID playerId, final String cardName) throws MageException {
try { if (testMode) {
rmiExecutor.execute( return GameManager.getInstance().cheat(gameId, sessionId, playerId, cardName);
new Runnable() { } else {
@Override return false;
public void run() { }
if (testMode)
GameManager.getInstance().cheat(gameId, sessionId, playerId, cardName);
}
}
);
}
catch (Exception ex) {
handleException(ex);
}
} }
public void handleException(Exception ex) throws MageException { public void handleException(Exception ex) throws MageException {

View file

@ -235,14 +235,17 @@ public class GameController implements GameCallback {
updateGame(); updateGame();
} }
public void cheat(UUID sessionId, UUID playerId, String cardName) { public boolean cheat(UUID sessionId, UUID playerId, String cardName) {
String clazz = Sets.findCard(cardName); String clazz = Sets.findCard(cardName);
if (clazz != null) { if (clazz != null) {
Card card = CardImpl.createCard(clazz); Card card = CardImpl.createCard(clazz);
Set<Card> cards = new HashSet<Card>(); Set<Card> cards = new HashSet<Card>();
cards.add(card); cards.add(card);
game.loadCards(cards, playerId); game.loadCards(cards, playerId);
updateGame(); card.moveToZone(Zone.HAND, null, game, false);
return true;
} else {
return false;
} }
} }

View file

@ -113,8 +113,8 @@ public class GameManager {
gameControllers.get(gameId).cheat(sessionId, playerId, deckList); gameControllers.get(gameId).cheat(sessionId, playerId, deckList);
} }
public void cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) { public boolean cheat(UUID gameId, UUID sessionId, UUID playerId, String cardName) {
gameControllers.get(gameId).cheat(sessionId, playerId, cardName); return gameControllers.get(gameId).cheat(sessionId, playerId, cardName);
} }
void timeout(UUID gameId, UUID sessionId) { void timeout(UUID gameId, UUID sessionId) {

View file

@ -13,11 +13,10 @@ public class LandTest extends MageAPI {
@Test @Test
public void testPlayingLandInMainPhase() throws Exception { public void testPlayingLandInMainPhase() throws Exception {
//TODO: add test framework callback for synchronization and removing Thread.sleep calls Given.I.have.a.card("Mountain");
Given.I.have.a.card("Island");
And.phase.is("Precombat Main", mine); And.phase.is("Precombat Main", mine);
When.I.play("Island"); When.I.play("Mountain");
Then.battlefield.has("Island"); Then.battlefield.has("Mountain");
And.graveyards.empty(); And.graveyards.empty();
} }

View file

@ -1,7 +1,8 @@
package org.mage.test.base; package org.mage.test.base;
import mage.game.turn.Phase;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.mage.test.bdd.StepController;
import org.mage.test.bdd.StepState;
import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/** /**
@ -19,7 +20,6 @@ public class MageAPI {
@BeforeClass @BeforeClass
public static void startServer() throws Exception { public static void startServer() throws Exception {
MageBase.getInstance().start(); MageBase.getInstance().start();
Thread.sleep(3000);
} }
public void giveme(String card) throws Exception { public void giveme(String card) throws Exception {
@ -50,4 +50,23 @@ public class MageAPI {
public boolean checkGraveyardsEmpty() throws Exception { public boolean checkGraveyardsEmpty() throws Exception {
return MageBase.getInstance().checkGraveyardsEmpty(); 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;
}
} }

View file

@ -16,6 +16,7 @@ import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -54,6 +55,7 @@ public class MageBase {
private static GameView gameView; private static GameView gameView;
private static String phaseToWait; private static String phaseToWait;
private static Object sync = new Object(); private static Object sync = new Object();
private static Object syncStart = new Object();
public void start() throws Exception { public void start() throws Exception {
if (server == null) { 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(), "Human", Sets.loadDeck("UW Control.dck"));
server.joinTable(sessionId, roomId, table.getTableId(), "Computer", Sets.loadDeck("UW Control.dck")); server.joinTable(sessionId, roomId, table.getTableId(), "Computer", Sets.loadDeck("UW Control.dck"));
server.startGame(sessionId, roomId, table.getTableId()); 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()); logger.info("ASK >> " + message.getMessage());
if (message.getMessage().equals("Do you want to take a mulligan?")) { if (message.getMessage().equals("Do you want to take a mulligan?")) {
server.sendPlayerBoolean(gameId, sessionId, false); server.sendPlayerBoolean(gameId, sessionId, false);
}
synchronized (syncStart) {
syncStart.notify();
} }
} else if (callback.getMethod().equals("gameTarget")) { } else if (callback.getMethod().equals("gameTarget")) {
GameClientMessage message = (GameClientMessage) callback.getData(); GameClientMessage message = (GameClientMessage) callback.getData();
@ -176,8 +191,8 @@ public class MageBase {
} }
public void giveme(String cardName) throws Exception { public boolean giveme(String cardName) throws Exception {
server.cheat(gameId, sessionId, playerId, cardName); return server.cheat(gameId, sessionId, playerId, cardName);
} }
public boolean checkIhave(String cardName) throws Exception { public boolean checkIhave(String cardName) throws Exception {

View file

@ -15,5 +15,5 @@ package org.mage.test.bdd;
* *
*/ */
public class StepController { public class StepController {
public static StepState currentState = StepState.NONE; public static StepState currentState = StepState.UNKNOWN;
} }

View file

@ -4,5 +4,5 @@ public enum StepState {
GIVEN, GIVEN,
WHEN, WHEN,
THEN, THEN,
NONE UNKNOWN
} }

View file

@ -1,6 +1,10 @@
package org.mage.test.bdd.and; package org.mage.test.bdd.and;
import org.mage.test.bdd.StepState;
import org.mage.test.bdd.given.I;
public class And { public class And {
public static Phase phase; public static Phase phase = new Phase(StepState.UNKNOWN);
public static Graveyards graveyards; public static Graveyards graveyards = new Graveyards();
public static I I = new I(StepState.UNKNOWN);
} }

View file

@ -2,19 +2,29 @@ package org.mage.test.bdd.and;
import org.mage.test.base.MageAPI; import org.mage.test.base.MageAPI;
import org.mage.test.base.MageBase; 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 sun.reflect.generics.reflectiveObjects.NotImplementedException;
import static org.mage.test.base.MageAPI.*; import static org.mage.test.base.MageAPI.*;
import static org.mage.test.base.MageAPI.Owner.*; import static org.mage.test.base.MageAPI.Owner.*;
public class Phase { public class Phase {
public static void is(String phase, MageAPI.Owner owner) throws Exception { private StepState step;
if ("Precombat Main".equals(phase) && (owner.equals(mine) || owner.equals(me))) { public Phase(StepState step) {
MageBase.getInstance().goToPhase("Precombat Main - play spells and sorceries."); this.step = step;
Thread.sleep(3000); }
return; 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();
} }
} }

View file

@ -1,5 +1,6 @@
package org.mage.test.bdd.given; package org.mage.test.bdd.given;
import org.mage.test.base.MageAPI;
import org.mage.test.base.MageBase; import org.mage.test.base.MageBase;
import org.mage.test.bdd.StepController; import org.mage.test.bdd.StepController;
import org.mage.test.bdd.StepState; import org.mage.test.bdd.StepState;
@ -10,11 +11,15 @@ public class A {
this.step = step; this.step = step;
} }
public void card(String cardName) throws Exception { public void card(String cardName) throws Exception {
MageBase.getInstance().giveme(cardName); StepState current = MageAPI.defineStep(this.step);
Thread.sleep(4000); if (current.equals(StepState.GIVEN)) {
if (!MageBase.getInstance().checkIhave(cardName)) { if (!MageBase.getInstance().giveme(cardName)) {
throw new IllegalStateException("Couldn't find a card in hand: " + 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;
} }
} }

View file

@ -5,5 +5,5 @@ import org.mage.test.bdd.and.Phase;
public class Given { public class Given {
public static I I = new I(StepState.GIVEN); public static I I = new I(StepState.GIVEN);
public static Phase phase; public static Phase phase = new Phase(StepState.GIVEN);
} }

View file

@ -3,7 +3,7 @@ package org.mage.test.bdd.then;
import org.mage.test.base.MageBase; import org.mage.test.base.MageBase;
public class Battlefield { public class Battlefield {
public static boolean has(String cardName) throws Exception { public boolean has(String cardName) throws Exception {
return MageBase.getInstance().checkBattlefield(cardName); return MageBase.getInstance().checkBattlefield(cardName);
} }
} }

View file

@ -1,5 +1,5 @@
package org.mage.test.bdd.then; package org.mage.test.bdd.then;
public class Then { public class Then {
public static Battlefield battlefield; public static Battlefield battlefield = new Battlefield();
} }

View file

@ -3,8 +3,7 @@ package org.mage.test.bdd.when;
import org.mage.test.base.MageBase; import org.mage.test.base.MageBase;
public class I { public class I {
public static void play(String cardName) throws Exception { public void play(String cardName) throws Exception {
MageBase.getInstance().playCard(cardName); MageBase.getInstance().playCard(cardName);
Thread.sleep(3000);
} }
} }

View file

@ -1,5 +1,5 @@
package org.mage.test.bdd.when; package org.mage.test.bdd.when;
public class When { public class When {
public static I I; public static I I = new I();
} }