Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TestExample |
|
| 0.0;0 |
1 | package biz.xsoftware.examples.timer; | |
2 | ||
3 | import java.util.TimerTask; | |
4 | ||
5 | import junit.framework.TestCase; | |
6 | import biz.xsoftware.examples.advanced.ScheduleListener; | |
7 | import biz.xsoftware.mock.CalledMethod; | |
8 | import biz.xsoftware.mock.MockObjectFactory; | |
9 | import biz.xsoftware.mock.MockObject; | |
10 | ||
11 | /** | |
12 | * This example has the following tests | |
13 | * | |
14 | * testBasicCalendar | |
15 | * | |
16 | * Tests a few things | |
17 | * 1. Tests scheduling an event | |
18 | * 2. Tests that event was scheduled at the propert time | |
19 | * 3. Tests the event going off and verifying expected event behavior | |
20 | * | |
21 | * testCancelOfEvent | |
22 | * | |
23 | * Tests scheduling and cancelling the event. | |
24 | * | |
25 | * @author Dean Hiller | |
26 | */ | |
27 | public class TestExample extends TestCase { | |
28 | ||
29 | private MockObject mockListener; | |
30 | private MockObject mockTimer; | |
31 | private CalendarService calendar; | |
32 | ||
33 | /** | |
34 | * @showcode | |
35 | */ | |
36 | public TestExample(String name) { | |
37 | 2 | super(name); |
38 | 2 | } |
39 | /** | |
40 | * @showcode | |
41 | */ | |
42 | @Override | |
43 | public void setUp() { | |
44 | //Create a mockTimer which acts as a cache for TimerTasks..... | |
45 | 2 | mockTimer = MockObjectFactory.createMock(TimerInterface.class); |
46 | 2 | calendar = new CalendarServiceImpl((TimerInterface) mockTimer); |
47 | ||
48 | //Create and add a mockListener which we use to verify receiving of the | |
49 | //proper events.... | |
50 | 2 | mockListener = MockObjectFactory.createMock(ScheduleListener.class); |
51 | 2 | calendar.addScheduleListener((ScheduleListener)mockListener); |
52 | 2 | } |
53 | /** | |
54 | * @showcode | |
55 | */ | |
56 | @Override | |
57 | public void tearDown() { | |
58 | 2 | calendar.removeScheduleListener((ScheduleListener)mockListener); |
59 | 2 | } |
60 | /** | |
61 | * Tests a few things | |
62 | * 1. Tests scheduling an event | |
63 | * 2. Tests that event was scheduled at the propert time | |
64 | * 3. Tests the event going off and verifying expected event behavior | |
65 | * | |
66 | * @showcode | |
67 | */ | |
68 | public void testBasicCalendar() { | |
69 | 1 | String title = "some event"; |
70 | 1 | long delay = 50000; |
71 | 1 | calendar.addEvent(title, delay); |
72 | ||
73 | //When an event is added, we expect schedule on the timer to be called... | |
74 | 1 | CalledMethod method = mockTimer.expect("schedule"); |
75 | //param[0] is the mock object's cached timer task.... | |
76 | 1 | TimerTask task = (TimerTask)method.getParameter(0); |
77 | 1 | assertEquals("Should have set the timer for "+delay+" ms", new Long(delay), method.getParameter(1)); |
78 | ||
79 | //We already verified it was scheduled in the future, so there is | |
80 | //no need to wait till run the task then | |
81 | //run the task now instead of waiting 50000ms | |
82 | 1 | task.run(); |
83 | ||
84 | //expect that the listener's eventStarted method is called with the | |
85 | //propert event title | |
86 | 1 | method = mockListener.expect("eventStarted"); |
87 | 1 | assertEquals("title should be the same", title, method.getParameter(0)); |
88 | 1 | } |
89 | ||
90 | /** | |
91 | * Tests scheduling and cancelling the event. | |
92 | * | |
93 | * @showcode | |
94 | */ | |
95 | public void testCancelOfEvent() { | |
96 | 1 | String title = "some event"; |
97 | 1 | long delay = 50000; |
98 | 1 | calendar.addEvent(title, delay); |
99 | ||
100 | 1 | CalledMethod method = mockTimer.expect("schedule"); |
101 | //param[0] is a timer task | |
102 | 1 | TimerTask task = (TimerTask)method.getParameter(0); |
103 | 1 | assertEquals("Should have set the timer for "+delay+" ms", new Long(delay), method.getParameter(1)); |
104 | ||
105 | 1 | calendar.cancelEvent(title); |
106 | ||
107 | //make sure the event is actually cancelled | |
108 | 1 | mockTimer.expect("cancelTask"); |
109 | 1 | TimerTask cancelledTask = (TimerTask)method.getParameter(0); |
110 | //Make sure the correct TimerTask is cancelled.... | |
111 | 1 | assertSame(task, cancelledTask); |
112 | 1 | } |
113 | } |