KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > TestRunner


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  */

26  
27 package com.sun.enterprise.management;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.Collections JavaDoc;
37
38 import javax.net.ssl.X509TrustManager;
39 import java.security.cert.Certificate JavaDoc;
40 import java.security.cert.CertificateException JavaDoc;
41 import java.security.KeyStoreException JavaDoc;
42 import java.security.NoSuchAlgorithmException JavaDoc;
43
44 import javax.management.MBeanServer JavaDoc;
45 import javax.management.MBeanServerConnection JavaDoc;
46 import javax.management.ObjectName JavaDoc;
47 import javax.management.MBeanInfo JavaDoc;
48 import javax.management.JMException JavaDoc;
49
50 import junit.extensions.ActiveTestSuite;
51 import junit.framework.TestSuite;
52
53 import com.sun.appserv.management.DomainRoot;
54 import com.sun.appserv.management.base.Util;
55 import com.sun.appserv.management.client.TrustStoreTrustManager;
56 import com.sun.appserv.management.client.ConnectionSource;
57 import com.sun.appserv.management.client.AppserverConnectionSource;
58 import com.sun.appserv.management.client.TLSParams;
59 import com.sun.appserv.management.client.HandshakeCompletedListenerImpl;
60 import com.sun.appserv.management.client.ProxyFactory;
61 import com.sun.appserv.management.util.jmx.ObjectNameComparator;
62 import com.sun.appserv.management.util.jmx.JMXUtil;
63
64 import com.sun.enterprise.management.util.jmx.JMXTestBase;
65 import com.sun.appserv.management.util.stringifier.SmartStringifier;
66
67
68 /**
69     Class that supports running all the unit tests for AMX
70  */

