tests: removed outdated/unused code

This commit is contained in:
Oleg Agafonov 2025-06-15 11:24:09 +04:00
parent 4732fdf527
commit 4f8eb30e4c
23 changed files with 0 additions and 665 deletions

View file

@ -1,22 +0,0 @@
package org.mage.test.clientside;
import org.junit.Test;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.bdd.and.And;
import org.mage.test.clientside.bdd.given.Given;
import org.mage.test.clientside.bdd.then.Then;
import org.mage.test.clientside.bdd.when.When;
import static org.mage.test.clientside.base.MageAPI.Owner.*;
public class LandTest extends MageAPI {
@Test
public void testPlayingLandInMainPhase() throws Exception {
Given.I.have.a.card("Mountain");
And.phase.is("Precombat Main", mine);
When.I.play("Mountain");
Then.battlefield.has("Mountain");
And.graveyards.empty();
}
}

View file

@ -1,19 +0,0 @@
package org.mage.test.clientside;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.clientside.base.MageAPIExtended;
import static org.mage.test.clientside.base.MageAPI.Owner.mine;
public class LandTestExtended extends MageAPIExtended {
@Test
public void testPlayingLandInMainPhase() throws Exception {
addCard("Mountain", Zone.HAND);
setPhase("Precombat Main", mine);
play("Mountain");
assertBattlefield("Mountain");
assertGraveyardsCount(0);
}
}

View file

@ -1,22 +0,0 @@
package org.mage.test.clientside;
import org.junit.Test;
import org.mage.test.clientside.base.MageAPI;
public class LightningHelixTest extends MageAPI {
@Test
public void testPlayTargetOpponent() throws Exception {
//Given.I.have.a.card("Lightning Helix");
//And.battlefield.has("Mountain","Plains");
//And.phase.is("Precombat Main", mine);
//And.phase.is("End of Turn", ai);
//And.lifes(20,20);
//When.I.play("Lightning Helix");
//Then.my.life(23);
//And.ai.life(17);
//And.my.graveyard.has("Lightning Helix");
//And.ai.graveyard.empty();
//Then.graveyards.empty();
}
}

View file

@ -1,10 +0,0 @@
package org.mage.test.clientside.base;
/**
* Command pattern.
*
* @author nantuko
*/
abstract public class Command {
abstract public void execute() throws Exception;
}

View file

