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.agent;
019    
020    import jade.jademx.JadeMXSuiteTest;
021    import jade.jademx.config.jademx.onto.ConfigAgentSpecifier;
022    import jade.jademx.config.jademx.onto.ConfigPlatform;
023    import jade.jademx.config.jademx.onto.ConfigRuntime;
024    import jade.jademx.config.jademx.onto.JademxConfig;
025    import jade.jademx.mbean.JadeFactory;
026    import jade.jademx.mbean.JadeRuntimeMBean;
027    import jade.jademx.server.JadeMXServer;
028    import jade.jademx.server.JadeMXServerFactory;
029    import jade.jademx.util.ThrowableUtil;
030    import jade.util.Logger;
031    
032    import javax.management.MBeanServer;
033    import javax.management.ObjectName;
034    
035    import junit.framework.Test;
036    import junit.framework.TestCase;
037    import junit.framework.TestSuite;
038    
039    /** 
040     * JademxAgentProxy test
041     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
042     *  >Caboodle Networks, Inc.</a>
043     */
044    public class JademxAgentProxyTest extends TestCase {
045    
046        /** my logger */
047        private final Logger logger = 
048            Logger.getMyLogger(JademxAgentProxyTest.class.getName());
049        
050        // original inject message before AIDs localized
051        //  "(REQUEST"+
052        //  " :sender  ( agent-identifier :name pinger@diamond:1099/JADE )"+
053        //  " :receiver  (set ( agent-identifier :name pingee@diamond:1099/JADE ) )"+
054        //  " :content  \"((action (agent-identifier :name pingee@diamond:1099/JADE) (ping)))\" "+
055        //  " :reply-with  ping1  :language  fipa-sl  :ontology  ping  :protocol  fipa-request"+
056        //  " :conversation-id  ping-conv-1127942689278-18131271 )";
057    
058    //    /** reply-id used in message to inject */
059    //    private static final String INJECT_REPLY_ID = "pinger@diamond:1099/JADE1127942689278";
060    //    /** AID for pingee agent in message to inject */
061    //    private static final String PINGEE_INJECT_AID = "pingee@diamond:1099/JADE";
062    //    /** AID for pinger agent in message to inject */
063    //    private static final String PINGER_INJECT_AID = "pinger@diamond:1099/JADE";
064        /** local name for pingee agent */
065        private static final String PINGEE_LOCAL_NAME = "pingee";
066        
067    //    /** message to inject */
068    //    private final static String injectMsg = 
069    //        "(REQUEST"+
070    //        " :sender  ( agent-identifier :name pinger )"+
071    //        " :receiver  (set ( agent-identifier :name pingee ) )"+
072    //        " :reply-to (set ( agent-identifier :name pinger ) )"+
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 = buildPingerPingeeAgentCfg();
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 buildPingerPingeeAgentCfg() {
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                        "pinger",
158                        "jade.jademx.agent.JademxNopAgent" );
159            platform.addAgentSpecifier( agentSpecifier );
160            agentSpecifier =
161                new ConfigAgentSpecifier(
162                        PINGEE_LOCAL_NAME,
163                        "jade.jademx.agent.JademxPingAgent" );
164            platform.addAgentSpecifier( agentSpecifier );
165            logger.log( Logger.FINE,"jademxconfig:"+jademxConfig);
166            return jademxConfig;
167        }
168        
169        // TESTS
170        
171        /**
172         * test null ObjectName to constructor
173         */
174        public void testConstructorNullObjectName() {
175            try {
176                new JademxPingAgentProxy( null, mBeanServer );
177                fail("missed null object name to proxy constructor");
178            }
179            catch ( IllegalArgumentException iae ) {
180                assertTrue(true);
181            }
182        }
183        
184        
185        /**
186         * test null MBeanServer to constructor
187         */
188        public void testConstructorNullMBeanServer() {
189            ObjectName on = null;
190            try {
191                on = new ObjectName("x:foo=bar");
192            }
193            catch ( Exception e ) {
194                fail(ThrowableUtil.errMsg("failed to create ObjectName", e ));
195            }
196            try {
197                new JademxPingAgentProxy( on, null );
198                fail("missed null MBean server to proxy constructor");
199            }
200            catch ( IllegalArgumentException iae ) {
201                assertTrue(true);
202            }
203        }
204    
205       
206        // suite
207    
208        /**
209         * return the implicit suite of tests
210         * @return the implicit suite of tests
211         */
212        public static Test suite() {
213            return new TestSuite( 
214                    JademxAgentProxyTest.class, 
215                    JadeMXSuiteTest.nameWithClass( JademxAgentProxyTest.class, 
216                    "testing JademxAgentProxy: proxy class for JademxAgent") );
217        }
218    
219    
220        
221    }