KickJava   Java API By Example, From Geeks To Geeks.

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


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.soap.SOAPFactory;
36 import org.apache.axis2.transport.http.SimpleHTTPServer;
37 import org.apache.axis2.util.Utils;
38
39 import javax.xml.namespace.QName JavaDoc;
40 import javax.xml.stream.XMLOutputFactory;
41 import javax.xml.stream.XMLStreamException;
42
43 public class TCPEchoRawXMLTest extends TestCase {
44     private EndpointReference targetEPR =
45             new EndpointReference(AddressingConstants.WSA_TO,
46                     "tcp://127.0.0.1:"
47             + (UtilServer.TESTING_PORT)
48             + "/axis/services/EchoXMLService/echoOMElement");
49     private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
50     private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
51     private QName JavaDoc transportName = new QName JavaDoc("http://localhost/my", "NullTransport");
52
53     private MessageContext mc;
54     private SimpleHTTPServer sas;
55     private ServiceDescription service;
56     private ServiceContext serviceContext;
57     
58     private boolean finish = false;
59
60     public TCPEchoRawXMLTest() {
61         super(TCPEchoRawXMLTest.class.getName());
62     }
63
64     public TCPEchoRawXMLTest(String JavaDoc testName) {
65         super(testName);
66     }
67
68     protected void setUp() throws Exception JavaDoc {
69         UtilsTCPServer.start();
70
71         
72         //create and deploy the service
73
service =
74                 Utils.createSimpleService(serviceName,
75         Echo.class.getName(),
76                         operationName);
77         UtilsTCPServer.deployService(service);
78         
79         ServiceDescription service =
80                    Utils.createSimpleService(
81                        serviceName,
82                        org.apache.axis2.engine.Echo.class.getName(),
83                        operationName);
84                serviceContext = UtilServer.createAdressedEnabledClientSide(service);
85     }
86
87     protected void tearDown() throws Exception JavaDoc {
88         UtilsTCPServer.stop();
89     }
90
91     private OMElement createEnvelope() {
92         OMFactory fac = OMAbstractFactory.getOMFactory();
93         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
94         OMElement method = fac.createOMElement("echoOMElement", omNs);
95         OMElement value = fac.createOMElement("myValue", omNs);
96         value.addChild(fac.createText(value, "Isaac Assimov, the foundation Sega"));
97         method.addChild(value);
98         
99         return method;
100     }
101
102     public void testEchoXMLASync() throws Exception JavaDoc {
103                 OMElement payload = createEnvelope();
104
105         org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(serviceContext);
106
107         call.setTo(targetEPR);
108         call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
109         call.setTransportInfo(Constants.TRANSPORT_TCP, Constants.TRANSPORT_TCP, false);
110
111         Callback callback = new Callback() {
112             public void onComplete(AsyncResult result) {
113                 try {
114                     result.getResponseEnvelope().serializeWithCache(new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
115                 } catch (XMLStreamException e) {
116                     reportError(e);
117                 } finally {
118                     finish = true;
119                 }
120             }
121
122             public void reportError(Exception JavaDoc e) {
123                 e.printStackTrace();
124                 finish = true;
125             }
126         };
127
128         call.invokeNonBlocking(operationName.getLocalPart(), payload, callback);
129         int index = 0;
130         while (!finish) {
131             Thread.sleep(1000);
132             index++;
133             if(index > 10 ){
134                 throw new AxisFault("Server is shutdown as the Async response take too longs time");
135             }
136         }
137         call.close();
138     }
139
140     public void testEchoXMLSync() throws Exception JavaDoc {
141         SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
142
143         OMElement payload = createEnvelope();
144
145         org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(serviceContext);
146
147         call.setTo(targetEPR);
148         call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
149         call.setTransportInfo(Constants.TRANSPORT_TCP, Constants.TRANSPORT_TCP, false);
150
151         OMElement result =
152                 (OMElement) call.invokeBlocking(operationName.getLocalPart(), payload);
153         result.serializeWithCache(new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
154         call.close();
155     }
156     
157     public void testEchoXMLCompleteSync() throws Exception JavaDoc {
158           ServiceDescription service =
159               Utils.createSimpleService(
160                   serviceName,
161           Echo.class.getName(),
162                   operationName);
163
164           OMFactory fac = OMAbstractFactory.getOMFactory();
165
166           OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
167           OMElement method = fac.createOMElement("echoOMElement", omNs);
168           OMElement value = fac.createOMElement("myValue", omNs);
169           value.setText("Isaac Assimov, the foundation Sega");
170           method.addChild(value);
171
172           org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call(serviceContext);
173           call.setTo(targetEPR);
174           call.engageModule(new QName JavaDoc(Constants.MODULE_ADDRESSING));
175           call.setTransportInfo(Constants.TRANSPORT_TCP, Constants.TRANSPORT_TCP, true);
176
177           OMElement result = (OMElement) call.invokeBlocking(operationName.getLocalPart(), method);
178           result.serializeWithCache(new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
179           call.close();
180
181       }
182       
183   
184 }
185
Popular Tags