db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r807715 - /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java
Date Tue, 25 Aug 2009 17:01:09 GMT
Author: rhillegas
Date: Tue Aug 25 17:01:09 2009
New Revision: 807715

URL: http://svn.apache.org/viewvc?rev=807715&view=rev
Log:
DERBY-4356: Port 807685 from trunk to 10.4 branch.

Modified:
    db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java

Modified: db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java?rev=807715&r1=807714&r2=807715&view=diff
==============================================================================
--- db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java
(original)
+++ db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java
Tue Aug 25 17:01:09 2009
@@ -29,11 +29,35 @@
 
 
 /**
+ * <p>
  * Test the ManagementMBean interface provided by Derby
  * which has two implementations. A built in one and
  * one that can be created by a user.
+ * </p>
+ *
+ * <p>
+ * If you set the debug flag (-Dderby.tests.debug=true), then the test
+ * will print out the number of MBeans which it finds. This should be
+ * EXPECTED_BEAN_COUNT but could be something greater if MBeans
+ * are left hanging around from other tests.
+ * </p>
  */
 public class ManagementMBeanTest extends MBeanTest {
+
+    private static final String MANAGEMENT = "Management";
+    private static final String VERSION = "Version";
+
+    // 1 NetworkServer, 1 JDBC, 2 Version, 2 Management beans
+    private static final int EXPECTED_BEAN_COUNT = 6;
+    
+    // MBean names
+    private static final String[] MBEAN_TYPES =
+    {
+        "NetworkServer",
+        MANAGEMENT,
+        "JDBC",
+        VERSION,
+    };
     
     public ManagementMBeanTest(String name) {
         super(name);
@@ -64,34 +88,119 @@
     {
         // Test fixtures start off active
         assertBooleanAttribute(true, mbean, "ManagementActive");
-        
-        int derbyMbeanCount = getDerbyDomainMBeans().size();
-        assertTrue("DerbyMBeanCount:" + derbyMbeanCount, derbyMbeanCount >= 2);
+
+        // may include MBeans left over from other engines which ran
+        // in earlier tests
+        StatsTuple originalStats = getCurrentStats( "Original" );
+
+        assertTrue("DerbyMBeanCount:" + originalStats.getBeanCount(), originalStats.getBeanCount()
>= EXPECTED_BEAN_COUNT );
         
         // Should be a no-op
         invokeOperation(mbean, "startManagement");
         assertBooleanAttribute(true, mbean, "ManagementActive");
         
         // so should have the same number of MBeans registered
-        assertEquals(derbyMbeanCount, getDerbyDomainMBeans().size());
+        StatsTuple nopStats = getCurrentStats( "NOP" );
+
+        compareStats( originalStats, nopStats );
         
         // now stop management
         invokeOperation(mbean, "stopManagement");
         assertBooleanAttribute(false, mbean, "ManagementActive");
         
-        // leaving only management MBeans, the one registered
-        // by this test and the one registered by Derby
-        // (which has the system key property).
-        Set<ObjectName> managementOnly = getDerbyDomainMBeans();
-        assertEquals(2, managementOnly.size());
-        for (ObjectName name : managementOnly)
+        // the stop should have brought down 1 JDBC bean, 1 NetworkServer bean
+        // and 2 Version beans. it should have left 2 Management beans standing.
+        StatsTuple afterStopping = getCurrentStats( "After Stopping" );
+        
+        int[] expectedCounts = new int[ MBEAN_TYPES.length ];
+        for ( int i = 0; i < MBEAN_TYPES.length; i++ )
         {
-            assertEquals("Management", name.getKeyProperty("type"));
+            int expectedDifference = 1;
+
+            if ( MANAGEMENT.equals( MBEAN_TYPES[ i ] ) ) { expectedDifference = 0; }
+            else if ( VERSION.equals( MBEAN_TYPES[ i ] ) ) { expectedDifference = 2; }
+
+            expectedCounts[ i ] = originalStats.typeCounts[ i ] - expectedDifference;
         }
+        StatsTuple expectedStats = new StatsTuple( null, expectedCounts );
+
+        compareStats( expectedStats, afterStopping );
         
-        // now start management again and have the same MBeans.
+        // now start management again and have the original MBeans.
         invokeOperation(mbean, "startManagement");
         assertBooleanAttribute(true, mbean, "ManagementActive");
-        assertEquals(derbyMbeanCount, getDerbyDomainMBeans().size());
+        
+        StatsTuple afterRestarting = getCurrentStats( "After Restarting" );
+
+        compareStats( originalStats, afterRestarting );
+    }
+
+    /**
+     * Get information on the current MBeans.
+     */
+    private StatsTuple getCurrentStats( String tag ) throws Exception
+    {
+        Set<ObjectName> beanNames = getDerbyDomainMBeans();
+        StatsTuple retval = new StatsTuple( beanNames, countMBeanTypes( beanNames ) );
+
+        println( tag + " bean count = " + retval.getBeanCount() );
+
+        return retval;
+    }
+
+
+    /**
+     * Verify that the mbean information is what we expect.
+     */
+    private void compareStats( StatsTuple expected, StatsTuple actual ) throws Exception
+    {
+        assertEquals( expected.getBeanCount(), actual.getBeanCount() );
+
+        for ( int i = 0; i < MBEAN_TYPES.length; i++ )
+        {
+            assertEquals( MBEAN_TYPES[ i ], expected.typeCounts[ i ], actual.typeCounts[
i ] );
+        }
+    }
+
+    /**
+     * Count the number of MBeans per type.
+     */
+    private int[] countMBeanTypes( Set<ObjectName> names ) throws Exception
+    {
+        int[] retval = new int[ MBEAN_TYPES.length ];
+
+        for (ObjectName name : names)
+        {
+            String beanType = name.getKeyProperty("type");
+
+            for ( int i = 0; i < MBEAN_TYPES.length; i++ )
+            {
+                if ( MBEAN_TYPES[ i ].equals( beanType ) ) { retval[ i ]++; }
+            }
+        }
+
+        return retval;
     }
+
+    private static final class StatsTuple
+    {
+        Set<ObjectName> beanNames;
+        int[]  typeCounts;
+
+        public StatsTuple( Set<ObjectName> beanNames, int[] typeCounts )
+        {
+            this.beanNames = beanNames;
+            this.typeCounts = typeCounts;
+        }
+
+        public int getBeanCount()
+        {
+            int total = 0;
+
+            for ( int i = 0; i < typeCounts.length; i++ ) { total += typeCounts[ i ];
}
+
+            return total;
+        }
+    }
+
 }



Mime
View raw message