KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpXBeanDeployerTest


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.http;
18
19 import java.io.File JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import javax.jbi.messaging.ExchangeStatus;
24 import javax.jbi.messaging.InOut;
25 import javax.jbi.servicedesc.ServiceEndpoint;
26 import javax.wsdl.Binding;
27 import javax.wsdl.Definition;
28 import javax.wsdl.Port;
29 import javax.wsdl.PortType;
30 import javax.wsdl.Service;
31 import javax.wsdl.factory.WSDLFactory;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import junit.framework.TestCase;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.servicemix.client.DefaultServiceMixClient;
39 import org.apache.servicemix.components.util.EchoComponent;
40 import org.apache.servicemix.jbi.container.ActivationSpec;
41 import org.apache.servicemix.jbi.container.JBIContainer;
42 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
43 import org.apache.servicemix.jbi.jaxp.StringSource;
44 import org.w3c.dom.Document JavaDoc;
45
46 public class HttpXBeanDeployerTest extends TestCase {
47
48     private static Log logger = LogFactory.getLog(HttpXBeanDeployerTest.class);
49
50     protected JBIContainer container;
51     
52     protected void setUp() throws Exception JavaDoc {
53         container = new JBIContainer();
54         container.setUseMBeanServer(false);
55         container.setCreateMBeanServer(false);
56         container.setEmbedded(true);
57         container.init();
58     }
59     
60     protected void tearDown() throws Exception JavaDoc {
61         if (container != null) {
62             container.shutDown();
63         }
64     }
65
66     public void test() throws Exception JavaDoc {
67         // HTTP Component
68
HttpComponent component = new HttpComponent();
69         container.activateComponent(component, "HTTPComponent");
70         
71         // Add a receiver component
72
ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() {
73             public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
74                 try {
75                     Definition def = WSDLFactory.newInstance().newDefinition();
76                     PortType type = def.createPortType();
77                     type.setUndefined(false);
78                     type.setQName(new QName JavaDoc("http://test", "MyConsumerInterface"));
79                     Binding binding = def.createBinding();
80                     binding.setQName(new QName JavaDoc("http://test", "MyConsumerBinding"));
81                     binding.setUndefined(false);
82                     binding.setPortType(type);
83                     Service svc = def.createService();
84                     svc.setQName(new QName JavaDoc("http://test", "MyConsumerService"));
85                     Port port = def.createPort();
86                     port.setBinding(binding);
87                     port.setName("myConsumer");
88                     svc.addPort(port);
89                     def.setTargetNamespace("http://test");
90                     def.addNamespace("tns", "http://test");
91                     def.addPortType(type);
92                     def.addBinding(binding);
93                     def.addService(svc);
94                     Document JavaDoc doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def);
95                     return doc;
96                 } catch (Exception JavaDoc e) {
97                     throw new RuntimeException JavaDoc(e);
98                 }
99             }
100         });
101         asEcho.setEndpoint("myConsumer");
102         asEcho.setService(new QName JavaDoc("http://test", "MyConsumerService"));
103         container.activateComponent(asEcho);
104         
105         // Start container
106
container.start();
107
108         // Deploy SU
109
URL JavaDoc url = getClass().getClassLoader().getResource("xbean/xbean.xml");
110         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
111         path = path.getParentFile();
112         component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath());
113         component.getServiceUnitManager().start("xbean");
114         
115         // Test wsdls
116
assertNotNull(container.getRegistry().getEndpointDescriptor(
117                 container.getRegistry().getEndpoint(
118                         new QName JavaDoc("http://test", "MyProviderService"), "myProvider")));
119         assertNotNull(container.getRegistry().getEndpointDescriptor(
120                 container.getRegistry().getExternalEndpointsForService(
121                         new QName JavaDoc("http://test", "MyConsumerService"))[0]));
122         assertNotNull(container.getRegistry().getEndpointDescriptor(
123                 container.getRegistry().getExternalEndpointsForService(
124                         new QName JavaDoc("http://test", "MySoapService"))[0]));
125         
126         // Test
127
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
128         InOut me = client.createInOutExchange();
129         me.setService(new QName JavaDoc("http://test", "MyProviderService"));
130         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
131         client.sendSync(me);
132         if (me.getStatus() == ExchangeStatus.ERROR) {
133             if (me.getFault() != null) {
134                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
135             } else if (me.getError() != null) {
136                 throw me.getError();
137             } else {
138                 fail("Received ERROR status");
139             }
140         } else {
141             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
142         }
143         client.done(me);
144     }
145     
146 }
147
Popular Tags