View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Verify that messaging works in the context of a transaction and message invokation is done via the
36   * {@link TransactionSynchronization} messagei
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
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  		// this is a sanity check that we haven't sent the message before the trans is committed. dont remove this
68                  // line.
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  		// this is a sanity check that we haven't sent the message before the trans is committed. dont remove this
92                  // line.
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 }