KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > support > CLISupportMBeanImplTest


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/support/CLISupportMBeanImplTest.java,v 1.3 2005/12/25 03:45:46 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:46 $
28  */

29  
30 package com.sun.cli.jmx.support;
31
32 import java.lang.reflect.Array JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34
35 import java.math.BigDecimal JavaDoc;
36 import java.math.BigInteger JavaDoc;
37 import java.lang.Number JavaDoc;
38
39 import java.net.URL JavaDoc;
40 import java.net.URI JavaDoc;
41
42 import javax.management.*;
43
44 import com.sun.cli.util.ClassUtil;
45 import com.sun.cli.util.stringifier.ArrayStringifier;
46 import com.sun.cli.util.stringifier.SmartStringifier;
47
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.util.ClassUtil;
54 import com.sun.cli.util.ArrayConversion;
55
56 import com.sun.cli.jmx.test.CLISupportTestee;
57 import com.sun.cli.jmx.test.CLISupportSimpleTestee;
58
59
60 public final class CLISupportMBeanImplTest extends junit.framework.TestCase
61 {
62     MBeanServer mServer;
63     CLISupportMBeanProxy mProxy;
64     
65     private final static String JavaDoc ALIAS_BASE = "test-alias-";
66     private final static String JavaDoc CLI_TEST_ALIAS_NAME = ALIAS_BASE + "generic-test";
67
68
69     private final static String JavaDoc ALL_ALIAS = "all";
70     private final static String JavaDoc SIMPLE_TESTEE_ALIAS = "StdTestee";
71     private final static String JavaDoc SUPPORT_TESTEE_ALIAS = "SupportTestee";
72     
73     private final static String JavaDoc [] ALL_TARGETS = new String JavaDoc [] { ALL_ALIAS };
74     private final static String JavaDoc [] SIMPLE_TESTEE_TARGET = new String JavaDoc [] { SIMPLE_TESTEE_ALIAS };
75     private final static String JavaDoc [] SUPPORT_TESTEE_TARGET = new String JavaDoc [] { SUPPORT_TESTEE_ALIAS };
76
77     
78         public
79     CLISupportMBeanImplTest( )
80     {
81     }
82     
83     private class TestFailedException extends Exception JavaDoc
84     {
85         TestFailedException( String JavaDoc msg )
86         {
87             super( msg );
88         }
89     }
90     
91     
92         public void
93     testPropertiesOnly( ) throws Exception JavaDoc
94     {
95         final String JavaDoc operationName = "testPropertiesOnly";
96         final String JavaDoc argString = "foo=bar";
97         final InvokeResult [] results = (InvokeResult [])
98             mProxy.mbeanInvoke( operationName, argString, SUPPORT_TESTEE_TARGET );
99             
100         final InvokeResult result = results[ 0 ];
101         
102         if ( result.getResultType() != InvokeResult.SUCCESS )
103         {
104             throw new TestFailedException( "invocation failed: " + operationName + "(" + argString + ")" );
105         }
106         
107     }
108
109     
110     private final static String JavaDoc ARGS_11 =
111     "hello,true,x,99,999,9999,999999,99.9999,999.999999,12345678910,12345678910.12345678910";
112     
113         public void
114     test11ObjectArgs( ) throws Exception JavaDoc
115     {
116         invokeSimpleTestee( "test11ObjectArgs", ARGS_11 );
117     }
118     
119         public void
120     test11MixedArgs( ) throws Exception JavaDoc
121     {
122         invokeSimpleTestee( "test11MixedArgs", ARGS_11 );
123     }
124     
125         private Object JavaDoc
126     doTest( final String JavaDoc operationName, final String JavaDoc arg )
127         throws Exception JavaDoc
128     {
129         final InvokeResult invokeResult = invokeSimpleTestee( operationName, arg );
130         
131         if ( invokeResult.getResultType() != InvokeResult.SUCCESS )
132         {
133             fail( "invocation failed for " + operationName + "(" + arg + ")" );
134         }
135         
136         return( invokeResult.mResult );
137     }
138     
139         private void
140     doStringTest( final String JavaDoc arg, final String JavaDoc expected)
141         throws Exception JavaDoc
142     {
143         final String JavaDoc result = (String JavaDoc)doTest( "testString", arg );
144         assertEquals( expected, result );
145     }
146     
147         private void
148     doObjectTest( final String JavaDoc arg, final Object JavaDoc expected)
149         throws Exception JavaDoc
150     {
151         final Object JavaDoc result = doTest( "testObject", arg );
152         assertEquals( expected, result );
153     }
154     
155         private void
156     doIntegerTest( final String JavaDoc arg, final Object JavaDoc expected)
157         throws Exception JavaDoc
158     {
159         final Integer JavaDoc result = (Integer JavaDoc)doTest( "testInteger", arg );
160         assertEquals( expected, result );
161     }
162     
163         private void
164     checkEqualArray( final Object JavaDoc [] expected, final Object JavaDoc [] actual )
165     {
166         assertEquals( expected.getClass(), actual.getClass() );
167         assertEquals( expected.length, actual.length );
168         
169         for( int i = 0; i < expected.length; ++i )
170         {
171             if ( expected[ i ] == null )
172             {
173                 assertEquals( null, actual[ i ] );
174             }
175             else
176             {
177                 assertEquals( expected[ i ].getClass(), actual[ i ].getClass() );
178             
179                 if ( ClassUtil.objectIsArray( expected[ i ] ) )
180                 {
181                     if ( ClassUtil.objectIsPrimitiveArray( expected[ i ] ) )
182                     {
183                         final Object JavaDoc [] e = ArrayConversion.toAppropriateType( expected[ i ] );
184                         final Object JavaDoc [] a = ArrayConversion.toAppropriateType( actual[ i ] );
185                         
186                         checkEqualArray( e, a );
187                     }
188                     else
189                     {
190                         checkEqualArray( (Object JavaDoc [])expected[ i ], (Object JavaDoc [])actual[ i ] );
191                     }
192                 }
193                 else
194                 {
195                     assertEquals( expected[ i ], actual[ i ] );
196                 }
197             }
198         }
199     }
200     
201         private void
202     doObjectArrayTest( final String JavaDoc arg, final Object JavaDoc [] expected)
203         throws Exception JavaDoc
204     {
205         final Object JavaDoc [] result = (Object JavaDoc [])doTest( "testObjectArray", arg );
206         checkEqualArray( expected, result );
207     }
208     
209     
210     
211         public void
212     testEmptyQuotedString( ) throws Exception JavaDoc
213     {
214         doStringTest( "\"\"", "");
215     }
216     
217         public void
218     testEmptyQuotedStringWithCast( ) throws Exception JavaDoc
219     {
220         doStringTest( "(String)\"\"", "");
221     }
222     
223         public void
224     testEmptyStringWithCast( ) throws Exception JavaDoc
225     {
226         doStringTest( "(String)", "");
227     }
228     
229         public void
230     testEscapedString( ) throws Exception JavaDoc
231     {
232         final String JavaDoc result = "\"\n\r\t\"";
233         
234         doStringTest( "\\\"\\n\\r\\t\\\"", result);
235     }
236     
237     
238         public void
239     testNumericStringNoCast( ) throws Exception JavaDoc
240     {
241         doStringTest( "10", "10");
242     }
243     
244         public void
245     testStringContaining_word_null( ) throws Exception JavaDoc
246     {
247         doStringTest( "\"null\"", "null");
248     }
249     
250         public void
251     testNullStringWithoutCast( ) throws Exception JavaDoc
252     {
253         doStringTest( "null", null);
254     }
255     
256         public void
257     testNullStringWithCast( ) throws Exception JavaDoc
258     {
259         doStringTest( "(String)null", null);
260     }
261     
262         public void
263     testNullObjectWithCast( ) throws Exception JavaDoc
264     {
265         doObjectTest( "(Object)null", null);
266     }
267     
268         public void
269     testNullObjectWithoutCast( ) throws Exception JavaDoc
270     {
271         doObjectTest( "null", null);
272     }
273     
274         public void
275     testStringAsObject( ) throws Exception JavaDoc
276     {
277         doObjectTest( "(Object)hello", "hello");
278     }
279     
280         public void
281     testNullIntegerWithCast( ) throws Exception JavaDoc
282     {
283         doIntegerTest( "(Integer)null", null);
284     }
285     
286         public void
287     testInteger( ) throws Exception JavaDoc
288     {
289         doIntegerTest( "(Integer)10", new Integer JavaDoc( 10 ));
290         doIntegerTest( "-10", new Integer JavaDoc( -10 ));
291     }
292     
293         public void
294     testNullIntegerWithoutCast( ) throws Exception JavaDoc
295     {
296         doIntegerTest( "null", null);
297     }
298     
299     
300         public void
301     testEmptyArray( ) throws Exception JavaDoc
302     {
303         doObjectArrayTest( "{}", new Object JavaDoc [ 0 ]);
304     }
305     
306         public void
307     testEmptyObjectArrayWithCast( ) throws Exception JavaDoc
308     {
309         doObjectArrayTest( "(Object){}", new Object JavaDoc [ 0 ]);
310         doObjectArrayTest( "(String){}", new String JavaDoc [ 0 ]);
311         doObjectArrayTest( "(Character){}", new Character JavaDoc [ 0 ]);
312         doObjectArrayTest( "(Boolean){}", new Boolean JavaDoc [ 0 ]);
313         doObjectArrayTest( "(Byte){}", new Byte JavaDoc [ 0 ]);
314         doObjectArrayTest( "(Short){}", new Short JavaDoc [ 0 ]);
315         doObjectArrayTest( "(Integer){}", new Integer JavaDoc [ 0 ]);
316         doObjectArrayTest( "(Long){}", new Long JavaDoc [ 0 ]);
317         doObjectArrayTest( "(Float){}", new Float JavaDoc [ 0 ]);
318         doObjectArrayTest( "(Double){}", new Double JavaDoc [ 0 ]);
319         doObjectArrayTest( "(BigInteger){}", new BigInteger JavaDoc [ 0 ]);
320         doObjectArrayTest( "(BigDecimal){}", new BigDecimal JavaDoc [ 0 ]);
321         doObjectArrayTest( "(Number){}", new Number JavaDoc [ 0 ]);
322     }
323     
324         public void
325     testArrayWithEmptyElements()
326         throws Exception JavaDoc
327     {
328         final String JavaDoc s = new String JavaDoc( "" );
329         
330         doObjectArrayTest( "(String){,,,}", new String JavaDoc [] { s,s,s,s } );
331         doObjectArrayTest( "{,,,}", new Object JavaDoc [] { s,s,s,s } );
332     }
333     
334         public void
335     testArrayWithNullElements()
336         throws Exception JavaDoc
337     {
338         doObjectArrayTest( "{null,null}", new Object JavaDoc [] { null, null } );
339         
340         doObjectArrayTest( "{(String)null,(Object)null}", new Object JavaDoc [] { null, null } );
341     }
342     
343     
344         public void
345     testArrayWithCastAndElementsWithCast()
346         throws Exception JavaDoc
347     {
348         doObjectArrayTest( "(Number){(BigInteger)0,(BigDecimal)10.0}",
349                 new Number JavaDoc [] { new BigInteger JavaDoc( "0" ), new BigDecimal JavaDoc( "10.0" ) } );
350         
351         // now see that incompatible casts are rejected
352
final String JavaDoc input = "(Number){(String)hello}";
353         
354         final InvokeResult invokeResult = invokeSimpleTestee( "testObjectArray", input );
355         if ( invokeResult.getResultType() == InvokeResult.SUCCESS )
356         {
357             fail( "expected this construct to fail: " + input );
358         }
359     }
360
361         public void
362     testMixedArrayWithCasts()
363         throws Exception JavaDoc
364     {
365         doObjectArrayTest(
366             "{(Character)c,(Boolean)true,(Byte)99," +
367             "(Short)-999,(Integer)0,(Long)99999," +
368             "(BigInteger)99999999999999999999999999," +
369             "(BigDecimal)123456789123456789.123456789123456789," +
370             "\"hello\",(String)hello,hello,}",
371                 new Object JavaDoc []
372                 {
373                     new Character JavaDoc( 'c' ),
374                     new Boolean JavaDoc( "true" ),
375                     new Byte JavaDoc( (byte)99 ),
376                     new Short JavaDoc( (short)-999 ),
377                     new Integer JavaDoc( 0 ),
378                     new Long JavaDoc( 99999 ),
379                     new BigInteger JavaDoc( "99999999999999999999999999" ),
380                     new BigDecimal JavaDoc( "123456789123456789.123456789123456789" ),
381                     "hello",
382                     "hello",
383                     "hello",
384                     "",
385                 }
386             );
387     }
388     
389         public void
390     testNullIllegalForSimpleType( ) throws Exception JavaDoc
391     {
392         try
393         {
394             final InvokeResult result = invokeSimpleTestee( "test_int", "null" );
395             if ( result.getResultType() == InvokeResult.SUCCESS )
396             {
397                 fail( "expected failure trying to pass 'null' for an int" );
398             }
399         }
400         catch( Exception JavaDoc e )
401         {
402         }
403     }
404     
405         public void
406     testCaseSensitivity()
407         throws Exception JavaDoc
408     {
409         InvokeResult result;
410         
411         result = invokeSimpleTestee( "testcasesensitivity1", null );
412         assertEquals( "testcasesensitivity1", result.mResult );
413         
414         result = invokeSimpleTestee( "testCASESENSITIVITY1", null );
415         assertEquals( "testCASESENSITIVITY1", result.mResult );
416         
417         result = invokeSimpleTestee( "testCaseSensitivity1", null );
418         assertEquals( "testCaseSensitivity1", result.mResult );
419         
420         try
421         {
422             result = invokeSimpleTestee( "testcaseSensitivity1", null );
423             if ( result.getResultType() == InvokeResult.SUCCESS )
424             {
425                 fail( "expected ambiguous match to prevent execution of method: " + result.mResult );
426             }
427         }
428         catch( Exception JavaDoc e )
429         {
430             // good, expected this
431
}
432     }
433     
434         public void
435     testCaseInsensitivity()
436         throws Exception JavaDoc
437     {
438         final InvokeResult result = invokeSimpleTestee( "testcasesensitivity2", null );
439         assertEquals( "testCaseSensitivity2", result.mResult );
440     }
441
442     
443         public void
444     testURL()
445         throws Exception JavaDoc
446     {
447         final URL JavaDoc input = new URL JavaDoc( "http://www.sun.com?foo=bar&bar=foo" );
448         final InvokeResult result = invokeSimpleTestee( "testURL", input.toString() );
449         assertEquals( input, result.mResult );
450     }
451     
452         public void
453     testURI()
454         throws Exception JavaDoc
455     {
456         final URI JavaDoc input = new URI JavaDoc( "service:jmx:jmxmp://localhost:" );
457         final InvokeResult result = invokeSimpleTestee( "testURI", input.toString() );
458         assertEquals( input, result.mResult );
459     }
460     
461     
462     
463     /*
464     
465     these are not yet implemented
466     
467         public void
468     testProperties1Arg( ) throws Exception
469     {
470         final String operationName = "testProperties1Arg";
471         final String argString = "p1=p1,foo=bar";
472         final InvokeResult [] results = (InvokeResult [])
473             mProxy.mbeanInvoke( operationName, argString, SUPPORT_TESTEE_TARGET );
474             
475         final InvokeResult result = results[ 0 ];
476         
477         if ( result.getResultType() != InvokeResult.SUCCESS )
478         {
479             result.mThrowable.printStackTrace();
480             fail( "invocation failed: " + operationName + "(" + argString + ")");
481         }
482         
483     }
484         public void
485     testProperties2Args( ) throws Exception
486     {
487         final String operationName = "testProperties2Args";
488         final String argString = "p1=p1,p3=3,foo=bar";
489         final InvokeResult [] results = (InvokeResult [])
490             mProxy.mbeanInvoke( operationName, argString, SUPPORT_TESTEE_TARGET );
491             
492         final InvokeResult result = results[ 0 ];
493         
494         if ( result.getResultType() != InvokeResult.SUCCESS )
495         {
496             fail( "invocation failed: " + operationName + "(" + argString + ")");
497         }
498     }
499     */

500     
501
502         private InvokeResult []
503     invoke( String JavaDoc operationName, String JavaDoc args, String JavaDoc [] targets )
504         throws Exception JavaDoc
505     {
506         final InvokeResult [] results = mProxy.mbeanInvoke( operationName, args, targets );
507         
508         return( results );
509     }
510     
511         private InvokeResult
512     invokeSupportTestee( String JavaDoc operationName, String JavaDoc args)
513         throws Exception JavaDoc
514     {
515         final InvokeResult [] results = invoke( operationName, args, SUPPORT_TESTEE_TARGET );
516         assert( results.length == 1 );
517         
518         return( results[ 0 ] );
519     }
520     
521         private InvokeResult
522     invokeSimpleTestee( String JavaDoc operationName, String JavaDoc args)
523         throws Exception JavaDoc
524     {
525         final InvokeResult [] results = invoke( operationName, args, SIMPLE_TESTEE_TARGET );
526         assert( results.length == 1 );
527         
528         return( results[ 0 ] );
529     }
530     
531         public void
532     testNamedInvoke( ) throws Exception JavaDoc
533     {
534         invokeSupportTestee( "testNamed", "p1=hello" );
535         
536         invokeSupportTestee( "testNamed", "p1=hello,p2=there" );
537         
538         invokeSupportTestee( "testNamed", "p1=hello,p2=there,p3=!!!" );
539         
540         invokeSupportTestee( "testNamed", "p1=hello,p2=there,p3=!!!,p4=foobar" );
541     }
542
543 //-------------------------------------------------------------------------------------------------
544

545         private String JavaDoc
546     makeArgList( final String JavaDoc [] args )
547     {
548         final int numArgs = args.length;
549         String JavaDoc result = null;
550         
551         if ( numArgs != 0 )
552         {
553             final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
554         
555             for( int i = 0; i < numArgs; ++i )
556             {
557                 buf.append( args[ i ] );
558                 buf.append( "," );
559             }
560             // strip trailing ","
561
buf.setLength( buf.length() - 1 );
562             
563             result = new String JavaDoc( buf ) ;
564         }
565         
566         return( result );
567     }
568     
569     
570         private String JavaDoc
571     getCastType( String JavaDoc type )
572         throws ClassNotFoundException JavaDoc
573     {
574         String JavaDoc result = type;
575         
576         if ( ClassUtil.classnameIsArray( result ) )
577         {
578             final Class JavaDoc theClass = ClassUtil.getClassFromName(result);
579             
580             final Class JavaDoc elementClass = ClassUtil.getInnerArrayElementClass( theClass );
581             
582             result = elementClass.getName();
583         }
584         
585         return( result );
586     }
587     
588     
589     
590         private InvokeResult.ResultType
591     testOperationGenerically(
592         final boolean namedArgs,
593         final ObjectName targetName,
594         final MBeanOperationInfo operationInfo )
595         throws Exception JavaDoc
596     {
597         final MBeanParameterInfo [] paramInfos = operationInfo.getSignature();
598         final int numParams = paramInfos.length;
599         
600         final String JavaDoc [] strings = new String JavaDoc [ numParams ];
601         final String JavaDoc operationName = operationInfo.getName();
602         
603         // create an object of the correct type for each parameter.
604
// The actual value is not important.
605
for( int i = 0; i < numParams; ++i )
606         {
607             final MBeanParameterInfo paramInfo = paramInfos[ i ];
608             final String JavaDoc paramType = paramInfos[ i ].getType();
609             final Class JavaDoc theClass = ClassUtil.getClassFromName( paramType );
610             
611             final Object JavaDoc paramObject = ClassUtil.InstantiateDefault( theClass );
612             final String JavaDoc paramString = SmartStringifier.toString( paramObject );
613             final String JavaDoc castString = "(" + getCastType( paramType ) + ")";
614             
615             final String JavaDoc paramName = namedArgs ? (paramInfo.getName() + '=') : "";
616             
617             strings[ i ] = paramName + castString + paramString;
618         }
619         
620         // convert the arguments to strings
621
final String JavaDoc argString = makeArgList( strings );
622         
623         final String JavaDoc [] args = new String JavaDoc [] { targetName.toString() };
624         
625         final InvokeResult [] results = (InvokeResult [])mProxy.mbeanInvoke( operationName, argString, args );
626         final InvokeResult result = results[ 0 ];
627         
628         if ( result.getResultType() == InvokeResult.SUCCESS )
629         {
630             // p( "SUCCESS: " + operationName + "(" + SmartStringifier.toString( paramInfos ) + ")");
631
}
632         else
633         {
634             final String JavaDoc paramInfosString = SmartStringifier.toString( paramInfos );
635             
636             result.mThrowable.printStackTrace();
637         }
638         
639         return( result.getResultType() );
640     }
641     
642     static private final Class JavaDoc [] GENERICALLY_TESTABLE_CLASSES =
643     {
644         boolean.class,
645         char.class,
646         byte.class, short.class, int.class, long.class,
647         float.class, double.class,
648         Boolean JavaDoc.class,
649         Character JavaDoc.class,
650         Byte JavaDoc.class, Short JavaDoc.class, Integer JavaDoc.class, Long JavaDoc.class,
651         Float JavaDoc.class,
652         Double JavaDoc.class,
653         Number JavaDoc.class,
654         String JavaDoc.class,
655         Object JavaDoc.class,
656         java.math.BigDecimal JavaDoc.class,
657         java.math.BigInteger JavaDoc.class,
658         java.net.URL JavaDoc.class,
659         java.net.URI JavaDoc.class
660
661     };
662         private boolean
663     isGenericallyTestableClass( final Class JavaDoc theClass )
664         throws ClassNotFoundException JavaDoc
665     {
666         boolean isTestable = false;
667         
668         Class JavaDoc testClass = theClass;
669         if ( ClassUtil.classIsArray( theClass ) )
670         {
671             // we can test all arrays of supported types
672
testClass = ClassUtil.getInnerArrayElementClass( theClass );
673         }
674         
675         final Class JavaDoc [] classes = GENERICALLY_TESTABLE_CLASSES;
676         final int numClasses = classes.length;
677         for( int i = 0; i < numClasses; ++i )
678         {
679             if ( testClass == classes[ i ] )
680             {
681                 isTestable = true;
682                 break;
683             }
684         }
685         
686         if ( ! isTestable )
687         {
688             assert( testClass == java.util.Properties JavaDoc.class );
689         }
690         
691         return( isTestable );
692     }
693     
694         private boolean
695     isGenericallyTestable( final MBeanOperationInfo operationInfo )
696         throws ClassNotFoundException JavaDoc
697     {
698         boolean isTestable = true;
699         
700         final MBeanParameterInfo [] paramInfos = operationInfo.getSignature();
701         final int numParams = paramInfos.length;
702         for( int i = 0; i < numParams; ++i )
703         {
704             final Class JavaDoc theClass = ClassUtil.getClassFromName( paramInfos[i].getType() );
705             
706             if ( ! isGenericallyTestableClass( theClass ) )
707             {
708                 isTestable = false;
709                 break;
710             }
711         }
712         
713         return( isTestable );
714     }
715     
716     
717         private void
718     testGeneric( boolean namedTest, ObjectName objectName ) throws Exception JavaDoc
719     {
720         final MBeanInfo info = mServer.getMBeanInfo( objectName );
721         final MBeanOperationInfo [] opInfos = info.getOperations();
722         
723         int notTestedCount = 0;
724         for( int i = 0; i < opInfos.length; ++i )
725         {
726             try
727             {
728                 if ( isGenericallyTestable( opInfos[ i ] ) )
729                 {
730                     final InvokeResult.ResultType resultType =
731                         testOperationGenerically( namedTest, objectName, opInfos[ i ] );
732                         
733                     if ( resultType != InvokeResult.SUCCESS )
734                     {
735                         fail( "invocation failure on: " + SmartStringifier.toString( opInfos[ i ] ) );
736                     }
737                 }
738                 else
739                 {
740                     ++notTestedCount;
741                 }
742             }
743             catch( Exception JavaDoc e )
744             {
745                 fail( "FAILURE: " + SmartStringifier.toString( opInfos[ i ] ) );
746             }
747         }
748     }
749     
750         public void
751     testGenericOrdered()
752         throws Exception JavaDoc
753     {
754         final ObjectName [] allObjects = mProxy.mbeanFind( SUPPORT_TESTEE_ALIAS );
755         assert( allObjects.length == 1 );
756         
757         testGeneric( false, allObjects[ 0 ] );
758     }
759     
760         public void
761     testGenericNamed()
762         throws Exception JavaDoc
763     {
764         final ObjectName [] allObjects = mProxy.mbeanFind( SUPPORT_TESTEE_ALIAS );
765         assert( allObjects.length == 1 );
766         
767         testGeneric( false, allObjects[ 0 ] );
768     }
769     
770     
771         public void
772     testMBeanFind( ) throws Exception JavaDoc
773     {
774         final ObjectName [] results = mProxy.mbeanFind( ALL_TARGETS );
775         
776         assertEquals( 3, results.length );
777     }
778     
779     
780         public void
781     testMBeanInspect( ) throws Exception JavaDoc
782     {
783         final InspectRequest request = new InspectRequest();
784         
785         final InspectResult [] results = mProxy.mbeanInspect( request, ALL_TARGETS );
786         
787         assertEquals( 3, results.length );
788     }
789     
790         public void
791     testMBeanGet( ) throws Exception JavaDoc
792     {
793         final ResultsForGetSet [] results = mProxy.mbeanGet( "*", SIMPLE_TESTEE_TARGET );
794         
795         assertEquals( 1, results.length );
796         assertEquals( 2, results[ 0 ].getAttributes().size() );
797     }
798     
799         public void
800     testMBeanSet( ) throws Exception JavaDoc
801     {
802         final ResultsForGetSet [] results = mProxy.mbeanSet( "NotifMillis=1000", SIMPLE_TESTEE_TARGET );
803         
804         assertEquals( 1, results.length );
805         assertEquals( 1, results[ 0 ].getAttributes().size() );
806         
807         final AttributeList attrs = mProxy.mbeanGet( "NotifMillis", SIMPLE_TESTEE_TARGET )[ 0 ].getAttributes();
808         final Attribute expected = new Attribute( "NotifMillis", new Long JavaDoc ( 1000 ));
809         assertEquals( expected, attrs.get( 0 ) );
810     }
811     
812         public void
813     testMBeanCreateDelete( ) throws Exception JavaDoc
814     {
815         final String JavaDoc name = "test:name=testtemp";
816         final String JavaDoc className = "com.sun.cli.jmx.test.CLISupportSimpleTestee";
817         
818         mProxy.mbeanCreate( name, className, null );
819         
820         ObjectName [] results = mProxy.mbeanFind( new String JavaDoc [] { name } );
821         assertEquals( 1, results.length );
822         
823         mProxy.mbeanUnregister( name );
824         results = mProxy.mbeanFind( new String JavaDoc [] { name } );
825         assertEquals( 0, results.length );
826         
827         
828     }
829     
830     
831         
832         private void
833     deleteTestAliases() throws Exception JavaDoc
834     {
835         assert( mProxy != null );
836         
837         final String JavaDoc [] aliases = mProxy.listAliases( false );
838         assert( aliases != null );
839         
840         for( int i = 0; i < aliases.length; ++i )
841         {
842             final String JavaDoc name = aliases[ i ];
843             
844             if ( name.startsWith( ALIAS_BASE ) )
845             {
846                 mProxy.deleteAlias( name );
847             }
848         }
849     }
850     
851     
852     /*
853         Unlike tests for the AliasMgr, this test checks for recursive resolution of aliases
854         and resolution to actual objects.
855      */

856         public void
857     testAliases( ) throws Exception JavaDoc
858     {
859         deleteTestAliases();
860         
861         // create an alias for each MBean
862
final ObjectName [] names = mProxy.mbeanFind( new String JavaDoc [] { StandardAliases.ALL_ALIAS } );
863         final int numNames = names.length;
864         
865         // create test alias for each existing MBean
866
for( int i = 0; i < numNames; ++i )
867         {
868             final String JavaDoc aliasName = ALIAS_BASE + (i+1);
869             mProxy.createAlias( aliasName, names[ i ].toString() );
870         }
871         
872         // now verify that each of them resolves correctly
873
for( int i = 0; i < numNames; ++i )
874         {
875             final String JavaDoc aliasName = ALIAS_BASE + (i+1);
876             
877             final String JavaDoc aliasValue = mProxy.resolveAlias( aliasName );
878             if ( aliasValue == null )
879             {
880                 fail( "can't resolve alias after creating it: " + aliasName );
881             }
882             
883             if ( ! names[ i ].toString().equals( aliasValue ))
884             {
885                 fail( "alias does not resolve to value it was created with: " + aliasName );
886             }
887         }
888         
889         // create an alias consisting of all aliases
890
final String JavaDoc ALL_ALIASES_NAME = ALIAS_BASE + "all";
891         final String JavaDoc [] aliases = mProxy.listAliases( false );
892         final String JavaDoc allAliases = ArrayStringifier.stringify( aliases, " " );
893         mProxy.createAlias( ALL_ALIASES_NAME, allAliases );
894         
895         // create a recursive alias
896
String JavaDoc allAliasesName = ALL_ALIASES_NAME;
897         for( int i = 0; i < 5; ++i )
898         {
899             mProxy.createAlias( allAliasesName + i, allAliasesName );
900             allAliasesName = allAliasesName + i;
901         }
902         
903         // verify that the alias to all of them produces the same set of names as we started with
904
final ObjectName [] resolvedNames = mProxy.resolveTargets( new String JavaDoc [] { allAliasesName } );
905         //p( "all aliases = " + ArrayStringifier.stringify( resolvedNames, "\n" ) );
906
if ( resolvedNames.length != numNames )
907         {
908             fail( "alias resolution produce wrong number of results" );
909         }
910         
911         deleteTestAliases();
912     }
913     
914     
915         private void
916     verifySetup( CLISupportMBeanProxy proxy ) throws Exception JavaDoc
917     {
918         // must be at least one MBean
919
final ObjectName [] all = proxy.resolveTargets( ALL_TARGETS );
920         assert( all.length != 0 );
921         
922         // verify that the AliasMgr and CLI are available.
923
final String JavaDoc [] aliases = proxy.listAliases( false );
924         assert( aliases.length != 0 );
925         
926         // verify that required aliases are in place
927
assert( proxy.resolveAlias( ALL_ALIAS ) != null );
928         assert( proxy.resolveAlias( SIMPLE_TESTEE_ALIAS ) != null );
929         assert( proxy.resolveAlias( SUPPORT_TESTEE_ALIAS ) != null );
930         
931         
932     }
933     
934     
935         private MBeanServer
936     createAgent( )
937     {
938         return( MBeanServerFactory.createMBeanServer( "Test" ) );
939     }
940     
941         private void
942     registerMBean( MBeanServer conn, Object JavaDoc mbean, String JavaDoc name )
943         throws Exception JavaDoc
944     {
945         conn.registerMBean( mbean, new ObjectName( name ) );
946     }
947     
948     private final static CLISupportTestee CLI_SUPPORT_TESTEE = new CLISupportTestee( );
949         public void
950     setUp() throws Exception JavaDoc
951     {
952         mServer = createAgent( );
953         
954         // CLI_SUPPORT_TESTEE is very expensive to create, so we'll always reuse the same one
955
registerMBean( mServer, CLI_SUPPORT_TESTEE, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET );
956         
957         registerMBean( mServer, new CLISupportSimpleTestee( ),
958             CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET );
959         
960         final AliasMgr aliasMgr = new AliasMgr( new AliasMgrHashMapImpl() );
961         aliasMgr.createAlias( ALL_ALIAS, "*" );
962         aliasMgr.createAlias( SIMPLE_TESTEE_ALIAS, CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET );
963         aliasMgr.createAlias( SUPPORT_TESTEE_ALIAS, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET );
964         
965         final CLISupportMBeanImpl cliSupport = new CLISupportMBeanImpl( mServer, aliasMgr );
966         
967         mProxy = new CLISupportMBeanProxy( aliasMgr, cliSupport );
968         
969         verifySetup( mProxy );
970     }
971     
972         public void
973     tearDown()
974         throws Exception JavaDoc
975     {
976         mProxy.mbeanUnregister( mProxy.resolveAlias( SUPPORT_TESTEE_ALIAS ) );
977         mProxy.mbeanUnregister( mProxy.resolveAlias( SIMPLE_TESTEE_ALIAS ) );
978         mProxy = null;
979         mServer = null;
980     }
981
982 };
983
984
Popular Tags