1   /*
2    * $Id: TransactionNotificationsTestCase.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.transaction;
12  
13  import org.mule.api.context.notification.ServerNotification;
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          muleContext.registerListener(new TransactionNotificationListener()
30          {
31              public void onNotification(ServerNotification notification)
32              {
33                  if (notification.getAction() == TransactionNotification.TRANSACTION_BEGAN)
34                  {
35                      assertEquals("begin", notification.getActionName());
36                      latch.countDown();
37                  }
38                  else
39                  {
40                      if (notification.getAction() == TransactionNotification.TRANSACTION_COMMITTED)
41                      {
42                          assertEquals("commit", notification.getActionName());
43                          latch.countDown();
44                      }
45                      else
46                      {
47                          if (notification.getAction() == TransactionNotification.TRANSACTION_ROLLEDBACK)
48                          {
49                              assertEquals("rollback", notification.getActionName());
50                              latch.countDown();
51                          }
52                      }
53                  }
54  
55              }
56          });
57  
58          // the code is simple and deceptive :) The trick is this dummy transaction is handled by
59          // a global TransactionCoordination instance, which binds it to the current thread.
60          Transaction transaction = new DummyTransaction();
61          transaction.begin();
62          transaction.commit();
63          transaction.rollback();
64  
65          // Wait for the notifcation event to be fired as they are queued
66          latch.await(2000, TimeUnit.MILLISECONDS);
67          assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
68      }
69  
70  
71      private class DummyTransaction extends AbstractTransaction
72      {
73  
74          protected void doBegin() throws TransactionException
75          {
76              // nothing to do
77          }
78  
79          protected void doCommit() throws TransactionException
80          {
81              // nothing to do
82          }
83  
84          protected void doRollback() throws TransactionException
85          {
86              // nothing to do
87          }
88  
89          public int getStatus() throws TransactionException
90          {
91              return 0;
92          }
93  
94          public Object getResource(Object key)
95          {
96              return null;
97          }
98  
99          public boolean hasResource(Object key)
100         {
101             return false;
102         }
103 
104         public void bindResource(Object key, Object resource) throws TransactionException
105         {
106             // nothing to do
107         }
108 
109         public void setRollbackOnly() throws TransactionException
110         {
111             // nothing to do
112         }
113     }
114 
115 }