Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/timer2
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24740/input/javasrc/biz/xsoftware/examples/timer2
Added Files:
TestExample.java TimerInterface.java SysUnderTest.java
Log Message:
original commit of mocklib2 that will become mocklib3
--- NEW FILE: TimerInterface.java ---
/*
* Created on Jun 20, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.timer2;
import java.util.Date;
import java.util.TimerTask;
/**
* A user created Timer interface to make testing easier.
*
* @author Dean Hiller
*/
public interface TimerInterface {
public void cancel();
public int purge();
public void schedule(TimerTask task, Date time);
public void schedule(TimerTask task, Date firstTime, long period);
public void schedule(TimerTask task, long delay);
public void schedule(TimerTask task, long delay, long period) ;
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period);
public void scheduleAtFixedRate(TimerTask task, long delay, long period);
}
--- NEW FILE: TestExample.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.timer2;
import java.util.TimerTask;
import junit.framework.TestCase;
import biz.xsoftware.examples.timer.ScheduleListener;
import biz.xsoftware.mock.CalledMethod;
import biz.xsoftware.mock.MockObjectFactory;
import biz.xsoftware.mock.MockObject;
/**
* JUnit Suite of TestCases for demonstrating mocking out a user
* created Timer interface.
*
* @author Dean Hiller
*/
public class TestExample extends TestCase {
private MockObject mockListener;
private MockObject mockTimer;
private SysUnderTest calendar;
/**
* @showcode
*/
public TestExample(String name) {
super(name);
}
/**
* @showcode
*/
@Override
public void setUp() {
mockTimer = MockObjectFactory.createMock(TimerInterface.class);
calendar = new SysUnderTest((TimerInterface) mockTimer);
mockListener = MockObjectFactory.createMock(ScheduleListener.class);
calendar.addScheduleListener((ScheduleListener)mockListener);
}
/**
* @showcode
*/
@Override
public void tearDown() {
calendar.removeScheduleListener((ScheduleListener)mockListener);
}
/**
* @showcode
*/
public void testBasicCalendar() {
String title = "some event";
long delay = 50000;
calendar.addEvent(title, delay);
CalledMethod method = mockTimer.expect("schedule");
//param[0] is a timer task
TimerTask task = (TimerTask)method.getParameter(0);
assertEquals("Should have set the timer for "+delay+" ms", new Long(delay), method.getParameter(1));
//run the task now instead of waiting 50000ms
task.run();
method = mockListener.expect("eventStarted");
assertEquals("title should be the same", title, method.getParameter(0));
}
}
--- NEW FILE: SysUnderTest.java ---
/*
* Created on Jun 28, 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package biz.xsoftware.examples.timer2;
import java.util.TimerTask;
import javax.swing.event.EventListenerList;
import biz.xsoftware.examples.timer.ScheduleListener;
/**
* The SysUnderTest here is a Calendar which notifies users of a
* scheduled event.
*/
public class SysUnderTest {
private TimerInterface timer;
private EventListenerList listenerList = new EventListenerList();
/**
* @showcode
*/
public SysUnderTest(TimerInterface t) {
timer = t;
}
/**
* @showcode
*/
public void addEvent(String title, long delay) {
timer.schedule(new CalendarEvent(title), delay);
}
/**
* @showcode
*/
public void addScheduleListener(ScheduleListener l) {
listenerList.add(ScheduleListener.class, l);
}
/**
* @showcode
*/
public void removeScheduleListener(ScheduleListener l) {
listenerList.remove(ScheduleListener.class, l);
}
private class CalendarEvent extends TimerTask {
private String title;
/**
* @showcode
*/
public CalendarEvent(String title) {
this.title = title;
}
/**
* @showcode
*/
@Override
public void run() {
fireEventStarted(title);
}
}
/**
* @showcode
*/
protected void fireEventStarted(String title) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ScheduleListener.class) {
((ScheduleListener)listeners[i+1]).eventStarted(title);
}
}
}
}
|