FileDocCategorySizeDatePackage
TestRPC.javaAPI DocApache Axis 1.49668Sat Apr 22 18:57:28 BST 2006test.RPCDispatch

TestRPC

public class TestRPC extends TestCase
Test org.apache.axis.handlers.RPCDispatcher
author
Sam Ruby

Fields Summary
private final String
header
private final String
footer
private org.apache.axis.configuration.SimpleProvider
provider
private org.apache.axis.server.AxisServer
engine
private String
SOAPAction
Constructors Summary
public TestRPC(String name)


       
        super(name);
        engine.init();
    
Methods Summary
public static voidmain(java.lang.String[] args)

      try {
        TestRPC tester = new TestRPC("RPC test");
        tester.testReverseString();
        tester.testReverseData();
        tester.testSimpleFault();
        tester.testReverseStringThatShouldFail();
        tester.testReverseStringWithCommaDelimiter();
        tester.testReverseStringWithSpaceDelimiter();
        tester.testReverseStringWithStar();
      } catch (Exception e) {
        e.printStackTrace();
      }
    
private final java.lang.Objectrpc(java.lang.String method, java.lang.Object[] parms)
Invoke a given RPC method, and return the result

return
Deserialized result


        // Create the message context
        MessageContext msgContext = new MessageContext(engine);

        // Set the dispatch either by SOAPAction or methodNS
        String methodNS = null;
        msgContext.setTargetService(SOAPAction);

        // Construct the soap request
        SOAPEnvelope envelope = new SOAPEnvelope();
        msgContext.setRequestMessage(new Message(envelope));
        RPCElement body = new RPCElement(methodNS, method, parms);
        envelope.addBodyElement(body);

        // Invoke the Axis engine
        engine.invoke(msgContext);

        // Extract the response Envelope
        Message message = msgContext.getResponseMessage();
        envelope = (SOAPEnvelope)message.getSOAPEnvelope();
        assertNotNull("SOAP envelope was null", envelope);

        // Extract the body from the envelope
        body = (RPCElement)envelope.getFirstBody();
        assertNotNull("SOAP body was null", body);

        // Extract the list of parameters from the body
        Vector arglist = body.getParams();
        assertNotNull("SOAP argument list was null", arglist);
        if (arglist.size()==0) return null;

        // Return the first parameter
        RPCParam param = (RPCParam) arglist.get(0);
        return param.getObjectValue();
    
public voidtestMessageContextImplicit()
Test a simple method that returns a field from the message context

        // Register the targetService service
        SOAPService tgtSvc = new SOAPService(new RPCProvider());
        tgtSvc.setOption("className", "test.RPCDispatch.Service");
        tgtSvc.setOption("allowedMethods", "targetServiceImplicit");
        provider.deployService(new QName(null, SOAPAction), tgtSvc);

        // invoke the service and verify the result
        assertEquals("SOAP Action did not equal the targetService.", 
            SOAPAction, rpc("targetServiceImplicit", new Object[] {}));
    
public voidtestNull()
Test a simple method that accepts and returns a null

        // Register the echoInt service
        SOAPService echoInt = new SOAPService(new RPCProvider());
        echoInt.setOption("className", "test.RPCDispatch.Service");
        echoInt.setOption("allowedMethods", "echoInt");
        provider.deployService(new QName(null, SOAPAction), echoInt);

        // invoke the service and verify the result
        assertNull("The result was not null as expected.", rpc("echoInt", new Object[] {null}));
    
public voidtestReverseData()
Test a method that reverses a data structure

        // Register the reverseData service
        SOAPService reverse = new SOAPService(new RPCProvider());
        reverse.setOption("className", "test.RPCDispatch.Service");
        reverse.setOption("allowedMethods", "reverseData");
        provider.deployService(new QName(null, SOAPAction), reverse);

        // invoke the service and verify the result
        Data input    = new Data(5, "abc", 3);
        Data expected = new Data(3, "cba", 5);
        assertEquals("Did not reverse the data as expected.", expected, rpc("reverseData", new Object[] {input}));
    
public voidtestReverseString()
Test a simple method that reverses a string

        // Register the reverseString service
        SOAPService reverse = new SOAPService(new RPCProvider());
        reverse.setOption("className", "test.RPCDispatch.Service");
        reverse.setOption("allowedMethods", "reverseString");
        provider.deployService(new QName(null,SOAPAction), reverse);

        // invoke the service and verify the result
        assertEquals("Did not reverse the string correctly.", "cba",
                     rpc("reverseString", new Object[] {"abc"}));
    
public voidtestReverseStringThatShouldFail()
Test a simple method that reverses a string (but fails)

        try {
            // Register the reverseString service
            SOAPService reverse = new SOAPService(new RPCProvider());
            reverse.setOption("className", "test.RPCDispatch.Service");
            reverse.setOption("allowedMethods", "reverseString2");
            provider.deployService(new QName(null,SOAPAction), reverse);

            // invoke the service and verify the result
            rpc("reverseString", new Object[] {"abc"});
            throw new junit.framework.AssertionFailedError("Should not reach here");
        } catch (AxisFault af){
            //This test should cause an exception.
            return;
        }
    
public voidtestReverseStringWithCommaDelimiter()
Test a simple method that reverses a string (with a comma delimiter

        // Register the reverseString service
        SOAPService reverse = new SOAPService(new RPCProvider());
        reverse.setOption("className", "test.RPCDispatch.Service");
        reverse.setOption("allowedMethods", "reverseString2,reverseString");
        provider.deployService(new QName(null,SOAPAction), reverse);

        // invoke the service and verify the result
        assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object[] {"abc"}));
    
public voidtestReverseStringWithSpaceDelimiter()
Test a simple method that reverses a string (with a space delimiter

        // Register the reverseString service
        SOAPService reverse = new SOAPService(new RPCProvider());
        reverse.setOption("className", "test.RPCDispatch.Service");
        reverse.setOption("allowedMethods", "reverseString2 reverseString");
        provider.deployService(new QName(null,SOAPAction), reverse);

        // invoke the service and verify the result
        assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object[] {"abc"}));
    
public voidtestReverseStringWithStar()
Test a simple method that reverses a string (with a '*'

        // Register the reverseString service
        SOAPService reverse = new SOAPService(new RPCProvider());
        reverse.setOption("className", "test.RPCDispatch.Service");
        reverse.setOption("allowedMethods", "*");
        provider.deployService(new QName(null,SOAPAction), reverse);

        // invoke the service and verify the result
        assertEquals("Did not reverse the string correctly.", "cba",
                     rpc("reverseString", new Object[] {"abc"}));
    
public voidtestSimpleFault()
Test faults

        // Register the reverseData service
        SOAPService simpleFault = new SOAPService(new RPCProvider());
        simpleFault.setOption("className", "test.RPCDispatch.Service");
        simpleFault.setOption("allowedMethods", "simpleFault");
        provider.deployService(new QName(null, SOAPAction), simpleFault);

        try {
            rpc("simpleFault", new Object[] {"foobar"});
        } catch (AxisFault result) {
            assertEquals("faultString was not set correctly.",
                "test.RPCDispatch.Service$TestFault: foobar",
                result.getFaultString());
            return;
        }

        fail("Did not get an expected fault!");