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