package test.util.proc;
import net.sf.nebulacards.comm.*;
import net.sf.nebulacards.game.*;
import net.sf.nebulacards.main.*;
import net.sf.nebulacards.util.*;
import net.sf.nebulacards.util.proc.*;
import junit.framework.*;
import OneProcGame;
import Utility;
/**
* Test the ContinuousPlayProc class. This test does not guarantee the
* correct functioning of a subclass of ContinuousPlayProc. It only attempts
* to ensure that ContinuousPlayProc obeys its contract of when each of its
* methods is called.
* @author James Ranson
*/
public class ContinuousPlayProcTest
extends TestCase
{
final int NUM_PLAYERS = 3;
OneProcGame opg;
Queue eventLog;
public ContinuousPlayProcTest(String s) { super(s); }
class MyEvent
{
public int id;
public String msg;
public MyEvent() {}
public MyEvent(int a, String b) { id = a; msg = b; }
public String toString() { return "" + id + " " + msg; }
public boolean equals(Object o)
{
if (!(o instanceof MyEvent)) return false;
return (((MyEvent)o).id == id && ((MyEvent)o).msg.equals(msg));
}
}
protected void log(MyEvent me)
{
eventLog.enqueue(me);
}
protected void log(int id, String msg)
{
log(new MyEvent(id, msg));
}
protected MyEvent readLog() { return (MyEvent)eventLog.dequeue(); }
class MyCPProc extends ContinuousPlayProc
{
public MyCPProc(CardGame g) { super(g); }
protected void beforeAsking(int who, Communicator[] coms,
Communicator bc)
{
log(who, "beforeAsking");
}
protected boolean processPlay(PlayingCard c, int who,
Communicator[] coms, Communicator bc)
{
log(who, "processPlay");
if (c == null || c.isNull())
{
return false;
} else {
m_game.getHands()[who].remove(0);
return true;
}
}
}
class MyPlayer extends UIAdapter
{
private PlayingCard play = NullPlayingCard.getInstance();
public void yourTurn()
{
log(getPosition(), "yourTurn");
submitPlay(play);
play = new PlayingCard(getPosition(), getPosition());
}
public void accepted() { log(getPosition(), "accepted"); }
public void rejected() { log(getPosition(), "rejected"); }
}
public void setUp()
{
eventLog = new Queue();
opg = new OneProcGame("ContinuousPlayProc Test", NUM_PLAYERS);
opg.setProc(new MyCPProc(opg));
ComManager.AddResult cmar;
MyPlayer mp;
while ((cmar = opg.add(mp = new MyPlayer())) != null)
{
for (int i = 0; i < 3; i++)
opg.getHands()[cmar.getPosition()].add(new PlayingCard(i, i));
cmar.publicize();
}
}
public void testRun()
throws InterruptedException
{
GameProcedure gp = opg.getNextProcedure();
gp.runProcedure(Utility.getComs(opg), opg.getBroadcastCommunicator());
// There should be at least one player out of cards.
boolean properQuit = false;
for (int i = 0; i < opg.getCapacity(); i++)
properQuit |= opg.getHands()[i].isEmpty();
assertTrue(properQuit);
// check that the event log contains the correct entries.
// there should have been two full cycles, and one partial cycle.
MyEvent me;
for (int cycle = 0; cycle < 3; cycle++)
{
for (int player = 0; player < NUM_PLAYERS; player++)
{
if (cycle == 2 && player > 0) break;
String err = "(cycle=" + cycle + ", player=" + player + ")";
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "beforeAsking");
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "yourTurn");
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "processPlay");
// the first cycle contains a rejection.
if (cycle == 0)
{
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "rejected");
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "yourTurn");
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "processPlay");
}
me = readLog();
assertEquals(err, me.id, player);
assertEquals(err, me.msg, "accepted");
}
}
// there should be no extraneous messages in the log.
assertNull(readLog());
}
}