You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by gd...@apache.org on 2001/06/20 14:26:57 UTC

cvs commit: xml-axis/java/test/RPCDispatch TestSerializedRPC.java PackageTests.java

gdaniels    01/06/20 05:26:53

  Modified:    java/test/RPCDispatch PackageTests.java
  Added:       java/test/RPCDispatch TestSerializedRPC.java
  Log:
  Add test for RPC which also parses a message.
  
  Revision  Changes    Path
  1.2       +1 -0      xml-axis/java/test/RPCDispatch/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/RPCDispatch/PackageTests.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PackageTests.java	2001/05/21 19:14:50	1.1
  +++ PackageTests.java	2001/06/20 12:26:38	1.2
  @@ -17,6 +17,7 @@
           TestSuite suite = new TestSuite();
   
           suite.addTestSuite(TestRPC.class);
  +        suite.addTestSuite(TestSerializedRPC.class);
   
           return suite;
       }
  
  
  
  1.1                  xml-axis/java/test/RPCDispatch/TestSerializedRPC.java
  
  Index: TestSerializedRPC.java
  ===================================================================
  package test.RPCDispatch;
  
  import junit.framework.TestCase;
  
  import org.apache.axis.*;
  import org.apache.axis.encoding.*;
  import org.apache.axis.handlers.soap.*;
  import org.apache.axis.message.*;
  import org.apache.axis.server.*;
  import org.apache.axis.registries.*;
  
  import java.util.Vector;
  import java.io.*;
  
  /**
   * Test org.apache.axis.handlers.RPCDispatcher
   *
   * @author Sam Ruby <ru...@us.ibm.com>
   */
  public class TestSerializedRPC extends TestCase {
  
      private final String header =
          "<?xml version=\"1.0\"?>\n" +
          "<soap:Envelope " +
               "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
               "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
               "xmlns:xsi=\"" + Constants.URI_CURRENT_SCHEMA_XSI + "\" " +
               "xmlns:xsd=\"" + Constants.URI_CURRENT_SCHEMA_XSD + "\">\n" +
               "<soap:Body>\n";
  
      private final String footer =
               "</soap:Body>\n" +
          "</soap:Envelope>\n";
  
      private AxisServer engine = new AxisServer();
      private HandlerRegistry hr;
      private HandlerRegistry sr;
      private Handler RPCDispatcher;
  
      private String SOAPAction = "urn:reverse";
  
      public TestSerializedRPC(String name) {
          super(name);
          engine.init();
          hr = (HandlerRegistry) engine.getHandlerRegistry();
          sr = (HandlerRegistry) engine.getServiceRegistry();
          RPCDispatcher = hr.find("RPCDispatcher");
          
          // Register the reverseString service
          SOAPService reverse = new SOAPService(RPCDispatcher, "RPCDispatcher");
          reverse.addOption("className", "test.RPCDispatch.Service");
          reverse.addOption("methodName", "reverseString");
          sr.add(SOAPAction, reverse);
      }
  
      /**
       * Invoke a given RPC method, and return the result
       * @param soapAction action to be performed
       * @param request XML body of the request
       * @return Deserialized result
       */
      private final Object rpc(String method, String bodyStr,
                               boolean setService)
          throws AxisFault
      {
  
          // Create the message context
          MessageContext msgContext = new MessageContext(engine);
          
          // Set the dispatch either by SOAPAction or methodNS
          String methodNS = "";
          if (setService) {
              msgContext.setTargetService(SOAPAction);
          } else {
              methodNS = SOAPAction;
          }
  
          String bodyElemHead = "<m:" + method + " xmlns:m=\"" +
                            methodNS + "\">";
          String bodyElemFoot = "</m:" + method + ">";
          // Construct the soap request
          String msgStr = header + bodyElemHead + bodyStr +
                          bodyElemFoot + footer;
          msgContext.setRequestMessage(new Message(msgStr));
          
          // Invoke the Axis engine
          try {
              engine.invoke(msgContext);
          } catch (AxisFault af) {
              return af;
          }
  
          // Extract the response Envelope
          Message message = msgContext.getResponseMessage();
          SOAPEnvelope envelope = (SOAPEnvelope)message.getAsSOAPEnvelope();
          assertNotNull("envelope", envelope);
  
          // Extract the body from the envelope
          RPCElement body = (RPCElement)envelope.getFirstBody();
          assertNotNull("body", body);
  
          // Extract the list of parameters from the body
          Vector arglist = body.getParams();
          assertNotNull("arglist", arglist);
          assert("param.size()>0", arglist.size()>0);
  
          // Return the first parameter
          RPCParam param = (RPCParam) arglist.get(0);
          return param.getValue();
      }
  
      /**
       * Test a simple method that reverses a string
       */
      public void testSerReverseString() throws Exception {
          String arg = "<arg0 xsi:type=\"xsd:string\">abc</arg0>";
          // invoke the service and verify the result
          assertEquals("cba", rpc("reverseString", arg, true));
      }
  
      public void testSerReverseBodyDispatch() throws Exception {
          String arg = "<arg0 xsi:type=\"xsd:string\">abc</arg0>";
          // invoke the service and verify the result
          assertEquals("cba", rpc("reverseString", arg, false));
      }
  }