[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/clientaddress ...

Ron Sigal ron_sigal at yahoo.com
Wed Nov 21 01:57:21 EST 2007


  User: rsigal  
  Date: 07/11/21 01:57:21

  Modified:    src/tests/org/jboss/test/remoting/clientaddress  Tag:
                        remoting_2_x ClientAddressTestParent.java
  Log:
  JBREM-792: Added testGetAddressSeenByServer().
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +124 -14   JBossRemoting/src/tests/org/jboss/test/remoting/clientaddress/Attic/ClientAddressTestParent.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ClientAddressTestParent.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/tests/org/jboss/test/remoting/clientaddress/Attic/ClientAddressTestParent.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -b -r1.1.2.1 -r1.1.2.2
  --- ClientAddressTestParent.java	20 Nov 2007 09:20:07 -0000	1.1.2.1
  +++ ClientAddressTestParent.java	21 Nov 2007 06:57:21 -0000	1.1.2.2
  @@ -43,6 +43,8 @@
   import org.jboss.remoting.Remoting;
   import org.jboss.remoting.ServerInvocationHandler;
   import org.jboss.remoting.ServerInvoker;
  +import org.jboss.remoting.callback.Callback;
  +import org.jboss.remoting.callback.HandleCallbackException;
   import org.jboss.remoting.callback.InvokerCallbackHandler;
   import org.jboss.remoting.transport.Connector;
   import org.jboss.remoting.transport.PortUtil;
  @@ -54,14 +56,19 @@
      
      private static final String GET_ADDRESS = "getAddress";
      private static final String OPEN_CONNECTION = "openConnection";
  +   private static final String COPY = "copy:";
  +   private static final String SEND_CALLBACK = "sendCallback";
      private static final int ANSWER = 17;
      
      private static boolean firstTime = true;
      
  -   private Connector connector;
  -   private InvokerLocator serverLocator;
  -   private TestInvocationHandler invocationHandler;
  -   private int callbackPort;
  +   protected Connector connector;
  +   protected InvokerLocator serverLocator;
  +   protected String locatorURI;
  +   protected String host;
  +   protected int port;
  +   protected TestInvocationHandler invocationHandler;
  +   protected int callbackPort;
   
      
      public void setUp() throws Exception
  @@ -84,15 +91,21 @@
      }
      
      
  +   /**
  +    * Verifies that a valid InetAddress for the client is passed to the
  +    * org.jboss.remoting.ServerInvocationHandler in the
  +    * org.jboss.remoting.InvocationRequest requestPayload.
  +    * @throws Throwable
  +    */
      public void testClientAddress() throws Throwable
      {
         log.info("entering " + getName());
         
         // Start server.
  -      String locatorURI = setupServer();
  +      setupServer();
         
         // Create client.
  -      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI + "/?timeout=1000");
  +      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI + "/?timeout=10000");
         HashMap clientConfig = new HashMap();
         clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
         addExtraClientConfig(clientConfig);
  @@ -141,7 +154,58 @@
         assertEquals(ANSWER, t.result);
         
         client.disconnect();
  -      connector.stop();
  +      shutdownServer();
  +      log.info(getName() + " PASSES");
  +   }
  +   
  +   
  +   /**
  +    * Verifies that the address returned by
  +    * org.jboss.remoting.Client.getAddressSeenByServer() is valid.
  +    */
  +   public void testGetAddressSeenByServer() throws Throwable
  +   {
  +      log.info("entering " + getName());
  +      
  +      // Start server.
  +      setupServer();
  +      
  +      // Create client.
  +      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI + "/?timeout=10000");
  +      HashMap clientConfig = new HashMap();
  +      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
  +      addExtraClientConfig(clientConfig);
  +      Client client = new Client(clientLocator1, clientConfig);
  +      client.connect();
  +      log.info("client is connected");
  +      
  +      // Test connections.
  +      assertEquals("abc", client.invoke("copy:abc"));
  +      log.info("connection is good");
  +      
  +      // Get address as seen by server and create callback Connector with that address.
  +      InetAddress callbackAddress = client.getAddressSeenByServer();
  +      log.info("client address seen by server: " + callbackAddress);
  +      String callbackHost = callbackAddress.getHostAddress();
  +      int callbackPort = PortUtil.findFreePort(callbackHost);
  +      String callbackLocatorURI = getCallbackTransport() + "://" + callbackHost + ":" + callbackPort;
  +      InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
  +      log.info("callback locator: " + callbackLocator);
  +      HashMap callbackConfig = new HashMap();
  +      addExtraCallbackConfig(callbackConfig);
  +      Connector callbackConnector = new Connector(callbackLocator, callbackConfig);
  +      callbackConnector.start();
  +      TestCallbackHandler callbackHandler = new TestCallbackHandler();
  +      client.addListener(callbackHandler, callbackLocator);
  +      
  +      // Tell server to send a callback, and verify it was received.
  +      client.invoke(SEND_CALLBACK);
  +      assertEquals(1, callbackHandler.counter);
  +      
  +      client.removeListener(callbackHandler);
  +      callbackConnector.stop();
  +      client.disconnect();
  +      shutdownServer();
         log.info(getName() + " PASSES");
      }
      
  @@ -149,11 +213,17 @@
      protected abstract String getTransport();
      
      
  -   protected String setupServer() throws Exception
  +   protected String getCallbackTransport()
  +   {
  +      return getTransport();
  +   }
  +   
  +   
  +   protected void setupServer() throws Exception
      {
  -      String host = InetAddress.getLocalHost().getHostAddress();
  -      int port = PortUtil.findFreePort(host);
  -      String locatorURI = getTransport() + "://" + host + ":" + port; 
  +      host = InetAddress.getLocalHost().getHostAddress();
  +      port = PortUtil.findFreePort(host);
  +      locatorURI = getTransport() + "://" + host + ":" + port; 
         serverLocator = new InvokerLocator(locatorURI);
         log.info("Starting remoting server with locator uri of: " + locatorURI);
         HashMap config = new HashMap();
  @@ -162,18 +232,37 @@
         connector = new Connector(serverLocator, config);
         connector.create();
         invocationHandler = new TestInvocationHandler();
  -      connector.addInvocationHandler("sample", invocationHandler);
  +      connector.addInvocationHandler("test", invocationHandler);
         connector.start();
  -      return locatorURI;
  +   }
  +   
  +   
  +   protected void shutdownServer()
  +   {
  +      if (connector != null)
  +         connector.stop();
  +   }
  +   
  +   
  +   protected String reconstructLocator(InetAddress address)
  +   {
  +      return getTransport() + "://" + address.getHostAddress() + ":" + port;
  +
      }
      
      protected void addExtraClientConfig(Map config) {}
      protected void addExtraServerConfig(Map config) {}
  +   protected void addExtraCallbackConfig(Map config) {}
      
   
      class TestInvocationHandler implements ServerInvocationHandler
      {
  -      public void addListener(InvokerCallbackHandler callbackHandler) {}
  +      private InvokerCallbackHandler callbackHandler;
  +      
  +      public void addListener(InvokerCallbackHandler callbackHandler)
  +      {
  +         this.callbackHandler = callbackHandler;
  +      }
         
         public Object invoke(final InvocationRequest invocation) throws Throwable
         {
  @@ -202,6 +291,15 @@
               s.close();
               return null;
            }
  +         else if (SEND_CALLBACK.equals(command))
  +         {
  +            callbackHandler.handleCallback(new Callback("callback"));
  +            return null;
  +         }
  +         else if (command.startsWith(COPY))
  +         {
  +            return command.substring(5);
  +         }
            else
            {
               throw new Exception("unrecognized command: " + command);
  @@ -212,4 +310,16 @@
         public void setMBeanServer(MBeanServer server) {}
         public void setInvoker(ServerInvoker invoker) {}
      }
  +   
  +   
  +   class TestCallbackHandler implements InvokerCallbackHandler
  +   {
  +      public int counter;
  +      
  +      public void handleCallback(Callback callback) throws HandleCallbackException
  +      {
  +         counter++;
  +         log.info("received callback");
  +      }      
  +   }
   }
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list