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.ObjectName;
021    
022    import jade.jademx.JadeMXSuiteTest;
023    import jade.jademx.agent.JademxPingAgent;
024    import jade.jademx.server.JadeMXServer;
025    import jade.jademx.server.JadeMXServerFactory;
026    import jade.wrapper.PlatformState;
027    import junit.framework.Test;
028    import junit.framework.TestCase;
029    import junit.framework.TestSuite;
030    
031    /** 
032     * JadePlatform MBean test
033     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
034     *  >Caboodle Networks, Inc.</a>
035     */
036    public class JadePlatformTest extends TestCase {
037        
038        ///** test(suite) name */
039        //private static String name = 
040        //    JadeMXSuiteTest.nameWithClass( 
041        //            JadePlatformTest.class, 
042        //                  "testing JadePlatform: JADE platform MBean class");
043        
044        
045        /** jadeMXServer we're using */
046        private JadeMXServer jadeMXServer = null;
047        
048        /** runtime for test use */
049        private JadeRuntime jadeRuntime = null;
050    
051        /** resource name for configuration of one empty platform */
052        private static final String EMPTY_PLATFORM_CONFIG_RESOURCE =
053            "jade/jademx/config/one-empty-platform.xml";
054        /** resource name for configuration of two ping agents */
055        private static final String TWO_PING_CONFIG_RESOURCE =
056            "jade/jademx/config/two-ping-agents.xml";
057        /** name for 1st ping agent */
058        private static final String AGENT_LOCAL_NAME_PINGER1 = "pinger1";
059        /** name for 2nd ping agent */
060        private static final String AGENT_LOCAL_NAME_PINGER2 = "pinger2";
061        /** intentionally non-existent agent name */
062        private static final String AGENT_LOCAL_NAME_BAD = "nosuchagent";
063        /** intentionally non-existent class name */
064        private static final String BAD_CLASS_NAME = "no.such.class.Name";
065        
066        // tests
067        
068        
069        /* (non-Javadoc)
070         * @see junit.framework.TestCase#setUp()
071         */
072        protected void setUp() throws Exception {
073            //System.err.println( ">>>starting test "+getName());
074            // first get JadeMXServer
075            jadeMXServer = JadeMXServerFactory.jadeMXServerBySysProp();
076            // now get a factory to use
077            JadeFactory jadeFactory = 
078                new JadeFactory( jadeMXServer );
079            jadeRuntime = (JadeRuntime)jadeFactory.runtimeInstance();
080            //JadeMXSuiteTest.listMBeans(mBeanServer, "at end of setUp()");
081        }
082        
083        
084        
085        /* (non-Javadoc)
086         * @see junit.framework.TestCase#tearDown()
087         */
088        protected void tearDown() throws Exception {
089            //JadeMXSuiteTest.listMBeans(mBeanServer,
090            //"at start of JadePlatformTest.tearDown()");
091            if ( null != jadeRuntime ) {
092                jadeRuntime.shutdown();
093            }
094            //JadeMXSuiteTest.listMBeans( 
095            //        jadeMXServer.getMBeanServer(),
096            //          "at end of JadePlatformTest.tearDown() for test "+getName());
097        }
098        
099        /**
100         * test getting platform's runtime
101         * @throws JademxException some unexpected error
102         */
103        public void testGetJadeRuntime() throws JademxException {
104            JadePlatform platformMBeans[] = null;
105            int platformCount = 0;
106            platformMBeans = jadeRuntime.platformsFromConfigResource( 
107                    EMPTY_PLATFORM_CONFIG_RESOURCE );
108            // should be exactly one platform returned
109            assertNotNull( 
110                    "JadeRuntime.platformsFromConfigResources()"+
111                    " returned null", 
112                    platformMBeans );
113            platformCount = platformMBeans.length;
114            assertEquals( "expected exactly one platform to be returned",
115                    1, platformCount);
116            JadeRuntime jr = platformMBeans[0].getJadeRuntime();
117            assertEquals( jadeRuntime, jr );
118        }
119        
120        /**
121         * test getting thread information about a platform
122         * @throws JademxException some unexpected error
123         */
124        public void testThreadInfo() throws JademxException {
125            JadePlatform platformMBeans[] = null;
126            int platformCount = 0;
127            String DEFAULT_THREAD_GROUP_NAME = "JADE runtime";
128            String DEFAULT_FIRST_PLATFORM_THREAD_NAME = "JADE platform 0";
129            platformMBeans = jadeRuntime.platformsFromConfigResource( 
130                    EMPTY_PLATFORM_CONFIG_RESOURCE );
131            // should be exactly one platform returned
132            assertNotNull( 
133                    "JadeRuntime.platformsFromConfigResources()"+
134                    " returned null", 
135                    platformMBeans );
136            platformCount = platformMBeans.length;
137            assertEquals( "expected exactly one platform to be returned",
138                    1, platformCount);
139            String threadGroupName = platformMBeans[0].getThreadGroupName();
140            assertEquals( "expected thread group name \""+
141                    DEFAULT_THREAD_GROUP_NAME+"\""+" but got \""+threadGroupName+
142                    "\"", DEFAULT_THREAD_GROUP_NAME, threadGroupName);
143            long threadId = platformMBeans[0].getThreadId();
144            assertTrue( "expected non-negative threadId, but got"+threadId,
145                    (threadId >= 0) );
146            String threadName = platformMBeans[0].getThreadName();
147            assertEquals( "expected thread name \""+
148                    DEFAULT_FIRST_PLATFORM_THREAD_NAME+"\""+" but got \""+
149                    threadName+
150                    "\"", DEFAULT_FIRST_PLATFORM_THREAD_NAME, threadName);
151        }
152        
153        /**
154         * test that platform begins life in ready state
155         * @throws JademxException some unexpected error
156         */
157        public void testPlatformInitialState() throws JademxException {
158            JadePlatform platformMBeans[] = null;
159            String PLATFORM_READY_STATE_NAME = "Ready";
160            platformMBeans = jadeRuntime.platformsFromConfigResource( 
161                    EMPTY_PLATFORM_CONFIG_RESOURCE );
162            // should be exactly one platform returned
163            assertNotNull( 
164                    "JadeRuntime.platformsFromConfigResource()"+
165                    " returned null", 
166                    platformMBeans );
167            int platformCount = platformMBeans.length;
168            assertEquals( "expected exactly one platform to be returned",
169                    1, platformCount);        
170            int stateCode = platformMBeans[0].getStateCode();
171            assertEquals( "expected initial state code "+
172                    PlatformState.cPLATFORM_STATE_READY+
173                    " but got "+stateCode,
174                    PlatformState.cPLATFORM_STATE_READY, stateCode);
175            String stateName = platformMBeans[0].getStateName();
176            assertEquals( "expected initial state name "+
177                    PLATFORM_READY_STATE_NAME+
178                    " but got "+stateName,
179                    PLATFORM_READY_STATE_NAME, stateName);
180        }
181        
182        /**
183         * test agent name retrieval
184         * @throws JademxException some unexpected error
185         */
186        public void testAgentNameRetrieval() throws JademxException {
187    
188            JadePlatform platformMBean = null;
189            JadePlatform platformMBeans[] = 
190                jadeRuntime.platformsFromConfigResource( 
191                        TWO_PING_CONFIG_RESOURCE );
192            // should be exactly one platform returned
193            assertNotNull( 
194                    "JadeRuntime.platformsFromConfigResource()"+
195                    " returned null", 
196                    platformMBeans );
197            int platformCount = platformMBeans.length;
198            assertEquals( "expected exactly one platform to be returned",
199                    1, platformCount);
200            platformMBean = platformMBeans[0];
201    
202            // test JadePlatform.getAgentNames()
203            String agentNames[] = platformMBean.getAgentNames();
204            int agentCount = agentNames.length;
205            if ( 2 != agentCount ) {
206                for ( int i = 0; i < agentNames.length; i++ ) {
207                    System.err.println("agent "+i+": "+agentNames[i]);
208                }
209                assertTrue("expected 2 agent names but got back "+agentCount,
210                        (2==agentCount));
211            }
212            String agentName;
213            String typeProp = 
214                JadeBaseMBean.OBJ_NAME_KEY_TYPE + "=" + 
215                JadeAgentMBeanDef.TYPE;
216            
217            String nameProp =
218                JadeBaseMBean.OBJ_NAME_KEY_NAME + "=" + 
219                AGENT_LOCAL_NAME_PINGER1;
220            agentName = platformMBean.getAgentName( AGENT_LOCAL_NAME_PINGER1 );
221            assertNotNull( "got null looking for agent MBean for "+
222                    AGENT_LOCAL_NAME_PINGER1, agentName );
223            assertTrue( "string \""+typeProp+
224                    "\" not contained in agent MBean name \""+agentName+"\"",
225                    (agentName.indexOf(typeProp)>=0));
226            assertTrue( "string \""+nameProp+
227                    "\" not contained in agent MBean name \""+agentName+"\"",
228                    (agentName.indexOf(nameProp)>=0));
229            
230            nameProp =
231                JadeBaseMBean.OBJ_NAME_KEY_NAME + "=" + 
232                AGENT_LOCAL_NAME_PINGER2;
233            agentName = platformMBean.getAgentName( AGENT_LOCAL_NAME_PINGER2 );
234            assertNotNull( "got null looking for agent MBean for "+
235                    AGENT_LOCAL_NAME_PINGER2, agentName );
236            assertTrue( "string \""+typeProp+
237                    "\" not contained in agent MBean name \""+agentName+"\"",
238                    (agentName.indexOf(typeProp)>=0));
239            assertTrue( "string \""+nameProp+
240                    "\" not contained in agent MBean name \""+agentName+"\"",
241                    (agentName.indexOf(nameProp)>=0));
242    
243            try {
244                agentName = platformMBean.getAgentName( AGENT_LOCAL_NAME_BAD );
245                fail("calling JadePlatform.getAgentName(\""+AGENT_LOCAL_NAME_BAD+
246                        "\") should raise an exception");
247            }
248            catch ( JademxException e ) {
249                assertTrue(true);
250            }
251            
252        }
253        
254        /**
255         * test <code>JadePlatform.createNewAgentReturnName()</code>
256         * @throws JademxException some unexpected error
257         */
258        public void testAgentCreateReturnName() throws JademxException {
259            // create the platform
260            JadePlatform platformMBeans[] = jadeRuntime.platformsFromConfigResource( 
261                    EMPTY_PLATFORM_CONFIG_RESOURCE );
262            // test agent creation where it should work
263            String agentName = platformMBeans[0].createNewAgentReturnName( 
264                    AGENT_LOCAL_NAME_PINGER1, 
265                    JademxPingAgent.class.getName(), 
266                    new Object[0] );
267            String typeProp = 
268                JadeBaseMBean.OBJ_NAME_KEY_TYPE + "=" + 
269                JadeAgentMBeanDef.TYPE;
270            String nameProp =
271                JadeBaseMBean.OBJ_NAME_KEY_NAME + "=" + 
272                AGENT_LOCAL_NAME_PINGER1;
273            assertNotNull( "got null looking trying to create agent "+
274                    AGENT_LOCAL_NAME_PINGER1, agentName );
275            assertTrue( "string \""+typeProp+
276                    "\" not contained in agent MBean name \""+agentName+"\"",
277                    (agentName.indexOf(typeProp)>=0));
278            assertTrue( "string \""+nameProp+
279                    "\" not contained in agent MBean name \""+agentName+"\"",
280                    (agentName.indexOf(nameProp)>=0));
281            
282            ObjectName agentNames[] = platformMBeans[0].getAgentObjectNames();
283            assertEquals( agentName, agentNames[0].toString() );
284            
285            // test agent creation where it should fail: duplicate agent name
286            try {
287                agentName = platformMBeans[0].createNewAgentReturnName( 
288                        AGENT_LOCAL_NAME_PINGER1, 
289                        JademxPingAgent.class.getName(), 
290                        new Object[0] );
291                fail("calling JadePlatform.createNewAgentReturnName()"+
292                        " for existing agent name \""+AGENT_LOCAL_NAME_PINGER1+
293                        "\" should raise an exception");
294            }
295            catch ( JademxException e ) {
296                assertTrue(true);
297            }
298            
299            // test agent creation where it should fail: bad class name
300            try {
301                agentName = platformMBeans[0].createNewAgentReturnName( 
302                        AGENT_LOCAL_NAME_PINGER2, 
303                        BAD_CLASS_NAME, 
304                        new Object[0] );
305                fail("calling JadePlatform.createNewAgentReturnName()"+
306                        " with class name \""+BAD_CLASS_NAME+
307                        "\" should raise an exception");
308            }
309            catch ( JademxException e ) {
310                assertTrue(true);
311            }
312            
313        }
314    
315        //TODO: try testing JadeAgent with the ping agent
316        
317        // suite
318    
319        /**
320         * return the implicit suite of tests
321         * @return the implicit suite of tests
322         */
323        public static Test suite() {
324            return new TestSuite( 
325                    JadePlatformTest.class, 
326                    JadeMXSuiteTest.nameWithClass( JadePlatformTest.class, 
327                    "testing JadePlatform: jade platform MBean class") );
328        }
329    
330    
331        
332    }