1 17 package org.apache.servicemix.jbi.messaging; 18 19 import javax.jbi.messaging.DeliveryChannel; 20 import javax.jbi.messaging.ExchangeStatus; 21 import javax.jbi.messaging.InOut; 22 import javax.jbi.messaging.MessageExchangeFactory; 23 import javax.jbi.messaging.MessagingException; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.xml.namespace.QName ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.servicemix.components.util.ComponentSupport; 30 import org.apache.servicemix.jbi.container.ActivationSpec; 31 import org.apache.servicemix.jbi.container.JBIContainer; 32 import org.apache.servicemix.jbi.jaxp.StringSource; 33 import org.apache.servicemix.tck.SenderComponent; 34 35 public class DeliveryChannelImplTest extends TestCase { 36 37 protected JBIContainer container; 38 39 protected void setUp() throws Exception { 40 container = new JBIContainer(); 41 container.setEmbedded(true); 42 container.init(); 43 container.start(); 44 } 45 46 protected void tearDown() throws Exception { 47 container.shutDown(); 48 } 49 50 public void testExchangeFactoryOnOpenChannel() throws Exception { 51 TestComponent component = new TestComponent(null, null); 53 container.activateComponent(new ActivationSpec("component", component)); 54 DeliveryChannel channel = component.getChannel(); 55 MessageExchangeFactory mef = channel.createExchangeFactory(); 57 assertNotNull(mef); 58 assertNotNull(mef.createInOnlyExchange()); 59 } 60 61 public void testExchangeFactoryOnClosedChannel() throws Exception { 62 TestComponent component = new TestComponent(null, null); 64 container.activateComponent(new ActivationSpec("component", component)); 65 DeliveryChannel channel = component.getChannel(); 66 channel.close(); 68 MessageExchangeFactory mef = channel.createExchangeFactory(); 69 assertNotNull(mef); 70 try { 71 mef.createInOnlyExchange(); 72 fail("Exchange creation should have failed (JBI: 5.5.2.1.4)"); 73 } catch (MessagingException e) { 74 } 76 } 77 78 public void testSendSyncOnSameComponent() throws Exception { 79 TestComponent component = new TestComponent(new QName ("service"), "endpoint"); 81 container.activateComponent(new ActivationSpec("component", component)); 82 final DeliveryChannel channel = component.getChannel(); 83 84 new Thread () { 86 public void run() { 87 try { 88 InOut me = (InOut) channel.accept(5000); 89 NormalizedMessage nm = me.createMessage(); 90 nm.setContent(new StringSource("<response/>")); 91 me.setOutMessage(nm); 92 channel.sendSync(me); 93 } catch (MessagingException e) { 94 e.printStackTrace(); 95 fail("Exception caught: " + e); 96 } 97 } 98 }.start(); 99 100 MessageExchangeFactory factory = channel.createExchangeFactoryForService(new QName ("service")); 101 InOut me = factory.createInOutExchange(); 102 NormalizedMessage nm = me.createMessage(); 103 nm.setContent(new StringSource("<request/>")); 104 me.setInMessage(nm); 105 channel.sendSync(me); 106 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 107 me.setStatus(ExchangeStatus.DONE); 108 channel.send(me); 109 } 110 111 public static class TestComponent extends ComponentSupport { 112 public TestComponent(QName service, String endpoint) { 113 super(service, endpoint); 114 } 115 public DeliveryChannel getChannel() throws MessagingException { 116 return getContext().getDeliveryChannel(); 117 } 118 } 119 120 } 121 | Popular Tags |