1 17 package org.apache.servicemix.eip; 18 19 import javax.jbi.messaging.ExchangeStatus; 20 import javax.jbi.messaging.InOnly; 21 import javax.jbi.messaging.InOut; 22 import javax.xml.namespace.QName ; 23 24 import org.apache.servicemix.eip.patterns.ContentBasedRouter; 25 import org.apache.servicemix.eip.support.RoutingRule; 26 import org.apache.servicemix.eip.support.XPathPredicate; 27 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 28 import org.apache.servicemix.tck.ReceiverComponent; 29 import org.w3c.dom.Node ; 30 31 public class ContentBasedRouterTest extends AbstractEIPTest { 32 33 private ContentBasedRouter router; 34 35 protected void setUp() throws Exception { 36 super.setUp(); 37 38 router = new ContentBasedRouter(); 39 router.setRules(new RoutingRule[] { 40 new RoutingRule( 41 new XPathPredicate("/hello/@id = '1'"), 42 createServiceExchangeTarget(new QName ("target1"))), 43 new RoutingRule( 44 new XPathPredicate("/hello/@id = '2'"), 45 createServiceExchangeTarget(new QName ("target2"))), 46 new RoutingRule( 47 null, 48 createServiceExchangeTarget(new QName ("target3"))) 49 }); 50 configurePattern(router); 51 activateComponent(router, "router"); 52 } 53 54 public void testInOnly() throws Exception { 55 ReceiverComponent rec1 = activateReceiver("target1"); 56 ReceiverComponent rec2 = activateReceiver("target2"); 57 ReceiverComponent rec3 = activateReceiver("target3"); 58 59 InOnly me = client.createInOnlyExchange(); 60 me.setService(new QName ("router")); 61 me.getInMessage().setContent(createSource("<hello id='1' />")); 62 client.sendSync(me); 63 assertEquals(ExchangeStatus.DONE, me.getStatus()); 64 65 rec1.getMessageList().assertMessagesReceived(1); 66 rec2.getMessageList().assertMessagesReceived(0); 67 rec3.getMessageList().assertMessagesReceived(0); 68 69 rec1.getMessageList().flushMessages(); 70 rec2.getMessageList().flushMessages(); 71 rec3.getMessageList().flushMessages(); 72 73 me = client.createInOnlyExchange(); 74 me.setService(new QName ("router")); 75 me.getInMessage().setContent(createSource("<hello id='2' />")); 76 client.sendSync(me); 77 assertEquals(ExchangeStatus.DONE, me.getStatus()); 78 79 rec1.getMessageList().assertMessagesReceived(0); 80 rec2.getMessageList().assertMessagesReceived(1); 81 rec3.getMessageList().assertMessagesReceived(0); 82 83 rec1.getMessageList().flushMessages(); 84 rec2.getMessageList().flushMessages(); 85 rec3.getMessageList().flushMessages(); 86 87 me = client.createInOnlyExchange(); 88 me.setService(new QName ("router")); 89 me.getInMessage().setContent(createSource("<hello id='3' />")); 90 client.sendSync(me); 91 assertEquals(ExchangeStatus.DONE, me.getStatus()); 92 93 rec1.getMessageList().assertMessagesReceived(0); 94 rec2.getMessageList().assertMessagesReceived(0); 95 rec3.getMessageList().assertMessagesReceived(1); 96 } 97 98 public void testInOut() throws Exception { 99 activateComponent(new ReturnMockComponent("<from1/>"), "target1"); 100 activateComponent(new ReturnMockComponent("<from2/>"), "target2"); 101 activateComponent(new ReturnMockComponent("<from3/>"), "target3"); 102 103 InOut me = client.createInOutExchange(); 104 me.setService(new QName ("router")); 105 me.getInMessage().setContent(createSource("<hello id='1' />")); 106 client.sendSync(me); 107 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 108 Node node = new SourceTransformer().toDOMNode(me.getOutMessage()); 109 assertEquals("from1", node.getFirstChild().getNodeName()); 110 client.done(me); 111 112 me = client.createInOutExchange(); 113 me.setService(new QName ("router")); 114 me.getInMessage().setContent(createSource("<hello id='2' />")); 115 client.sendSync(me); 116 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 117 node = new SourceTransformer().toDOMNode(me.getOutMessage()); 118 assertEquals("from2", node.getFirstChild().getNodeName()); 119 client.done(me); 120 121 me = client.createInOutExchange(); 122 me.setService(new QName ("router")); 123 me.getInMessage().setContent(createSource("<hello id='3' />")); 124 client.sendSync(me); 125 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 126 node = new SourceTransformer().toDOMNode(me.getOutMessage()); 127 assertEquals("from3", node.getFirstChild().getNodeName()); 128 client.done(me); 129 } 130 131 } 132 | Popular Tags |