1
2
3
4
5
6
7 package org.mule.api;
8
9 import org.mule.tck.junit4.AbstractMuleContextTestCase;
10
11 import edu.emory.mathcs.backport.java.util.concurrent.Callable;
12 import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
13 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
14 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
15 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
16 import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException;
17 import org.junit.Test;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 public class FutureMessageResultTestCase extends AbstractMuleContextTestCase
25 {
26 private static Callable Dummy = new Callable()
27 {
28 public Object call()
29 {
30 return null;
31 }
32 };
33
34 private volatile boolean wasCalled;
35
36 @Test
37 public void testCreation()
38 {
39 try
40 {
41 new FutureMessageResult(null, muleContext);
42 fail();
43 }
44 catch (NullPointerException npe)
45 {
46
47 }
48
49 try
50 {
51 FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
52 f.setExecutor(null);
53 fail();
54 }
55 catch (IllegalArgumentException iex)
56 {
57
58 }
59
60 }
61
62 @Test
63 public void testExecute() throws ExecutionException, InterruptedException, MuleException
64 {
65 Callable c = new Callable()
66 {
67 public Object call()
68 {
69 wasCalled = true;
70 return null;
71 }
72 };
73
74 FutureMessageResult f = new FutureMessageResult(c, muleContext);
75 f.execute();
76
77 assertNull(f.getMessage());
78 assertTrue(wasCalled);
79 }
80
81 @Test
82 public void testExecuteWithShutdownExecutor()
83 {
84 ExecutorService e = Executors.newCachedThreadPool();
85 e.shutdown();
86
87 FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
88 f.setExecutor(e);
89
90 try
91 {
92 f.execute();
93 fail();
94 }
95 catch (RejectedExecutionException rex)
96 {
97
98 }
99 }
100
101 @Test
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 }