KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > test > CLISupportTester


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  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/test/CLISupportTester.java,v 1.3 2005/12/25 03:45:52 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:52 $
28  */

29  
30 package com.sun.cli.jmx.test;
31
32 // java imports
33
//
34
import java.lang.reflect.Array JavaDoc;
35
36 // RI imports
37
//
38
import javax.management.*;
39 import javax.management.remote.JMXServiceURL JavaDoc;
40
41 import com.sun.jmx.trace.Trace;
42
43 import com.sun.cli.util.stringifier.*;
44 import com.sun.cli.util.ClassUtil;
45
46 import com.sun.cli.jmx.support.CLISupportStrings;
47 import com.sun.cli.jmx.support.StandardAliases;
48 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
49 import com.sun.cli.jmx.support.InspectRequest;
50 import com.sun.cli.jmx.support.InspectResult;
51 import com.sun.cli.jmx.support.InvokeResult;
52 import com.sun.cli.jmx.support.ResultsForGetSet;
53 import com.sun.cli.jmx.support.InspectResultStringifier;
54 import com.sun.cli.jmx.support.ResultsForGetSetStringifier;
55 import com.sun.cli.jmx.support.InvokeResultStringifier;
56
57
58 public final class CLISupportTester
59 {
60     final MBeanServerConnection mServer;
61     final CLISupportMBeanProxy mProxy;
62     final TestLog mLog;
63     
64     final static private String JavaDoc ALIAS_BASE = "test-alias-";
65     private final static String JavaDoc CLI_TEST_ALIAS_NAME = ALIAS_BASE + "generic-test";
66
67         public
68     CLISupportTester( MBeanServerConnection conn, CLISupportMBeanProxy proxy )
69         throws Exception JavaDoc
70     {
71         this( conn, proxy, new StdOutTestLog() );
72     }
73     
74         public
75     CLISupportTester( MBeanServerConnection conn, CLISupportMBeanProxy proxy, TestLog logger)
76         throws Exception JavaDoc
77     {
78         mServer = conn;
79         mLog = logger;
80         
81         mProxy = proxy;
82     }
83
84         private void
85     p( Object JavaDoc arg )
86     {
87         mLog.println( arg );
88     }
89     
90         private void
91     p( Object JavaDoc arg, boolean newline)
92     {
93         if ( newline )
94         {
95             p( arg );
96         }
97         else
98         {
99             mLog.print( arg );
100         }
101     }
102     
103         private void
104     begin( String JavaDoc msg )
105     {
106         p( msg + "...", false);
107     }
108     
109         private static String JavaDoc
110     quote( Object JavaDoc str )
111     {
112         return( "\"" + str.toString() + "\"" );
113     }
114     
115
116         private void
117     TestMBeanList( CLISupportMBeanProxy test ) throws Exception JavaDoc
118     {
119         final String JavaDoc [] testStrings = new String JavaDoc [] { StandardAliases.ALL_ALIAS };
120         
121         final ObjectName [] results = test.mbeanFind( testStrings );
122         final int numObjects = Array.getLength( results );
123         
124         final ArrayStringifier testStringsStringifier = new ArrayStringifier( "," );
125         
126         p( "\nmbean-list results: " + numObjects +
127             " mbeans for expr " + testStringsStringifier.stringify( testStrings ) );
128         
129         final String JavaDoc str = ArrayStringifier.stringify( results, "\n", ObjectStringifier.DEFAULT);
130         p ( str );
131         
132         p( "TestMBeanList...DONE: listed " + numObjects + " for " + testStrings[ 0 ] );
133     }
134     
135         private void
136     TestMBeanInspect( CLISupportMBeanProxy test, String JavaDoc [] patterns ) throws Exception JavaDoc
137     {
138         begin( "TestMBeanInspect" );
139         
140         final InspectRequest request = new InspectRequest();
141         
142         final InspectResult [] results = test.mbeanInspect( request, patterns );
143         final int numResults = Array.getLength( results );
144         
145         final String JavaDoc summary = "" + numResults +
146                 " mbeans for expr " + ArrayStringifier.stringify( patterns, ",");
147                 
148         final String JavaDoc str = ArrayStringifier.stringify( results, "\n\n", new InspectResultStringifier());
149         
150         p( "DONE: inspected " + summary);
151     }
152     
153         private void
154     TestMBeanGet( CLISupportMBeanProxy test, String JavaDoc [] targets ) throws Exception JavaDoc
155     {
156         begin( "TestMBeanGet" );
157         
158         final ResultsForGetSet [] results = test.mbeanGet( "*", targets );
159         final int numResults = Array.getLength( results );
160         
161         final String JavaDoc summary = "" + numResults +
162                 " mbeans for expr " + ArrayStringifier.stringify( targets, ",");
163                 
164         final ResultsForGetSetStringifier resultStringifier =
165             new ResultsForGetSetStringifier( );
166             
167         final String JavaDoc str = ArrayStringifier.stringify( results, "\n\n", resultStringifier);
168         
169         p ( str );
170         
171         p( "DONE: inspected " + summary);
172     }
173     
174     
175         private String JavaDoc
176     InvokeResultsToString( InvokeResult [] results)
177     {
178         return( ArrayStringifier.stringify( results, "\n", new InvokeResultStringifier() ) );
179     }
180     
181     
182         private void
183     testInvoke( String JavaDoc operationName, String JavaDoc args, String JavaDoc [] targets )
184         throws Exception JavaDoc
185     {
186         InvokeResult [] results = null;
187         
188         results = mProxy.mbeanInvoke( operationName, args, targets );
189         
190         p( InvokeResultsToString( results ) + "\n" );
191     }
192     
193         private void
194     TestNamedInvoke( CLISupportMBeanProxy test, String JavaDoc [] targets ) throws Exception JavaDoc
195     {
196         begin( "TestNamedInvoke" );
197         
198         testInvoke( "testNamed", "p1=hello", targets );
199         
200         testInvoke( "testNamed", "p1=hello,p2=there", targets );
201         
202         testInvoke( "testNamed", "p1=hello,p2=there,p3=!!!", targets );
203         
204         testInvoke( "testNamed", "p1=hello,p2=there,p3=!!!,p4=foobar", targets );
205         
206         p( "DONE ");
207     }
208
209
210         private void
211     TestMBeanInvoke( CLISupportMBeanProxy test, String JavaDoc [] targets ) throws Exception JavaDoc
212     {
213         TestNamedInvoke( test, targets );
214     }
215
216     
217     
218         private void
219     deleteTestAliases() throws Exception JavaDoc
220     {
221         final String JavaDoc [] aliases = mProxy.listAliases( false );
222         
223         for( int i = 0; i < aliases.length; ++i )
224         {
225             final String JavaDoc name = aliases[ i ];
226             
227             if ( name.startsWith( ALIAS_BASE ) )
228             {
229                 p( "deleteTestAliases: deleting: " + name );
230                 mProxy.deleteAlias( name );
231             }
232         }
233     }
234     
235         private void
236     TestAliases( ) throws Exception JavaDoc
237     {
238         begin( "TestAliases" );
239         
240         deleteTestAliases();
241         
242         int failureCount = 0;
243         
244         // create an alias for each MBean
245
final ObjectName [] names = mProxy.mbeanFind( new String JavaDoc [] { StandardAliases.ALL_ALIAS } );
246         final int numNames = Array.getLength( names );
247         
248         // create test alias for each existing MBean
249
for( int i = 0; i < numNames; ++i )
250         {
251             final String JavaDoc aliasName = ALIAS_BASE + (i+1);
252             mProxy.createAlias( aliasName, names[ i ].toString() );
253         }
254         
255         // now verify that each of them resolves correctly
256
for( int i = 0; i < numNames; ++i )
257         {
258             final String JavaDoc aliasName = ALIAS_BASE + (i+1);
259             
260             final String JavaDoc aliasValue = mProxy.resolveAlias( aliasName );
261             if ( aliasValue == null || ! names[ i ].toString().equals( aliasValue ))
262             {
263                 ++failureCount;
264                 p( "FAILURE: alias " + aliasName + ": " +
265                     quote( aliasValue ) + " != " + quote( names[ i ].toString() ) );
266             }
267         }
268         
269         // create an alias consisting of all aliases
270
final String JavaDoc ALL_ALIASES_NAME = ALIAS_BASE + "all";
271         final String JavaDoc [] aliases = mProxy.listAliases( false );
272         final String JavaDoc allAliases = ArrayStringifier.stringify( aliases, " " );
273         mProxy.createAlias( ALL_ALIASES_NAME, allAliases );
274         
275         // create a recursive alias
276
String JavaDoc allAliasesName = ALL_ALIASES_NAME;
277         for( int i = 0; i < 5; ++i )
278         {
279             mProxy.createAlias( allAliasesName + i, allAliasesName );
280             allAliasesName = allAliasesName + i;
281         }
282         
283         // verify that the alias to all of them produces the same set of names as we started with
284
final ObjectName [] resolvedNames = mProxy.resolveTargets( new String JavaDoc [] { allAliasesName } );
285         //p( "all aliases = " + ArrayStringifier.stringify( resolvedNames, "\n" ) );
286
if ( Array.getLength( resolvedNames ) != numNames )
287         {
288             ++failureCount;
289         }
290         
291         deleteTestAliases();
292         
293         if ( failureCount == 0 )
294         {
295             p( "DONE" );
296         }
297         else
298         {
299             p( "FAILURES = " + failureCount );
300         }
301     }
302     
303     /*
304         Convert the parameters to String suitable for consumption by the CLISupportMBean
305      */

306         private String JavaDoc
307     MakeArgList( final String JavaDoc [] args )
308     {
309         final int numArgs = Array.getLength( args );
310         String JavaDoc result = null;
311         
312         if ( numArgs != 0 )
313         {
314             final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
315         
316             for( int i = 0; i < numArgs; ++i )
317             {
318                 buf.append( args[ i ] );
319                 buf.append( "," );
320             }
321             // strip trailing ","
322
buf.setLength( buf.length() - 1 );
323             
324             result = new String JavaDoc( buf ) ;
325         }
326         
327         return( result );
328     }
329     
330     
331         private String JavaDoc
332     getCastType( String JavaDoc type )
333         throws ClassNotFoundException JavaDoc
334     {
335         String JavaDoc result = type;
336         
337         if ( ClassUtil.classnameIsArray( result ) )
338         {
339             final Class JavaDoc theClass = ClassUtil.getClassFromName(result);
340             
341             final Class JavaDoc elementClass = ClassUtil.getInnerArrayElementClass( theClass );
342             
343             result = elementClass.getName();
344         }
345         
346         return( result );
347     }
348     
349     
350     
351         private InvokeResult.ResultType
352     TestOperationGenerically(
353         final CLISupportMBeanProxy test,
354         final boolean namedArgs,
355         final ObjectName targetName,
356         final MBeanOperationInfo operationInfo )
357         throws Exception JavaDoc
358     {
359         final MBeanParameterInfo [] paramInfos = operationInfo.getSignature();
360         final int numParams = Array.getLength( paramInfos );
361         
362         final String JavaDoc [] strings = new String JavaDoc [ numParams ];
363         final String JavaDoc operationName = operationInfo.getName();
364         
365         // create an object of the correct type for each parameter.
366
// The actual value is not important.
367
for( int i = 0; i < numParams; ++i )
368         {
369             final MBeanParameterInfo paramInfo = paramInfos[ i ];
370             final String JavaDoc paramType = paramInfos[ i ].getType();
371             final Class JavaDoc theClass = ClassUtil.getClassFromName( paramType );
372             
373             final Object JavaDoc paramObject = ClassUtil.InstantiateDefault( theClass );
374             final String JavaDoc paramString = SmartStringifier.toString( paramObject );
375             final String JavaDoc castString = "(" + getCastType( paramType ) + ")";
376             
377             final String JavaDoc paramName = namedArgs ? (paramInfo.getName() + '=') : "";
378             
379             strings[ i ] = paramName + castString + paramString;
380         }
381         
382         // convert the arguments to strings
383
final String JavaDoc argString = MakeArgList( strings );
384         
385         final String JavaDoc [] args = new String JavaDoc [] { targetName.toString() };
386         
387         final InvokeResult [] results = (InvokeResult [])test.mbeanInvoke( operationName, argString, args );
388         final InvokeResult result = results[ 0 ];
389         
390         if ( result.getResultType() == InvokeResult.SUCCESS )
391         {
392             // p( "SUCCESS: " + operationName + "(" + SmartStringifier.toString( paramInfos ) + ")");
393
}
394         else
395         {
396             final String JavaDoc paramInfosString = SmartStringifier.toString( paramInfos );
397             
398             p( "FAILURE: " + operationName + "(" + paramInfosString + ")" +
399                 " with " + argString );
400             result.mThrowable.printStackTrace();
401         }
402         
403         return( result.getResultType() );
404     }
405     
406     static private final Class JavaDoc [] GENERICALLY_TESTABLE_CLASSES =
407     {
408         boolean.class,
409         char.class,
410         byte.class, short.class, int.class, long.class,
411         float.class, double.class,
412         Boolean JavaDoc.class,
413         Character JavaDoc.class,
414         Byte JavaDoc.class, Short JavaDoc.class, Integer JavaDoc.class, Long JavaDoc.class,
415         Float JavaDoc.class,
416         Double JavaDoc.class,
417         Number JavaDoc.class,
418         String JavaDoc.class,
419         Object JavaDoc.class,
420         java.math.BigDecimal JavaDoc.class,
421         java.math.BigInteger JavaDoc.class,
422         java.net.URL JavaDoc.class,
423         java.net.URI JavaDoc.class
424
425     };
426         private boolean
427     IsGenericallyTestableClass( final Class JavaDoc theClass )
428         throws ClassNotFoundException JavaDoc
429     {
430         boolean isTestable = false;
431         
432         Class JavaDoc testClass = theClass;
433         if ( ClassUtil.classIsArray( theClass ) )
434         {
435             // we can test all arrays of supported types
436
testClass = ClassUtil.getInnerArrayElementClass( theClass );
437         }
438         
439         final Class JavaDoc [] classes = GENERICALLY_TESTABLE_CLASSES;
440         final int numClasses = Array.getLength( classes );
441         for( int i = 0; i < numClasses; ++i )
442         {
443             if ( testClass == classes[ i ] )
444             {
445                 isTestable = true;
446                 break;
447             }
448         }
449         
450         if ( ! isTestable )
451         {
452             assert( testClass == java.util.Properties JavaDoc.class );
453         }
454         
455         return( isTestable );
456     }
457     
458         private boolean
459     IsGenericallyTestable( final MBeanOperationInfo operationInfo )
460         throws ClassNotFoundException JavaDoc
461     {
462         boolean isTestable = true;
463         
464         final MBeanParameterInfo [] paramInfos = operationInfo.getSignature();
465         final int numParams = Array.getLength( paramInfos );
466         for( int i = 0; i < numParams; ++i )
467         {
468             final Class JavaDoc theClass = ClassUtil.getClassFromName( paramInfos[i].getType() );
469             
470             if ( ! IsGenericallyTestableClass( theClass ) )
471             {
472                 isTestable = false;
473                 break;
474             }
475         }
476         
477         return( isTestable );
478     }
479     
480     
481         private void
482     TestGeneric( CLISupportMBeanProxy test, boolean namedTest, ObjectName objectName ) throws Exception JavaDoc
483     {
484         final MBeanInfo info = mServer.getMBeanInfo( objectName );
485         final MBeanOperationInfo [] opInfo = info.getOperations();
486         
487         begin( "TestGeneric" );
488         
489         int successCount = 0;
490         int failureCount = 0;
491         int notTestedCount = 0;
492         for( int i = 0; i < Array.getLength( opInfo ); ++i )
493         {
494             try
495             {
496                 if ( IsGenericallyTestable( opInfo[ i ] ) )
497                 {
498                     final InvokeResult.ResultType resultType = TestOperationGenerically( test,namedTest, objectName, opInfo[ i ] );
499                     if ( resultType == InvokeResult.SUCCESS )
500                     {
501                         ++successCount;
502                     }
503                     else
504                     {
505                         ++failureCount;
506                     }
507                 }
508                 else
509                 {
510                     ++notTestedCount;
511                 }
512             }
513             catch( Exception JavaDoc e )
514             {
515                 p( "FAILURE: " + SmartStringifier.toString( opInfo[ i ] ) );
516             }
517         }
518         
519         p( "DONE " + (namedTest ? "NAMED":"ORDERED") +
520             ": SUCCESSES = " + successCount +
521             ", FAILURES = " + failureCount +
522             ", NOT TESTABLE = " + notTestedCount );
523     }
524     
525         private void
526     TestGeneric( CLISupportMBeanProxy test, String JavaDoc [] targets ) throws Exception JavaDoc
527     {
528         final ObjectName [] allObjects = test.mbeanFind( targets );
529         
530         assert( allObjects.length >= 1 );
531         
532         for( int i = 0; i < allObjects.length; ++i )
533         {
534             TestGeneric( test, false, allObjects[ i ] );
535         }
536         
537         for( int i = 0; i < allObjects.length; ++i )
538         {
539             TestGeneric( test, true, allObjects[ i ] );
540         }
541     }
542     
543         private void
544     VerifySetup( CLISupportMBeanProxy proxy ) throws Exception JavaDoc
545     {
546         // must be at least one MBean
547
final ObjectName [] all = proxy.resolveTargets( new String JavaDoc [] { "*" } );
548         assert( all.length != 0 );
549         
550         // verify that the AliasMgr and CLI are available.
551
final String JavaDoc [] aliases = proxy.listAliases( false );
552         assert( aliases.length != 0 );
553         
554         // verify that required aliases are in place
555
assert( proxy.resolveAlias( StandardAliases.ALL_ALIAS ) != null );
556         assert( proxy.resolveAlias( StandardAliases.CLI_ALIAS ) != null );
557         assert( proxy.resolveAlias( StandardAliases.ALIAS_MGR_ALIAS ) != null );
558         
559     }
560     
561     
562         public void
563     Run() throws Exception JavaDoc
564     {
565         final CLISupportMBeanProxy proxy = mProxy;
566         
567         try
568         {
569             // ensure certain aliases are present, as we use them
570
final String JavaDoc [] all = new String JavaDoc [] { StandardAliases.ALL_ALIAS };
571         
572             VerifySetup( proxy );
573             
574             TestAliases( );
575             
576             TestMBeanList( proxy );
577             
578             TestMBeanGet( proxy, all );
579             
580             TestMBeanInspect( proxy, all );
581             
582             proxy.deleteAlias( CLI_TEST_ALIAS_NAME );
583             proxy.createAlias( CLI_TEST_ALIAS_NAME, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET );
584             final String JavaDoc [] testMBean = new String JavaDoc [] { CLI_TEST_ALIAS_NAME };
585             
586             TestMBeanInvoke( proxy, testMBean );
587             
588             TestGeneric( proxy, testMBean);
589             
590             
591             p( "DONE" );
592             
593                 
594         }
595         catch( Exception JavaDoc e )
596         {
597             e.printStackTrace();
598         }
599     }
600
601
602 };
603
604
605
Popular Tags