KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URI JavaDoc;
20
21 import javax.jbi.messaging.InOut;
22 import javax.transaction.TransactionManager JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.activemq.broker.BrokerService;
28 import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
29 import org.apache.geronimo.transaction.context.TransactionContextManager;
30 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
31 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
32 import org.apache.servicemix.client.DefaultServiceMixClient;
33 import org.apache.servicemix.client.Destination;
34 import org.apache.servicemix.components.util.EchoComponent;
35 import org.apache.servicemix.jbi.container.JBIContainer;
36 import org.apache.servicemix.jbi.jaxp.StringSource;
37 import org.apache.servicemix.jbi.nmr.flow.Flow;
38 import org.apache.servicemix.jbi.nmr.flow.jca.JCAFlow;
39 import org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow;
40 import org.apache.servicemix.tck.ExchangeCompletedListener;
41
42 public class HttpTxTest extends TestCase {
43
44     private ExchangeCompletedListener listener;
45     private JBIContainer jbi;
46     private TransactionManager JavaDoc tm;
47     private BrokerService broker;
48     
49     protected void setUp() throws Exception JavaDoc {
50         broker = new BrokerService();
51         broker.setUseJmx(false);
52         broker.setPersistent(false);
53         broker.addConnector("tcp://localhost:61616");
54         broker.start();
55
56         TransactionManagerImpl exTransactionManager = new TransactionManagerImpl(600, new XidFactoryImpl(), null, null);
57         TransactionContextManager transactionContextManager = new TransactionContextManager(exTransactionManager, exTransactionManager);
58         tm = (TransactionManager JavaDoc) new GeronimoTransactionManager(transactionContextManager);
59
60         JCAFlow jcaFlow = new JCAFlow();
61         jcaFlow.setTransactionContextManager(transactionContextManager);
62         
63         jbi = new JBIContainer();
64         jbi.setFlows(new Flow[] { new SedaFlow(), jcaFlow });
65         jbi.setEmbedded(true);
66         jbi.setUseMBeanServer(false);
67         jbi.setCreateMBeanServer(false);
68         jbi.setTransactionManager(tm);
69         jbi.setAutoEnlistInTransaction(true);
70         listener = new ExchangeCompletedListener();
71         jbi.addListener(listener);
72         jbi.init();
73         jbi.start();
74     }
75     
76     protected void tearDown() throws Exception JavaDoc {
77         listener.assertExchangeCompleted();
78         jbi.shutDown();
79         broker.stop();
80     }
81     
82     public void testSync() throws Exception JavaDoc {
83         EchoComponent echo = new EchoComponent();
84         echo.setService(new QName JavaDoc("urn:test", "echo"));
85         echo.setEndpoint("echo");
86         jbi.activateComponent(echo, "echo");
87         
88         HttpEndpoint ep0 = new HttpEndpoint();
89         ep0.setService(new QName JavaDoc("urn:test", "s0"));
90         ep0.setEndpoint("ep0");
91         ep0.setLocationURI("http://localhost:8192/ep1/");
92         ep0.setRoleAsString("provider");
93         ep0.setSoap(true);
94         
95         HttpEndpoint ep1 = new HttpEndpoint();
96         ep1.setService(new QName JavaDoc("urn:test", "s1"));
97         ep1.setEndpoint("ep1");
98         ep1.setTargetService(new QName JavaDoc("urn:test", "echo"));
99         ep1.setLocationURI("http://localhost:8192/ep1/");
100         ep1.setRoleAsString("consumer");
101         ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
102         ep1.setSoap(true);
103         
104         HttpSpringComponent http = new HttpSpringComponent();
105         http.setEndpoints(new HttpEndpoint[] { ep0, ep1 });
106         jbi.activateComponent(http, "http");
107
108         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
109         Destination d = client.createDestination("service:urn:test:s0");
110         InOut me = d.createInOutExchange();
111         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
112         
113         tm.begin();
114         boolean ok = client.sendSync(me);
115         assertTrue(ok);
116         client.done(me);
117         tm.commit();
118     }
119     
120     public void testAsync() throws Exception JavaDoc {
121         EchoComponent echo = new EchoComponent();
122         echo.setService(new QName JavaDoc("urn:test", "echo"));
123         echo.setEndpoint("echo");
124         jbi.activateComponent(echo, "echo");
125         
126         HttpEndpoint ep0 = new HttpEndpoint();
127         ep0.setService(new QName JavaDoc("urn:test", "s0"));
128         ep0.setEndpoint("ep0");
129         ep0.setLocationURI("http://localhost:8192/ep1/");
130         ep0.setRoleAsString("provider");
131         ep0.setSoap(true);
132         
133         HttpEndpoint ep1 = new HttpEndpoint();
134         ep1.setService(new QName JavaDoc("urn:test", "s1"));
135         ep1.setEndpoint("ep1");
136         ep1.setTargetService(new QName JavaDoc("urn:test", "echo"));
137         ep1.setLocationURI("http://localhost:8192/ep1/");
138         ep1.setRoleAsString("consumer");
139         ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
140         ep1.setSoap(true);
141         
142         HttpSpringComponent http = new HttpSpringComponent();
143         http.setEndpoints(new HttpEndpoint[] { ep0, ep1 });
144         jbi.activateComponent(http, "http");
145
146         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
147         Destination d = client.createDestination("service:urn:test:s0");
148         InOut me = d.createInOutExchange();
149         me.getInMessage().setContent(new StringSource("<hello>world</hello>"));
150         
151         tm.begin();
152         client.send(me);
153         tm.commit();
154         me = (InOut) client.receive();
155         client.done(me);
156     }
157     
158 }
159
Popular Tags