1 17 package org.apache.servicemix.itests; 18 19 import java.io.StringReader ; 20 21 import javax.xml.namespace.QName ; 22 import javax.xml.parsers.DocumentBuilder ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.activemq.ActiveMQConnectionFactory; 27 import org.apache.activemq.broker.BrokerService; 28 import org.apache.servicemix.http.HttpEndpoint; 29 import org.apache.servicemix.http.HttpSpringComponent; 30 import org.apache.servicemix.jbi.container.ActivationSpec; 31 import org.apache.servicemix.jbi.container.JBIContainer; 32 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 33 import org.apache.servicemix.soap.SoapHelper; 34 import org.apache.servicemix.tck.ReceiverComponent; 35 import org.apache.servicemix.wsn.client.NotificationBroker; 36 import org.apache.servicemix.wsn.component.WSNComponent; 37 import org.w3._2005._08.addressing.AttributedURIType; 38 import org.w3._2005._08.addressing.EndpointReferenceType; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.InputSource ; 42 43 public class WSNComponentTest extends TestCase { 44 45 public static QName NOTIFICATION_BROKER = new QName ("http://servicemix.org/wsnotification", "NotificationBroker"); 46 47 private JBIContainer jbi; 48 private BrokerService jmsBroker; 49 private NotificationBroker wsnBroker; 50 private WSNComponent wsnComponent; 51 52 protected void setUp() throws Exception { 53 jmsBroker = new BrokerService(); 54 jmsBroker.setPersistent(false); 55 jmsBroker.addConnector("vm://localhost"); 56 jmsBroker.start(); 57 58 jbi = new JBIContainer(); 59 jbi.setEmbedded(true); 60 jbi.init(); 61 jbi.start(); 62 63 wsnComponent = new WSNComponent(); 64 wsnComponent.setConnectionFactory(new ActiveMQConnectionFactory("vm://localhost")); 65 ActivationSpec as = new ActivationSpec(); 66 as.setComponentName("servicemix-wsn2005"); 67 as.setComponent(wsnComponent); 68 jbi.activateComponent(as); 69 70 wsnBroker = new NotificationBroker(jbi); 71 } 72 73 protected void tearDown() throws Exception { 74 if (jbi != null) { 75 jbi.shutDown(); 76 } 77 if (jmsBroker != null) { 78 jmsBroker.stop(); 79 } 80 } 81 82 public void testDynamicSubscription() throws Exception { 83 HttpSpringComponent httpComponent = new HttpSpringComponent(); 84 85 HttpEndpoint httpWSNBroker = new HttpEndpoint(); 86 httpWSNBroker.setService(new QName ("http://servicemix.org/wsnotification", "NotificationBroker")); 87 httpWSNBroker.setEndpoint("Broker"); 88 httpWSNBroker.setRoleAsString("consumer"); 89 httpWSNBroker.setLocationURI("http://localhost:8192/WSNBroker/"); 90 httpWSNBroker.setSoap(true); 91 92 HttpEndpoint httpReceiver = new HttpEndpoint(); 93 httpReceiver.setService(new QName ("receiver")); 94 httpReceiver.setEndpoint("endpoint"); 95 httpReceiver.setLocationURI("http://localhost:8192/Receiver/"); 96 httpReceiver.setDefaultMep(SoapHelper.IN_ONLY); 97 httpReceiver.setSoap(true); 98 99 httpComponent.setEndpoints(new HttpEndpoint[] { httpWSNBroker, httpReceiver }); 100 jbi.activateComponent(new ActivationSpec("servicemix-http", httpComponent)); 101 102 ReceiverComponent receiver = new ReceiverComponent(); 103 receiver.setService(new QName ("receiver")); 104 receiver.setEndpoint("endpoint"); 105 jbi.activateComponent(new ActivationSpec("receiver", receiver)); 106 107 EndpointReferenceType epr = new EndpointReferenceType(); 108 epr.setAddress(new AttributedURIType()); 109 epr.getAddress().setValue("http://localhost:8192/Receiver/?http.soap=true"); 110 wsnBroker.subscribe(epr, "myTopic", null); 111 wsnBroker.notify("myTopic", parse("<hello>world</hello>")); 112 113 receiver.getMessageList().assertMessagesReceived(1); 114 } 115 116 private Element parse(String txt) throws Exception { 117 DocumentBuilder builder = new SourceTransformer().createDocumentBuilder(); 118 InputSource is = new InputSource (new StringReader (txt)); 119 Document doc = builder.parse(is); 120 return doc.getDocumentElement(); 121 } 122 123 } 124 | Popular Tags |