@ -1,42 +0,0 @@
package org.mage.test.clientside.base;
import org.junit.BeforeClass;
import org.mage.test.clientside.bdd.StepController;
import org.mage.test.clientside.bdd.StepState;
/**
* Parent class for all Mage tests.
* Provides basic actions in mage game and assert functions to check game state.
*/
public class MageAPI {
public enum Owner {
mine,
me,
ai
}
@BeforeClass
public static void startServer() throws Exception {
MageBase.getInstance().start();
}
/**
* 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

@ -1,35 +0,0 @@
package org.mage.test.clientside.base;
import mage.constants.Zone;
import org.mage.test.clientside.bdd.and.And;
import org.mage.test.clientside.bdd.given.Given;
import org.mage.test.clientside.bdd.then.Then;
import org.mage.test.clientside.bdd.when.When;
import static org.mage.test.clientside.base.MageAPI.Owner.mine;
/**
* Contains wrappers for bdd calls.
*/
public class MageAPIExtended extends MageAPI {
public void addCard(String cardName, Zone zone) throws Exception {
Given.I.have.a.card("Mountain");
}
public void setPhase(String phase, Owner owner) throws Exception {
And.phase.is("Precombat Main", mine);
}
public void play(String cardName) throws Exception {
When.I.play("Mountain");
}
public void assertBattlefield(String cardName) throws Exception {
Then.battlefield.has("Mountain");
}
public void assertGraveyardsCount(int count) throws Exception {
And.graveyards.empty();
}
}

View file

@ -1,262 +0,0 @@
package org.mage.test.clientside.base;
import mage.constants.MultiplayerAttackOption;
import mage.constants.RangeOfInfluence;
import mage.game.match.MatchOptions;
import mage.interfaces.MageException;
import mage.interfaces.Server;
import mage.interfaces.ServerState;
import mage.interfaces.callback.CallbackClient;
import mage.interfaces.callback.CallbackClientDaemon;
import mage.interfaces.callback.ClientCallback;
import mage.server.Main;
import mage.sets.Sets;
import mage.util.Logging;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Date;
import java.util.UUID;
/**
* Base for starting Mage server. Controls interactions between MageAPI and Mage
* Server.
*
* @author nantuko
*/
public class MageBase {
/**
* MageBase single instance
*/
private static MageBase instance = new MageBase();
/**
* Default logger
*/
private static Logger logger = Logging.getLogger(MageBase.class.getName());
public static MageBase getInstance() {
return instance;
}
private static UUID sessionId;
public static Server server;
private static String userName;
private static ServerState serverState;
private static CallbackClientDaemon callbackDaemon;
private static UUID gameId;
private static UUID playerId;
private static CardView cardPlayed;
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) {
String[] args = new String[]{"-testMode=true"};
Main.main(args);
connect("player", "localhost", 17171);
UUID roomId = server.getMainRoomId();
MatchOptions options = new MatchOptions("1", "Two Player Duel", false);
options.getPlayerTypes().add("Human");
options.getPlayerTypes().add("Computer - default");
options.setDeckType("Limited");
options.setAttackOption(MultiplayerAttackOption.LEFT);
options.setRange(RangeOfInfluence.ALL);
options.setWinsNeeded(1);
TableView table = server.createTable(sessionId, roomId, options);
System.out.println("Cards in the deck: " + Sets.loadDeck("UW Control.dck").getCards().size());
server.joinTable(sessionId, roomId, table.getTableId(), "Human", "Human", Sets.loadDeck("UW Control.dck"));
server.joinTable(sessionId, roomId, table.getTableId(), "Computer", "Computer - default", Sets.loadDeck("UW Control.dck"));
server.startMatch(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");
}
}
}
}
public static void connect(String userName, String serverName, int port) {
try {
System.setSecurityManager(null);
Registry reg = LocateRegistry.getRegistry(serverName, port);
server = (Server) reg.lookup("mage-server");
sessionId = server.registerClient(userName, UUID.randomUUID());
CallbackClient client = new CallbackClient() {
@Override
public void processCallback(ClientCallback callback) {
logger.log(Level.INFO, "IN >> {0} - {1}", new Object[]{callback.getMessageId(), callback.getMethod()});
try {
if (callback.getMethod().equals("startGame")) {
TableClientMessage data = (TableClientMessage) callback.getData();
gameId = data.getGameId();
playerId = data.getPlayerId();
server.joinGame(gameId, sessionId);
} else if (callback.getMethod().equals("gameInit")) {
server.ack("gameInit", sessionId);
} else if (callback.getMethod().equals("gameAsk")) {
GameClientMessage message = (GameClientMessage) callback.getData();
logger.log(Level.INFO, "ASK >> {0}", 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();
logger.log(Level.INFO, "TARGET >> {0} >> {1}", new Object[]{message.getMessage(), message.getTargets()});
if (message.getMessage().equals("Select a starting player")) {
logger.log(Level.INFO, " Sending >> {0}", playerId);
server.sendPlayerUUID(gameId, sessionId, playerId);
}
} else if (callback.getMethod().equals("gameSelect")) {
GameClientMessage message = (GameClientMessage) callback.getData();
logger.log(Level.INFO, "SELECT >> {0}", message.getMessage());
if (phaseToWait == null) {
synchronized (sync) {
sync.wait();
}
}
if (!message.getMessage().startsWith(phaseToWait)) {
server.sendPlayerBoolean(gameId, sessionId, false);
} else {
phaseToWait = null;
}
/*if (!message.getMessage().startsWith("Precombat Main - play spells and sorceries.")) {
server.sendPlayerBoolean(gameId, sessionId, false);
} else {
if (cardPlayed == null) {
CardsView cards = message.getGameView().getHand();
CardView landToPlay = null;
for (CardView card : cards.values()) {
//System.out.println(card.getName());
if (card.getName().equals("Plains") || card.getName().equals("Island")) {
landToPlay = card;
}
}
if (landToPlay != null) {
logger.info("Playing " + landToPlay);
server.sendPlayerUUID(gameId, sessionId, landToPlay.getId());
cardPlayed = landToPlay;
} else {
logger.warning("Couldn't find land to play");
}
} else {
logger.info("Checking battlefield...");
boolean foundPlayer = false;
boolean foundLand = false;
for (PlayerView player: message.getGameView().getPlayers()) {
if (player.getPlayerId().equals(playerId)) {
foundPlayer = true;
for (PermanentView permanent : player.getBattlefield().values()) {
if (permanent.getId().equals(cardPlayed.getId())) {
foundLand = true;
}
}
break;
}
}
logger.info(" found player: " + foundPlayer);
logger.info(" found land: " + foundLand);
System.exit(0);
}
} */
}
} catch (Exception e) {
logger.info(e.getMessage());
}
}
};
callbackDaemon = new CallbackClientDaemon(sessionId, client, server);
serverState = server.getServerState();
} catch (MageException ex) {
logger.log(Level.SEVERE, null, ex);
} catch (RemoteException ex) {
logger.log(Level.SEVERE, "Unable connect to server - ", ex);
} catch (NotBoundException ex) {
logger.log(Level.SEVERE, "Unable connect to server - ", ex);
}
}
public boolean giveme(String cardName) throws Exception {
return server.cheat(gameId, sessionId, playerId, cardName);
}
public boolean checkIhave(String cardName) throws Exception {
if (cardName == null) {
return false;
}
gameView = server.getGameView(gameId, sessionId, playerId);
for (CardView card : gameView.getHand().values()) {
if (CardUtil.haveSameNames(card.getName(), cardName)) {
return true;
}
}
return false;
}
public void goToPhase(String phase) {
phaseToWait = phase;
synchronized (sync) {
sync.notify();
}
}
public void playCard(String cardName) throws Exception {
gameView = server.getGameView(gameId, sessionId, playerId);
CardsView cards = gameView.getHand();
CardView cardToPlay = null;
for (CardView card : cards.values()) {
if (CardUtil.haveSameNames(card.getName(), cardName)) {
cardToPlay = card;
}
}
if (cardToPlay == null) {
throw new IllegalArgumentException("Couldn't find " + cardName + " in the hand.");
}
if (cardToPlay != null) {
logger.info("Playing " + cardToPlay);
server.sendPlayerUUID(gameId, sessionId, cardToPlay.getId());
cardPlayed = cardToPlay;
}
}
public boolean checkBattlefield(String cardName) throws Exception {
gameView = server.getGameView(gameId, sessionId, playerId);
for (PlayerView player : gameView.getPlayers()) {
if (player.getPlayerId().equals(playerId)) {
for (PermanentView permanent : player.getBattlefield().values()) {
if (permanent.getName().equals(cardName)) {
return true;
}
}
}
}
return false;
}
public boolean checkGraveyardsEmpty() throws Exception {
gameView = server.getGameView(gameId, sessionId, playerId);
for (PlayerView player : gameView.getPlayers()) {
if (player.getGraveyard().size() > 0) {
return false;
}
}
return true;
}
}

