1 17 package org.apache.servicemix.jbi.nmr; 18 19 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 20 21 import org.apache.servicemix.MessageExchangeListener; 22 import org.apache.servicemix.jbi.container.ActivationSpec; 23 import org.apache.servicemix.jbi.container.JBIContainer; 24 import org.apache.servicemix.jbi.container.SubscriptionSpec; 25 import org.apache.servicemix.jbi.nmr.flow.Flow; 26 import org.apache.servicemix.jbi.nmr.flow.FlowProvider; 27 import org.apache.servicemix.tck.Receiver; 28 import org.apache.servicemix.tck.ReceiverComponent; 29 import org.apache.servicemix.tck.SenderComponent; 30 31 import javax.jbi.messaging.MessageExchange; 32 import javax.jbi.messaging.MessagingException; 33 34 import java.util.List ; 35 36 import junit.framework.TestCase; 37 38 public class SubscriptionTest extends TestCase { 39 40 public void testStNullAsync() throws Exception { 41 runTest("st", null, false); 42 } 43 44 public void testStStAsync() throws Exception { 45 runTest("st", "st", false); 46 } 47 48 public void testStSedaAsync() throws Exception { 49 runTest("st", "seda", false); 50 } 51 52 public void testSedaNullAsync() throws Exception { 53 runTest("seda", null, false); 54 } 55 56 public void testSedaStAsync() throws Exception { 57 runTest("seda", "st", false); 58 } 59 60 public void testSedaSedaAsync() throws Exception { 61 runTest("seda", "seda", false); 62 } 63 64 public void testStNullSync() throws Exception { 65 runTest("st", null, true); 66 } 67 68 public void testStStSync() throws Exception { 69 runTest("st", "st", true); 70 } 71 72 public void testStSedaSync() throws Exception { 73 runTest("st", "seda", true); 74 } 75 76 public void testSedaNullSync() throws Exception { 77 runTest("seda", null, true); 78 } 79 80 public void testSedaStSync() throws Exception { 81 runTest("seda", "st", true); 82 } 83 84 public void testSedaSedaSync() throws Exception { 85 runTest("seda", "seda", true); 86 } 87 88 private void runTest(String flowName, String subscriptionFlowName, boolean sync) throws Exception { 89 JBIContainer container = new JBIContainer(); 90 try { 91 container.setEmbedded(true); 92 if (subscriptionFlowName != null && !subscriptionFlowName.equals(flowName)) { 93 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName), 94 FlowProvider.getFlow(subscriptionFlowName)}); 95 } else { 96 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName) }); 97 } 98 if (subscriptionFlowName != null) { 99 container.getDefaultBroker().getSubscriptionManager().setFlowName(subscriptionFlowName); 100 } 101 container.init(); 105 container.start(); 106 107 SenderListener sender = new SenderListener(); 108 ActivationSpec senderActivationSpec = new ActivationSpec("sender", sender); 109 senderActivationSpec.setFailIfNoDestinationEndpoint(false); 110 container.activateComponent(senderActivationSpec); 111 112 Receiver receiver1 = new ReceiverComponent(); 113 container.activateComponent(createReceiverAS("receiver1", receiver1)); 114 115 Receiver receiver2 = new ReceiverComponent(); 116 container.activateComponent(createReceiverAS("receiver2", receiver2)); 117 118 sender.sendMessages(1, sync); 119 120 Thread.sleep(100); 121 122 assertEquals(1, receiver1.getMessageList().getMessageCount()); 123 assertEquals(1, receiver2.getMessageList().getMessageCount()); 124 assertEquals(0, sender.responses.size()); 125 } finally { 126 container.shutDown(); 127 } 128 } 129 130 private ActivationSpec createReceiverAS(String id, Object component) { 131 ActivationSpec as = new ActivationSpec(id, component); 132 SubscriptionSpec ss = new SubscriptionSpec(); 133 ss.setService(SenderComponent.SERVICE); 134 as.setEndpoint(id); 135 as.setSubscriptions(new SubscriptionSpec[] { ss }); 136 return as; 137 } 138 139 public static class SenderListener extends SenderComponent implements MessageExchangeListener { 140 141 public List responses = new CopyOnWriteArrayList(); 142 143 public void onMessageExchange(MessageExchange exchange) throws MessagingException { 144 responses.add(exchange); 145 } 146 147 } 148 149 } 150 | Popular Tags |