KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > itests > WSNComponentTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.itests;
18
19 import java.io.StringReader JavaDoc;
20
21 import javax.xml.namespace.QName JavaDoc;
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 public class WSNComponentTest extends TestCase {
44
45     public static QName JavaDoc NOTIFICATION_BROKER = new QName JavaDoc("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 JavaDoc {
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 JavaDoc {
74         if (jbi != null) {
75             jbi.shutDown();
76         }
77         if (jmsBroker != null) {
78             jmsBroker.stop();
79         }
80     }
81
82     public void testDynamicSubscription() throws Exception JavaDoc {
83         HttpSpringComponent httpComponent = new HttpSpringComponent();
84
85         HttpEndpoint httpWSNBroker = new HttpEndpoint();
86         httpWSNBroker.setService(new QName JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc parse(String JavaDoc txt) throws Exception JavaDoc {
117         DocumentBuilder JavaDoc builder = new SourceTransformer().createDocumentBuilder();
118         InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(txt));
119         Document JavaDoc doc = builder.parse(is);
120         return doc.getDocumentElement();
121     }
122
123 }
124
Popular Tags