1   /*
2    * $Id: FutureMessageResultTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.umo;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.umo.transformer.TransformerException;
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);
46              fail();
47          }
48          catch (NullPointerException npe)
49          {
50              // OK: see FutureTask(Callable)
51          }
52  
53          try
54          {
55              FutureMessageResult f = new FutureMessageResult(Dummy);
56              f.setExecutor(null);
57              fail();
58          }
59          catch (IllegalArgumentException iex)
60          {
61              // OK: no null ExecutorService
62          }
63  
64      }
65  
66      public void testExecute() throws ExecutionException, InterruptedException, TransformerException
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);
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);
90          f.setExecutor(e);
91  
92          try
93          {
94              f.execute();
95              fail();
96          }
97          catch (RejectedExecutionException rex)
98          {
99              // OK: fail with shutdown Executor
100         }
101     }
102 
103     public void testExecuteWithTimeout()
104         throws ExecutionException, InterruptedException, TransformerException
105     {
106         Callable c = new Callable()
107         {
108             public Object call() throws InterruptedException
109             {
110                 // I'm slow, have patience with me
111                 Thread.sleep(3000L);
112                 wasCalled = true;
113                 return null;
114             }
115         };
116 
117         FutureMessageResult f = new FutureMessageResult(c);
118         f.execute();
119 
120         try
121         {
122             f.getMessage(500L);
123             fail();
124         }
125         catch (TimeoutException tex)
126         {
127             // OK: we did not wait long enough for our straggler, so let's tell him
128             // to forget about his task
129             f.cancel(true);
130         }
131         finally
132         {
133             assertFalse(wasCalled);
134         }
135     }
136 
137 }