001    // jademx - JADE management using JMX
002    // Copyright 2005-2006 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.unit;
019    
020    
021    import jade.jademx.JadeMXSuiteTest;
022    import jade.jademx.config.jademx.onto.ConfigAgentSpecifier;
023    import jade.jademx.config.jademx.onto.ConfigPlatform;
024    import jade.jademx.config.jademx.onto.ConfigRuntime;
025    import jade.jademx.config.jademx.onto.JademxConfig;
026    import jade.jademx.mbean.JadeFactory;
027    import jade.jademx.mbean.JadeRuntimeMBean;
028    import jade.jademx.server.JadeMXServer;
029    import jade.jademx.server.JadeMXServerFactory;
030    import jade.jademx.util.ThrowableUtil;
031    import jade.util.Logger;
032    
033    import javax.management.MBeanServer;
034    import javax.management.ObjectName;
035    
036    import junit.framework.Test;
037    import junit.framework.TestCase;
038    import junit.framework.TestSuite;
039    
040    /** 
041     * test the null agent
042     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
043     *  >Caboodle Networks, Inc.</a>
044     */
045    public class NullAgentTest extends TestCase {
046    
047        /** my logger */
048        private final Logger logger = 
049            Logger.getMyLogger(NullAgentTest.class.getName());
050        
051        // original inject message before AIDs localized
052        //  "(REQUEST"+
053        //  " :sender  ( agent-identifier :name pinger@diamond:1099/JADE )"+
054        //  " :receiver  (set ( agent-identifier :name pingee@diamond:1099/JADE ) )"+
055        //  " :content  \"((action (agent-identifier :name pingee@diamond:1099/JADE) (ping)))\" "+
056        //  " :reply-with  ping1  :language  fipa-sl  :ontology  ping  :protocol  fipa-request"+
057        //  " :conversation-id  ping-conv-1127942689278-18131271 )";
058    
059    //    /** reply-id used in message to inject */
060    //    private static final String INJECT_REPLY_ID = "pinger@diamond:1099/JADE1127942689278";
061    //    /** AID for pingee agent in message to inject */
062    //    private static final String PINGEE_INJECT_AID = "pingee@diamond:1099/JADE";
063    //    /** AID for pinger agent in message to inject */
064    //    private static final String PINGER_INJECT_AID = "pinger@diamond:1099/JADE";
065    //    /** local name for pingee agent */
066    //    private static final String PINGEE_LOCAL_NAME = "pingee";
067    //    
068    //    /** message to inject */
069    //    private final static String injectMsg = 
070    //        "(REQUEST"+
071    //        " :sender  ( agent-identifier :name pinger )"+
072    //        " :receiver  (set ( agent-identifier :name pingee ) )"+
073    //        " :content  \"((action (agent-identifier :name pingee@diamond:1099/JADE) (ping)))\" "+
074    //        " :reply-with  ping1  :language  fipa-sl  :ontology  ping  :protocol  fipa-request"+
075    //        " :conversation-id  ping-conv-1127942689278-18131271 )";
076    //    
077    //    /** message to expect */
078    //    private final static String expectMsg = 
079    //        "(INFORM"+
080    //        " :sender  ( agent-identifier :name pingee@diamond:1099/JADE )"+
081    //        " :receiver  (set ( agent-identifier :name pinger@diamond:1099/JADE ) )"+
082    //        " :content  \"((result (action (agent-identifier :name pingee@diamond:1099/JADE) (ping)) pong))\" "+
083    //        " :reply-with  pinger@diamond:1099/JADE1127942689278  :in-reply-to  ping1  "+
084    //        ":language  fipa-sl  :ontology  ping  :protocol  fipa-request"+
085    //        " :conversation-id  ping-conv-1127942689278-18131271 )";
086        
087        // SETUP/TEARDOWN
088        
089        /** MBeanServer being used */
090        private MBeanServer mBeanServer = null;
091        /** created JadeRuntimeMBean */
092        private JadeRuntimeMBean runtime = null;
093        /** ObjectName for JadeRuntimeMBean */
094        private ObjectName runtimeON = null;
095        
096        
097        /* (non-Javadoc)
098         * @see junit.framework.TestCase#setUp()
099         */
100        protected void setUp() throws Exception {
101            // create a jademx config of one ping agent
102            JademxConfig jademxConfig = buildNullAgentCfg();
103            // start the configuration
104            // first get JadeMXServer
105            JadeMXServer jadeMXServer = null;
106            try {
107                jadeMXServer = JadeMXServerFactory.jadeMXServerBySysProp();
108            } 
109            catch (Exception e) {
110                fail(ThrowableUtil.errMsg("problem getting jademx server",e));
111            }
112            mBeanServer = jadeMXServer.getMBeanServer();
113            // now get a factory to use
114            JadeFactory jadeFactory = new JadeFactory( jadeMXServer );
115            // instantiate the configuration
116            try {
117                runtime = jadeFactory.instantiateRuntime( jademxConfig );
118            } 
119            catch (Exception e) {
120                fail(ThrowableUtil.errMsg("problem instantiating configuration",e));
121            }
122            // get ObjectName for runtime
123            runtimeON = runtime.getObjectName();
124            logger.log( Logger.FINE,"runtimeON:"+runtimeON);
125        }
126        
127        /* (non-Javadoc)
128         * @see junit.framework.TestCase#tearDown()
129         */
130        protected void tearDown() throws Exception {
131            // shutdown the configuration
132            try {
133                mBeanServer.invoke( runtimeON, 
134                        JadeRuntimeMBean.OPER_SHUTDOWN, 
135                        new Object[]{},
136                        JadeRuntimeMBean.OPER_SHUTDOWN_SIGNATURE );
137            }
138            catch ( Exception e ) {
139                fail(ThrowableUtil.errMsg("problem shutting down JADE runtime", e));
140            }
141        }
142        
143        /**
144         * make a configuration for testing use programmatically
145         */
146        private JademxConfig buildNullAgentCfg() {
147            JademxConfig jademxConfig = new JademxConfig();
148            ConfigRuntime runtime = new ConfigRuntime();
149            jademxConfig.addRuntime( runtime );
150            ConfigPlatform platform = new ConfigPlatform();
151            runtime.addPlatform( platform );
152            platform.addOption( "port=1917" );
153            platform.addOption( "nomtp=true" );
154            ConfigAgentSpecifier agentSpecifier;
155            agentSpecifier =
156                new ConfigAgentSpecifier(
157                        "null",
158                        "jade.jademx.unit.NullAgent" );
159            platform.addAgentSpecifier( agentSpecifier );
160            logger.log( Logger.FINE,"jademxconfig:"+jademxConfig);
161            return jademxConfig;
162        }
163        
164        // TESTS
165        
166        /** test unit testing with calls via MBeanServer */
167        public void testNullAgent() {
168            // setUp and tearDown started and stopped null agent
169            assertTrue(true);
170    
171        }
172        
173        // suite
174    
175        /**
176         * return the implicit suite of tests
177         * @return the implicit suite of tests
178         */
179        public static Test suite() {
180            return new TestSuite( 
181                    NullAgentTest.class, 
182                    JadeMXSuiteTest.nameWithClass( NullAgentTest.class, 
183                    "testing null agent") );
184        }
185        
186    }