KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > BrokerTest


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.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 JavaDoc;
30 import org.w3c.dom.DocumentFragment JavaDoc;
31
32 import javax.jbi.JBIException;
33 import javax.jbi.messaging.InOnly;
34 import javax.jbi.servicedesc.ServiceEndpoint;
35 import javax.xml.namespace.QName JavaDoc;
36
37 import junit.framework.TestCase;
38
39 public class BrokerTest extends TestCase {
40     
41     public void testExternalRouting() throws Exception JavaDoc {
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 JavaDoc fragment) {
55                 try {
56                     SourceTransformer st = new SourceTransformer();
57                     String JavaDoc xml = st.toString(fragment);
58                     return (ServiceEndpoint) new XStream(new DomDriver()).fromXML(xml);
59                 } catch (Exception JavaDoc e) {
60                     throw new RuntimeException JavaDoc(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 JavaDoc service;
80         private String JavaDoc endpoint;
81         public TestExternalEndpoint(QName JavaDoc service, String JavaDoc endpoint) {
82             this.service = service;
83             this.endpoint = endpoint;
84         }
85         public DocumentFragment JavaDoc getAsReference(QName JavaDoc operationName) {
86             try {
87                 SourceTransformer st = new SourceTransformer();
88                 String JavaDoc xml = new XStream(new DomDriver()).toXML(this);
89                 Document JavaDoc doc = (Document JavaDoc) st.toDOMNode(new StringSource(xml));
90                 DocumentFragment JavaDoc df = doc.createDocumentFragment();
91                 df.appendChild(doc.getDocumentElement());
92                 return df;
93             } catch (Exception JavaDoc e) {
94                 throw new RuntimeException JavaDoc(e);
95             }
96         }
97         public String JavaDoc getEndpointName() {
98             return endpoint;
99         }
100         public QName JavaDoc[] getInterfaces() {
101             return null;
102         }
103         public QName JavaDoc getServiceName() {
104             return service;
105         }
106     }
107     
108 }
109
Popular Tags