You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by rh...@apache.org on 2003/01/26 03:31:08 UTC

cvs commit: xml-rpc/src/test/org/apache/xmlrpc LiteXmlRpcTransportTest.java LocalServerRpcTest.java XmlRpcTransportTest.java ClientServerRpcTest.java

rhoegg      2003/01/25 18:31:08

  Modified:    src/test/org/apache/xmlrpc ClientServerRpcTest.java
  Added:       src/test/org/apache/xmlrpc LiteXmlRpcTransportTest.java
                        LocalServerRpcTest.java XmlRpcTransportTest.java
  Log:
  - Created abstract base class LocalServerRpcTest to provide a WebServer on
     the localhost for testing XML-RPC client code.
  - Modified ClientServerRpcTest to inherit from LocalServerRpcTest
  - Created XmlRpcTransportTest to serve as a base test class for all implementors
    of XmlRpcTransport
  - Created LiteXmlRpcTransportTest as a concrete implementation of
     XmlRpcTransportTest
  Modified ClientServerRpcTest to
  
  Revision  Changes    Path
  1.14      +5 -44     xml-rpc/src/test/org/apache/xmlrpc/ClientServerRpcTest.java
  
  Index: ClientServerRpcTest.java
  ===================================================================
  RCS file: /home/cvs/xml-rpc/src/test/org/apache/xmlrpc/ClientServerRpcTest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ClientServerRpcTest.java	21 Oct 2002 13:08:51 -0000	1.13
  +++ ClientServerRpcTest.java	26 Jan 2003 02:31:08 -0000	1.14
  @@ -75,14 +75,9 @@
    * @version $Id$
    */
   public class ClientServerRpcTest
  -    extends TestCase 
  +    extends LocalServerRpcTest
   {
       /**
  -     * The name of our RPC handler.
  -     */
  -    private static final String HANDLER_NAME = "TestHandler";
  -
  -    /**
        * The identifier or fully qualified class name of the SAX driver
        * to use.  This is generally <code>uk.co.wilson.xml.MinML</code>,
        * but could be changed to
  @@ -96,36 +91,11 @@
        */
       private static final int NBR_REQUESTS = 1000;
   
  -    private static final int SERVER_PORT = 8081;
  -
  -    /**
  -     * The value to use in our request parameter.
  -     */
  -    private static final String REQUEST_PARAM_VALUE = "foobar";
  -
       /**
        * The number of calls to batch in the multicall.
        */
       private static final int NUM_MULTICALLS = 10;
   
  -    /**
  -     * The value to use in our request parameter.
  -     */
  -    private static final String REQUEST_PARAM_XML =
  -        "<value>" + REQUEST_PARAM_VALUE + "</value>";
  -
  -    /**
  -     * A RPC request of our echo server in XML.
  -     */
  -    private static final String RPC_REQUEST =
  -        "<?xml version=\"1.0\"?>\n" +
  -        "<methodCall>\n" +
  -        " <methodName>" + HANDLER_NAME + ".echo</methodName>\n" +
  -        " <params><param>" + REQUEST_PARAM_XML + "</param></params>\n" +
  -        "</methodCall>\n";
  -
  -    private WebServer webServer;
  -
       private XmlRpcServer server;
   
       private XmlRpcClient client;
  @@ -170,8 +140,7 @@
           webServerSysHandler.addSystemHandler("multicall", new MultiCall());
   
           // WebServer (contains its own XmlRpcServer instance)
  -        webServer = new WebServer(SERVER_PORT, localhost);
  -        webServer.addHandler(HANDLER_NAME, new TestHandler());
  +        setUpWebServer();
           webServer.addHandler("system", webServerSysHandler);
   
           // XML-RPC client(s)
  @@ -203,7 +172,7 @@
       {
           try
           {
  -            webServer.start();
  +            startWebServer();
           }
           catch (RuntimeException e)
           {
  @@ -219,7 +188,7 @@
       {
           try
           {
  -            webServer.shutdown();
  +            stopWebServer();
           }
           catch (Exception e)
           {
  @@ -313,12 +282,4 @@
               fail(e.getMessage());
           }
       }   
  - 
  -    protected class TestHandler
  -    {
  -        public String echo(String message)
  -        {
  -            return message;
  -        }
  -    }
   }
  
  
  
  1.1                  xml-rpc/src/test/org/apache/xmlrpc/LiteXmlRpcTransportTest.java
  
  Index: LiteXmlRpcTransportTest.java
  ===================================================================
  package org.apache.xmlrpc;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import junit.framework.*;
  
  /**
   * Tests the LiteXmlRpcTransport implementation.
   *
   * @author <a href="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
   * @version $Id: LiteXmlRpcTransportTest.java,v 1.1 2003/01/26 02:31:08 rhoegg Exp $
   */
  public class LiteXmlRpcTransportTest extends XmlRpcTransportTest {
      
      /** Creates a new instance of LiteXmlRpcTransportTest */
      public LiteXmlRpcTransportTest(String testName) {
          super(testName);
      }
      
      /**
       * Return the Test
       */
      public static Test suite() 
      {
          return new TestSuite(LiteXmlRpcTransportTest.class);
      }
      
      /** 
       * Required by superclass.
       */
      protected XmlRpcTransport getTransport(URL url) {
          return new LiteXmlRpcTransport(url);
      }
      
  }
  
  
  
  1.1                  xml-rpc/src/test/org/apache/xmlrpc/LocalServerRpcTest.java
  
  Index: LocalServerRpcTest.java
  ===================================================================
  package org.apache.xmlrpc;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import junit.framework.*;
  
  /**
   * Abstract base class for tests that require a local WebServer for
   * test XML-RPC messages.
   *
   * @author <a href="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
   * @version $Id: LocalServerRpcTest.java,v 1.1 2003/01/26 02:31:08 rhoegg Exp $
   */
  abstract public class LocalServerRpcTest
      extends TestCase 
  {
  
      
      /**
       * The name of our RPC handler.
       */
      protected static final String HANDLER_NAME = "TestHandler";
  
      /**
       * The value to use in our request parameter.
       */
      protected static final String REQUEST_PARAM_VALUE = "foobar";
  
      protected static final int SERVER_PORT;
  
      /**
       * The value to use in our request parameter.
       */
      protected static final String REQUEST_PARAM_XML =
          "<value>" + REQUEST_PARAM_VALUE + "</value>";
  
      /**
       * A RPC request of our echo server in XML.
       */
      protected static final String RPC_REQUEST =
          "<?xml version=\"1.0\"?>\n" +
          "<methodCall>\n" +
          " <methodName>" + HANDLER_NAME + ".echo</methodName>\n" +
          " <params><param>" + REQUEST_PARAM_XML + "</param></params>\n" +
          "</methodCall>\n";
      
      public LocalServerRpcTest(String message) {
          super(message);
      }
      
      /**
       * Static constructor
       * - initializes test port
       * 
       * TODO: can we initialize this from a properties file?
       */
      static {
          SERVER_PORT = 8081;
      }
  
      protected WebServer webServer;
  
      /**
       * Sets up a @link WebServer instance listening on the localhost.
       *
       * @param port Port to use for the WebServer
       */
      private void setUpWebServer(int port) {
          webServer = new WebServer(port);
          webServer.addHandler(HANDLER_NAME, new TestHandler());
      }
      
      /**
       * Sets up the @link WebServer with the default port.
       */
      protected void setUpWebServer() {
          setUpWebServer(SERVER_PORT);
      }
  
      /**
       * Starts the WebServer so tests can be run against it.
       */
      protected void startWebServer() {
          webServer.start();
      }
      
      /**
       * Stops the WebServer
       */
      protected void stopWebServer() {
          webServer.shutdown();
      }
      
      protected class TestHandler {
          public String echo(String message) {
              return message;
          }
      }
  }
  
  
  
  1.1                  xml-rpc/src/test/org/apache/xmlrpc/XmlRpcTransportTest.java
  
  Index: XmlRpcTransportTest.java
  ===================================================================
  package org.apache.xmlrpc;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.BufferedInputStream;
  import java.io.InputStream;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import junit.framework.*;
  
  /**
   * Abstract Test class to be extended by the tests for each Transport.
   * Guarantees that transports implement a base contract expected by the
   * XmlRpcClient classes
   *
   * @author <a href="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
   * @version $Id: XmlRpcTransportTest.java,v 1.1 2003/01/26 02:31:08 rhoegg Exp $
   */
  abstract public class XmlRpcTransportTest
      extends LocalServerRpcTest
  {
      /**
       * Constructor
       */
      public XmlRpcTransportTest(String testName)
      {
          super(testName);
      }
      
      /**
       * This should return a new instance of the XmlRpcTransport being tested.
       */
      abstract protected XmlRpcTransport getTransport(URL url);
      
      /**
       * Sets up the WebServer and starts it.
       */
      public void setUp() {
          setUpWebServer();
          startWebServer();
      }
      
      /**
       * Shuts down the WebServer
       */
      public void tearDown() {
          stopWebServer();
      }
      
      /**
       * This test is to enforce that every alternate implementation of 
       * XmlRpcTransport provides a minimum of the same functionality as
       * @link DefaultXmlRpcTransport.  We trust DefaultXmlRpcTransport
       * because it is used by default in XmlRpcClient, which is tested
       * in @link ClientServerRpcTest.
       */
      public void testSendXmlRpc() {
          
          try {
              URL testUrl = buildURL("localhost", SERVER_PORT);
              XmlRpcTransport controlTransport = new DefaultXmlRpcTransport(testUrl);
              XmlRpcTransport testTransport = getTransport(testUrl);
              InputStream controlResponse = controlTransport.sendXmlRpc(RPC_REQUEST.getBytes());
              InputStream testResponse = testTransport.sendXmlRpc(RPC_REQUEST.getBytes());
              assertTrue(
                  "Response from XmlRpcTransport does not match that of DefaultXmlRpcTransport.",
                  equalsInputStream(controlResponse, testResponse));
          }
          catch (MalformedURLException e) {
              e.printStackTrace();
              fail(e.getMessage());
          }
          catch (IOException e) {
              e.printStackTrace();
              fail(e.getMessage());
          }
          catch (XmlRpcClientException e) {
              e.printStackTrace();
              fail(e.getMessage());
          }
      }
      
      private URL buildURL(String hostname, int port) throws MalformedURLException {
          return new URL("http://" + hostname + ':' + port + "/RPC2");
      }
      
      protected boolean equalsInputStream(InputStream is1, InputStream is2) throws IOException {
          BufferedInputStream stream1 = new BufferedInputStream(is1);
          BufferedInputStream stream2 = new BufferedInputStream(is2);
          int char1 = is1.read();
          int char2 = is2.read();
          while ((char1 != -1) && (char2 != -1) && (char1 == char2)) {
              char1 = is1.read();
              char2 = is2.read();
          }
          return char1 == char2;
      }
  }