001 package biz.xsoftware.examples.timer;
002
003 import java.util.TimerTask;
004
005 import junit.framework.TestCase;
006 import biz.xsoftware.examples.advanced.ScheduleListener;
007 import biz.xsoftware.mock.CalledMethod;
008 import biz.xsoftware.mock.MockObjectFactory;
009 import biz.xsoftware.mock.MockObject;
010
011 /**
012 * This example has the following tests
013 *
014 * testBasicCalendar
015 *
016 * Tests a few things
017 * 1. Tests scheduling an event
018 * 2. Tests that event was scheduled at the propert time
019 * 3. Tests the event going off and verifying expected event behavior
020 *
021 * testCancelOfEvent
022 *
023 * Tests scheduling and cancelling the event.
024 *
025 * @author Dean Hiller
026 */
027 public class TestExample extends TestCase {
028
029 private MockObject mockListener;
030 private MockObject mockTimer;
031 private CalendarService calendar;
032
033 /**
034 * @showcode
035 */
036 public TestExample(String name) {
037 super(name);
038 }
039 /**
040 * @showcode
041 */
042 @Override
043 public void setUp() {
044 //Create a mockTimer which acts as a cache for TimerTasks.....
045 mockTimer = MockObjectFactory.createMock(TimerInterface.class);
046 calendar = new CalendarServiceImpl((TimerInterface) mockTimer);
047
048 //Create and add a mockListener which we use to verify receiving of the
049 //proper events....
050 mockListener = MockObjectFactory.createMock(ScheduleListener.class);
051 calendar.addScheduleListener((ScheduleListener)mockListener);
052 }
053 /**
054 * @showcode
055 */
056 @Override
057 public void tearDown() {
058 calendar.removeScheduleListener((ScheduleListener)mockListener);
059 }
060 /**
061 * Tests a few things
062 * 1. Tests scheduling an event
063 * 2. Tests that event was scheduled at the propert time
064 * 3. Tests the event going off and verifying expected event behavior
065 *
066 * @showcode
067 */
068 public void testBasicCalendar() {
069 String title = "some event";
070 long delay = 50000;
071 calendar.addEvent(title, delay);
072
073 //When an event is added, we expect schedule on the timer to be called...
074 CalledMethod method = mockTimer.expect("schedule");
075 //param[0] is the mock object's cached timer task....
076 TimerTask task = (TimerTask)method.getParameter(0);
077 assertEquals("Should have set the timer for "+delay+" ms", new Long(delay), method.getParameter(1));
078
079 //We already verified it was scheduled in the future, so there is
080 //no need to wait till run the task then
081 //run the task now instead of waiting 50000ms
082 task.run();
083
084 //expect that the listener's eventStarted method is called with the
085 //propert event title
086 method = mockListener.expect("eventStarted");
087 assertEquals("title should be the same", title, method.getParameter(0));
088 }
089
090 /**
091 * Tests scheduling and cancelling the event.
092 *
093 * @showcode
094 */
095 public void testCancelOfEvent() {
096 String title = "some event";
097 long delay = 50000;
098 calendar.addEvent(title, delay);
099
100 CalledMethod method = mockTimer.expect("schedule");
101 //param[0] is a timer task
102 TimerTask task = (TimerTask)method.getParameter(0);
103 assertEquals("Should have set the timer for "+delay+" ms", new Long(delay), method.getParameter(1));
104
105 calendar.cancelEvent(title);
106
107 //make sure the event is actually cancelled
108 mockTimer.expect("cancelTask");
109 TimerTask cancelledTask = (TimerTask)method.getParameter(0);
110 //Make sure the correct TimerTask is cancelled....
111 assertSame(task, cancelledTask);
112 }
113 }
|