1
2
3
4
5
6
7
8
9
10
11 package org.mule.api;
12
13 import org.mule.tck.AbstractMuleTestCase;
14
15 import edu.emory.mathcs.backport.java.util.concurrent.Callable;
16 import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
17 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
18 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
19 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
20 import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException;
21
22 public class FutureMessageResultTestCase extends AbstractMuleTestCase
23 {
24 private static Callable Dummy = new Callable()
25 {
26 public Object call()
27 {
28 return null;
29 }
30 };
31
32 volatile boolean wasCalled;
33
34 protected void doSetUp() throws Exception
35 {
36 super.doSetUp();
37 wasCalled = false;
38 }
39
40 public void testCreation()
41 {
42 try
43 {
44 new FutureMessageResult(null, muleContext);
45 fail();
46 }
47 catch (NullPointerException npe)
48 {
49
50 }
51
52 try
53 {
54 FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
55 f.setExecutor(null);
56 fail();
57 }
58 catch (IllegalArgumentException iex)
59 {
60
61 }
62
63 }
64
65 public void testExecute() throws ExecutionException, InterruptedException, MuleException
66 {
67 Callable c = new Callable()
68 {
69 public Object call()
70 {
71 wasCalled = true;
72 return null;
73 }
74 };
75
76 FutureMessageResult f = new FutureMessageResult(c, muleContext);
77 f.execute();
78
79 assertNull(f.getMessage());
80 assertTrue(wasCalled);
81 }
82
83 public void testExecuteWithShutdownExecutor()
84 {
85 ExecutorService e = Executors.newCachedThreadPool();
86 e.shutdown();
87
88 FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
89 f.setExecutor(e);
90
91 try
92 {
93 f.execute();
94 fail();
95 }
96 catch (RejectedExecutionException rex)
97 {
98
99 }
100 }
101
102 public void testExecuteWithTimeout()
103 throws ExecutionException, InterruptedException, MuleException
104 {
105 Callable c = new Callable()
106 {
107 public Object call() throws InterruptedException
108 {
109
110 Thread.sleep(3000L);
111 wasCalled = true;
112 return null;
113 }
114 };
115
116 FutureMessageResult f = new FutureMessageResult(c, muleContext);
117 f.execute();
118
119 try
120 {
121 f.getMessage(500L);
122 fail();
123 }
124 catch (TimeoutException tex)
125 {
126
127
128 f.cancel(true);
129 }
130 finally
131 {
132 assertFalse(wasCalled);
133 }
134 }
135
136 }