1 |
tim |
1.1 |
import java.util.concurrent.*; |
2 |
|
|
import junit.framework.TestCase; |
3 |
|
|
|
4 |
|
|
/** |
5 |
|
|
* Tests the AtomicBoolean implementation. |
6 |
|
|
*/ |
7 |
|
|
public class ExecutorsTest extends TestCase { |
8 |
|
|
|
9 |
|
|
public void testExecuteRunnable () { |
10 |
|
|
try { |
11 |
|
|
Executor e = new DirectExecutor(); |
12 |
|
|
Task task = new Task(); |
13 |
|
|
|
14 |
|
|
assertFalse("task should not be complete", task.isCompleted()); |
15 |
|
|
|
16 |
|
|
Future<String> future = Executors.execute(e, task, TEST_STRING); |
17 |
|
|
String result = future.get(); |
18 |
|
|
|
19 |
|
|
assertTrue("task should be complete", task.isCompleted()); |
20 |
|
|
assertSame("should return test string", TEST_STRING, result); |
21 |
|
|
} |
22 |
|
|
catch (ExecutionException ex) { |
23 |
|
|
fail("Unexpected execution exception: " + ex); |
24 |
|
|
} |
25 |
|
|
catch (InterruptedException ex) { |
26 |
|
|
fail("Unexpected interruption exception: " + ex); |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
public void testInvokeRunnable () { |
31 |
|
|
try { |
32 |
|
|
Executor e = new DirectExecutor(); |
33 |
|
|
Task task = new Task(); |
34 |
|
|
|
35 |
|
|
assertFalse("task should not be complete", task.isCompleted()); |
36 |
|
|
|
37 |
|
|
Executors.invoke(e, task); |
38 |
|
|
|
39 |
|
|
assertTrue("task should be complete", task.isCompleted()); |
40 |
|
|
} |
41 |
|
|
catch (ExecutionException ex) { |
42 |
|
|
fail("Unexpected execution exception: " + ex); |
43 |
|
|
} |
44 |
|
|
catch (InterruptedException ex) { |
45 |
|
|
fail("Unexpected interruption exception: " + ex); |
46 |
|
|
} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
public void testExecuteCallable () { |
50 |
|
|
try { |
51 |
|
|
Executor e = new DirectExecutor(); |
52 |
|
|
Future<String> future = Executors.execute(e, new StringTask()); |
53 |
|
|
String result = future.get(); |
54 |
|
|
|
55 |
|
|
assertSame("should return test string", TEST_STRING, result); |
56 |
|
|
} |
57 |
|
|
catch (ExecutionException ex) { |
58 |
|
|
fail("Unexpected execution exception: " + ex); |
59 |
|
|
} |
60 |
|
|
catch (InterruptedException ex) { |
61 |
|
|
fail("Unexpected interruption exception: " + ex); |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
public void testInvokeCallable () { |
66 |
|
|
try { |
67 |
|
|
Executor e = new DirectExecutor(); |
68 |
|
|
String result = Executors.invoke(e, new StringTask()); |
69 |
|
|
|
70 |
|
|
assertSame("should return test string", TEST_STRING, result); |
71 |
|
|
} |
72 |
|
|
catch (ExecutionException ex) { |
73 |
|
|
fail("Unexpected execution exception: " + ex); |
74 |
|
|
} |
75 |
|
|
catch (InterruptedException ex) { |
76 |
|
|
fail("Unexpected interruption exception: " + ex); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
private static final String TEST_STRING = "a test string"; |
81 |
|
|
|
82 |
|
|
private static class Task implements Runnable { |
83 |
|
|
public void run() { completed = true; } |
84 |
|
|
public boolean isCompleted() { return completed; } |
85 |
|
|
public void reset() { completed = false; } |
86 |
|
|
private boolean completed = false; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
private static class StringTask implements Callable<String> { |
90 |
|
|
public String call() { return TEST_STRING; } |
91 |
|
|
} |
92 |
|
|
} |