KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap12 > TestRPC


1 package test.soap12;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.AxisFault;
5 import org.apache.axis.Message;
6 import org.apache.axis.MessageContext;
7 import org.apache.axis.constants.Use;
8 import org.apache.axis.configuration.SimpleProvider;
9 import org.apache.axis.description.ServiceDesc;
10 import org.apache.axis.handlers.soap.SOAPService;
11 import org.apache.axis.message.RPCElement;
12 import org.apache.axis.message.RPCParam;
13 import org.apache.axis.message.SOAPEnvelope;
14 import org.apache.axis.providers.java.RPCProvider;
15 import org.apache.axis.server.AxisServer;
16 import org.apache.axis.soap.SOAPConstants;
17 import org.apache.axis.transport.local.LocalTransport;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21
22 import java.util.Vector JavaDoc;
23
24 /**
25  * A test for RPC response
26  *
27  * @author Andras Avar (andras.avar@nokia.com)
28  */

29 public class TestRPC extends TestCase {
30
31     private SimpleProvider provider = new SimpleProvider();
32     private AxisServer server = new AxisServer(provider);
33
34     public TestRPC(String JavaDoc name) {
35         super(name);
36         server.init();
37     }
38
39     private RPCElement rpc(String JavaDoc method, Object JavaDoc[] params)
40         throws AxisFault, SAXException JavaDoc
41     {
42         String JavaDoc SERVICE_NAME = "echoservice";
43         LocalTransport transport = new LocalTransport(server);
44
45         SOAPService service = new SOAPService(new RPCProvider());
46         service.setEngine(server);
47         service.setOption("className", "test.soap12.Echo");
48         service.setOption("allowedMethods", "*");
49         service.setOption("use", Use.ENCODED);
50
51         ServiceDesc desc = service.getInitializedServiceDesc(null);
52         desc.setDefaultNamespace(method);
53
54         provider.deployService(SERVICE_NAME, service);
55
56         MessageContext msgContext = new MessageContext(server);
57         msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
58         msgContext.setEncodingStyle(SOAPConstants.SOAP12_CONSTANTS.getEncodingURI());
59
60         String JavaDoc methodNS = null;
61         msgContext.setTargetService(SERVICE_NAME);
62
63         // Construct the soap request
64
SOAPEnvelope envelope = new SOAPEnvelope(msgContext.getSOAPConstants());
65         msgContext.setRequestMessage(new Message(envelope));
66         RPCElement body = new RPCElement(methodNS, method, params);
67
68         envelope.addBodyElement(body);
69
70         server.invoke(msgContext);
71
72         Message message = msgContext.getResponseMessage();
73         envelope = (SOAPEnvelope)message.getSOAPEnvelope();
74         assertNotNull("SOAP envelope was null", envelope);
75         body = (RPCElement)envelope.getFirstBody();
76
77         return body;
78     }
79
80     public void testRPCReturn() throws Exception JavaDoc {
81
82         RPCElement body = rpc("echo", new Object JavaDoc[] {"abc"});
83         assertNotNull("SOAP body was null", body);
84
85         // Check RPC result
86
Vector JavaDoc arglist = body.getParams();
87         assertNotNull("SOAP argument list was null", arglist);
88         RPCParam param = (RPCParam) arglist.get(1);
89         assertTrue("Not expected result", ((String JavaDoc)param.getObjectValue()).equals("abc"));
90
91         // Check DOM
92
Element e = body.getAsDOM();
93         NodeList JavaDoc l = e.getElementsByTagNameNS("http://www.w3.org/2003/05/soap-rpc","result");
94         assertTrue("No result element was fount", l.getLength() == 1);
95         String JavaDoc ptr = l.item(0).getFirstChild().getNodeValue();
96         assertNotNull("Ptr to the result value was null", ptr);
97         l = e.getElementsByTagName(ptr);
98         assertTrue("No return element was fount", l.getLength() == 1);
99         String JavaDoc res = l.item(0).getFirstChild().getNodeValue();
100         assertTrue("Not expected result", res.equals("abc"));
101     }
102 }
103
Popular Tags