View Javadoc

1   /*
2    * $Id:AbstractExternalTransactionTestCase.java 8215 2007-09-05 16:56:51Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.vm.functional.transactions;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.mule.api.transaction.TransactionCallback;
16  import org.mule.api.transaction.TransactionConfig;
17  import org.mule.module.client.MuleClient;
18  import org.mule.transaction.TransactionTemplate;
19  
20  import javax.transaction.Transaction;
21  
22  /** Test transaction behavior when "joinExternal" is set to disallow joining external transactions
23   * There is one test per legal transactional behavior (e.g. ALWAYS_BEGIN).
24   */
25  public class NoExternalTransactionTestCase extends AbstractExternalTransactionTestCase
26  {
27      public static final long WAIT = 3000L;
28  
29      protected static final Log logger = LogFactory.getLog(NoExternalTransactionTestCase.class);
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "org/mule/test/config/no-external-transaction-config.xml";
35      }
36  
37      public void testBeginOrJoinTransaction() throws Exception
38      {
39          init();
40          TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_BEGIN_OR_JOIN, false);
41  
42          logger.debug("TM is a " + tm.getClass().toString());
43          tm.begin();
44          final Transaction tx = tm.getTransaction();
45          final TestResource resource1 = new TestResource(tm);
46          tx.enlistResource(resource1);
47          assertNotNull(tx);
48  
49          String result;
50          Exception ex = null;
51  
52          // This will throw, becasue nested transactions are not supported
53          try
54          {
55              result = tt.execute(new TransactionCallback<String>()
56              {
57                  public String doInTransaction() throws Exception
58                  {
59                      return "OK";
60                  }
61              });
62          }
63          catch (Exception e)
64          {
65              ex = e;
66              logger.debug("saw exception " + e.getMessage());
67          }
68          assertNotNull(ex);
69          tm.rollback();
70  
71          // now try with no active transaction
72          result = tt.execute(new TransactionCallback<String>()
73          {
74              public String doInTransaction() throws Exception
75              {
76                  Transaction muleTx = tm.getTransaction();
77                  muleTx.enlistResource(resource1);
78                  resource1.setValue(15);
79                  return "OK";
80              }
81          });
82  
83          // Mule began and committed the transaction
84          assertEquals(15, resource1.getPersistentValue());
85      }
86  
87      public void testBeginTransaction() throws Exception
88      {
89          init();
90          TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_ALWAYS_BEGIN, false);
91  
92          tm.begin();
93          final Transaction tx = tm.getTransaction();
94          final TestResource resource1 = new TestResource(tm);
95          tx.enlistResource(resource1);
96          assertNotNull(tx);
97  
98          String result;
99          Exception ex = null;
100 
101         // This will throw, because nested transactions are not supported
102         try
103         {
104             result = tt.execute(new TransactionCallback<String>()
105             {
106                 public String doInTransaction() throws Exception
107                 {
108                     return "OK";
109                 }
110             });
111         }
112         catch (Exception e)
113         {
114             ex = e;
115             logger.debug("saw exception " + e.getMessage());
116         }
117         assertNotNull(ex);
118         tm.rollback();
119 
120         // now try with no active transaction
121         result = tt.execute(new TransactionCallback<String>()
122         {
123             public String doInTransaction() throws Exception
124             {
125                 Transaction muleTx = tm.getTransaction();
126                 muleTx.enlistResource(resource1);
127                 resource1.setValue(15);
128                 return "OK";
129             }
130         });
131 
132         // Mule began and committed the transaction
133         assertEquals(15, resource1.getPersistentValue());
134     }
135 
136     public void testNoTransactionProcessing() throws Exception
137     {
138         init();
139         TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_NONE, false);
140 
141         tm.begin();
142         final Transaction tx = tm.getTransaction();
143         final TestResource resource1 = new TestResource(tm);
144 
145         assertNotNull(tx);
146         tx.enlistResource(resource1);
147         resource1.setValue(14);
148         String result = tt.execute(new TransactionCallback<String>()
149         {
150             public String doInTransaction() throws Exception
151             {
152                 Transaction muleTx = tm.getTransaction();
153                 assertNotNull(muleTx);
154                 return "OK";
155             }
156         });
157 
158         // transaction ignored, no commit
159         assertEquals("OK", result);
160         assertEquals(14, resource1.getValue());
161         assertEquals(0, resource1.getPersistentValue());
162         tm.commit();
163 
164         // Now it's committed
165         assertEquals(14, resource1.getPersistentValue());
166 
167         result = tt.execute(new TransactionCallback<String>()
168         {
169             public String doInTransaction() throws Exception
170             {
171                 Transaction muleTx = tm.getTransaction();
172                 assertNull(muleTx);
173                 return "OK";
174             }
175         });
176     }
177 
178     public void testAlwaysJoinTransaction() throws Exception
179     {
180         init();
181         TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_ALWAYS_JOIN, false);
182 
183         tm.begin();
184         final Transaction tx = tm.getTransaction();
185         final TestResource resource1 = new TestResource(tm);
186         tx.enlistResource(resource1);
187         assertNotNull(tx);
188 
189         Exception ex = null;
190         String result;
191         try
192         {
193             // Thjis will throw, because Mule sees no transaction to join
194             result = tt.execute(new TransactionCallback<String>()
195             {
196                 public String doInTransaction() throws Exception
197                 {
198                     Transaction muleTx = tm.getTransaction();
199                     assertSame(tx, muleTx);
200                     resource1.setValue(14);
201                     return "OK";
202                 }
203             });
204         }
205         catch (Exception e)
206         {
207             ex = e;
208             logger.debug("saw exception " + e.getMessage());
209         }
210 
211         // Not committed yet, since Mule joined the external transaction
212         assertNotNull(ex);
213         tm.rollback();
214 
215         // try with no active transaction.. Should still throw
216         try
217         {
218             result = tt.execute(new TransactionCallback<String>()
219             {
220                 public String doInTransaction() throws Exception
221                 {
222                     return "OK";
223                 }
224             });
225         }
226         catch (Exception e)
227         {
228             ex = e;
229             logger.debug("saw exception " + e.getMessage());
230         }
231         assertNotNull(ex);
232     }
233 
234     public void testJoinTransactionIfPossible() throws Exception
235     {
236         init();
237         TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_JOIN_IF_POSSIBLE, false);
238 
239         tm.begin();
240         final Transaction tx = tm.getTransaction();
241         final TestResource resource1 = new TestResource(tm);
242         tx.enlistResource(resource1);
243         assertNotNull(tx);
244         String result = tt.execute(new TransactionCallback<String>()
245         {
246             public String doInTransaction() throws Exception
247             {
248                 Transaction muleTx = tm.getTransaction();
249                 assertSame(tx, muleTx);
250                 resource1.setValue(14);
251                 return "OK";
252             }
253         });
254 
255         // Not committed yet, since Mule saw no transaction to join
256         assertEquals("OK", result);
257         assertEquals(14, resource1.getValue());
258         assertEquals(0, resource1.getPersistentValue());
259         tm.commit();
260 
261         // Now it's committed
262         assertEquals(14, resource1.getPersistentValue());
263 
264         // try with no active transaction.. Should run with none
265         result = tt.execute(new TransactionCallback<String>()
266         {
267             public String doInTransaction() throws Exception
268             {
269                 Transaction muleTx = tm.getTransaction();
270                 assertNull(muleTx);
271                 return "OK";
272             }
273         });
274         assertEquals("OK", result);
275     }
276 
277     public void testNoTransactionAllowed() throws Exception
278     {
279         init();
280         TransactionTemplate<String> tt = createTransactionTemplate(TransactionConfig.ACTION_NEVER, false);
281 
282         tm.begin();
283         final Transaction tx = tm.getTransaction();
284         final TestResource resource1 = new TestResource(tm);
285         tx.enlistResource(resource1);
286         assertNotNull(tx);
287 
288         // This will not throw since Mule sees no transaction
289         String result = tt.execute(new TransactionCallback<String>()
290         {
291             public String doInTransaction() throws Exception
292             {
293                 resource1.setValue(14);
294                 return "OK";
295             }
296         });
297 
298         // Not committed yet, since Mule saw no transaction to join
299         assertEquals("OK", result);
300         assertEquals(14, resource1.getValue());
301         assertEquals(0, resource1.getPersistentValue());
302         tm.commit();
303 
304         // Now it's committed
305         assertEquals(14, resource1.getPersistentValue());
306     }
307 
308         /** Check that the configuration specifies considers external transactions */
309     public void testConfiguration() throws Exception
310     {
311         MuleClient client = new MuleClient(muleContext);
312         tm = client.getMuleContext().getTransactionManager();
313         tm.begin();
314 
315         // This will fail, since there will be no Mule transaction to join
316         client.send("vm://entry?connector=vm-normal", "OK", null);
317         Object response = client.request("queue", WAIT);
318         assertNull(response);
319 
320         tm.commit();
321     }
322 
323 }