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;
/**
* @author James Ranson
*/
public class DealProcTest
extends TestCase
{
class MyDealProc extends DealProc
{
public MyDealProc(CardGame g)
{
super(g);
}
public PileOfCards[] deal()
{
PileOfCards[] result = new PileOfCards[hands.length];
for (int i = 0; i < result.length; i++)
if (hands[i] != null)
result[i] = new PileOfCards(hands[i]);
return result;
}
}
class MyClient extends UIAdapter
{
PileOfCards hand;
public void dealHand(PileOfCards h) { hand = h; }
}
OneProcGame opg;
MyClient[] clients;
PileOfCards[] hands;
public DealProcTest(String s) { super(s); }
public void setUp()
{
opg = new OneProcGame("DealProc Test", 2);
opg.setProc(new MyDealProc(opg));
clients = new MyClient[2];
for (int i = 0; i < clients.length; i++)
{
ComManager.AddResult cmar = opg.add(clients[i] = new MyClient());
cmar.publicize();
}
hands = new PileOfCards[opg.getCapacity()];
for (int i = 1; i < hands.length; i++)
{
hands[i] = new PileOfCards(1);
hands[i].add(new PlayingCard(i, i));
}
}
public void testRun()
throws InterruptedException
{
GameProcedure gp = opg.getNextProcedure();
PileOfCards constantHand = new PileOfCards(2);
constantHand.add(new PlayingCard(-2, -2));
int whoIsZero = -1;
for (int i = 0; i < clients.length; i++)
if (clients[i].getPosition() == 0)
whoIsZero = i;
clients[whoIsZero].hand = constantHand;
gp.runProcedure(Utility.getComs(opg), opg.getBroadcastCommunicator());
// The client in 0 position should have an unaltered hand.
assertSame(constantHand, clients[whoIsZero].hand);
// everyone else should have received the correct hand.
for (int i = 0; i < clients.length; i++)
{
if (i != whoIsZero)
{
assertEquals("client " + i,
clients[i].hand,
hands[clients[i].getPosition()]
);
}
}
}
}