001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License"); you may not use this file except in
005: * compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.opensource.org/licenses/ecl1.php
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
010: * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: */
013: package edu.iu.uis.eden.messaging;
014:
015: import javax.xml.namespace.QName;
016:
017: import org.junit.Test;
018: import org.kuali.bus.services.KSBServiceLocator;
019: import org.kuali.bus.test.KSBTestCase;
020: import org.springframework.transaction.TransactionStatus;
021: import org.springframework.transaction.support.TransactionCallback;
022: import org.springframework.transaction.support.TransactionSynchronization;
023:
024: import edu.iu.uis.eden.messaging.callbacks.SimpleCallback;
025: import edu.iu.uis.eden.messaging.serviceproxies.AsynchronousMessageCaller;
026:
027: /**
028: * Verify that messaging works in the context of a transaction and message invokation is done via the
029: * {@link TransactionSynchronization} messagei
030: *
031: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
032: *
033: */
034: public class TransactionMessagingTest extends KSBTestCase {
035:
036: @Override
037: public boolean startClient1() {
038: return true;
039: }
040:
041: @Override
042: public void setUp() throws Exception {
043: super .setUp();
044: AsynchronousMessageCaller.CALLED_TRANS_COMMITTED = false;
045: AsynchronousMessageCaller.CALLED_TRANS_ROLLEDBACKED = false;
046: }
047:
048: @Test
049: public void testMessageSentOnCommittedTransaction()
050: throws Exception {
051: KSBTestUtils.setMessagingToAsync();
052:
053: KSBServiceLocator.getTransactionTemplate().execute(
054: new TransactionCallback() {
055: public Object doInTransaction(
056: TransactionStatus status) {
057:
058: QName serviceName = new QName(
059: "testAppsSharedQueue", "sharedQueue");
060: SimpleCallback callback = new SimpleCallback();
061: KEWJavaService testJavaAsyncService = (KEWJavaService) KSBServiceLocator
062: .getMessageHelper()
063: .getServiceAsynchronously(serviceName,
064: callback);
065: testJavaAsyncService
066: .invoke(new ClientAppServiceSharedPayloadObj(
067: "message content", false));
068:
069: // this is a sanity check that we haven't sent the message before the trans is committed. dont remove this
070: // line.
071: assertFalse(AsynchronousMessageCaller.CALLED_TRANS_COMMITTED);
072: return null;
073: }
074: });
075:
076: assertTrue("Message not sent transactionallY",
077: AsynchronousMessageCaller.CALLED_TRANS_COMMITTED);
078:
079: }
080:
081: @Test
082: public void testMessageNotSentOnRolledBackTransaction()
083: throws Exception {
084: KSBTestUtils.setMessagingToAsync();
085:
086: KSBServiceLocator.getTransactionTemplate().execute(
087: new TransactionCallback() {
088: public Object doInTransaction(
089: TransactionStatus status) {
090:
091: QName serviceName = new QName(
092: "testAppsSharedQueue", "sharedQueue");
093: SimpleCallback callback = new SimpleCallback();
094: KEWJavaService testJavaAsyncService = (KEWJavaService) KSBServiceLocator
095: .getMessageHelper()
096: .getServiceAsynchronously(serviceName,
097: callback);
098: testJavaAsyncService
099: .invoke(new ClientAppServiceSharedPayloadObj(
100: "message content", false));
101:
102: status.setRollbackOnly();
103: // this is a sanity check that we haven't sent the message before the trans is committed. dont remove this
104: // line.
105: assertFalse(AsynchronousMessageCaller.CALLED_TRANS_ROLLEDBACKED);
106: return null;
107: }
108: });
109:
110: assertFalse("Message not sent transactionallY",
111: AsynchronousMessageCaller.CALLED_TRANS_COMMITTED);
112: assertTrue("Message not sent transactionallY",
113: AsynchronousMessageCaller.CALLED_TRANS_ROLLEDBACKED);
114:
115: }
116:
117: }
|