1 |
|
2 |
import java.util.Date; |
3 |
import java.util.TimerTask; |
4 |
import java.util.concurrent.*; |
5 |
import junit.framework.TestCase; |
6 |
|
7 |
/** |
8 |
* Tests the TimerExecutor method |
9 |
*/ |
10 |
public class TimerExecutorTest extends TestCase { |
11 |
|
12 |
public void testTimedExecute () { |
13 |
TimerExecutor te = TimerExecutors.newTimerExecutor(new DirectExecutor()); |
14 |
Date inASecond= new Date(System.currentTimeMillis() + 1000); |
15 |
flag = false; |
16 |
TimerTask timerTask = te.schedule(new Runnable() { |
17 |
public void run () { |
18 |
flag = true; |
19 |
} |
20 |
}, inASecond); |
21 |
try { |
22 |
Thread.sleep(3000); |
23 |
} |
24 |
catch (InterruptedException e) { |
25 |
fail("task interrupted"); |
26 |
} |
27 |
assertTrue("flag should have been set", flag); |
28 |
} |
29 |
|
30 |
private static class DirectExecutor implements Executor { |
31 |
public void execute (Runnable r) { |
32 |
r.run(); |
33 |
} |
34 |
} |
35 |
|
36 |
private boolean flag = false; |
37 |
} |