Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 97681 invoked from network); 25 Aug 2009 17:01:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 25 Aug 2009 17:01:06 -0000 Received: (qmail 44765 invoked by uid 500); 25 Aug 2009 17:01:32 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 44736 invoked by uid 500); 25 Aug 2009 17:01:31 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 44727 invoked by uid 99); 25 Aug 2009 17:01:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Aug 2009 17:01:31 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Aug 2009 17:01:30 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0A21923888D0; Tue, 25 Aug 2009 17:01:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: derby-commits@db.apache.org From: rhillegas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090825170110.0A21923888D0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ /** + *

* Test the ManagementMBean interface provided by Derby * which has two implementations. A built in one and * one that can be created by a user. + *

+ * + *

+ * 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. + *

*/ 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 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 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 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 beanNames; + int[] typeCounts; + + public StatsTuple( Set 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; + } + } + }