001    // jademx - JADE management using JMX
002    // Copyright 2004-2005 Caboodle Networks, Inc.
003    //
004    // This library is free software; you can redistribute it and/or
005    // modify it under the terms of the GNU Lesser General Public
006    // License as published by the Free Software Foundation; either
007    // version 2.1 of the License, or (at your option) any later version.
008    //
009    // This library is distributed in the hope that it will be useful,
010    // but WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012    // Lesser General Public License for more details.
013    //
014    // You should have received a copy of the GNU Lesser General Public
015    // License along with this library; if not, write to the Free Software
016    // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
017    
018    package jade.jademx.util;
019    
020    import java.util.Arrays;
021    
022    import javax.management.MBeanAttributeInfo;
023    import javax.management.MBeanConstructorInfo;
024    import javax.management.MBeanInfo;
025    import javax.management.MBeanNotificationInfo;
026    import javax.management.MBeanOperationInfo;
027    import javax.management.MBeanParameterInfo;
028    import javax.management.Notification;
029    
030    import jade.jademx.JadeMXSuiteTest;
031    import junit.framework.Test;
032    import junit.framework.TestCase;
033    import junit.framework.TestSuite;
034    
035    /**
036     * test jade.jademx.util.ThrowableUtil
037     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
038     *  >Caboodle Networks, Inc.</a>
039     */
040    public class MBeanUtilTest extends TestCase {
041    
042        // attributes
043        /** arguments attribute name */
044        public static final String ATTR_ARGUMENTS = "Arguments";
045        MBeanAttributeInfo aI[] = new MBeanAttributeInfo[] {
046                new MBeanAttributeInfo( 
047                        ATTR_ARGUMENTS, 
048                        Object[].class.getName(), 
049                        "agent startup arguments",
050                        true, false, false )
051        };
052        
053        // constructors
054        MBeanConstructorInfo cI[] = new MBeanConstructorInfo[0];
055        
056        // operations
057        /** shutdown operation name */
058        public static final String OPER_FOO_NAME = "foo";
059        /** unitTestInject operation description */
060        private final static String OPER_FOO_DESC = 
061            "kill this agent";
062        /** signature for a kill operation */
063        public final static String OPER_FOO_SIGNATURE[] = { 
064            "bar",
065            "baz"
066        };
067        /** return type for unitTestInject operation */
068        public final static String OPER_FOO_TYPE = 
069            void.class.getName();
070        MBeanParameterInfo pIKill[] = new MBeanParameterInfo[] {
071                new MBeanParameterInfo("a", "bar", "desc1"),
072                new MBeanParameterInfo("b", "baz", "desc2")
073        };
074        
075        MBeanOperationInfo oI[] = new MBeanOperationInfo[] {
076                new MBeanOperationInfo( OPER_FOO_NAME, 
077                        OPER_FOO_DESC, 
078                        pIKill, 
079                        OPER_FOO_TYPE, 
080                        MBeanOperationInfo.ACTION )//,
081        };
082        
083        // notifications
084        
085        /** notification that unit test failed */
086        public final static String NOTIF_UNIT_TEST_FAILURE_NAME =
087            "unitTestFailure";
088        /** notification that unit test succeeded */
089        public final static String NOTIF_UNIT_TEST_SUCCESS_NAME =
090            "unitTestSuccess";
091        String notifications[] = {
092                NOTIF_UNIT_TEST_FAILURE_NAME, 
093                NOTIF_UNIT_TEST_SUCCESS_NAME 
094        };
095        
096        String myClassName = getClass().getName();
097        /** expected notification description */
098        String NOTIF_INFO_DESCRIPTION = 
099            "notification set for " + myClassName;
100        /** expected notifications */
101        MBeanNotificationInfo nI[] = new MBeanNotificationInfo[] {
102                new MBeanNotificationInfo(
103                        notifications,
104                        Notification.class.getName(),
105                        NOTIF_INFO_DESCRIPTION )
106        };
107        
108        /** expected MBeanInfo */
109        private MBeanInfo mbix = 
110            new MBeanInfo( "classname", "description", aI, cI, oI, nI );
111        
112        /**
113         * test signaturesEqual()
114         */
115        public void testSignaturesEqual() {
116            final String sig1[] = new String[] { "a", "b" };
117            final String sig2[] = new String[] { "a", "c" };
118            
119            assertTrue( MBeanUtil.signaturesEqual( null, null ) );
120            assertFalse( MBeanUtil.signaturesEqual( sig1, null ) );
121            assertFalse( MBeanUtil.signaturesEqual( null, sig1 ) );
122            assertTrue( MBeanUtil.signaturesEqual( sig1, sig1 ) );
123            assertFalse( MBeanUtil.signaturesEqual( sig1, sig2 ) );
124            
125        }
126        
127        /**
128         * test mergeMBeanInfo()
129         */
130        public void testMergeMBeanInfo() {
131            final String CLASSNAME = "c";
132            final String DESCRIPTION = "d";
133            
134            MBeanInfo mbi;
135    
136            mbi = MBeanUtil.mergeMBeanInfo( CLASSNAME, DESCRIPTION, null, null );
137            assertEquals( CLASSNAME, mbi.getClassName() );
138            assertEquals( DESCRIPTION, mbi.getDescription() );
139            assertEquals( 0, mbi.getAttributes().length );
140            assertEquals( 0, mbi.getConstructors().length );
141            assertEquals( 0, mbi.getOperations().length );
142            assertEquals( 0, mbi.getNotifications().length );
143    
144            mbi = MBeanUtil.mergeMBeanInfo( CLASSNAME, DESCRIPTION, null, mbix );
145            assertEquals( CLASSNAME, mbi.getClassName() );
146            assertEquals( DESCRIPTION, mbi.getDescription() );
147            assertTrue( Arrays.equals( mbi.getAttributes(), mbix.getAttributes() ) );
148            assertTrue( Arrays.equals( mbi.getConstructors(), mbix.getConstructors() ) );
149            assertTrue( Arrays.equals( mbi.getOperations(), mbix.getOperations() ) );
150            assertTrue( Arrays.equals( mbi.getNotifications(), mbix.getNotifications() ) );
151            
152            mbi = MBeanUtil.mergeMBeanInfo( CLASSNAME, DESCRIPTION, mbix, null );
153            assertEquals( CLASSNAME, mbi.getClassName() );
154            assertEquals( DESCRIPTION, mbi.getDescription() );
155            assertTrue( Arrays.equals( mbi.getAttributes(), mbix.getAttributes() ) );
156            assertTrue( Arrays.equals( mbi.getConstructors(), mbix.getConstructors() ) );
157            assertTrue( Arrays.equals( mbi.getOperations(), mbix.getOperations() ) );
158            assertTrue( Arrays.equals( mbi.getNotifications(), mbix.getNotifications() ) );
159            
160        }
161        
162        /**
163         * test MBeanUtil.mBeanHasOper()
164         */
165        public void testMBeanHasOper() {
166            final String sig[] = new String[] { "bar", "quux" };
167            assertFalse( MBeanUtil.mBeanHasOper( mbix, OPER_FOO_NAME, sig ) );
168        }
169        
170        // suite
171        
172        /**
173         * return the implicit suite of tests
174         * @return the implicit suite of tests
175         */
176        public static Test suite() {
177            return new TestSuite( 
178                    MBeanUtilTest.class, 
179                    JadeMXSuiteTest.nameWithClass( MBeanUtilTest.class, 
180                    "testing MBeanUtil: MBean utilities") );
181        }
182        
183    
184    }