View Javadoc

1   /*
2    * $Id: TransactionNotificationsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.transaction;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.context.notification.TransactionNotificationListener;
15  import org.mule.api.transaction.Transaction;
16  import org.mule.api.transaction.TransactionException;
17  import org.mule.context.notification.TransactionNotification;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
21  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22  
23  public class TransactionNotificationsTestCase extends AbstractMuleTestCase
24  {
25      public void testTransactionNotifications() throws Exception
26      {
27          final CountDownLatch latch = new CountDownLatch(3);
28  
29          // the code is simple and deceptive :) The trick is this dummy transaction is handled by
30          // a global TransactionCoordination instance, which binds it to the current thread.
31          Transaction transaction = new DummyTransaction(muleContext);
32  
33          muleContext.registerListener(new TransactionNotificationListener<TransactionNotification>()
34          {
35              public void onNotification(TransactionNotification notification)
36              {
37                  if (notification.getAction() == TransactionNotification.TRANSACTION_BEGAN)
38                  {
39                      assertEquals("begin", notification.getActionName());
40                      latch.countDown();
41                  }
42                  else
43                  {
44                      if (notification.getAction() == TransactionNotification.TRANSACTION_COMMITTED)
45                      {
46                          assertEquals("commit", notification.getActionName());
47                          latch.countDown();
48                      }
49                      else
50                      {
51                          if (notification.getAction() == TransactionNotification.TRANSACTION_ROLLEDBACK)
52                          {
53                              assertEquals("rollback", notification.getActionName());
54                              latch.countDown();
55                          }
56                      }
57                  }
58              }
59          }, transaction.getId());
60  
61  
62          transaction.begin();
63          transaction.commit();
64          transaction.rollback();
65  
66          // Wait for the notifcation event to be fired as they are queued
67          latch.await(2000, TimeUnit.MILLISECONDS);
68          assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
69      }
70  
71  
72      private class DummyTransaction extends AbstractSingleResourceTransaction
73      {
74  
75          private DummyTransaction(MuleContext muleContext)
76          {
77              super(muleContext);
78          }
79  
80          protected void doBegin() throws TransactionException
81          {
82  
83          }
84  
85          protected void doCommit() throws TransactionException
86          {
87  
88          }
89  
90          protected void doRollback() throws TransactionException
91          {
92              
93          }
94      }
95  
96  }