1 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 ; 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 serviceName = new QName ("EchoXMLService"); 49 private QName operationName = new QName ("echoOMElement"); 50 private QName transportName = new QName ("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 testName) { 64 super(testName); 65 } 66 67 protected void setUp() throws Exception { 68 UtilsTCPServer.start(); 69 70 71 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 { 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 { 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 (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 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 |