Let's test the next switch. We choose
the relief valve. First the test code. We notice (since we cut and pasted it) that this test is very similar to
the boiler test. We have an opportunity for |
refactoring, but let's wait until we have two complete examples before we try to merge them together. |
package simulator.r10.unittest;
import unittest.framework.*;
import simulator.r10.*;
class TestReliefValve extends Test implements SimulationInterface
{private int onMessageSent, offMessageSent,
otherMessageSent;
private Thread simulation;
public void setUp()
{onMessageSent = 0;
offMessageSent = 0;
otherMessageSent = 0;
PIA.register = 0x0000;
PIA.setInputs(0x003F);
startSimulator();}
public void runTest()
{testReliefValveOn();
testReliefValveOff();}
private void testReliefValveOn()
{PIA.write(0x2000);
pauseOneQuarterSecond();
should(onMessageSent == 1, "Got
reliefValveOn " + onMessageSent + " instead of once");
should(offMessageSent == 0, "Got
a different message");}
private void testReliefValveOff()
{PIA.write(0x0000);
pauseOneQuarterSecond();
should(onMessageSent == 1, "Got
a different message");
should(offMessageSent == 1, "Got
reliefValveOff " + offMessageSent + " instead of once");
should(otherMessageSent == 0, "Got
some other message");}
public void tearDown()
{stopSimulator();}
public void boilerOff()
{otherMessageSent++;}
public void boilerOn()
{otherMessageSent++;}
public void reliefValveOff()
{offMessageSent++;}
public void reliefValveOn()
{onMessageSent++;}
private void startSimulator()
{simulation = new Simulator(this);
simulation.start();}
private void pauseOneQuarterSecond()
{try
{Thread.sleep(250);}
catch (InterruptedException exception)
{};}
private void stopSimulator()
{simulation.stop();
simulation = null;};}
|
We add this to the test suite, and
create stubs for the methods we expect to be calling, now compile it all, and try to run it. It fails. Let's make
some changes to the simulator class. And add our new methods
reliefValveOn() and reliefValveOff()
to our SimulationInterface. |
|
package simulator.r11;
public class Simulator extends Thread
{SimulationInterface gui;
boolean boilerIsOn = false;
boolean reliefValveIsOn = false;
static final int BoilerSwitch =
0x1000;
static final int ReliefValveSwitch
= 0x2000;
public Simulator (SimulationInterface
aGUI)
{super();
gui = aGUI;}
public void run()
{while (true)
{checkBoilerSwitch();
checkReliefValveSwitch();
sleepOneTenthSecond();};}
private void checkBoilerSwitch()
{if (wasBoilerJustSwitchedOn())
turnOnBoiler();
if (wasBoilerJustSwitchedOff())
turnOffBoiler();}
private boolean wasBoilerJustSwitchedOn()
{return isBoilerSwitchedOn() &&
boilerIsOff();}
private boolean wasBoilerJustSwitchedOff()
{return isBoilerSwitchedOff() &&
boilerIsOn;}
private boolean isBoilerSwitchedOn()
{return !isBoilerSwitchedOff();}
private boolean isBoilerSwitchedOff()
{return (PIA.register & BoilerSwitch)
== 0;}
private boolean boilerIsOff()
{return !boilerIsOn;}
private void turnOnBoiler()
{gui.boilerOn();
boilerIsOn = true;}
private void turnOffBoiler()
{gui.boilerOff();
boilerIsOn = false;}
private void checkReliefValveSwitch()
{if (wasReliefValveJustSwitchedOn())
turnOnReliefValve();
if (wasReliefValveJustSwitchedOff())
turnOffReliefValve();}
private boolean wasReliefValveJustSwitchedOn()
{return isReliefValveSwitchedOn()
&& reliefValveIsOff();}
private boolean wasReliefValveJustSwitchedOff()
{return isReliefValveSwitchedOff()
&& reliefValveIsOn;}
private boolean isReliefValveSwitchedOn()
{return !isReliefValveSwitchedOff();}
private boolean isReliefValveSwitchedOff()
{return (PIA.register & ReliefValveSwitch)
== 0;}
private boolean reliefValveIsOff()
{return !reliefValveIsOn;}
private void turnOnReliefValve()
{gui.reliefValveOn();
reliefValveIsOn = true;}
private void turnOffReliefValve()
{gui.reliefValveOff();
reliefValveIsOn = false;}
private void sleepOneTenthSecond()
{try
{sleep(100);}
catch (InterruptedException exception)
{};};}
|
Now we run the unit test and it
passes. Before we can add our next unit test we need to spend some time refactoring the two tests we already have
so that we can add the third simply.  |
|