001    // jademx - JADE management using JMX
002    // Copyright 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.mbean;
019    
020    import javax.management.Attribute;
021    import javax.management.AttributeList;
022    import javax.management.MBeanException;
023    import javax.management.MBeanNotificationInfo;
024    import javax.management.MBeanServer;
025    import javax.management.ObjectName;
026    import javax.management.ReflectionException;
027    
028    import jade.jademx.JadeMXSuiteTest;
029    import jade.jademx.server.JadeMXServer;
030    import jade.jademx.server.JadeMXServerFactory;
031    import junit.framework.Test;
032    import junit.framework.TestCase;
033    import junit.framework.TestSuite;
034    
035    /** 
036     * JadeAgent MBean test
037     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
038     *  >Caboodle Networks, Inc.</a>
039     */
040    public class JadeBaseTest extends TestCase  {
041        
042        ///** test(suite) name */
043        //private static String name = 
044        //    JadeMXSuiteTest.nameWithClass( 
045        //            JadePlatformTest.class, 
046        //                  "testing JadeBase: JADE base MBean class");
047        
048        
049        /** jadeMXServer we're using */
050        private JadeMXServer jadeMXServer = null;
051        
052        /** ... */
053        private MBeanServer mBeanServer = null;
054        
055    //    /** runtime for test use */
056    //    private JadeRuntime jadeRuntime = null;
057    //
058    //    /** mbean for platform under test */
059    //    JadePlatform platformMBean = null;
060    
061        ///** resource name for configuration of one empty platform */
062        //private static final String EMPTY_PLATFORM_CONFIG_RESOURCE =
063        //    "jade/jademx/config/one-empty-platform.xml";
064        ///** resource name for configuration of two ping agents */
065        //private static final String TWO_PING_CONFIG_RESOURCE =
066        //    "jade/jademx/config/two-ping-agents.xml";
067        ///** name for 1st ping agent */
068        //private static final String AGENT_LOCAL_NAME_PINGER1 = "pinger1";
069        ///** name for 2nd ping agent */
070        //private static final String AGENT_LOCAL_NAME_PINGER2 = "pinger2";
071        
072        /** instantiated version of JadeBase */
073        private JadeNil jadeNil = null;
074    
075        //
076        
077        /** 
078         * a class for testing bare JadeBAse
079         */
080        private class JadeNil extends JadeBase {
081    
082            /**
083             * make a JadeNil
084             * @param jadeFactory jade factory to use
085             * @param name name for MBean
086             */
087            public JadeNil(JadeFactory jadeFactory, String name) {
088                super(jadeFactory, "nil", name);
089            }
090            
091            /**
092             * call <code>setNotificationInfo(null)</code>
093             */
094            public void setNullNotificationInfo() {
095                setNotificationInfo(null);
096            }
097            /**
098             * call <code>setNotificationInfo()</code> with empty array
099             */
100            public void setEmptyNotificationInfo() {
101                setNotificationInfo( new MBeanNotificationInfo[0]);
102            }        
103            /**
104             * call <code>addNotificationInfo()</code> twice
105             */
106            public void addNullNotificationInfoTwice() {
107                addNotificationInfo( null );
108                addNotificationInfo( null );
109            }  
110        }
111    
112        
113        // tests
114        
115        
116        /* (non-Javadoc)
117         * @see junit.framework.TestCase#setUp()
118         */
119        protected void setUp() throws Exception {
120            // first get JadeMXServer
121            jadeMXServer = JadeMXServerFactory.jadeMXServerBySysProp();
122            // now get a factory to use
123            JadeFactory jadeFactory = 
124                new JadeFactory( jadeMXServer );
125            mBeanServer = jadeMXServer.getMBeanServer();
126            jadeNil = new JadeNil( jadeFactory, "naught");
127    //        jadeRuntime = (JadeRuntime)jadeFactory.runtimeInstance();
128    //        //JadeMXSuiteTest.listMBeans(mBeanServer, "at end of setUp()");
129    //        JadePlatform platformMBeans[] = 
130    //            jadeRuntime.platformsFromConfigResource( 
131    //                    TWO_PING_CONFIG_RESOURCE );
132    //        platformMBean = platformMBeans[0];
133    
134        }
135        
136        
137        
138        /* (non-Javadoc)
139         * @see junit.framework.TestCase#tearDown()
140         */
141        protected void tearDown() throws Exception {
142            //JadeMXSuiteTest.listMBeans(mBeanServer,
143            //"at start of JadePlatformTest.tearDown()");
144    //        if ( null != jadeRuntime ) {
145    //            jadeRuntime.shutdown();
146    //        }
147            //JadeMXSuiteTest.listMBeans(mBeanServer,
148            //"at end of JadePlatformTest.tearDown()");
149        }
150    
151        
152        /**
153         * test <code>JadeBase.preRegister()</code>
154         * @throws JademxException unexpected problems with jademx
155         */
156        public void testJadeBasePreRegister() throws Exception  {
157            ObjectName on = jadeNil.preRegister( mBeanServer, null );
158            assertNotNull("null ObjectName from JadeBase.preRegister()",on);
159        }
160        
161        /**
162         * exercise <code>JadeBase.setNotificationInfo(null)</code>
163         */
164        public void testJadeBaseSetNullNotificationInfo() {
165            jadeNil.setNullNotificationInfo();
166        }
167        
168        /**
169         * exercise <code>JadeBase.setNotificationInfo(null)</code>
170         */
171        public void testJadeBaseSetEmptyNotificationInfo() {
172            jadeNil.setNullNotificationInfo();
173        }    
174        
175        /**
176         * exercise <code>JadeBase.addNotificationInfo()</code>
177         */
178        public void testJadeBaseAddNullNotificationInfoTwice() {
179            jadeNil.addNullNotificationInfoTwice();
180        }    
181        
182        /**
183         * try to get non-existent attributes
184         */
185        public void testJadeBaseGetBadAttributes() {
186            try {
187                jadeNil.getAttributes( new String[] {"NoSuchAttr"});
188                fail("didn't get exception looking for non-existent attribute");
189            }
190            catch ( RuntimeException re ) {
191                assertTrue(true);
192            }
193            try {
194                jadeNil.getAttributes( 
195                        new String[] {JadeBaseMBean.ATTR_TYPE,"NoSuchAttr"});
196                fail("didn't get exception looking for non-existent "+
197                        "attribute after existing attribute");
198            }
199            catch ( RuntimeException re ) {
200                assertTrue(true);
201            }
202        }
203        
204        /**
205         * test getting mbean name when not registered
206         */
207        public void testJadeBaseUnregisteredToString() {
208            String expectedS = "unregistered jademx nil "+
209            "naught(jade.jademx.mbean.JadeFactory"+
210            "[objectNameDomain=jade,threadGroupName=JADE runtime,"+
211            "runtimeName=default])"; 
212            String s = jadeNil.toString();
213            assertEquals( "unexpected result from JadeBase.toString(), expected \""+
214                    expectedS+"\" but got \""+s+"\"",
215                    expectedS, s );
216        }
217        
218        /**
219         * try getting empty attribute list
220         */
221        public void testJadeBaseGetNoAttributes() {
222            AttributeList aList = jadeNil.getAttributes( new String[0]);
223            assertEquals("expected no attributes but got "+aList.size(),
224                    0,aList.size());
225        }
226    
227        /**
228         * try to set non-existent attributes
229         */
230        public void testJadeBaseSetBadAttributes() {
231            try {
232                AttributeList aList = new AttributeList( 1 );
233                aList.add( new Attribute("foo","bar"));
234                jadeNil.setAttributes( aList );
235                fail("didn't get exception trying to set non-existent attribute");
236            }
237            catch ( RuntimeException re ) {
238                assertTrue(true);
239            }
240        }   
241        
242        /**
243         * exercise <code>JadeBase.invoke()</code>
244         */
245        public void testJadeBaseInvoke() throws MBeanException {
246            try {
247                jadeNil.invoke("noSuchAction",new Object[0],new String[0]);
248                fail("didn't get exception invoking non-existent operation");
249            }
250            catch (ReflectionException e) {
251                assertTrue(true);
252            }
253        }
254        
255        // suite
256    
257        /**
258         * return the implicit suite of tests
259         * @return the implicit suite of tests
260         */
261        public static Test suite() {
262            return new TestSuite( 
263                    JadeBaseTest.class, 
264                    JadeMXSuiteTest.nameWithClass( JadeBaseTest.class, 
265                    "testing JadeBase: JADE base MBean class") );
266        }
267    
268    
269        
270    }