You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2009/08/25 18:10:26 UTC

svn commit: r807685 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java

Author: rhillegas
Date: Tue Aug 25 16:10:25 2009
New Revision: 807685

URL: http://svn.apache.org/viewvc?rev=807685&view=rev
Log:
DERBY-4356: Better insulate ManagementMBeanTest from environment left over from previous JUnit tests.

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java?rev=807685&r1=807684&r2=807685&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/management/ManagementMBeanTest.java Tue Aug 25 16:10:25 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;
+        }
+    }
+
 }