1
2
3
4
5
6
7
8
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
59
60 Transaction transaction = new DummyTransaction();
61 transaction.begin();
62 transaction.commit();
63 transaction.rollback();
64
65
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
77 }
78
79 protected void doCommit() throws TransactionException
80 {
81
82 }
83
84 protected void doRollback() throws TransactionException
85 {
86
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
107 }
108
109 public void setRollbackOnly() throws TransactionException
110 {
111
112 }
113 }
114
115 }