KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > tcp > TCPTwoChannelEchoRawXMLTest


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis2.tcp;
18
19
20
21 import junit.framework.TestCase;
22 import org.apache.axis2.Constants;
23 import org.apache.axis2.addressing.AddressingConstants;
24 import org.apache.axis2.addressing.EndpointReference;
25 import org.apache.axis2.clientapi.AsyncResult;
26 import org.apache.axis2.clientapi.Callback;
27 import org.apache.axis2.context.MessageContext;
28 import org.apache.axis2.context.ServiceContext;
29 import org.apache.axis2.description.ServiceDescription;
30 import org.apache.axis2.engine.AxisFault;
31 import org.apache.axis2.engine.Echo;
32 import org.apache.axis2.integration.UtilServer;
33 import org.apache.axis2.integration.UtilsTCPServer;
34 import org.apache.axis2.om.*;
35 import org.apache.axis2.transport.http.SimpleHTTPServer;
36 import org.apache.axis2.util.Utils;
37
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.stream.XMLOutputFactory;
40 import javax.xml.stream.XMLStreamException;
41
42 public class TCPTwoChannelEchoRawXMLTest extends TestCase {
43     private EndpointReference targetEPR =
44             new EndpointReference(AddressingConstants.WSA_TO,
45                     "tcp://127.0.0.1:"
46             + (UtilServer.TESTING_PORT)
47             + "/axis/services/EchoXMLService/echoOMElement");
48     private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
49     private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
50     private QName JavaDoc transportName = new QName JavaDoc("http://localhost/my", "NullTransport");
51
52     private MessageContext mc;
53     private SimpleHTTPServer sas;
54     private ServiceDescription service;
55     private ServiceContext serviceContext;
56     
57     private boolean finish = false;
58
59     public TCPTwoChannelEchoRawXMLTest() {
60         super(TCPTwoChannelEchoRawXMLTest.class.getName());
61     }
62
63     public TCPTwoChannelEchoRawXMLTest(String JavaDoc testName) {
64         super(testName);
65     }
66
67     protected void setUp() throws Exception JavaDoc {
68         UtilsTCPServer.start();
69
70         
71         //create and deploy the service
72
service =
73                 Utils.createSimpleService(serviceName,
74         Echo.class.getName(),
75                         operationName);
76         UtilsTCPServer.deployService(service);
77         
78         ServiceDescription service =
79                    Utils.createSimpleService(
80                        serviceName,
81                        org.apache.axis2.engine.Echo.class.getName(),
82                        operationName);
83                serviceContext = UtilServer.createAdressedEnabledClientSide(service);
84     }
85
86     protected void tearDown() throws Exception JavaDoc {
87         UtilsTCPServer.stop();
88     }
89
90     private OMElement createEnvelope() {
91         OMFactory fac = OMAbstractFactory.getOMFactory();
92         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
93         OMElement method = fac.createOMElement("echoOMElement", omNs);
94         OMElement value = fac.createOMElement("myValue", omNs);
95         value.addChild(fac.createText(value, "Isaac Assimov, the foundation Sega"));
96         method.addChild(value);
97         
98         return method;
99     }
100
101     public void testEchoXMLCompleteASync() throws Exception JavaDoc {
102             ServiceDescription service =
103                 Utils.createSimpleService(
104                     serviceName,
105             Echo.class.getName(),
106                     operationName);
107
108             
109
110             OMFactory fac = OMAbstractFactory.getOMFactory();
111
112             OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
113             OMElement method = fac.createOMElement("echoOMElement", omNs);
114             OMElement value = fac.createOMElement("myValue", omNs);
115             value.setText("Isaac Assimov, the foundation Sega");
116             method.addChild(value);
117
118             org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(serviceContext);
119             call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
120
121             try {
122                 call.setTo(targetEPR);
123                 call.setTransportInfo(Constants.TRANSPORT_TCP, Constants.TRANSPORT_TCP, true);
124                 Callback callback = new Callback() {
125                     public void onComplete(AsyncResult result) {
126                         try {
127                             result.getResponseEnvelope().serialize(
128                                 new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
129                         } catch (XMLStreamException e) {
130                             reportError(e);
131                         } finally {
132                             finish = true;
133                         }
134                     }
135
136                     public void reportError(Exception JavaDoc e) {
137                         e.printStackTrace();
138                         finish = true;
139                     }
140                 };
141
142                 call.invokeNonBlocking(operationName.getLocalPart(), method, callback);
143                 int index = 0;
144                 while (!finish) {
145                     Thread.sleep(1000);
146                     index++;
147                     if (index > 10) {
148                         throw new AxisFault("Server is shutdown as the Async response take too longs time");
149                     }
150                 }
151             } finally {
152                 call.close();
153             }
154
155         }
156 }
157
Popular Tags