View Javadoc

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