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 jade.jademx.server.JadeMXServer;
021    import jade.jademx.server.JadeMXServerFactory;
022    import jade.jademx.util.ThrowableUtil;
023    import jade.jademx.JadeMXSuiteTest;
024    import junit.framework.Test;
025    import junit.framework.TestCase;
026    import junit.framework.TestSuite;
027    
028    /** 
029     * JadeRuntime MBean test
030     * @author David Bernstein, <a href="http://www.caboodlenetworks.com"
031     *  >Caboodle Networks, Inc.</a>
032     */
033    public class JadeFactoryTest extends TestCase {
034        
035        /** the JadeMXServer */
036        private JadeMXServer jadeMXServer = null;
037        
038        ///** test(suite) name */
039        //private static String name = 
040        //    JadeMXSuiteTest.nameWithClass( 
041        //            JadeRuntimeTest.class, 
042        //                  "testing JadeRuntime: JADE runtime MBean class");
043        
044        // tests
045        
046        
047        /* (non-Javadoc)
048         * @see junit.framework.TestCase#setUp()
049         */
050        protected void setUp() throws Exception {
051            // first get JadeMXServer
052            try {
053                jadeMXServer = JadeMXServerFactory.jadeMXServerBySysProp();
054            }
055            catch ( Exception e ) {
056                fail( ThrowableUtil.errMsg( 
057                        "problem creating JadeMXServer by system property", e) );
058            }
059            // now get its MBeanServer
060            //jadeMXServer = jadeMXServer.getMBeanServer();
061            //JadeMXSuiteTest.listMBeans(jadeMXServer,"at end of setup()");
062        }
063        
064        
065        
066        /* (non-Javadoc)
067         * @see junit.framework.TestCase#tearDown()
068         */
069        //protected void tearDown() throws Exception {
070            //JadeMXSuiteTest.listMBeans(jadeMXServer,"at end of tearDown()");
071        //}
072        
073        /**
074         * guts of testing a factory
075         * @param jadeFactory factory to test
076         * @param domain expected object name domain for factory
077         * @param runtimeName expected runtimeName for factory 
078         * @param threadGroupName expected threadGroupName for factory 
079         */
080        private void doIt( 
081                JadeFactory jadeFactory, String domain, 
082                String runtimeName, String threadGroupName ) {
083            //   verify runtime instance
084            JadeRuntimeMBean jadeRuntime = null;
085            try {
086                jadeRuntime = jadeFactory.runtimeInstance();
087                assertTrue( jadeRuntime + " not instanceof JadeRuntimeMBean", 
088                        jadeRuntime instanceof JadeRuntimeMBean );
089            }
090            catch ( Exception e ) {
091                fail( ThrowableUtil.errMsg( 
092                        "problem getting runtime instance from jade factory", e) );
093            }
094            //   verify object name domain
095            assertEquals( "returned JadeFactory object name domain \""+
096                    jadeFactory.getObjectNameDomain()+
097                    "\" not one expected: \""+domain+"\"", 
098                    domain, jadeFactory.getObjectNameDomain() );
099            //   verify runtime name
100            assertEquals( "returned JadeFactory runtime name \""+
101                    jadeFactory.getRuntimeName() +
102                    "\" not one expected: \""+runtimeName+"\"",
103                    runtimeName, jadeFactory.getRuntimeName() );
104            //   verify thread group name
105            assertEquals( "returned JadeFactory thread group name \""+
106                    jadeFactory.getThreadGroupName() +
107                    "\" not one expected: \""+threadGroupName+"\"",
108                    threadGroupName, jadeFactory.getThreadGroupName() );
109            // verify stringified object
110            String expectedStringified =
111                JadeFactory.class.getName() +
112                "[objectNameDomain="+domain+
113                ",threadGroupName="+threadGroupName+
114                ",runtimeName="+runtimeName+"]";
115            assertEquals( "returned JadeFactory.toString() \""+
116                    jadeFactory.toString() +
117                    "\" not one expected: \""+expectedStringified+"\"",
118                    expectedStringified, jadeFactory.toString() );
119            
120            // shutdown the runtime MBean
121            try {
122                jadeRuntime.shutdown();
123            }
124            catch ( Exception e) {
125                fail( ThrowableUtil.errMsg( 
126                        "problem shutting down JadeRuntime", e) );
127            }
128        }
129        
130        /** default name for thread group, must match that in JadeFactory */
131        private static final String THREAD_GROUP_DEFAULT_NAME =
132            "JADE runtime";
133        /** default name property for runtime instance, must match factory's */
134        private static final String RUNTIME_NAME_DEFAULT = "default";
135    
136        /**
137         * Test using default JadeFactory
138         */
139        public void testDefaultFactory() {
140            doIt( new JadeFactory( jadeMXServer ), 
141                  JadeFactory.OBJECT_NAME_DOMAIN_DEFAULT,
142                  RUNTIME_NAME_DEFAULT,
143                  THREAD_GROUP_DEFAULT_NAME );
144        }
145        
146        /**
147         * Test using JadeFactory specifying object domain
148         */
149        public void testFactorySpecifyingDomain() {
150            String objDomain = "FooObjDomain";
151            doIt( new JadeFactory( jadeMXServer, objDomain ), 
152                    objDomain, RUNTIME_NAME_DEFAULT, THREAD_GROUP_DEFAULT_NAME ); 
153        }
154        
155        /**
156         * Test using JadeFactory specifying object domain and thread group
157         */
158        public void testFactorySpecifyingDomainRuntime() {
159            String objDomain = "BarObjDomain";
160            String runtimeName = "QuuxRuntime";
161            doIt( new JadeFactory( jadeMXServer, objDomain, runtimeName ), 
162                    objDomain, runtimeName, THREAD_GROUP_DEFAULT_NAME ); 
163        }
164        /**
165         * Test using JadeFactory specifying object domain and thread group
166         */
167        public void testFactorySpecifyingDomainRuntimeThreadGroup() {
168            String objDomain = "BarObjDomain";
169            String runtimeName = "FooRuntime";
170            String threadGroupName = "BazThreadGroup";
171            doIt( new JadeFactory( jadeMXServer, objDomain, runtimeName, 
172                    threadGroupName ), 
173                    objDomain, runtimeName, threadGroupName ); 
174        }
175        
176        // suite
177    
178        /**
179         * return the implicit suite of tests
180         * @return the implicit suite of tests
181         */
182        public static Test suite() {
183            return new TestSuite( 
184                    JadeFactoryTest.class, 
185                    JadeMXSuiteTest.nameWithClass( JadeFactoryTest.class, 
186                    "testing JadeFactory: jade runtime MBean factory class") );
187        }
188    
189    
190        
191    }