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;
import CountingUI;
/**
* Test the QuestionAnswerProc class.
* @author James Ranson
*/
public class QuestionAnswerProcTest
extends TestCase
{
final int NUM_PLAYERS = 3;
OneProcGame opg;
MyPlayer[] players;
public QuestionAnswerProcTest(String s) { super(s); }
class MyQAProc extends QuestionAnswerProc
{
protected String getQuestion() { return "What do you say?"; }
protected boolean parseAnswer(String s)
{
// false first time, then true.
return s.equals("2");
}
}
class MyPlayer extends CountingUI
{
public void respond(String s)
{
super.respond(s);
submitResponse("" + respondCount);
}
}
public void setUp()
{
opg = new OneProcGame("QuestionAnswerProc Test", NUM_PLAYERS);
players = new MyPlayer[NUM_PLAYERS];
ComManager.AddResult cmar;
MyPlayer mp;
while ((cmar = opg.add(mp = new MyPlayer())) != null)
{
players[cmar.getPosition()] = mp;
cmar.publicize();
}
}
/**
* The procedure should fail if it is run before setPlayer is called.
*/
public void testNoSetPlayer()
throws InterruptedException
{
QuestionAnswerProc qap = new MyQAProc();
opg.setProc(qap);
GameProcedure gp = opg.getNextProcedure();
try {
gp.runProcedure(Utility.getComs(opg),
opg.getBroadcastCommunicator());
} catch (ArrayIndexOutOfBoundsException e) {
// this is supposed to happen
return;
}
fail();
}
/**
* Test the success scenario.
*/
public void testSuccess()
throws InterruptedException
{
final int who = NUM_PLAYERS - 1;
QuestionAnswerProc qap = new MyQAProc();
opg.setProc(qap);
qap.setPlayer(who);
assertEquals(who, qap.getPlayer());
players[who].reset();
GameProcedure gp = opg.getNextProcedure();
gp.runProcedure(Utility.getComs(opg), opg.getBroadcastCommunicator());
// the respond method should have been called twice because the first
// response was marked invalid.
assertEquals(players[who].respondCount, 2);
}
}