View file

@ -1,12 +0,0 @@
package org.mage.test.clientside.base.exception;
/**
* Thrown when server couldn't create card with given name.
*
* @author nantuko
*/
public class CardNotFoundException extends RuntimeException {
public CardNotFoundException(String s) {
super(s);
}
}

View file

@ -1,32 +0,0 @@
package org.mage.test.clientside.bdd;
import org.junit.Test;
import org.mage.test.clientside.base.Command;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.base.exception.CardNotFoundException;
import org.mage.test.clientside.bdd.and.And;
import org.mage.test.clientside.bdd.given.Given;
import org.mage.test.clientside.bdd.then.Then;
import org.mage.test.clientside.bdd.when.When;
import static org.mage.test.clientside.base.MageAPI.Owner.mine;
/**
* Tests BDD classes.
*/
public class BDDTests extends MageAPI {
@Test
public void testNonExistingCard() throws Exception {
Expect.expect(CardNotFoundException.class, new Command() {
@Override
public void execute() throws Exception {
Given.I.have.a.card("Super Puper Card");
And.phase.is("Precombat Main", mine);
When.I.play("Super Puper Card");
Then.battlefield.has("Mountain");
And.graveyards.empty();
}
});
}
}

View file

@ -1,23 +0,0 @@
package org.mage.test.clientside.bdd;
import org.junit.Assert;
import org.mage.test.clientside.base.Command;
import static junit.framework.TestCase.*;
/**
* Asserts expecting exception.
*
* @author nantuko
*/
public class Expect {
public static void expect(Class<? extends RuntimeException> t, Command command) {
try {
command.execute();
} catch (Throwable e) {
assertEquals(e.getClass().getName(), t.getName())
return;
}
throw new AssertionError("Expected exception wasn't thrown: " + t.getName());
}
}

View file

