KickJava   Java API By Example, From Geeks To Geeks.

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


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

212
213         for (int i = 0; i < nbRuns; i++) {
214             System.gc();
215             long dt = testInOnly(str, true);
216             System.err.println("Streaming: " + dt);
217             tearDown();
218             setUp();
219         }
220     }
221
222     public void testInOutWithBigMessage() throws Exception JavaDoc {
223         int sizeInKb = 640*1024;
224
225         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
226         sb.append("<hello>\n");
227
228         for (int j = 0; j < sizeInKb - 15; j++) {
229             sb.append((char) ('A' + (int) (Math.random() * ('Z' - 'A' + 1))));
230         }
231
232         sb.append("</hello>\n");
233         String JavaDoc str = sb.toString();
234
235         testInOut(str, true);
236     }
237 }
238
Popular Tags