Skip to content

Instantly share code, notes, and snippets.

@philou
Created December 16, 2011 10:15
Show Gist options
  • Save philou/1485479 to your computer and use it in GitHub Desktop.
Save philou/1485479 to your computer and use it in GitHub Desktop.
Randori Egg Cooker @ Key Consulting 15/12/2011
import java.util.Timer;
import java.util.TimerTask;
public class EggCooker {
private View view;
private long remainingTime;
private ITimer timer;
public EggCooker(View view, ITimer timer) {
this.view = view;
this.timer = timer;
}
public void cook() {
remainingTime = 180000;
view.setRemainingTime(remainingTime);
timer.schedule(new TimerTask() {
public void run() {
remainingTime -= 1000;
view.setRemainingTime(remainingTime);
if (remainingTime == 0) {
view.displayPopup();
}
}
}, 1000, 1000);
}
}
import static org.junit.Assert.*;
import java.util.Timer;
import org.junit.Test;
public class EggCookerTest extends Thread {
private TimerMock timer = new TimerMock();
private ViewMock view = new ViewMock();
private EggCooker eggCooker = new EggCooker(view, timer);
@Test
public void acceptance_test() {
eggCooker.cook();
for (int i = 0; i < 180; i++) {
assertFalse(view.popupHasBeenDisplayed);
this.timer.oneSecondHasPassed();
}
assertTrue(view.popupHasBeenDisplayed);
}
@Test
public void remaining_time_should_be_initialized_at_3_minutes() {
eggCooker.cook();
assertEquals(180000, view.setRemainingTimeCalledWith);
}
@Test
public void popup_should_not_be_displayed_immediatly_after_cook() {
eggCooker.cook();
assertFalse(view.popupHasBeenDisplayed);
}
@Test
public void remaining_time_should_decrement_every_second()
throws InterruptedException {
eggCooker.cook();
this.timer.oneSecondHasPassed();
assertEquals(179000, view.setRemainingTimeCalledWith);
}
}
import java.util.TimerTask;
public interface ITimer {
public void schedule(TimerTask timerTask, long dt, long pt);
}
import java.util.TimerTask;
public class TimerMock implements ITimer {
private TimerTask timerTask;
@Override
public void schedule(TimerTask timerTask, long dt, long pt) {
this.timerTask = timerTask;
}
public void oneSecondHasPassed() {
timerTask.run();
}
}
public interface View {
public abstract void setRemainingTime(long time);
public abstract void displayPopup();
}
public class ViewMock implements View {
public long setRemainingTimeCalledWith;
public boolean popupHasBeenDisplayed = false;
/*
* (non-Javadoc)
*
* @see View#setRemainingTime(long)
*/
@Override
public void setRemainingTime(long time) {
this.setRemainingTimeCalledWith = time;
}
@Override
public void displayPopup() {
this.popupHasBeenDisplayed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment