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

Ron Sigal ron_sigal at yahoo.com
Wed Oct 11 15:18:53 EDT 2006


  User: rsigal  
  Date: 06/10/11 15:18:53

  Added:       src/tests/org/jboss/test/remoting/detection/jndi   
                        RestartTestServer.java RestartTestClient.java
                        RestartTestCase.java
  Log:
  JBREM-581, JBREM-603:  Added new JNDIDetector test, and derived from it a unit test for SSL transports.
  
  Revision  Changes    Path
  1.1      date: 2006/10/11 19:18:53;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/RestartTestServer.java
  
  Index: RestartTestServer.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.detection.jndi;
  
  import org.jboss.jrunit.extensions.ServerTestCase;
  import org.jboss.logging.Logger;
  import org.jboss.remoting.InvocationRequest;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.ServerInvocationHandler;
  import org.jboss.remoting.ServerInvoker;
  import org.jboss.remoting.callback.InvokerCallbackHandler;
  import org.jboss.remoting.detection.jndi.JNDIDetector;
  import org.jboss.remoting.transport.Connector;
  import org.jnp.server.Main;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
   * @author <a href="mailto:mazz at jboss.com">John Mazzitelli</a>
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   */
  public class RestartTestServer extends ServerTestCase
  {
     public static int syncPort = 6402;
     
     protected static Logger log = Logger.getLogger(RestartTestServer.class);
     protected static String transport;
     protected static String host;
     protected static int port = 5402;
     protected static Thread serverThread;
     
     protected int detectorPort = 1099;
     protected String contextFactory = "org.jnp.interfaces.NamingContextFactory";
     protected String urlPackage = "org.jboss.naming:org.jnp.interfaces";
     protected Main jserver;
     protected JNDIDetector detector;
     protected Connector connector;
  
     public void setUp() throws Exception
     {
        host = InetAddress.getLocalHost().getHostName();
        final String locatorURI = getTransport() + "://" + host + ":" + port;
        log.info("This server's endpoint will be: " + locatorURI);
        
        serverThread = new Thread()
        {
           public void run()
           {
              try
              {
                 setupJNDI();
                 setupDetector();
                 setupServer(locatorURI);
                 ServerSocket ss = new ServerSocket(syncPort, 0, InetAddress.getLocalHost());
                 log.info("bound to: " + InetAddress.getLocalHost());
                 Socket s = ss.accept();
                 InputStream is = s.getInputStream();
                 OutputStream os = s.getOutputStream();
                 
                 // Indicate server is started.
                 os.write(3);
                 log.info("indicated server started");
                 
                 is.read();
                 log.info("got request to shut down server");
                 shutdownServer(locatorURI);
                 log.info("shut down server");
                 
                 is.read();
                 log.info("got request to restart server");
                 setupServer(locatorURI);
                 os.write(7);
                 log.info("restarted server");
                 
                 is.read();
                 log.info("got request to shut down server");
                 shutdownServer(locatorURI);
                 shutdownDetector();
                 shutdownJNDI();
                 log.info("shut down server");
              }
              catch (Exception e)
              {
                 log.error(e);
                 e.printStackTrace();
              }
           }
        };
        
        serverThread.start();
     }
     
     protected void setupJNDI() throws Exception
     {
        String host = InetAddress.getLocalHost().getHostAddress();
  
        jserver = new Main();
        jserver.setPort(detectorPort);
        jserver.setBindAddress(host);
        jserver.setRmiPort(31000);
        jserver.start();
        System.out.println("Started JNDI server on " + host + ":" + port);
     }
     
     protected void shutdownJNDI()
     {
        jserver.stop();
     }
  
     protected void setupDetector() throws Exception
     {
        // we need an MBeanServer to store our network registry and multicast detector services
        MBeanServer server = MBeanServerFactory.createMBeanServer();
  
        String detectorHost = InetAddress.getLocalHost().getHostName();
  
        detector = new JNDIDetector(getConfiguration());
        // set config info for detector and start it.
        detector.setPort(detectorPort);
        detector.setHost(detectorHost);
        detector.setContextFactory(contextFactory);
        detector.setURLPackage(urlPackage);
  
        server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
        detector.start();
        log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
     }
     
     protected void shutdownDetector() throws Exception
     {
        detector.stop();
     }
  
     /**
      * Sets up our JBoss/Remoting server by creating our Connector on the given locator URI
      * and installing our invocation handler that will handle incoming messages.
      *
      * @param locatorURI defines our server endpoing
      * @throws Exception
      */
     protected void setupServer(String locatorURI) throws Exception
     {
        InvokerLocator locator = new InvokerLocator(locatorURI);
        log.info("Starting remoting server with locator uri of: " + locatorURI);
        connector = new Connector(locator, getConfiguration());
        connector.create();
        SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
        connector.addInvocationHandler("sample", invocationHandler);
        connector.start();
     }
     
     
     protected void shutdownServer(String locatorURI)
     {
        connector.stop();
        log.info("Stopped remoting server with locator uri of: " + locatorURI);
     }
  
     
     protected String getTransport()
     {
        return "socket";
     }
     
     
     /**
      * @return configuration map for Connector and JNDIDetector
      */
     protected Map getConfiguration()
     {
        return new HashMap();
     }
     
     
     public static void main(String[] args)
     {
        try
        {
           RestartTestServer server = new RestartTestServer();
           server.setUp();
           serverThread.join();
        }
        catch (Exception e)
        {
           log.error(e);
           e.printStackTrace();
        }
     }
     
  
     /**
      * Simple invocation handler implementation.  This is the handler that processes incoming messages from clients.
      */
     public static class SampleInvocationHandler implements ServerInvocationHandler
     {
        /**
         * This is the method that is called when a new message comes in from a client.
         *
         * @param invocation the incoming client invocation, encapsulates the message object
         * @return the response object we send back to the client.
         * @throws Throwable
         */
        public Object invoke(InvocationRequest invocation) throws Throwable
        {
           // Print out the invocation request
           String msg = invocation.getParameter().toString();
  
           log.info("RECEIVED A CLIENT MESSAGE: " + msg);
  
           String response = "Server received your message that said [" + msg + "]";
  
           if(msg.indexOf("Welcome") > -1)
           {
              response = "Received your welcome message.  Thank you!";
           }
  
           log.info("Returning the following message back to the client: " + response);
  
           return response;
        }
  
        /**
         * Adds a callback handler that will listen for callbacks from the server invoker handler.
         *
         * @param callbackHandler
         */
        public void addListener(InvokerCallbackHandler callbackHandler)
        {
           // NO OP as we do not handle callback listeners in this example
        }
  
        /**
         * Removes the callback handler that was listening for callbacks from the server invoker handler.
         *
         * @param callbackHandler
         */
        public void removeListener(InvokerCallbackHandler callbackHandler)
        {
           // NO OP as we do not handle callback listeners in this example
        }
  
        /**
         * set the mbean server that the handler can reference
         *
         * @param server
         */
        public void setMBeanServer(MBeanServer server)
        {
           // NO OP as we do not need a reference to the MBeanServer for this handler
        }
  
        /**
         * set the invoker that owns this handler
         *
         * @param invoker
         */
        public void setInvoker(ServerInvoker invoker)
        {
           // NO OP as we do not need a reference back to the server invoker
        }
     }
  }
  
  
  
  1.1      date: 2006/10/11 19:18:53;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/RestartTestClient.java
  
  Index: RestartTestClient.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.detection.jndi;
  
  import org.jboss.logging.Logger;
  import org.jboss.remoting.Client;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.detection.jndi.JNDIDetector;
  import org.jboss.remoting.network.NetworkNotification;
  import org.jboss.remoting.network.NetworkRegistry;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.Notification;
  import javax.management.NotificationListener;
  import javax.management.ObjectName;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.util.Map;
  import java.util.HashMap;
  
  import junit.framework.TestCase;
  
  /**
   * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
   * @author <a href="mailto:mazz at jboss.com">John Mazzitelli</a>
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   */
  public class RestartTestClient extends TestCase implements NotificationListener
  {
     private static Logger log = Logger.getLogger(RestartTestClient.class);
     
     private int detectorPort = 1099;
     private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
     private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
     
     protected JNDIDetector detector;
     protected int serversDetected;
     protected boolean invocationSucceeded;
     protected Object lock = new Object();
  
  
     /**
      * Sets up NetworkRegistry and JNDIDetector so we can listen for any additions
      * or removals of remoting servers on the network.
      *
      * @throws Exception
      */
     public void setUp() throws Exception
     {
        // we need an MBeanServer to store our network registry and jndi detector services
        MBeanServer server = MBeanServerFactory.createMBeanServer();
  
        // the registry will house all remoting servers discovered
        NetworkRegistry registry = NetworkRegistry.getInstance();
        server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
        log.info("NetworkRegistry has been created");
  
        // register class as listener, so know when new server found
        registry.addNotificationListener(this, null, null);
        log.info("NetworkRegistry has added the client as a listener");
  
        String detectorHost = InetAddress.getLocalHost().getHostName();
        
        // jndi detector will detect new network registries that come online
        detector = new JNDIDetector(getConfiguration());
        // set config info for detector and start it.
        detector.setPort(detectorPort);
        detector.setHost(detectorHost);
        detector.setContextFactory(contextFactory);
        detector.setURLPackage(urlPackage);
  
        server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
        detector.start();
        log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
     }
     
     
     public void tearDown() throws Exception
     {
        detector.stop();
     }
     
     public void testDetections()
     {
        try
        {
           String host = InetAddress.getLocalHost().getHostName();
           Socket s = new Socket(host, RestartTestServer.syncPort);
           InputStream is = s.getInputStream();
           OutputStream os = s.getOutputStream();
           
           // Wait until server has been started.
           is.read();
           waitOnDetection();
           assertEquals(1, serversDetected);
           assertTrue(invocationSucceeded);
           log.info("PASSED first detection test");
           invocationSucceeded = false;
           
           // Tell server to shut down.
           os.write(5);
           waitOnDetection();
           assertEquals(0, serversDetected);
           log.info("PASSED second detection test");
           
           // Tell server to restart.
           os.write(7);
           waitOnDetection();
           assertEquals(1, serversDetected);
           assertTrue(invocationSucceeded);
           log.info("PASSED third detection test");
  
           // Tell server test is over.
           os.write(9);
        }
        catch (Exception e)
        {
           log.error(e);
           e.printStackTrace();
           fail();
        }
     }
  
     /**
      * Callback method from the broadcaster MBean this listener implementation is registered to. When a new server
      * is detected, a welcome message will immediately be sent to the newly discovered server.
      *
      * @param notification the notification object
      * @param handback     the handback object given to the broadcaster upon listener registration
      */
     public void handleNotification(Notification notification, Object handback)
     {
        try
        {
           // check to see if network notification
           if(notification instanceof NetworkNotification)
           {
              log.info("GOT A NETWORK-REGISTRY NOTIFICATION: " + notification.getType());
              
              NetworkNotification networkNotification = (NetworkNotification) notification;
              
              if(NetworkNotification.SERVER_ADDED.equals(networkNotification.getType()))
              { // notification is for new servers being added
                 log.info("New server(s) have been detected - getting locators and sending welcome messages");
                 InvokerLocator[] locators = networkNotification.getLocator();
                 for(int x = 0; x < locators.length; x++)
                 {
                    try
                    {
                       serversDetected++;
                       
                       // get the new found server's locator and invoke a call
                       InvokerLocator newServerLocator = locators[x];
                       log.info("detected: " + newServerLocator);
                       invocationSucceeded = false;
                       makeInvocation(newServerLocator.getLocatorURI());
                       invocationSucceeded = true;
                    }
                    catch(Throwable throwable)
                    {
                       throwable.printStackTrace();
                    }
                 }
              }
              else if(NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
              { // notification is for old servers that have gone down
                 InvokerLocator[] locators = networkNotification.getLocator();
                 for(int x = 0; x < locators.length; x++)
                 {
                    serversDetected--;
                    log.info("It has been detected that a server has gone down with a locator of: " + locators[x]);
                 }
              }
           }
           
           return;
        }
        finally
        {
           notifyDetection();
        }
     }
     
     
     protected void waitOnDetection() throws InterruptedException
     {
        synchronized (lock)
        {
           lock.wait();
        }
     }
     
     
     protected void notifyDetection()
     {
        synchronized (lock)
        {
           lock.notify();
        }
     }
  
     /**
      * Make call on remoting server based on locator uri provided.
      *
      * @param locatorURI the URI of the remote server we want to send the message to
      * @throws Throwable
      */
     public void makeInvocation(String locatorURI) throws Throwable
     {
        InvokerLocator locator = new InvokerLocator(locatorURI);
        log.info("Sending welcome message to remoting server with locator uri of: " + locatorURI);
  
        Client remotingClient = new Client(locator, getConfiguration());
        remotingClient.connect();
        Object response = remotingClient.invoke("Welcome Aboard!", null);
  
        log.info("The newly discovered server sent this response to our welcome message: " + response);
  
        remotingClient.disconnect();
        return;
     }
  
     
     /**
      * @return configuration map for JNDIDetector
      */
     protected Map getConfiguration()
     {
        return new HashMap();
     }
  }
  
  
  
  1.1      date: 2006/10/11 19:18:53;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/RestartTestCase.java
  
  Index: RestartTestCase.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.test.remoting.detection.jndi;
  
  import org.apache.log4j.Level;
  import org.jboss.jrunit.harness.TestDriver;
  
  /**
   * This JNDIDetector test case will start two JNDIDetectors in separate instances.
   * The client will
   * <ol>
   *  <li>detect that the server has started
   *  <li>detect that the server has shut down
   *  <li>detect that the server has restarted
   * </ol>
   *
   * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   */
  public class RestartTestCase extends TestDriver
  {
  
     /**
      * This method should call the addTestClasses() method with the client class to run, number of clients to run
      * and the server class to run.
      */
     public void declareTestClasses()
     {
        addTestClasses(RestartTestClient.class.getName(),
                       1,
                       RestartTestServer.class.getName());
     }
  
     protected Level getTestHarnessLogLevel()
     {
        return Level.DEBUG;
     }
  
     protected Level getTestLogLevel()
     {
        return Level.DEBUG;
     }
  
     /**
      * How long to wait for test results to be returned from the client(s).  If goes longer than the
      * specified limit, will throw an exception and kill the running test cases.  Default value is
      * RESULTS_TIMEOUT.
      *
      * @return
      */
     protected long getResultsTimeout()
     {
        return 300000;
     }
  
     /**
      * How long for the server test case to wait for tear down message.  If exceeds timeout,
      * will throw exception.  The default value is TEARDOWN_TIMEOUT.
      *
      * @return
      */
     protected long getTearDownTimeout()
     {
        return 300000;
     }
  
     /**
      * How long to allow each of the test cases to run their tests.  If exceeds this timeout
      * will throw exception and kill tests.  The default value is RUN_TEST_TIMEOUT.
      *
      * @return
      */
     protected long getRunTestTimeout()
     {
        return 300000;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list