1 17 package org.apache.servicemix.jbi.nmr; 18 19 import com.thoughtworks.xstream.XStream; 20 import com.thoughtworks.xstream.io.xml.DomDriver; 21 22 import org.apache.servicemix.client.DefaultServiceMixClient; 23 import org.apache.servicemix.client.ServiceMixClient; 24 import org.apache.servicemix.jbi.container.ActivationSpec; 25 import org.apache.servicemix.jbi.container.JBIContainer; 26 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 27 import org.apache.servicemix.jbi.jaxp.StringSource; 28 import org.apache.servicemix.tck.ReceiverComponent; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.DocumentFragment ; 31 32 import javax.jbi.JBIException; 33 import javax.jbi.messaging.InOnly; 34 import javax.jbi.servicedesc.ServiceEndpoint; 35 import javax.xml.namespace.QName ; 36 37 import junit.framework.TestCase; 38 39 public class BrokerTest extends TestCase { 40 41 public void testExternalRouting() throws Exception { 42 JBIContainer container = new JBIContainer(); 43 container.setEmbedded(true); 44 container.init(); 45 container.start(); 46 ReceiverComponent receiver = new ReceiverComponent() { 47 protected void init() throws JBIException { 48 ServiceEndpoint ep = new TestExternalEndpoint(getService(), getEndpoint()); 49 getContext().registerExternalEndpoint(ep); 50 setService(null); 51 setEndpoint(null); 52 } 53 54 public ServiceEndpoint resolveEndpointReference(DocumentFragment fragment) { 55 try { 56 SourceTransformer st = new SourceTransformer(); 57 String xml = st.toString(fragment); 58 return (ServiceEndpoint) new XStream(new DomDriver()).fromXML(xml); 59 } catch (Exception e) { 60 throw new RuntimeException (e); 61 } 62 } 63 }; 64 container.activateComponent(new ActivationSpec("receiver", receiver)); 65 ServiceMixClient client = new DefaultServiceMixClient(container); 66 ServiceEndpoint[] endpoints = client.getContext().getExternalEndpoints(null); 67 assertNotNull(endpoints); 68 assertEquals(1, endpoints.length); 69 assertNull(client.getContext().getEndpointDescriptor(endpoints[0])); 70 ServiceEndpoint se = client.getContext().resolveEndpointReference(endpoints[0].getAsReference(null)); 71 assertNull(client.getContext().getEndpointDescriptor(se)); 72 InOnly me = client.createInOnlyExchange(); 73 me.setEndpoint(se); 74 client.send(me); 75 receiver.getMessageList().assertMessagesReceived(1); 76 } 77 78 public static class TestExternalEndpoint implements ServiceEndpoint { 79 private QName service; 80 private String endpoint; 81 public TestExternalEndpoint(QName service, String endpoint) { 82 this.service = service; 83 this.endpoint = endpoint; 84 } 85 public DocumentFragment getAsReference(QName operationName) { 86 try { 87 SourceTransformer st = new SourceTransformer(); 88 String xml = new XStream(new DomDriver()).toXML(this); 89 Document doc = (Document ) st.toDOMNode(new StringSource(xml)); 90 DocumentFragment df = doc.createDocumentFragment(); 91 df.appendChild(doc.getDocumentElement()); 92 return df; 93 } catch (Exception e) { 94 throw new RuntimeException (e); 95 } 96 } 97 public String getEndpointName() { 98 return endpoint; 99 } 100 public QName [] getInterfaces() { 101 return null; 102 } 103 public QName getServiceName() { 104 return service; 105 } 106 } 107 108 } 109 | Popular Tags |