XP Home

Test the Warmer Switch Now

 

The next unit test will test the warmer switch. We refactored our unit tests so now this is trivial.  

package simulator.r14.unittest;

class TestWarmer extends SwitchTest
{public void runTest()
{runTest(WarmerSwitch);}

public void warmerOff()
{offMessageSent++;}

public void warmerOn()
{onMessageSent++;};}

We always test our test by creating stubs, compiling and running. Of course it fails. Now let's add some code to make it pass. We can add three methods checkWarmerSwitch(), turnOnWarmer() and turnOffWarmer(). Then change our run() method to call them.

package simulator.r15;

public class Simulator extends Thread implements Switches
{SimulationInterface gui;
private boolean boilerIsOn = false, reliefValveIsOn = false, warmerIsOn = false;

public Simulator (SimulationInterface aGUI)
{super();
gui = aGUI;}

public void run()
{while (true)
{checkBoilerSwitch();
checkReliefValveSwitch();
checkWarmerSwitch();
sleepOneTenthSecond();};}

private void checkBoilerSwitch()
{if (wasJustSwitchedOn(BoilerSwitch, boilerIsOn)) turnOnBoiler();
if (wasJustSwitchedOff(BoilerSwitch, boilerIsOn)) turnOffBoiler();}

private void checkReliefValveSwitch()
{if (wasJustSwitchedOn(ReliefValveSwitch, reliefValveIsOn)) turnOnReliefValve();
if (wasJustSwitchedOff(ReliefValveSwitch, reliefValveIsOn)) turnOffReliefValve();}

private void checkWarmerSwitch()
{if (wasJustSwitchedOn(WarmerSwitch, warmerIsOn)) turnOnWarmer();
if (wasJustSwitchedOff(WarmerSwitch, warmerIsOn)) turnOffWarmer();}

private void turnOnBoiler()
{gui.boilerOn();
boilerIsOn = true;}

private void turnOffBoiler()
{gui.boilerOff();
boilerIsOn = false;}

private void turnOnReliefValve()
{gui.reliefValveOn();
reliefValveIsOn = true;}

private void turnOffReliefValve()
{gui.reliefValveOff();
reliefValveIsOn = false;}

private void turnOnWarmer()
{gui.warmerOn();
warmerIsOn = true;}

private void turnOffWarmer()
{gui.warmerOff();
warmerIsOn = false;}

private boolean wasJustSwitchedOn(int aSwitch, boolean isOnNow)
{return isSwitchedOn(aSwitch) && !isOnNow;}

private boolean wasJustSwitchedOff(int aSwitch, boolean isOnNow)
{return isSwitchedOff(aSwitch) && isOnNow;}

private boolean isSwitchedOff(int aSwitch)
{return (PIA.register & aSwitch) == 0;}

private boolean isSwitchedOn(int aSwitch)
{return !isSwitchedOff(aSwitch);}

private void sleepOneTenthSecond()
{try
{sleep(100);}
catch (InterruptedException exception)
{};};}
We now run the unit tests again and they pass. Something is wrong with this code. It is getting way to large and hard to understand and there are lots of methods that look alike except for a couple name changes. I think it is time for a big refactoring. Spike Solution

ExtremeProgramming.org home | A Spike Solution | Refactor Again |

Copyright 1999 by Don Wells.