1   /*
2    * $Id: FutureMessageResultTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.api;
12  
13  import org.mule.api.FutureMessageResult;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.tck.AbstractMuleTestCase;
16  
17  import edu.emory.mathcs.backport.java.util.concurrent.Callable;
18  import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
19  import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
20  import edu.emory.mathcs.backport.java.util.concurrent.Executors;
21  import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException;
23  
24  public class FutureMessageResultTestCase extends AbstractMuleTestCase
25  {
26      private static Callable Dummy = new Callable()
27      {
28          public Object call()
29          {
30              return null;
31          }
32      };
33  
34      volatile boolean wasCalled;
35  
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39          wasCalled = false;
40      }
41  
42      public void testCreation()
43      {
44          try
45          {
46              new FutureMessageResult(null);
47              fail();
48          }
49          catch (NullPointerException npe)
50          {
51              // OK: see FutureTask(Callable)
52          }
53  
54          try
55          {
56              FutureMessageResult f = new FutureMessageResult(Dummy);
57              f.setExecutor(null);
58              fail();
59          }
60          catch (IllegalArgumentException iex)
61          {
62              // OK: no null ExecutorService
63          }
64  
65      }
66  
67      public void testExecute() throws ExecutionException, InterruptedException, TransformerException
68      {
69          Callable c = new Callable()
70          {
71              public Object call()
72              {
73                  wasCalled = true;
74                  return null;
75              }
76          };
77  
78          FutureMessageResult f = new FutureMessageResult(c);
79          f.execute();
80  
81          assertNull(f.getMessage());
82          assertTrue(wasCalled);
83      }
84  
85      public void testExecuteWithShutdownExecutor()
86      {
87          ExecutorService e = Executors.newCachedThreadPool();
88          e.shutdown();
89  
90          FutureMessageResult f = new FutureMessageResult(Dummy);
91          f.setExecutor(e);
92  
93          try
94          {
95              f.execute();
96              fail();
97          }
98          catch (RejectedExecutionException rex)
99          {
100             // OK: fail with shutdown Executor
101         }
102     }
103 
104     public void testExecuteWithTimeout()
105         throws ExecutionException, InterruptedException, TransformerException
106     {
107         Callable c = new Callable()
108         {
109             public Object call() throws InterruptedException
110             {
111                 // I'm slow, have patience with me
112                 Thread.sleep(3000L);
113                 wasCalled = true;
114                 return null;
115             }
116         };
117 
118         FutureMessageResult f = new FutureMessageResult(c);
119         f.execute();
120 
121         try
122         {
123             f.getMessage(500L);
124             fail();
125         }
126         catch (TimeoutException tex)
127         {
128             // OK: we did not wait long enough for our straggler, so let's tell him
129             // to forget about his task
130             f.cancel(true);
131         }
132         finally
133         {
134             assertFalse(wasCalled);
135         }
136     }
137 
138 }