1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging;
17
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20
21 import javax.xml.namespace.QName;
22
23 import org.junit.Test;
24 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
25 import org.kuali.rice.ksb.messaging.service.KSBJavaService;
26 import org.kuali.rice.ksb.messaging.serviceproxies.MessageSendingTransactionSynchronization;
27 import org.kuali.rice.ksb.service.KSBServiceLocator;
28 import org.kuali.rice.ksb.test.KSBTestCase;
29 import org.springframework.transaction.TransactionStatus;
30 import org.springframework.transaction.support.TransactionCallback;
31 import org.springframework.transaction.support.TransactionSynchronization;
32
33
34
35
36
37
38
39
40
41 public class TransactionMessagingTest extends KSBTestCase {
42
43 @Override
44 public boolean startClient1() {
45 return true;
46 }
47
48 @Override
49 public void setUp() throws Exception {
50 super.setUp();
51 MessageSendingTransactionSynchronization.CALLED_TRANS_COMMITTED.set(false);
52 MessageSendingTransactionSynchronization.CALLED_TRANS_ROLLEDBACKED.set(false);
53 }
54
55 @Test
56 public void testMessageSentOnCommittedTransaction() throws Exception {
57 KSBTestUtils.setMessagingToAsync();
58
59 KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback<Object>() {
60 public Object doInTransaction(TransactionStatus status) {
61
62 QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
63 KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper()
64 .getServiceAsynchronously(serviceName);
65 testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
66
67
68
69 assertFalse(MessageSendingTransactionSynchronization.CALLED_TRANS_COMMITTED.get());
70 return null;
71 }
72 });
73
74 assertTrue("Message not sent transactionallY", MessageSendingTransactionSynchronization.CALLED_TRANS_COMMITTED.get());
75
76 }
77
78 @Test
79 public void testMessageNotSentOnRolledBackTransaction() throws Exception {
80 KSBTestUtils.setMessagingToAsync();
81
82 KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback<Object>() {
83 public Object doInTransaction(TransactionStatus status) {
84
85 QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
86 KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper()
87 .getServiceAsynchronously(serviceName);
88 testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
89
90 status.setRollbackOnly();
91
92
93 assertFalse(MessageSendingTransactionSynchronization.CALLED_TRANS_ROLLEDBACKED.get());
94 return null;
95 }
96 });
97
98 assertFalse("Message not sent transactionallY", MessageSendingTransactionSynchronization.CALLED_TRANS_COMMITTED.get());
99 assertTrue("Message not sent transactionallY", MessageSendingTransactionSynchronization.CALLED_TRANS_ROLLEDBACKED.get());
100
101 }
102
103 }