@ -1,19 +0,0 @@
package org.mage.test.clientside.bdd;
/**
* Controls steps of bdd calls.
* Uses step for the same "And" calls depending on previous calls.
* So "And." can be either "Given." or "Then." depending on "Given" or "Then" was called previously.
*
* Example:
*
* Given.I.have.a.card("Island"); // remember step here
* And.battlefield.has("Plains"); // "And" replaced and Given.battlefield.has("Plains"); is called
*
* Then.graveyards.empty(); // remember step here
* And.battlefield.has("Plains"); // "And" replaced and Then.battlefield.has("Plains"); is called
*
*/
public class StepController {
public static StepState currentState = StepState.UNKNOWN;
}

View file

@ -1,8 +0,0 @@
package org.mage.test.clientside.bdd;
public enum StepState {
GIVEN,
WHEN,
THEN,
UNKNOWN
}

View file

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

View file

@ -1,25 +0,0 @@
package org.mage.test.clientside.bdd.and;
import org.junit.Assert;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.base.MageBase;
import org.mage.test.clientside.bdd.StepState;
import static junit.framework.TestCase.*;
public class Graveyards {
private StepState step;
public Graveyards(StepState step) {
this.step = step;
}
public boolean empty() throws Exception {
StepState current = MageAPI.defineStep(this.step);
if (current.equals(StepState.THEN)) {
boolean empty = MageBase.getInstance().checkGraveyardsEmpty();
assertTrue(empty);
return empty;
} else {
throw new AssertionError("Not implemented for step="+current);
}
}
}

View file

@ -1,27 +0,0 @@
package org.mage.test.clientside.bdd.and;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.base.MageBase;
import org.mage.test.clientside.bdd.StepState;
import static org.mage.test.clientside.base.MageAPI.Owner.*;
public class Phase {
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);
}
}
}

View file

@ -1,25 +0,0 @@
package org.mage.test.clientside.bdd.given;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.base.MageBase;
import org.mage.test.clientside.base.exception.CardNotFoundException;
import org.mage.test.clientside.bdd.StepState;
public class A {
private StepState step;
public A(StepState step) {
this.step = step;
}
public void card(String cardName) throws Exception {
StepState current = MageAPI.defineStep(this.step);
if (current.equals(StepState.GIVEN)) {
if (!MageBase.getInstance().giveme(cardName)) {
throw new CardNotFoundException("Couldn't create card: " + cardName);
}
} else if (current.equals(StepState.THEN)) {
if (!MageBase.getInstance().checkIhave(cardName)) {
throw new CardNotFoundException("Couldn't find requested card in hand: " + cardName);
}
}
}
}

View file

@ -1,9 +0,0 @@
package org.mage.test.clientside.bdd.given;
import org.mage.test.clientside.bdd.StepState;
import org.mage.test.clientside.bdd.and.Phase;
public class Given {
public static I I = new I(StepState.GIVEN);
public static Phase phase = new Phase(StepState.GIVEN);
}

View file

@ -1,10 +0,0 @@
package org.mage.test.clientside.bdd.given;
import org.mage.test.clientside.bdd.StepState;
public class Have {
public Have(StepState step) {
a = new A(step);
}
public A a;
}

View file

@ -1,10 +0,0 @@
package org.mage.test.clientside.bdd.given;
import org.mage.test.clientside.bdd.StepState;
public class I {
public I(StepState step) {
have = new Have(step);
}
public Have have;
}

View file

@ -1,20 +0,0 @@
package org.mage.test.clientside.bdd.then;
import org.mage.test.clientside.base.MageAPI;
import org.mage.test.clientside.base.MageBase;
import org.mage.test.clientside.bdd.StepState;
public class Battlefield {
private StepState step;
public Battlefield(StepState step) {
this.step = step;
}
public boolean has(String cardName) throws Exception {
StepState current = MageAPI.defineStep(this.step);
if (current.equals(StepState.THEN)) {
return MageBase.getInstance().checkBattlefield(cardName);
} else {
throw new AssertionError("Not implemented for step="+current);
}
}
}

View file

@ -1,9 +0,0 @@
package org.mage.test.clientside.bdd.then;
import org.mage.test.clientside.bdd.StepState;
import org.mage.test.clientside.bdd.and.Graveyards;
public class Then {
public static Battlefield battlefield = new Battlefield(StepState.THEN);
public static Graveyards graveyards = new Graveyards(StepState.THEN);
}

View file

@ -1,9 +0,0 @@
package org.mage.test.clientside.bdd.when;
import org.mage.test.clientside.base.MageBase;
public class I {
public void play(String cardName) throws Exception {
MageBase.getInstance().playCard(cardName);
}
}

View file

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