KickJava   Java API By Example, From Geeks To Geeks.

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


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.InOnly;
24 import javax.jbi.messaging.InOut;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.apache.activemq.ActiveMQConnectionFactory;
34 import org.apache.activemq.broker.BrokerService;
35 import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
36 import org.apache.activemq.pool.PooledConnectionFactory;
37 import org.apache.activemq.xbean.BrokerFactoryBean;
38 import org.apache.servicemix.client.DefaultServiceMixClient;
39 import org.apache.servicemix.components.jms.JmsReceiverComponent;
40 import org.apache.servicemix.components.jms.JmsServiceComponent;
41 import org.apache.servicemix.components.util.EchoComponent;
42 import org.apache.servicemix.jbi.container.ActivationSpec;
43 import org.apache.servicemix.jbi.container.JBIContainer;
44 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
45 import org.apache.servicemix.jbi.jaxp.StringSource;
46 import org.apache.servicemix.tck.Receiver;
47 import org.apache.servicemix.tck.ReceiverComponent;
48 import org.springframework.core.io.ClassPathResource;
49 import org.springframework.jms.core.JmsTemplate;
50
51 public class JMSComponentTest extends TestCase {
52
53     protected JBIContainer container;
54     protected BrokerService broker;
55     protected ActiveMQConnectionFactory connectionFactory;
56     
57     protected void setUp() throws Exception JavaDoc {
58         System.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
59         System.setProperty(Context.PROVIDER_URL, "tcp://localhost:61216");
60
61       
62         BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jms/activemq.xml"));
63         bfb.afterPropertiesSet();
64         broker = bfb.getBroker();
65         broker.start();
66         
67         container = new JBIContainer();
68         container.setUseMBeanServer(true);
69         container.setCreateMBeanServer(true);
70         container.setMonitorInstallationDirectory(false);
71         container.setNamingContext(new InitialContext JavaDoc());
72         container.init();
73         
74         connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61216");
75     }
76     
77     protected void tearDown() throws Exception JavaDoc {
78         if (container != null) {
79             container.shutDown();
80         }
81         if (broker != null) {
82             broker.stop();
83         }
84     }
85     
86     public void testProviderInOnly() throws Exception JavaDoc {
87         // JMS Component
88
JmsComponent component = new JmsComponent();
89         container.activateComponent(component, "JMSComponent");
90         
91         // Add a jms receiver
92
JmsReceiverComponent jmsReceiver = new JmsReceiverComponent();
93         JmsTemplate template = new JmsTemplate(connectionFactory);
94         template.setDefaultDestinationName("queue/A");
95         jmsReceiver.setTemplate(template);
96         jmsReceiver.afterPropertiesSet();
97         ActivationSpec asJmsReceiver = new ActivationSpec("jmsReceiver", jmsReceiver);
98         asJmsReceiver.setDestinationService(new QName JavaDoc("test", "receiver"));
99         container.activateComponent(asJmsReceiver);
100         
101         // Add a receiver component
102
Receiver receiver = new ReceiverComponent();
103         ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
104         asReceiver.setService(new QName JavaDoc("test", "receiver"));
105         container.activateComponent(asReceiver);
106         
107         // Start container
108
container.start();
109         
110         // Deploy SU
111
URL JavaDoc url = getClass().getClassLoader().getResource("provider/jms.wsdl");
112         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
113         path = path.getParentFile();
114         component.getServiceUnitManager().deploy("provider", path.getAbsolutePath());
115         component.getServiceUnitManager().start("provider");
116         
117         // Call it
118
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
119         InOnly in = client.createInOnlyExchange();
120         in.setInterfaceName(new QName JavaDoc("http://jms.servicemix.org/Test", "ProviderInterface"));
121         in.getInMessage().setContent(new StringSource("<hello>world</hello>"));
122         client.send(in);
123         
124         // Check we received the message
125
receiver.getMessageList().assertMessagesReceived(1);
126     }
127     
128     public void testProviderInOut() throws Exception JavaDoc {
129        // JMS Component
130
JmsComponent component = new JmsComponent();
131         container.activateComponent(component, "JMSComponent");
132         
133         // Add a jms receiver
134
JmsServiceComponent jmsReceiver = new JmsServiceComponent();
135         JmsTemplate template = new JmsTemplate(new PooledConnectionFactory(connectionFactory));
136         template.setDefaultDestinationName("queue/A");
137         jmsReceiver.setTemplate(template);
138         jmsReceiver.afterPropertiesSet();
139         ActivationSpec asJmsReceiver = new ActivationSpec("jmsReceiver", jmsReceiver);
140         asJmsReceiver.setDestinationService(new QName JavaDoc("test", "receiver"));
141         container.activateComponent(asJmsReceiver);
142         
143         // Add an echo component
144
EchoComponent echo = new EchoComponent();
145         ActivationSpec asEcho = new ActivationSpec("receiver", echo);
146         asEcho.setService(new QName JavaDoc("test", "receiver"));
147         container.activateComponent(asEcho);
148         
149         // Start container
150
container.start();
151         
152         // Deploy SU
153
URL JavaDoc url = getClass().getClassLoader().getResource("provider/jms.wsdl");
154         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
155         path = path.getParentFile();
156         component.getServiceUnitManager().deploy("provider", path.getAbsolutePath());
157         component.getServiceUnitManager().start("provider");
158         
159         // Call it
160
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
161         InOut inout = client.createInOutExchange();
162         inout.setInterfaceName(new QName JavaDoc("http://jms.servicemix.org/Test", "ProviderInterface"));
163         inout.getInMessage().setContent(new StringSource("<hello>world</hello>"));
164         boolean result = client.sendSync(inout);
165         assertTrue(result);
166         NormalizedMessage out = inout.getOutMessage();
167         assertNotNull(out);
168         Source JavaDoc src = out.getContent();
169         assertNotNull(src);
170         System.err.println(new SourceTransformer().toString(src));
171     }
172     
173     /*
174      * This test is not finished.
175      * But the feature is actually in the testProviderConsumerInOut test
176      *
177     public void testConsumerInOut() throws Exception {
178         // JMS Component
179         JmsComponent component = new JmsComponent();
180         container.activateComponent(component, "JMSComponent");
181         
182         // Start container
183         container.start();
184         
185         // Deploy SU
186         URL url = getClass().getClassLoader().getResource("consumer/jms.wsdl");
187         File path = new File(new URI(url.toString()));
188         path = path.getParentFile();
189         component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath());
190         component.getServiceUnitManager().start("consumer");
191         
192         // Call it
193         JmsTemplate template = new JmsTemplate(new PooledConnectionFactory(connectionFactory));
194         template.setDefaultDestinationName("queue/A");
195         template.afterPropertiesSet();
196         template.send(new MessageCreator() {
197             public Message createMessage(Session session) throws JMSException {
198                 return session.createTextMessage("<hello>world</hello>");
199             }
200         });
201
202         System.err.println("Sent");
203     }
204     */

205     
206     public void testProviderConsumerInOut() throws Exception JavaDoc {
207         // JMS Component
208
JmsComponent component = new JmsComponent();
209         container.activateComponent(component, "JMSComponent");
210         
211         // Add an echo component
212
EchoComponent echo = new EchoComponent();
213         ActivationSpec asEcho = new ActivationSpec("receiver", echo);
214         asEcho.setService(new QName JavaDoc("http://jms.servicemix.org/Test", "Echo"));
215         container.activateComponent(asEcho);
216         
217         // Start container
218
container.start();
219         
220         // Deploy Provider SU
221
{
222             URL JavaDoc url = getClass().getClassLoader().getResource("provider/jms.wsdl");
223             File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
224             path = path.getParentFile();
225             component.getServiceUnitManager().deploy("provider", path.getAbsolutePath());
226             component.getServiceUnitManager().start("provider");
227         }
228         
229         // Deploy Consumer SU
230
{
231             URL JavaDoc url = getClass().getClassLoader().getResource("consumer/jms.wsdl");
232             File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
233             path = path.getParentFile();
234             component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath());
235             component.getServiceUnitManager().start("consumer");
236         }
237         
238         // Call it
239
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
240         InOut inout = client.createInOutExchange();
241         inout.setInterfaceName(new QName JavaDoc("http://jms.servicemix.org/Test", "ProviderInterface"));
242         inout.getInMessage().setContent(new StringSource("<hello>world</hello>"));
243         boolean result = client.sendSync(inout);
244         assertTrue(result);
245         NormalizedMessage out = inout.getOutMessage();
246         assertNotNull(out);
247         Source JavaDoc src = out.getContent();
248         assertNotNull(src);
249         System.err.println(new SourceTransformer().toString(src));
250         
251     }
252     
253 }
254
Popular Tags