1   /*
2    * $Id: TransactionNotificationsTestCase.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.transaction;
12  
13  import org.mule.impl.internal.notifications.TransactionNotification;
14  import org.mule.impl.internal.notifications.TransactionNotificationListener;
15  import org.mule.tck.AbstractMuleTestCase;
16  import org.mule.umo.TransactionException;
17  import org.mule.umo.UMOTransaction;
18  import org.mule.umo.manager.UMOManager;
19  import org.mule.umo.manager.UMOServerNotification;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23  
24  public class TransactionNotificationsTestCase extends AbstractMuleTestCase
25  {
26      private UMOManager manager;
27  
28      protected void doSetUp() throws Exception
29      {
30          manager = getManager(true);
31          manager.start();
32      }
33  
34      protected void doTearDown() throws Exception
35      {
36          manager.stop();
37      }
38  
39      public void testTransactionNotifications() throws Exception
40      {
41          final CountDownLatch latch = new CountDownLatch(3);
42  
43          manager.registerListener(new TransactionNotificationListener()
44          {
45              public void onNotification(UMOServerNotification notification)
46              {
47                  if (notification.getAction() == TransactionNotification.TRANSACTION_BEGAN)
48                  {
49                      assertEquals("begin", notification.getActionName());
50                      latch.countDown();
51                  }
52                  else
53                  {
54                      if (notification.getAction() == TransactionNotification.TRANSACTION_COMMITTED)
55                      {
56                          assertEquals("commit", notification.getActionName());
57                          latch.countDown();
58                      }
59                      else
60                      {
61                          if (notification.getAction() == TransactionNotification.TRANSACTION_ROLLEDBACK)
62                          {
63                              assertEquals("rollback", notification.getActionName());
64                              latch.countDown();
65                          }
66                      }
67                  }
68  
69              }
70          });
71  
72          // the code is simple and deceptive :) The trick is this dummy transaction is handled by
73          // a global TransactionCoordination instance, which binds it to the current thread.
74          UMOTransaction transaction = new DummyTransaction();
75          transaction.begin();
76          transaction.commit();
77          transaction.rollback();
78  
79          // Wait for the notifcation event to be fired as they are queued
80          latch.await(2000, TimeUnit.MILLISECONDS);
81          assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
82      }
83  
84  
85      private class DummyTransaction extends AbstractTransaction
86      {
87  
88          protected void doBegin() throws TransactionException
89          {
90              // nothing to do
91          }
92  
93          protected void doCommit() throws TransactionException
94          {
95              // nothing to do
96          }
97  
98          protected void doRollback() throws TransactionException
99          {
100             // nothing to do
101         }
102 
103         public int getStatus() throws TransactionException
104         {
105             return 0;
106         }
107 
108         public Object getResource(Object key)
109         {
110             return null;
111         }
112 
113         public boolean hasResource(Object key)
114         {
115             return false;
116         }
117 
118         public void bindResource(Object key, Object resource) throws TransactionException
119         {
120             // nothing to do
121         }
122 
123         public void setRollbackOnly() throws TransactionException
124         {
125             // nothing to do
126         }
127     }
128 
129 }