KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import javax.jbi.messaging.ExchangeStatus;
25 import javax.jbi.messaging.InOut;
26 import javax.jbi.messaging.RobustInOnly;
27 import javax.wsdl.Definition;
28 import javax.wsdl.factory.WSDLFactory;
29 import javax.xml.namespace.QName JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31
32 import junit.framework.TestCase;
33
34 import org.apache.servicemix.client.DefaultServiceMixClient;
35 import org.apache.servicemix.components.http.HttpInvoker;
36 import org.apache.servicemix.components.http.HttpSoapClientMarshaler;
37 import org.apache.servicemix.components.util.EchoComponent;
38 import org.apache.servicemix.jbi.container.ActivationSpec;
39 import org.apache.servicemix.jbi.container.JBIContainer;
40 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
41 import org.apache.servicemix.tck.Receiver;
42 import org.apache.servicemix.tck.ReceiverComponent;
43
44 public class HttpConsumerTest extends TestCase {
45
46     protected JBIContainer container;
47     
48     protected void setUp() throws Exception JavaDoc {
49         container = new JBIContainer();
50         container.setUseMBeanServer(false);
51         container.setCreateMBeanServer(false);
52         container.setEmbedded(true);
53         container.init();
54     }
55     
56     protected void tearDown() throws Exception JavaDoc {
57         if (container != null) {
58             container.shutDown();
59         }
60     }
61     
62     protected long testInOnly(String JavaDoc msg, boolean streaming) throws Exception JavaDoc {
63         // HTTP Component
64
HttpComponent component = new HttpComponent();
65         ((HttpLifeCycle) component.getLifeCycle()).getConfiguration().setStreamingEnabled(streaming);
66         container.activateComponent(component, "HTTPComponent");
67         
68         // Add a receiver component
69
Receiver receiver = new ReceiverComponent();
70         ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
71         asReceiver.setService(new QName JavaDoc("http://http.servicemix.org/Test", "ConsumerInOnly"));
72         container.activateComponent(asReceiver);
73         
74         // Add the http invoker
75
HttpInvoker invoker = new HttpInvoker();
76         invoker.setDefaultInOut(false);
77         invoker.setUrl("http://localhost:8192/InOnly/");
78         invoker.setMarshaler(new HttpSoapClientMarshaler(true));
79         ActivationSpec asInvoker = new ActivationSpec("invoker", invoker);
80         asInvoker.setService(new QName JavaDoc("urn:test", "invoker"));
81         container.activateComponent(asInvoker);
82         
83         // Start container
84
container.start();
85
86         // Deploy SU
87
URL JavaDoc url = getClass().getClassLoader().getResource("consumer/http.wsdl");
88         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
89         path = path.getParentFile();
90         component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath());
91         component.getServiceUnitManager().start("consumer");
92         
93         // Call it
94
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
95         RobustInOnly in = client.createRobustInOnlyExchange();
96         in.setService(new QName JavaDoc("urn:test", "invoker"));
97         in.getInMessage().setContent(new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc(msg.getBytes())));
98         
99         long t0 = System.currentTimeMillis();
100         client.sendSync(in);
101         long t1 = System.currentTimeMillis();
102         assertEquals(ExchangeStatus.DONE, in.getStatus());
103         
104         // Check we received the message
105
receiver.getMessageList().assertMessagesReceived(1);
106         
107         return t1 - t0;
108     }
109         
110
111     protected long testInOut(String JavaDoc msg, boolean streaming) throws Exception JavaDoc {
112         // HTTP Component
113
HttpComponent component = new HttpComponent();
114         ((HttpLifeCycle) component.getLifeCycle()).getConfiguration().setStreamingEnabled(streaming);
115         container.activateComponent(component, "HTTPComponent");
116         
117         // Add a receiver component
118
EchoComponent echo = new EchoComponent();
119         ActivationSpec asReceiver = new ActivationSpec("echo", echo);
120         asReceiver.setService(new QName JavaDoc("http://http.servicemix.org/Test", "ConsumerInOut"));
121         container.activateComponent(asReceiver);
122         
123         // Add the http invoker
124
HttpInvoker invoker = new HttpInvoker();
125         invoker.setDefaultInOut(true);
126         invoker.setUrl("http://localhost:8192/InOut/");
127         ActivationSpec asInvoker = new ActivationSpec("invoker", invoker);
128         asInvoker.setService(new QName JavaDoc("urn:test", "invoker"));
129         container.activateComponent(asInvoker);
130         
131         // Start container
132
container.start();
133
134         // Deploy SU
135
URL JavaDoc url = getClass().getClassLoader().getResource("consumer/http.wsdl");
136         File JavaDoc path = new File JavaDoc(new URI JavaDoc(url.toString()));
137         path = path.getParentFile();
138         component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath());
139         component.getServiceUnitManager().start("consumer");
140
141         // Retrieve WSDL
142
Definition def = WSDLFactory.newInstance().newWSDLReader().readWSDL("http://localhost:8192/InOut/?wsdl");
143         assertNotNull(def);
144         
145         // Call it
146
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
147         InOut inout = client.createInOutExchange();
148         inout.setService(new QName JavaDoc("urn:test", "invoker"));
149         inout.getInMessage().setContent(new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc(msg.getBytes())));
150         
151         long t0 = System.currentTimeMillis();
152         client.sendSync(inout);
153         long t1 = System.currentTimeMillis();
154         assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
155         
156         // Check we received the message
157
assertNotNull(inout.getOutMessage());
158         assertNotNull(inout.getOutMessage().getContent());
159         System.out.println(new SourceTransformer().toString(inout.getOutMessage().getContent()));
160         
161         return t1 - t0;
162     }
163         
164     public void testInOnly() throws Exception JavaDoc {
165         testInOnly("<hello>world</hello>", false);
166     }
167     
168     public void testInOut() throws Exception JavaDoc {
169         testInOut("<hello>world</hello>", true);
170     }
171     
172     public void testPerfInOnlyWithBigMessage() throws Exception JavaDoc {
173         int nbRuns = 2;
174         int sizeInKb = 64;
175         
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177         sb.append("<hello>");
178         for (int i = 0; i < sizeInKb; i++) {
179             sb.append("<hello>");
180             for (int j = 0; j < 1024 - 15; j++) {
181                 sb.append((char) ('A' + (int)(Math.random() * ('Z' - 'A' + 1))));
182             }
183             sb.append("</hello>");
184         }
185         sb.append("</hello>");
186         String JavaDoc str = sb.toString();
187         
188         for(int i = 0; i < nbRuns; i++) {
189             System.gc();
190             long dt = testInOnly(str, false);
191             System.err.println("No Streaming: " + dt);
192             tearDown();
193             setUp();
194         }
195         
196         for(int i = 0; i < nbRuns; i++) {
197             System.gc();
198             long dt = testInOnly(str, true);
199             System.err.println("Streaming: " + dt);
200             tearDown();
201             setUp();
202         }
203     }
204         
205 }
206
Popular Tags