KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
20
21 import javax.jbi.messaging.ExchangeStatus;
22 import javax.jbi.messaging.InOnly;
23 import javax.jbi.messaging.MessageExchange;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.jbi.servicedesc.ServiceEndpoint;
26
27 import junit.framework.TestCase;
28
29 import org.apache.activemq.ActiveMQConnectionFactory;
30 import org.apache.activemq.broker.BrokerService;
31 import org.apache.activemq.xbean.BrokerFactoryBean;
32 import org.apache.servicemix.client.DefaultServiceMixClient;
33 import org.apache.servicemix.jbi.container.JBIContainer;
34 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
35 import org.apache.servicemix.jbi.jaxp.StringSource;
36 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
37 import org.apache.servicemix.jbi.resolver.URIResolver;
38 import org.apache.servicemix.tck.ReceiverComponent;
39 import org.springframework.core.io.ClassPathResource;
40 import org.w3c.dom.DocumentFragment JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42
43 public class JmsURITest extends TestCase {
44
45     protected JBIContainer container;
46     protected BrokerService broker;
47     protected ActiveMQConnectionFactory connectionFactory;
48
49     protected void setUp() throws Exception JavaDoc {
50         BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jms/activemq.xml"));
51         bfb.afterPropertiesSet();
52         broker = bfb.getBroker();
53         broker.start();
54
55         container = new JBIContainer();
56         container.setUseMBeanServer(true);
57         container.setCreateMBeanServer(true);
58         container.setMonitorInstallationDirectory(false);
59         container.init();
60         container.start();
61
62         connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61216");
63     }
64
65     protected void tearDown() throws Exception JavaDoc {
66         if (container != null) {
67             container.shutDown();
68         }
69         if (broker != null) {
70             broker.stop();
71         }
72     }
73
74     public void testResolveEndpoint() throws Exception JavaDoc {
75         JmsSpringComponent jms = new JmsSpringComponent();
76         ((JmsLifeCycle) jms.getLifeCycle()).getConfiguration().setConnectionFactory(connectionFactory);
77         JmsEndpoint ep = new JmsEndpoint();
78         ep.setRole(MessageExchange.Role.CONSUMER);
79         ep.setService(ReceiverComponent.SERVICE);
80         ep.setEndpoint(ReceiverComponent.ENDPOINT);
81         ep.setDefaultMep(MessageExchangeSupport.IN_ONLY);
82         ep.setJmsProviderDestinationName("foo.bar.myqueue");
83         ep.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE);
84         jms.setEndpoints(new JmsEndpoint[] { ep });
85         container.activateComponent(jms, "servicemix-jms");
86
87         ReceiverComponent receiver = new ReceiverComponent();
88         container.activateComponent(receiver, "receiver");
89
90         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
91         DocumentFragment JavaDoc epr = URIResolver.createWSAEPR("jms://queue/foo.bar.myqueue?jms.soap=true");
92         ServiceEndpoint se = client.getContext().resolveEndpointReference(epr);
93         assertNotNull(se);
94
95         InOnly inonly = client.createInOnlyExchange();
96         inonly.setEndpoint(se);
97         inonly.getInMessage().setContent(new StringSource("<hello>world</hello>"));
98         client.sendSync(inonly);
99
100         assertEquals(ExchangeStatus.DONE, inonly.getStatus());
101         receiver.getMessageList().assertMessagesReceived(1);
102         List JavaDoc msgs = receiver.getMessageList().flushMessages();
103         NormalizedMessage msg = (NormalizedMessage) msgs.get(0);
104         Element JavaDoc elem = new SourceTransformer().toDOMElement(msg);
105         assertEquals("http://www.w3.org/2003/05/soap-envelope", elem.getNamespaceURI());
106         assertEquals("env:Envelope", elem.getNodeName());
107         System.out.println(new SourceTransformer().contentToString(msg));
108
109         // Wait for DONE status
110
Thread.sleep(50);
111     }
112
113 }
114
Popular Tags