71 public final class TestRunner
72 {
73     final ConnectionSource mConn;
74     boolean mVerbose;
75     
76         public
77     TestRunner( final ConnectionSource conn )
78     {
79         mConn = conn;
80         mVerbose = false;
81     }
82
83     
84         public int
85     runSuite( String JavaDoc name, TestSuite suite )
86     {
87         System.out.println( "*** testing " + name + " ***");
88         
89         junit.textui.TestRunner runner = new junit.textui.TestRunner();
90         junit.framework.TestResult result = runner.doRun( suite, false );
91         
92         return( result.failureCount() );
93     }
94     
95          public int
96     testClass( final Class JavaDoc<junit.framework.TestCase> theClass )
97     {
98         final TestSuite suite = new TestSuite( theClass );
99         return( runSuite( theClass.getName(), suite ) );
100     }
101     
102     
103         public int
104     testClassThreaded( final Class JavaDoc<junit.framework.TestCase> theClass )
105     {
106         final TestSuite suite = new ActiveTestSuite( theClass );
107         
108         return( runSuite( theClass.getName(), suite ) );
109     }
110     
111     
112          public void
113     runTests( List JavaDoc<Class JavaDoc<junit.framework.TestCase>> testClasses, boolean threaded )
114         throws Exception JavaDoc
115     {
116         for( final Class JavaDoc<junit.framework.TestCase> theClass : testClasses )
117         {
118             final int failureCount = threaded ?
119                 testClassThreaded( theClass ) :
120                 testClass( theClass );
121                 
122             if ( failureCount != 0 )
123             {
124                 println( "Test " + theClass.getName() + " had failures: " + failureCount );
125             }
126         }
127     }
128     
129         public long
130     elapsed( final long start )
131     {
132         return( System.currentTimeMillis() - start );
133     }
134     
135         private long
136     testGetMBeanInfoSpeed(
137         final MBeanServerConnection JavaDoc conn,
138         final ObjectName JavaDoc[] objectNames )
139         throws IOException JavaDoc, JMException JavaDoc
140     {
141         // sorting provides consistent, ordered output
142
Arrays.sort( objectNames, ObjectNameComparator.INSTANCE );
143         
144         final long startAll = System.currentTimeMillis();
145         for( int i = 0; i < objectNames.length; ++i )
146         {
147             final ObjectName JavaDoc objectName = objectNames[ i ];
148             
149             final long start = System.currentTimeMillis();
150             
151             final MBeanInfo JavaDoc mbeanInfo = conn.getMBeanInfo( objectName );
152             
153             final long elapsed = elapsed( start );
154             
155             String JavaDoc id = objectName.toString();
156             String JavaDoc value;
157             
158             if ( (value = objectName.getKeyProperty( "type" )) != null )
159             {
160                 id = value;
161             }
162             else if ( (value = objectName.getKeyProperty( "j2eeType" )) != null )
163             {
164                 id = value;
165             }
166             
167             if ( (value = objectName.getKeyProperty( "name" )) != null )
168             {
169                 id = Util.concatenateProps( id, Util.makeNameProp( value ) );
170             }
171             
172             
173             //printVerbose( "GetMBeanInfo time for " + id + " = " + elapsed );
174
}
175         
176         final long elapsed = System.currentTimeMillis() - startAll;
177         return( elapsed );
178     }
179     
180     
181         protected void
182     printVerbose( final Object JavaDoc o )
183     {
184         if ( mVerbose )
185         {
186             println( o );
187         }
188     }
189     
190         private void
191     println( final Object JavaDoc o)
192     {
193         System.out.println( toString( o ) );
194     }
195     
196         private void
197     print( final Object JavaDoc o)
198     {
199         System.out.print( toString( o ) );
200     }
201     
202         private void
203     testGetMBeanInfoSpeed(
204         final MBeanServerConnection JavaDoc conn,
205         final String JavaDoc domain,
206         final String JavaDoc props )
207         throws IOException JavaDoc, JMException JavaDoc
208     {
209         final ObjectName JavaDoc pattern = Util.newObjectNamePattern( domain, props );
210         final Set JavaDoc<ObjectName JavaDoc> objectNameSet = JMXUtil.queryNames( conn, pattern, null);
211         
212         final ObjectName JavaDoc[] objectNames = new ObjectName JavaDoc[ objectNameSet.size() ];
213         objectNameSet.toArray( objectNames );
214         
215         final long elapsed = testGetMBeanInfoSpeed( conn, objectNames );
216         
217         println( "Time to getMBeanInfo on " + domain + ":" + props + " (" + objectNames.length + " MBeans)" +
218             " = " + elapsed + "ms" );
219     }
220     
221         public MBeanServerConnection JavaDoc
222     getMBeanServerConnection()
223         throws IOException JavaDoc
224     {
225         return( mConn == null ? null : mConn.getMBeanServerConnection( false ) );
226     }
227     
228         public void
229     testSpeed( )
230         throws IOException JavaDoc, JMException JavaDoc
231     {
232         final DomainRoot domainRootProxy = ProxyFactory.getInstance( mConn ).
233             createDomainRoot();
234         
235         final MBeanServerConnection JavaDoc conn = getMBeanServerConnection();
236         
237         testGetMBeanInfoSpeed( conn, Util.getObjectName( domainRootProxy ).getDomain(), JMXUtil.WILD_ALL );
238     }
239
240     
241     
242     /**
243         Comment in call to this for hard-coded test.
244      */

245         private void
246     testAppserverConnectionSource(
247         final String JavaDoc host,
248         final String JavaDoc user,
249         final String JavaDoc password )
250         throws IOException JavaDoc
251     {
252         MBeanServerConnection JavaDoc conn = null;
253         
254         final TestClientTrustStoreTrustManager tm = new TestClientTrustStoreTrustManager();
255         final HandshakeCompletedListenerImpl hcl = new HandshakeCompletedListenerImpl();
256         tm.setPrompt( true );
257         final TLSParams tlsParams =
258             new TLSParams( new X509TrustManager[] { tm }, hcl );
259             
260         println( "\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_RMI);
261             
262         final ConnectionSource rmiSource =
263             new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI,
264                 host, 8686, user, password, null);
265         conn = rmiSource.getMBeanServerConnection( true );
266         conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() );
267         
268         println( AppserverConnectionSource.PROTOCOL_RMI + " OK using " + rmiSource );
269         
270         
271         println( "\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_HTTP );
272         final Map JavaDoc<String JavaDoc,String JavaDoc> env = Collections.emptyMap();
273         
274         final ConnectionSource httpSource =
275             new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_HTTP,
276                 host, 1234, user, password, tlsParams, env);
277         conn = httpSource.getMBeanServerConnection( true );
278         assert conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() );
279     
280         println( AppserverConnectionSource.PROTOCOL_HTTP + " OK using " + httpSource );
281         
282     }
283     
284     
285         public static String JavaDoc
286     toString( Object JavaDoc o )
287     {
288         return( SmartStringifier.toString( o ) );
289     }
290     
291     /**
292         @param threaded if true, run the tests from each TestCase in separate threads
293         @param env arbitrary environment values for JMXTestBase
294      */

295         protected void
296     runAll(
297         final List JavaDoc<Class JavaDoc<junit.framework.TestCase>> testClasses,
298         final boolean threaded,
299         final Map JavaDoc<String JavaDoc,Object JavaDoc> env )
300         throws Exception JavaDoc
301     {
302         mVerbose = Boolean.valueOf( (String JavaDoc)env.get( PropertyKeys.VERBOSE_KEY ) ).booleanValue();
303         
304         //testSpeed();
305

306         // use the current connection which must contain the AMX MBeans
307
final MBeanServerConnection JavaDoc conn = getMBeanServerConnection();
308         
309         JMXTestBase.setGlobalConnection( conn );
310         JMXTestBase.setEnvValues( env );
311         
312         println( "\n--- " + testClasses.size() + " TEST CLASSES ---" );
313         
314         for( final Class JavaDoc<junit.framework.TestCase> theClass : testClasses )
315         {
316             println( theClass.getName() );
317         }
318         
319         println( "\n--- BEGIN TESTS ---" );
320         runTests( testClasses, threaded );
321     }
322 }
323
324
325
326
327
328
329
Popular Tags