User: schaefera
  Date: 01/12/08 21:01:44

  Added:       src/main/org/jboss/test/management/test
                        JSR77SpecUnitTestCase.java
  Log:
  Added Notification Transport to JSR-77 which works similar to EJB-Connector
  but with the tweak of JSR-77 interface.
  Added the adjusted JDBC JSR-77 shadow objects.
  Added JSR-77 to the Connector Factory enabling JSR-77 to start and stop
  Datasources.
  Added a testsuite for JSR-77 which tests the basic stuff inclusive the
  notification delivery.
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/management/test/JSR77SpecUnitTestCase.java
  
  Index: JSR77SpecUnitTestCase.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.test.management.test;
  
  import java.io.IOException;
  import java.net.InetAddress;
  import java.rmi.RemoteException;
  import java.util.Set;
  import javax.ejb.CreateException;
  import javax.ejb.Handle;
  import javax.management.Notification;
  import javax.management.NotificationListener;
  import javax.management.ObjectName;
  import javax.management.j2ee.ListenerRegistration;
  import javax.management.j2ee.Management;
  import javax.management.j2ee.ManagementHome;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.rmi.PortableRemoteObject;
  
  import junit.extensions.TestSetup;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.jboss.test.JBossTestCase;
  import org.jboss.test.JBossTestSetup;
  
  /**
   * Test of JSR-77 specification conformance using the ??ToDo
   * These test the basic JSR-77 handling and access.
   *
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>.
   * @version $Revision: 1.1 $
   *   
   * <p><b>Revisions:</b>
   *
   * <p><b>20011206 Andreas Schaefer:</b>
   * <ul>
   * <li> Creation
   * </ul>
   **/
  public class JSR77SpecUnitTestCase
     extends JBossTestCase
  {
     // Constants -----------------------------------------------------
     
     // Attributes ----------------------------------------------------
     
     // Static --------------------------------------------------------
     
     /**
      * Setup the test suite.
      */
     public static Test suite()
     {
        TestSuite lSuite = new TestSuite();
        lSuite.addTest( new TestSuite( JSR77SpecUnitTestCase.class ) );
  
        // Create an initializer for the test suite
        TestSetup lWrapper = new JBossTestSetup( lSuite )
        {
           protected void setUp() throws Exception
           {
              super.setUp();
  //            deployJ2ee( "jsr77-spec.jar" );
  //            flushAuthCache();
           }
           protected void tearDown() throws Exception
           {
  //            undeployJ2ee( "jsr77-spec.jar" );
  //            super.tearDown();
           
           }
        };
        return lWrapper;
     }
     
     // Constructors --------------------------------------------------
     
     public JSR77SpecUnitTestCase( String pName )
     {
        super( pName );
     }
     
     // Public --------------------------------------------------------
     
     /**
      * Test if a connection could be made to the Management MBean
      **/
     public void testConnect()
        throws
           Exception
     {
        getLog().debug("+++ testConnect");
        Management lManagement = getManagementEJB();
        String lDomain = lManagement.getDefaultDomain();
        getLog().debug( "+++ testConnect, domain: " + lDomain );
        lManagement.remove();
     }
     
     /**
      * Test if the management domain could be retrieved
      **/
     public void testGetManagementDomain()
        throws
           Exception
     {
        getLog().debug("+++ testGetManagementDomain");
        Management lManagement = getManagementEJB();
        Set lNames = lManagement.queryNames(
           new ObjectName(
              lManagement.getDefaultDomain() + ":type=J2EEManagement,*"
           )
        );
        if( lNames.isEmpty() ) {
           fail( "Could not found JSR-77 root object of type 'J2EEManagement'" );
        }
        if( lNames.size() > 1 ) {
           fail( "Found more than one JSR-77 root objects of type 'J2EEManagement'" );
        }
        ObjectName lManagementDomain = (ObjectName) lNames.iterator().next();
        getLog().debug( "+++ testGetManagementDomain, root: " + lManagementDomain );
        lManagement.remove();
     }
     
     /**
      * Test if the deployed EJB could be found directly
      * and through navigation
      **/
     public void testGetDataSource()
        throws
           Exception
     {
        getLog().debug("+++ testGetDataSource");
        Management lManagement = getManagementEJB();
        Set lNames = lManagement.queryNames(
           new ObjectName(
              lManagement.getDefaultDomain() + ":type=JDBCDataSource,name=DefaultDS,*"
           )
        );
        if( lNames.isEmpty() ) {
           fail( "Could not found JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        if( lNames.size() > 1 ) {
           fail( "Found more than one JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        ObjectName lDataSource = (ObjectName) lNames.iterator().next();
        getLog().debug( "+++ testGetDataSource, DefaultDS: " + lDataSource );
        getLog().debug(
           "+++ testGetDataSource, DefaultDS status: " +
           lManagement.getAttribute( lDataSource, "State" )
        );
        lManagement.remove();
     }
     
     /**
      * Test if default Datasource could be stopped and restarted
      **/
     public void testRestartDatasource()
        throws
           Exception
     {
        getLog().debug("+++ testRestartDatasource");
        Management lManagement = getManagementEJB();
        Set lNames = lManagement.queryNames(
           new ObjectName(
              lManagement.getDefaultDomain() + ":type=JDBCDataSource,name=DefaultDS,*"
           )
        );
        if( lNames.isEmpty() ) {
           fail( "Could not found JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        if( lNames.size() > 1 ) {
           fail( "Found more than one JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        ObjectName lDataSource = (ObjectName) lNames.iterator().next();
        getLog().debug( "+++ testRestartDatasource, DefaultDS: " + lDataSource );
        getLog().debug( "+++ testRestartDatasource, stop DefaultDS" );
        lManagement.invoke( lDataSource, "stop", new Object[] {}, new String[] {} );
        getLog().debug( "+++ testRestartDatasource, start DefaultDS" );
        lManagement.invoke( lDataSource, "start", new Object[] {}, new String[] {} );
        lManagement.remove();
     }
     
     /**
      * Test the notification delivery by restarting Default DataSource
      **/
     public void testNotificationDeliver()
        throws
           Exception
     {
        try {
        getLog().debug("+++ testNotificationDeliver");
        Management lManagement = getManagementEJB();
        Set lNames = lManagement.queryNames(
           new ObjectName(
              lManagement.getDefaultDomain() + ":type=JDBCDataSource,name=DefaultDS,*"
           )
        );
        if( lNames.isEmpty() ) {
           fail( "Could not found JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        if( lNames.size() > 1 ) {
           fail( "Found more than one JSR-77 JDBC DataSource named 'DefaultDS'" );
        }
        ObjectName lDataSource = (ObjectName) lNames.iterator().next();
        Listener lLocalListener = new Listener();
        ListenerRegistration lListenerFactory = lManagement.getListenerRegistry();
        getLog().debug( "+++ testNotificationDeliver, add Notification Listener" );
        lListenerFactory.addNotificationListener(
           lDataSource,
           lLocalListener,
           null,
           null
        );
        getLog().debug( "+++ testNotificationDeliver, stop DefaultDS" );
        lManagement.invoke( lDataSource, "stop", new Object[] {}, new String[] {} );
        getLog().debug( "+++ testNotificationDeliver, start DefaultDS" );
        lManagement.invoke( lDataSource, "start", new Object[] {}, new String[] {} );
        // Wait 5 seconds to ensure that the notifications are delivered
        Thread.sleep( 5000 );
        if( lLocalListener.getNumberOfNotifications() < 2 ) {
           fail( "Not enough notifications received: " + 
lLocalListener.getNumberOfNotifications() );
        }
        getLog().debug( "+++ testNotificationDeliver, remove Notification Listener" );
        lListenerFactory.removeNotificationListener(
           lDataSource,
           lLocalListener
        );
        lManagement.remove();
        }
        catch( Exception e ) {
           e.printStackTrace();
           throw e;
        }
     }
     
     // Z implementation ----------------------------------------------
     
     // Y overrides ---------------------------------------------------
     
     // Package protected ---------------------------------------------
     
     // Protected -----------------------------------------------------
     
     // Private -------------------------------------------------------
     
     private Management getManagementEJB()
        throws
           Exception
     {
        getLog().debug("+++ getManagementEJB()");
        Object lObject = getInitialContext().lookup( "ejb/mgmt/J2EEManagement" );
        ManagementHome lHome = (ManagementHome) PortableRemoteObject.narrow(
           lObject,
           ManagementHome.class
        );
        getLog().debug( "Found JSR-77 Management EJB (MEJB)" );
        return lHome.create();
     }
     
     // Inner classes -------------------------------------------------
     
     private class Listener implements NotificationListener {
        
        private int mNrOfNotifications = 0;
        
        public int getNumberOfNotifications() {
           return mNrOfNotifications;
        }
        
        public void handleNotification( Notification pNotification, Object pHandbank ) 
{
  //         getLog().debug( "Got notification: " + pNotification );
           mNrOfNotifications++;
        }
     }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to