Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/timer2
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv3813/input/javasrc/biz/xsoftware/examples/timer2
Modified Files:
TestExample.java
Added Files:
CalendarServiceImpl.java CalendarService.java
Removed Files:
SysUnderTest.java
Log Message:
change timer example to be CalendarService.
--- NEW FILE: CalendarServiceImpl.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.HashMap;
import java.util.Map;
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 CalendarServiceImpl implements CalendarService {
private TimerInterface timer;
private EventListenerList listenerList = new EventListenerList();
private Map<String, CalendarEvent> titleToEvent = new HashMap<String, CalendarEvent>();
/**
* @showcode
*/
public CalendarServiceImpl(TimerInterface t) {
timer = t;
}
/* (non-Javadoc)
* @see biz.xsoftware.examples.timer2.CalendarService#addEvent(java.lang.String, long)
*/
public void addEvent(String title, long delay) {
CalendarEvent evt = new CalendarEvent(title);
titleToEvent.put(title, evt);
timer.schedule(evt, delay);
}
/* (non-Javadoc)
* @see biz.xsoftware.examples.timer2.CalendarService#addScheduleListener(biz.xsoftware.examples.timer.ScheduleListener)
*/
public void addScheduleListener(ScheduleListener l) {
listenerList.add(ScheduleListener.class, l);
}
/* (non-Javadoc)
* @see biz.xsoftware.examples.timer2.CalendarService#removeScheduleListener(biz.xsoftware.examples.timer.ScheduleListener)
*/
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);
}
}
}
/* (non-Javadoc)
* @see biz.xsoftware.examples.timer2.CalendarService#cancelEvent(java.lang.String)
*/
public void cancelEvent(String title) {
CalendarEvent event = titleToEvent.get(title);
timer.cancelTask(event);
}
}
Index: TestExample.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/timer2/TestExample.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** TestExample.java 11 Sep 2006 03:47:39 -0000 1.3
--- TestExample.java 12 Sep 2006 13:27:56 -0000 1.4
***************
*** 35,39 ****
private MockObject mockListener;
private MockObject mockTimer;
! private SysUnderTest calendar;
/**
--- 35,39 ----
private MockObject mockListener;
private MockObject mockTimer;
! private CalendarService calendar;
/**
***************
*** 50,54 ****
//Create a mockTimer which acts as a cache for TimerTasks.....
mockTimer = MockObjectFactory.createMock(TimerInterface.class);
! calendar = new SysUnderTest((TimerInterface) mockTimer);
//Create and add a mockListener which we use to verify receiving of the
--- 50,54 ----
//Create a mockTimer which acts as a cache for TimerTasks.....
mockTimer = MockObjectFactory.createMock(TimerInterface.class);
! calendar = new CalendarServiceImpl((TimerInterface) mockTimer);
//Create and add a mockListener which we use to verify receiving of the
--- SysUnderTest.java DELETED ---
--- NEW FILE: CalendarService.java ---
package biz.xsoftware.examples.timer2;
import biz.xsoftware.examples.timer.ScheduleListener;
public interface CalendarService {
public abstract void addEvent(String title, long delay);
public abstract void cancelEvent(String title);
public abstract void addScheduleListener(ScheduleListener l);
public abstract void removeScheduleListener(ScheduleListener l);
}
|