1
2
3
4
5
6
7 package org.mule.transaction;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.context.notification.TransactionNotificationListener;
11 import org.mule.api.transaction.Transaction;
12 import org.mule.api.transaction.TransactionException;
13 import org.mule.context.notification.TransactionNotification;
14 import org.mule.tck.junit4.AbstractMuleContextTestCase;
15
16 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21
22 public class TransactionNotificationsTestCase extends AbstractMuleContextTestCase
23 {
24 @Test
25 public void testTransactionNotifications() throws Exception
26 {
27 final CountDownLatch latch = new CountDownLatch(3);
28
29
30
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
67 latch.await(2000, TimeUnit.MILLISECONDS);
68 assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
69 }
70
71 private class DummyTransaction extends AbstractSingleResourceTransaction
72 {
73
74 private DummyTransaction(MuleContext muleContext)
75 {
76 super(muleContext);
77 }
78
79 protected void doBegin() throws TransactionException
80 {
81
82 }
83
84 protected void doCommit() throws TransactionException
85 {
86
87 }
88
89 protected void doRollback() throws TransactionException
90 {
91
92 }
93 }
94
95 }