1 17 package org.apache.servicemix.jbi.messaging; 18 19 import org.apache.geronimo.transaction.ExtendedTransactionManager; 20 import org.apache.geronimo.transaction.context.TransactionContextManager; 21 import org.apache.servicemix.jbi.RuntimeJBIException; 22 import org.apache.servicemix.jbi.container.ActivationSpec; 23 import org.apache.servicemix.jbi.container.JBIContainer; 24 import org.apache.servicemix.jbi.nmr.flow.Flow; 25 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver; 26 import org.apache.servicemix.tck.AsyncReceiverPojo; 27 import org.apache.servicemix.tck.Receiver; 28 import org.apache.servicemix.tck.ReceiverComponent; 29 import org.apache.servicemix.tck.SenderComponent; 30 import org.jencks.factory.GeronimoTransactionManagerFactoryBean; 31 import org.jencks.factory.TransactionContextManagerFactoryBean; 32 import org.jencks.factory.TransactionManagerFactoryBean; 33 import org.springframework.transaction.TransactionStatus; 34 import org.springframework.transaction.jta.JtaTransactionManager; 35 import org.springframework.transaction.support.TransactionCallback; 36 import org.springframework.transaction.support.TransactionTemplate; 37 38 import javax.jbi.JBIException; 39 import javax.transaction.TransactionManager ; 40 import javax.transaction.UserTransaction ; 41 42 import junit.framework.TestCase; 43 44 47 public abstract class AbstractTransactionTest extends TestCase { 48 49 protected static final int NUM_MESSAGES = 10; 50 51 protected TransactionTemplate tt; 52 protected TransactionManager tm; 53 protected TransactionContextManager tcm; 54 protected JBIContainer senderContainer; 55 56 59 protected void setUp() throws Exception { 60 super.setUp(); 61 createTransactionLayer(); 62 senderContainer = createJbiContainer("senderContainer"); 63 } 64 65 protected void tearDown() throws Exception { 66 senderContainer.shutDown(); 67 } 68 69 protected void createTransactionLayer() throws Exception { 70 TransactionManagerFactoryBean tmcf = new TransactionManagerFactoryBean(); 71 tmcf.afterPropertiesSet(); 72 ExtendedTransactionManager etm = (ExtendedTransactionManager) tmcf.getObject(); 73 TransactionContextManagerFactoryBean tcmfb = new TransactionContextManagerFactoryBean(); 74 tcmfb.setTransactionManager(etm); 75 tcmfb.afterPropertiesSet(); 76 tcm = (TransactionContextManager) tcmfb.getObject(); 77 GeronimoTransactionManagerFactoryBean gtmfb = new GeronimoTransactionManagerFactoryBean(); 78 gtmfb.setTransactionContextManager(tcm); 79 gtmfb.afterPropertiesSet(); 80 tm = (TransactionManager) gtmfb.getObject(); 81 tt = new TransactionTemplate(new JtaTransactionManager((UserTransaction ) tm)); 82 } 83 84 protected JBIContainer createJbiContainer(String name) throws Exception { 85 JBIContainer container = new JBIContainer(); 86 container.setTransactionManager(tm); 87 container.setName(name); 88 container.setFlow(createFlow()); 89 container.setAutoEnlistInTransaction(true); 90 container.setEmbedded(true); 91 container.init(); 92 container.start(); 93 return container; 94 } 95 96 protected abstract Flow createFlow(); 97 98 protected void runSimpleTest(final boolean syncSend, final boolean syncReceive) throws Exception { 99 final SenderComponent sender = new SenderComponent(); 100 sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE)); 101 final Receiver receiver; 102 if (syncReceive) { 103 receiver = new ReceiverComponent(); 104 } else { 105 receiver = new AsyncReceiverPojo(); 106 } 107 108 senderContainer.activateComponent(new ActivationSpec("sender", sender)); 109 senderContainer.activateComponent(new ActivationSpec("receiver", receiver)); 110 111 tt.execute(new TransactionCallback() { 112 public Object doInTransaction(TransactionStatus status) { 113 try { 114 sender.sendMessages(NUM_MESSAGES, syncSend); 115 } catch (JBIException e) { 116 throw new RuntimeJBIException(e); 117 } 118 return null; 119 } 120 }); 121 receiver.getMessageList().assertMessagesReceived(NUM_MESSAGES); 122 } 123 124 public void testSyncSendSyncReceive() throws Exception { 125 runSimpleTest(true, true); 126 } 127 128 public void testAsyncSendSyncReceive() throws Exception { 129 runSimpleTest(false, true); 130 } 131 132 public void testSyncSendAsyncReceive() throws Exception { 133 runSimpleTest(true, false); 134 } 135 136 public void testAsyncSendAsyncReceive() throws Exception { 137 runSimpleTest(false, false); 138 } 139 140 } 141 | Popular Tags |