KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jms > JmsXBeanDeployerTest


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.jms;
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.naming.Context JavaDoc;
27 import javax.wsdl.Binding;
28 import javax.wsdl.Definition;
29 import javax.wsdl.Port;
30 import javax.wsdl.PortType;
31 import javax.wsdl.Service;
32 import javax.wsdl.factory.WSDLFactory;
33 import javax.xml.namespace.QName JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.apache.activemq.ActiveMQConnectionFactory;
38 import org.apache.activemq.broker.BrokerService;
39 import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
40 import org.apache.activemq.xbean.BrokerFactoryBean;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.apache.servicemix.client.DefaultServiceMixClient;
44 import org.apache.servicemix.components.util.EchoComponent;
45 import org.apache.servicemix.jbi.container.ActivationSpec;
46 import org.apache.servicemix.jbi.container.JBIContainer;
47 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
48 import org.apache.servicemix.jbi.jaxp.StringSource;
49 import org.springframework.core.io.ClassPathResource;
50 import org.w3c.dom.Document JavaDoc;
51
52 public class JmsXBeanDeployerTest extends TestCase {
53
54     private static Log logger = LogFactory.getLog(JmsXBeanDeployerTest.class);
55
56     protected JBIContainer container;
57     protected BrokerService broker;
58     protected ActiveMQConnectionFactory connectionFactory;
59     
60     protected void setUp() throws Exception JavaDoc {
61         System.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
62         System.setProperty(Context.PROVIDER_URL, "tcp://localhost:61216");
63       
64         BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jms/activemq.xml"));
65         bfb.afterPropertiesSet();
66         broker = bfb.getBroker();
67         broker.start();
68         
69         container = new JBIContainer();
70         //container.setUseMBeanServer(false);
71
//container.setCreateMBeanServer(false);
72
container.setEmbedded(true);
73         container.init();
74         
75         connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61216");
76     }
77     
78     protected void tearDown() throws Exception JavaDoc {
79         if (container != null) {
80             container.shutDown();
81         }
82         if (broker != null) {
83             broker.stop();
84         }
85     }
86
87     public void test() throws Exception JavaDoc {
88         // JMS Component
89
JmsComponent component = new JmsComponent();
90         container.activateComponent(component, "JMSComponent1");
91         
92         // Add a receiver component
93
ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() {
94             public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
95                 try {
96                     Definition def = WSDLFactory.newInstance().newDefinition();
97                     PortType type = def.createPortType();
98                     type.setUndefined(false);
99                     type.setQName(new QName JavaDoc("http://test", "MyConsumerInterface"));
100                     Binding binding = def.createBinding();
101                     binding.setQName(new QName JavaDoc("http://test", "MyConsumerBinding"));
102                     binding.setUndefined(false);
103                     binding.setPortType(type);
104                     Service svc = def.createService();
105                     svc.setQName(new QName JavaDoc("http://test", "MyConsumerService"));
106                     Port port = def.createPort();
107                     port.setBinding(binding);
108                     port.setName("myConsumer");
109                     svc.addPort(port);
110                     def.setTargetNamespace("http://test");
111                     def.addNamespace("tns", "http://test");
112                     def.addPortType(type);
113                     def.addBinding(binding);
114                     def.addService(svc);
115                     Document JavaDoc doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def);
116                     return doc;
117                 } catch (Exception JavaDoc e) {
118                     throw new RuntimeException JavaDoc(e);
119                 }
120             }
121         });
122         asEcho.setEndpoint("myConsumer");
123         asEcho.setService(new QName JavaDoc("http://test", "MyConsumerService"));
124         container.activateComponent(asEcho);
125         
126         // Start container
127
container.start();
128
129         // Deploy SU
130
URL JavaDoc url = getClass().getClassLoader().getResource("xbean/xbean.xml");
131         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
132         path = path.getParentFile();
133         component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath());
134         component.getServiceUnitManager().start("xbean");
135         
136         // Test wsdls
137
assertNotNull(container.getRegistry().getEndpointDescriptor(
138                 container.getRegistry().getEndpoint(
139                         new QName JavaDoc("http://test", "MyProviderService"), "myProvider")));
140         assertNotNull(container.getRegistry().getEndpointDescriptor(
141                 container.getRegistry().getExternalEndpointsForService(
142                         new QName JavaDoc("http://test", "MyConsumerService"))[0]));
143         
144         // Test
145
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
146         InOut me = client.createInOutExchange();
147         me.setService(new QName JavaDoc("http://test", "MyProviderService"));
148         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
149         client.sendSync(me);
150         if (me.getStatus() == ExchangeStatus.ERROR) {
151             if (me.getFault() != null) {
152                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
153             } else if (me.getError() != null) {
154                 throw me.getError();
155             } else {
156                 fail("Received ERROR status");
157             }
158         } else {
159             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
160         }
161     }
162     
163     public void testSoap() throws Exception JavaDoc {
164         // JMS Component
165
JmsComponent component = new JmsComponent();
166         container.activateComponent(component, "JMSComponent2");
167         
168         // Add a receiver component
169
ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() {
170             public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
171                 try {
172                     Definition def = WSDLFactory.newInstance().newDefinition();
173                     PortType type = def.createPortType();
174                     type.setUndefined(false);
175                     type.setQName(new QName JavaDoc("http://test", "MyConsumerInterface"));
176                     Binding binding = def.createBinding();
177                     binding.setUndefined(false);
178                     binding.setPortType(type);
179                     Service svc = def.createService();
180                     svc.setQName(new QName JavaDoc("http://test", "MyConsumerService"));
181                     Port port = def.createPort();
182                     port.setBinding(binding);
183                     port.setName("myConsumer");
184                     svc.addPort(port);
185                     def.setTargetNamespace("http://test");
186                     def.addNamespace("tns", "http://test");
187                     def.addPortType(type);
188                     def.addBinding(binding);
189                     def.addService(svc);
190                     Document JavaDoc doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def);
191                     return doc;
192                 } catch (Exception JavaDoc e) {
193                     throw new RuntimeException JavaDoc(e);
194                 }
195             }
196         });
197         asEcho.setEndpoint("myConsumer");
198         asEcho.setService(new QName JavaDoc("http://test", "MyConsumerService"));
199         container.activateComponent(asEcho);
200         
201         // Start container
202
container.start();
203
204         // Deploy SU
205
URL JavaDoc url = getClass().getClassLoader().getResource("xbean/xbean.xml");
206         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
207         path = path.getParentFile();
208         component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath());
209         component.getServiceUnitManager().start("xbean");
210         
211         // Test wsdls
212
assertNotNull(container.getRegistry().getEndpointDescriptor(
213                 container.getRegistry().getExternalEndpointsForService(
214                         new QName JavaDoc("http://test", "MyConsumerService"))[0]));
215         
216         // Test
217
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
218         InOut me = client.createInOutExchange();
219         me.setService(new QName JavaDoc("http://test", "MyProviderService"));
220         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
221         client.sendSync(me);
222         if (me.getStatus() == ExchangeStatus.ERROR) {
223             if (me.getFault() != null) {
224                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
225             } else if (me.getError() != null) {
226                 throw me.getError();
227             } else {
228                 fail("Received ERROR status");
229             }
230         } else {
231             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
232         }
233     }
234     
235 }
236
Popular Tags