KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > timer > TimerSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.timer;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Timer JavaDoc;
22 import java.util.TimerTask JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.springframework.scheduling.TestMethodInvokingTask;
27
28 /**
29  * @author Juergen Hoeller
30  * @since 20.02.2004
31  */

32 public class TimerSupportTests extends TestCase {
33
34     public void testTimerFactoryBean() throws Exception JavaDoc {
35         final TestTimerTask timerTask0 = new TestTimerTask();
36
37         TestMethodInvokingTask task1 = new TestMethodInvokingTask();
38         MethodInvokingTimerTaskFactoryBean mittfb = new MethodInvokingTimerTaskFactoryBean();
39         mittfb.setTargetObject(task1);
40         mittfb.setTargetMethod("doSomething");
41         mittfb.afterPropertiesSet();
42         final TimerTask JavaDoc timerTask1 = (TimerTask JavaDoc) mittfb.getObject();
43
44         final TestTimerTask timerTask2 = new TestTimerTask();
45
46         ScheduledTimerTask[] tasks = new ScheduledTimerTask[3];
47         tasks[0] = new ScheduledTimerTask(timerTask0, 0, 10, false);
48         tasks[1] = new ScheduledTimerTask(timerTask1, 10, 20, true);
49         tasks[2] = new ScheduledTimerTask(timerTask2, 20);
50
51         final List JavaDoc success = new ArrayList JavaDoc(3);
52         final Timer JavaDoc timer = new Timer JavaDoc(true) {
53             public void schedule(TimerTask JavaDoc task, long delay, long period) {
54                 if (task == timerTask0 && delay == 0 && period == 10) {
55                     success.add(Boolean.TRUE);
56                 }
57             }
58             public void scheduleAtFixedRate(TimerTask JavaDoc task, long delay, long period) {
59                 if (task == timerTask1 && delay == 10 && period == 20) {
60                     success.add(Boolean.TRUE);
61                 }
62             }
63             public void schedule(TimerTask JavaDoc task, long delay) {
64                 if (task == timerTask2 && delay == 20) {
65                     success.add(Boolean.TRUE);
66                 }
67             }
68             public void cancel() {
69                 success.add(Boolean.TRUE);
70             }
71         };
72
73         TimerFactoryBean timerFactoryBean = new TimerFactoryBean() {
74             protected Timer JavaDoc createTimer(boolean daemon) {
75                 return timer;
76             }
77         };
78         try {
79             timerFactoryBean.setScheduledTimerTasks(tasks);
80             timerFactoryBean.afterPropertiesSet();
81             assertTrue(timerFactoryBean.getObject() instanceof Timer JavaDoc);
82             timerTask0.run();
83             timerTask1.run();
84             timerTask2.run();
85         }
86         finally {
87             timerFactoryBean.destroy();
88         }
89
90         assertTrue("Correct Timer invocations", success.size() == 4);
91         assertTrue("TimerTask0 works", timerTask0.counter == 1);
92         assertTrue("TimerTask1 works", task1.counter == 1);
93         assertTrue("TimerTask2 works", timerTask2.counter == 1);
94     }
95
96
97     private static class TestTimerTask extends TimerTask JavaDoc {
98
99         private int counter = 0;
100
101         public void run() {
102             counter++;
103         }
104     }
105
106 }
107
Popular Tags