1
2
3
4
5
6
7
8
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
34
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
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 }