KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > DottedNameGetSetMBeanImplTest


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/mbeans/tests/com/sun/enterprise/admin/dottedname/DottedNameGetSetMBeanImplTest.java,v 1.3 2005/12/25 03:43:05 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:05 $
28  */

29  
30
31 package com.sun.enterprise.admin.dottedname;
32
33 import java.util.Collections JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.MBeanInfo JavaDoc;
41 import javax.management.MBeanServerConnection JavaDoc;
42 import javax.management.AttributeNotFoundException JavaDoc;
43 import javax.management.MBeanServerFactory JavaDoc;
44 import javax.management.JMException JavaDoc;
45 import javax.management.Attribute JavaDoc;
46 import javax.management.AttributeList JavaDoc;
47
48 import com.sun.enterprise.admin.mbeans.DottedNameGetSetMBeanImpl;
49 import com.sun.enterprise.admin.dottedname.DottedNamePropertySupport;
50 import com.sun.enterprise.admin.util.ArrayConversion;
51     
52
53
54 public final class DottedNameGetSetMBeanImplTest extends junit.framework.TestCase
55 {
56     MBeanServer JavaDoc mServer;
57     
58     DottedNameRegistry mRegistry;
59     DottedNameRegistry mMonitoringRegistry;
60     DottedNameGetSetMBean mGetSetMBean;
61     
62         public
63     DottedNameGetSetMBeanImplTest( )
64     {
65     }
66     
67     
68     //-----------------------------------------------------------------------------------------
69

70     private static final String JavaDoc DOMAIN = DottedNameAliasSupport.DOMAIN_SCOPE;
71     private static final String JavaDoc SERVER_NAME_BASE = "server";
72     private static final String JavaDoc CONFIG_SUFFIX = "-config";
73     private static final String JavaDoc TESTEE_OBJECTNAME_BASE = "Test:name=testee";
74     private static final int NUM_SERVERS = 2;
75     
76     static private String JavaDoc [] DOMAIN_SUBPARTS = (String JavaDoc [])ArrayConversion.setToArray(
77             DottedNameAliasSupport.DOMAIN_PARTS, new String JavaDoc [ DottedNameAliasSupport.DOMAIN_PARTS.size() ] );
78             
79     private static final int NUM_DOMAIN_SUBPARTS = DOMAIN_SUBPARTS.length;
80     
81     private static final String JavaDoc SIMPLE = "simple";
82     private static final String JavaDoc SIMPLE_NAME = SIMPLE;
83     
84     
85     private static final char BACKSLASH = '\\';
86     
87     // name will be "funky\.test" (literal '\' and literal '.' )
88
// must be registered as "funky\\\.test"
89
private static final String JavaDoc FUNKY_DOTTED_NAME = "test" + BACKSLASH + BACKSLASH +
90                                                         BACKSLASH + ".";
91     private static final String JavaDoc FUNKY_ID = "funky";
92     
93     /*
94         Implement a ServerInfo to satisfy our testee, DottedNameGetSetMBeanImpl.
95     */

96     static private class StubbedServerInfo implements DottedNameServerInfo
97     {
98             public
99         StubbedServerInfo()
100         {
101         }
102         
103         private static final Set JavaDoc SERVER_NAME_SET = initServerNames( NUM_SERVERS );
104         private static final Set JavaDoc CONFIG_NAME_SET = initConfigNames( NUM_SERVERS );
105             
106             private static Set JavaDoc
107         initServerNames( int numNames )
108         {
109             final HashSet JavaDoc s = new HashSet JavaDoc();
110             
111             // generate server names server0, server1, etc
112
for( int i = 0; i < numNames; ++i )
113             {
114                 s.add( getServerDottedName( "" + i ) );
115             }
116             
117             return( Collections.unmodifiableSet( s ) );
118         }
119         
120             private static Set JavaDoc
121         initConfigNames( int numNames )
122         {
123             final HashSet JavaDoc s = new HashSet JavaDoc();
124             
125             // generate config names server0-config, server1-config, etc
126
for( int i = 0; i < numNames; ++i )
127             {
128                 s.add( getConfigDottedName( "" + i ) );
129             }
130             
131             return( Collections.unmodifiableSet( s ) );
132         }
133             
134             public Set JavaDoc
135         getServerNames()
136         {
137             return( SERVER_NAME_SET );
138         }
139         
140             public Set JavaDoc
141         getConfigNames()
142         {
143             return( CONFIG_NAME_SET );
144         }
145         
146             public String JavaDoc
147         getConfigNameForServer( String JavaDoc serverName )
148             throws DottedNameServerInfo.UnavailableException
149         {
150             if ( SERVER_NAME_SET.contains( serverName ) )
151             {
152                 return( serverName + CONFIG_SUFFIX );
153             }
154             
155             throw new DottedNameServerInfo.UnavailableException( "" );
156         }
157         
158             public String JavaDoc []
159         getServerNamesForConfig( String JavaDoc configName )
160             throws DottedNameServerInfo.UnavailableException
161         {
162             final java.util.Iterator JavaDoc iter = getServerNames().iterator();
163             final java.util.ArrayList JavaDoc namesOut = new java.util.ArrayList JavaDoc();
164             
165             while ( iter.hasNext() )
166             {
167                 final String JavaDoc serverName = (String JavaDoc)iter.next();
168                 
169                 if ( configName.equals( getConfigNameForServer( serverName ) ) )
170                 {
171                     namesOut.add( serverName );
172                 }
173             }
174             
175             final String JavaDoc [] namesOutArray = new String JavaDoc [ namesOut.size() ];
176             namesOut.toArray( namesOutArray );
177             
178             return( namesOutArray );
179         }
180     }
181     
182     //-----------------------------------------------------------------------------------------
183

184     /*
185         Extend our testee, DottedNameGetSetMBeanImpl, so that we can supply a stubbed ServerInfo
186         to it.
187     */

188     static private class HookedDottedNameGetSetMBeanImpl
189         extends DottedNameGetSetMBeanImpl
190         implements DottedNameGetSetMBean
191     {
192             public
193         HookedDottedNameGetSetMBeanImpl(
194             final MBeanServerConnection JavaDoc conn,
195             final DottedNameRegistry registry,
196             final DottedNameRegistry monitoringRegistry )
197             throws Exception JavaDoc
198         {
199             super( conn, registry, monitoringRegistry );
200             
201             // we don't want to see log messages during unit testing
202
DottedNameLogger.getInstance().setLevel( java.util.logging.Level.SEVERE );
203         }
204         
205     
206             protected DottedNameServerInfo
207         createServerInfo( MBeanServerConnection JavaDoc conn )
208         {
209             return( new StubbedServerInfo( ) );
210         }
211         
212     }
213     
214     //-----------------------------------------------------------------------------------------
215

216     
217     //-----------------------------------------------------------------------------------------
218

219         static private TesteeMBean
220     createTestee()
221     {
222         final TesteeMBean testee = new Testee();
223         
224         return( testee );
225     }
226     
227         private static String JavaDoc
228     getServerDottedName( String JavaDoc id )
229     {
230         return( SERVER_NAME_BASE + id );
231     }
232     
233         private static String JavaDoc
234     getMonitoringDottedName( String JavaDoc id )
235     {
236         return( getServerDottedName( id ) );
237     }
238     
239         private static String JavaDoc
240     getConfigDottedName( String JavaDoc id )
241     {
242         return( getServerDottedName( id ) + CONFIG_SUFFIX );
243     }
244     
245     
246         ObjectName JavaDoc
247     registerAndAddTestee(
248         final Object JavaDoc mbean,
249         final String JavaDoc dottedName,
250         final String JavaDoc id,
251         final String JavaDoc category,
252         DottedNameRegistry registry )
253         throws JMException JavaDoc
254     {
255         final ObjectName JavaDoc objectName = new ObjectName JavaDoc( TESTEE_OBJECTNAME_BASE +
256                                 ",id=" + id +
257                                 ",category=" + category );
258         
259         mServer.registerMBean( mbean, objectName );
260
261         registry.add( dottedName, objectName );
262         assert( registry.dottedNameToObjectName( dottedName ) == objectName );
263         
264         return( objectName );
265     }
266     
267         ObjectName JavaDoc
268     registerAndAddTestee(
269         final String JavaDoc dottedName,
270         final String JavaDoc id,
271         final String JavaDoc category,
272         DottedNameRegistry registry )
273         throws JMException JavaDoc
274     {
275         final ObjectName JavaDoc objectName =
276             registerAndAddTestee( createTestee(), dottedName, id, category, registry );
277         
278         return( objectName );
279     }
280     
281     final String JavaDoc CATEGORY_CONFIG = "config";
282     final String JavaDoc CATEGORY_MONITORING = "monitoring";
283     final String JavaDoc CATEGORY_SPECIAL = "special";
284     
285         public void
286     setUp()
287         throws Exception JavaDoc
288     {
289         
290         mServer = MBeanServerFactory.newMBeanServer( "" + System.currentTimeMillis() );
291         
292         mRegistry = new DottedNameRegistry1To1Impl();
293         mMonitoringRegistry = new DottedNameRegistry1To1Impl();
294         
295         mGetSetMBean = new HookedDottedNameGetSetMBeanImpl( mServer, mRegistry, mMonitoringRegistry );
296         mServer.registerMBean( mGetSetMBean, new ObjectName JavaDoc( ":name=get-set-testee" ) );
297         
298         
299         /*
300             Set up configuration testees
301          */

302         for( int i = 0; i < NUM_SERVERS; ++i )
303         {
304             final String JavaDoc id = "" + i;
305             final String JavaDoc dottedName = getConfigDottedName( id );
306             
307             registerAndAddTestee( dottedName, id, CATEGORY_CONFIG, mRegistry );
308         }
309         // simulate the domain itself with some children
310
final ObjectName JavaDoc on = registerAndAddTestee( DOMAIN, DOMAIN, CATEGORY_CONFIG, mRegistry );
311         for( int i = 0; i < DOMAIN_SUBPARTS.length; ++i )
312         {
313             final String JavaDoc part = DOMAIN_SUBPARTS[ i ];
314             
315             registerAndAddTestee( DOMAIN + "." + part, part, CATEGORY_CONFIG, mRegistry );
316         }
317         
318         
319         //dm( com.sun.cli.util.stringifier.MBeanInfoStringifier.DEFAULT.stringify( mServer.getMBeanInfo( on ) ) );
320

321         /*
322             Set up monitoring testees
323          */

324         for( int i = 0; i < NUM_SERVERS; ++i )
325         {
326             final String JavaDoc id = "" + i;
327             final String JavaDoc dottedName = getMonitoringDottedName( id );
328             
329             registerAndAddTestee( dottedName, id, CATEGORY_MONITORING, mMonitoringRegistry);
330         }
331         // simulate the domain itself
332
registerAndAddTestee( DOMAIN,
333             DOMAIN, CATEGORY_MONITORING, mMonitoringRegistry);
334             
335         // Register an MBean with no properties
336
SIMPLE_OBJECT_NAME = registerAndAddTestee( new MonitoringTestee(), SIMPLE_NAME,
337             SIMPLE, CATEGORY_MONITORING, mMonitoringRegistry);
338     }
339
340 ObjectName JavaDoc SIMPLE_OBJECT_NAME = null;
341
342         static void
343     dm( Object JavaDoc o )
344     {
345         System.out.println( o.toString() );
346     }
347     
348         public void
349     tearDown()
350     {
351         mServer = null;
352     }
353         
354         private static String JavaDoc
355     valueName( String JavaDoc prefix, String JavaDoc valueName )
356     {
357         return( prefix + "." + valueName );
358     }
359     
360         Attribute JavaDoc
361     getAttribute( String JavaDoc name )
362     {
363         final Object JavaDoc result = mGetSetMBean.dottedNameGet( name );
364         
365         if ( result instanceof Exception JavaDoc )
366         {
367             ((Exception JavaDoc)result).printStackTrace();
368         }
369
370         assert( result instanceof Attribute JavaDoc ): "expecting Attribute, got " + result.getClass().getName();
371         return( (Attribute JavaDoc)result );
372     }
373     
374     private static final String JavaDoc PRIMARY_TESTEE = getServerDottedName( "0" );
375         static String JavaDoc
376     testeeValueName( String JavaDoc name )
377     {
378         return( PRIMARY_TESTEE + "." + name );
379     }
380     
381     
382     
383         Attribute JavaDoc
384     getMonitoringAttribute( String JavaDoc name )
385     {
386         final Object JavaDoc result = mGetSetMBean.dottedNameMonitoringGet( name );
387         
388         assert( result instanceof Attribute JavaDoc ) :
389             "expecting single Attribute, got " + result.getClass().getName();
390         return( (Attribute JavaDoc)result );
391     }
392     
393         public void
394     testGetchar()
395     {
396         final String JavaDoc dn = testeeValueName( "char" );
397         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Character JavaDoc( 'c' ) ) );
398     }
399     
400     
401         public void
402     testGetbyte()
403     {
404         final String JavaDoc dn = testeeValueName( "byte" );
405         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Byte JavaDoc( (byte)0 ) ) );
406     }
407     
408         public void
409     testGetshort()
410     {
411         final String JavaDoc dn = testeeValueName( "short" );
412         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Short JavaDoc( (short)0 ) ) );
413     }
414     
415         public void
416     testGetint() throws Exception JavaDoc
417     {
418         final String JavaDoc dn = testeeValueName( "int" );
419         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Integer JavaDoc( 0 ) ) );
420     }
421     
422         public void
423     testGetlong() throws Exception JavaDoc
424     {
425         final String JavaDoc dn = testeeValueName( "long" );
426         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Long JavaDoc( 0 ) ) );
427     }
428     
429         public void
430     testGetfloat() throws Exception JavaDoc
431     {
432         final String JavaDoc dn = testeeValueName( "float" );
433         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Float JavaDoc( 0.0 ) ) );
434     }
435     
436         public void
437     testGetdouble() throws Exception JavaDoc
438     {
439         final String JavaDoc dn = testeeValueName( "double" );
440         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Double JavaDoc( 0.0 ) ) );
441     }
442     
443     
444         public void
445     testGetString()throws Exception JavaDoc
446     {
447         final String JavaDoc dn = testeeValueName( "String" );
448         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, "" ) );
449     }
450     
451         public void
452     testGetStringArray()throws Exception JavaDoc
453     {
454         final String JavaDoc dn = testeeValueName( "StringArray" );
455         
456         final Attribute JavaDoc attr = getAttribute( dn );
457         assertEquals( 0, ((String JavaDoc [])attr.getValue()).length );
458     }
459     
460         public void
461     testGetWild()throws Exception JavaDoc
462     {
463         final Attribute JavaDoc [] attrs = (Attribute JavaDoc [])mGetSetMBean.dottedNameGet( "*.StringArray" );
464         
465         // will only look in "server.*" namespace
466
assertEquals( NUM_SERVERS + NUM_SERVERS * NUM_DOMAIN_SUBPARTS, attrs.length );
467     }
468     
469         public void
470     testGetWildProperty()throws Exception JavaDoc
471     {
472         final Attribute JavaDoc [] attrs = (Attribute JavaDoc [])mGetSetMBean.dottedNameGet( PRIMARY_TESTEE + ".property.*" );
473         assertEquals( 1, attrs.length );
474     }
475         static String JavaDoc
476     attributeToString( final Attribute JavaDoc attr )throws Exception JavaDoc
477     {
478         return( attr.getName() + " = " + attr.getValue().toString() );
479     }
480         static String JavaDoc
481     attributeListToString( final AttributeList JavaDoc attrs )throws Exception JavaDoc
482     {
483         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
484         
485         for( int i = 0; i < attrs.size(); ++i )
486         {
487             buf.append( attributeToString( (Attribute JavaDoc)attrs.get( i ) ) );
488             buf.append( "\n" );
489         }
490         return( buf.toString() );
491     }
492     
493         String JavaDoc
494     arrayToString( Object JavaDoc [] a )throws Exception JavaDoc
495     {
496         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
497         
498         buf.append( "{" );
499         for( int i = 0; i < a.length; ++i )
500         {
501             buf.append( a[ i ].toString() );
502             buf.append( ", " );
503         }
504         buf.append( "}" );
505         
506         return( buf.toString() );
507     }
508     
509         public void
510     testServerAliasIntoDomain()throws Exception JavaDoc
511     {
512         // eg "server0.applications"
513
final String JavaDoc target = getServerDottedName( "0" ) + "." + DOMAIN_SUBPARTS[ 0 ];
514         
515         getAttribute( target + ".int" );
516     }
517     
518         public synchronized void
519     testWildcardGetAll()
520     {
521         final Attribute JavaDoc[] list1 = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( "*" );
522         final Attribute JavaDoc[] list2 = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( "*.*" );
523         final Attribute JavaDoc[] list3 = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( "***.***" );
524         final Attribute JavaDoc[] list4 = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( "**********.*" );
525         
526         final int expectedSize = list1.length;
527         assert( expectedSize != 0 );
528         assertEquals( expectedSize, list2.length );
529         assertEquals( expectedSize, list3.length );
530         assertEquals( expectedSize, list4.length );
531     }
532     
533     
534         public synchronized void
535     testWildcardMonitoringGetAll()
536     {
537         final Attribute JavaDoc[] list1 = (Attribute JavaDoc[])mGetSetMBean.dottedNameMonitoringGet( "*" );
538         final Attribute JavaDoc[] list2 = (Attribute JavaDoc[])mGetSetMBean.dottedNameMonitoringGet( "*.*" );
539         final Attribute JavaDoc[] list3 = (Attribute JavaDoc[])mGetSetMBean.dottedNameMonitoringGet( "***.***" );
540         final Attribute JavaDoc[] list4 = (Attribute JavaDoc[])mGetSetMBean.dottedNameMonitoringGet( "**********.*" );
541         
542         final int expectedSize = list1.length;
543         assert( expectedSize != 0 );
544         assertEquals( expectedSize, list2.length );
545         assertEquals( expectedSize, list3.length );
546         assertEquals( expectedSize, list4.length );
547     }
548     
549     
550         public synchronized void
551     testWildcardGetAllMonitor()
552     {
553         final Attribute JavaDoc[] list = (Attribute JavaDoc[])mGetSetMBean.dottedNameMonitoringGet( "*" );
554         assert( list.length != 0 );
555     }
556     
557         public synchronized void
558     testNoProperties() throws Exception JavaDoc
559     {
560         mServer.getMBeanInfo(SIMPLE_OBJECT_NAME );
561         // the MonitoringTestee has no properties
562
final Object JavaDoc result = mGetSetMBean.dottedNameMonitoringGet( SIMPLE + ".property.foo" );
563         
564         assert( result instanceof AttributeNotFoundException JavaDoc ) :
565             "expected AttributeNotFoundException, got " + result.getClass().getName() +
566             " at ";
567     }
568     
569         public void
570     testWildcardListEmpty()
571     {
572         assert( mGetSetMBean.dottedNameList( new String JavaDoc [ 0 ] ).length != 0 );
573     }
574     
575         public synchronized void
576     testWildcardListAll()
577     {
578         final int length1 = dottedNameList( "*" ).length;
579         
580         assert( length1 != 0 );
581     }
582     
583         public synchronized void
584     testWildcardMonitoringListAll()
585     {
586         assert( dottedNameMonitoringList( "*" ).length != 0 );
587     }
588     
589         public void
590     testWildcardAllGetsOnlyServers()
591     {
592         // '*' has special meaning of only getting the server.xxx names, *not* domain.xxx or config.xxx
593
final String JavaDoc [] names = dottedNameList( "*" );
594         
595         final Set JavaDoc serverNames = new StubbedServerInfo().getServerNames();
596         for( int i = 0; i < names.length; ++i )
597         {
598             final DottedName dn = DottedNameFactory.getInstance().get( names[ i ] );
599             
600             assert( serverNames.contains( dn.getScope() ) );
601         }
602     }
603     
604         public void
605     testDomainGetsOnlyDomain()
606     {
607         // '*' has special meaning of only getting the server.xxx names, *not* domain.xxx or config.xxx
608
final String JavaDoc [] names = dottedNameList( "domain*" );
609         
610         for( int i = 0; i < names.length; ++i )
611         {
612             final DottedName dn = DottedNameFactory.getInstance().get( names[ i ] );
613             
614             assertEquals( dn.getScope(), DottedNameAliasSupport.DOMAIN_SCOPE );
615         }
616     }
617     
618         public void
619     testConfigGetsOnlyConfig()
620     {
621         // '*' has special meaning of only getting the server.xxx names, *not* domain.xxx or config.xxx
622
final String JavaDoc [] names = dottedNameList( getConfigDottedName( "0" ) + "*" );
623         
624         for( int i = 0; i < names.length; ++i )
625         {
626             final DottedName dn = DottedNameFactory.getInstance().get( names[ i ] );
627             
628             assertEquals( dn.getScope(), getConfigDottedName( "0" ) );
629         }
630     }
631     
632     
633         public void
634     testGetWildAttr()
635     {
636         Attribute JavaDoc[] attrs = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( "*.StringArray*" );
637         // we're using '*' which will select only server.xxx names. There should be one
638
// name for each server itself, and NUM_DOMAIN_SUBPARTS additional names per server.
639

640         assertEquals( NUM_SERVERS + NUM_SERVERS * NUM_DOMAIN_SUBPARTS, attrs.length );
641         
642         attrs = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet("*.*int*" );
643         assertEquals( NUM_SERVERS + NUM_SERVERS * NUM_DOMAIN_SUBPARTS, attrs.length );
644     }
645     
646         public void
647     testGetWildAttrSingleMBean()
648     {
649         // this should get all 17 attributes and 1 property and 1 attribute called "Properties"
650
final Attribute JavaDoc[] attrs =
651             (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( PRIMARY_TESTEE + ".*" );
652             
653         assertEquals( 17 + 1 + 1, attrs.length );
654     }
655     
656         public void
657     testMultiGet()
658     {
659         final String JavaDoc [] params = new String JavaDoc []
660         {
661             testeeValueName( "char" ),
662             testeeValueName( "int" ),
663             testeeValueName( "float" ),
664             testeeValueName( "Double" ),
665         };
666         
667         final Attribute JavaDoc[] attrs = (Attribute JavaDoc[])mGetSetMBean.dottedNameGet( params );
668             
669         assertEquals( params.length, attrs.length );
670         for( int i = 0; i < attrs.length; ++i )
671         {
672             assert( attrs[ i ] instanceof Attribute JavaDoc );
673         }
674     }
675     
676         Attribute JavaDoc
677     find( Object JavaDoc[] attrs, String JavaDoc name )
678     {
679         Attribute JavaDoc attr = null;
680         
681         final int size = attrs.length;
682         for( int i = 0; i < size; ++i )
683         {
684             final Attribute JavaDoc candidate = (Attribute JavaDoc)attrs[ i ];
685             
686             if ( candidate.getName().equals( name ) )
687             {
688                 attr = candidate;
689                 break;
690             }
691         }
692         return( attr );
693     }
694     
695         public synchronized void
696     testMultiSet()
697     {
698         final String JavaDoc [] params = new String JavaDoc []
699         {
700             testeeValueName( "int" ) + "=99",
701             testeeValueName( "char" ) + "=x",
702             testeeValueName( "String" ) + "=9.999",
703             testeeValueName( "Double" ) + "=9.999",
704         };
705         
706         final Object JavaDoc[] attrs = mGetSetMBean.dottedNameSet( params );
707         assertEquals( params.length, attrs.length );
708
709         assertEquals( new Attribute JavaDoc( testeeValueName( "int" ), new Integer JavaDoc( 99 ) ),
710                         find( attrs, testeeValueName( "int" )));
711                         
712
713         assertEquals( new Attribute JavaDoc( testeeValueName( "String" ), "9.999" ),
714                         find( attrs, testeeValueName( "String" )));
715                         
716
717         assertEquals( new Attribute JavaDoc( testeeValueName( "char" ), new Character JavaDoc( 'x' ) ),
718                         find( attrs, testeeValueName( "char" )));
719                         
720
721         assertEquals( new Attribute JavaDoc( testeeValueName( "Double" ), new Double JavaDoc( 9.999 ) ),
722                         find( attrs, testeeValueName( "Double" )));
723     }
724     
725         public void
726     testSetWild()
727     {
728         final Object JavaDoc result = mGetSetMBean.dottedNameSet( "*.int=1" );
729             
730         assert( result instanceof Exception JavaDoc );
731     }
732     
733         public void
734     testSetchar()
735     {
736         final String JavaDoc dn = testeeValueName( "char" );
737         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=x" } );
738         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Character JavaDoc( 'x' ) ) );
739     }
740     
741         public void
742     testSetbyte()
743     {
744         final String JavaDoc dn = testeeValueName( "byte" );
745         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
746         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Byte JavaDoc( (byte)100 ) ) );
747     }
748     
749         public void
750     testSetshort()
751     {
752         final String JavaDoc dn = testeeValueName( "short" );
753         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
754         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Short JavaDoc( (short)100 ) ) );
755     }
756     
757         public void
758     testSetint()
759     {
760         final String JavaDoc dn = testeeValueName( "int" );
761         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
762         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Integer JavaDoc( 100 ) ) );
763     }
764     
765         public void
766     testSetlong()
767     {
768         final String JavaDoc dn = testeeValueName( "long" );
769         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
770         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Long JavaDoc( 100 ) ) );
771     }
772     
773         public void
774     testSetfloat()
775     {
776         final String JavaDoc dn = testeeValueName( "float" );
777         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=99.99" } );
778         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Float JavaDoc( 99.99 ) ) );
779     }
780     
781         public void
782     testSetdouble()
783     {
784         final String JavaDoc dn = testeeValueName( "double" );
785         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=99.99" } );
786         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Double JavaDoc( 99.99 ) ) );
787     }
788     
789     
790     
791         public void
792     testSetCharacter()
793     {
794         final String JavaDoc dn = testeeValueName( "Character" );
795         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=x" } );
796         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Character JavaDoc( 'x' ) ) );
797     }
798     
799         public void
800     testSetByte()
801     {
802         final String JavaDoc dn = testeeValueName( "byte" );
803         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
804         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Byte JavaDoc( (byte)100 ) ) );
805     }
806     
807         public void
808     testSetShort()
809     {
810         final String JavaDoc dn = testeeValueName( "Short" );
811         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
812         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Short JavaDoc( (short)100 ) ) );
813     }
814     
815         public void
816     testSetInteger()
817     {
818         final String JavaDoc dn = testeeValueName( "Integer" );
819         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
820         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Integer JavaDoc( 100 ) ) );
821     }
822     
823         public void
824     testSetLong()
825     {
826         final String JavaDoc dn = testeeValueName( "Long" );
827         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=100" } );
828         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Long JavaDoc( 100 ) ) );
829     }
830     
831         public void
832     testSetFloat()
833     {
834         final String JavaDoc dn = testeeValueName( "Float" );
835         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=99.99" } );
836         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Float JavaDoc( 99.99 ) ) );
837     }
838     
839         public void
840     testSetDouble()
841     {
842         final String JavaDoc dn = testeeValueName( "Double" );
843         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=99.99" } );
844         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, new Double JavaDoc( 99.99 ) ) );
845     }
846     
847     
848         public void
849     testSetString()
850     {
851         final String JavaDoc dn = testeeValueName( "String" );
852         mGetSetMBean.dottedNameSet( new String JavaDoc [] { dn + "=hello world" } );
853         assertEquals( getAttribute( dn ), new Attribute JavaDoc( dn, "hello world" ) );
854     }
855     
856         public void
857     testSetStringArray()
858     {
859         final String JavaDoc dn = testeeValueName( "StringArray" );
860         final Attribute JavaDoc result = (Attribute JavaDoc)mGetSetMBean.dottedNameSet( dn + "=hello,there,world" );
861         
862         final Attribute JavaDoc attr = getAttribute( dn );
863         assertEquals( result, attr );
864         final String JavaDoc [] values = (String JavaDoc [])attr.getValue();
865         assertEquals( 3, values.length );
866         assertEquals( "hello", values[ 0 ] );
867         assertEquals( "there", values[ 1 ] );
868         assertEquals( "world", values[ 2 ] );
869     }
870     
871         public void
872     testSetIntegerArray()
873     {
874         final String JavaDoc dn = testeeValueName( "IntegerArray" );
875         mGetSetMBean.dottedNameSet( dn + "=1,2,3" );
876         
877         final Attribute JavaDoc attr = getAttribute( dn );
878         final Integer JavaDoc [] values = (Integer JavaDoc [])attr.getValue();
879         assertEquals( 3, values.length );
880         assertEquals( new Integer JavaDoc( 1 ), values[ 0 ] );
881         assertEquals( new Integer JavaDoc( 2 ), values[ 1 ] );
882         assertEquals( new Integer JavaDoc( 3 ), values[ 2 ] );
883     }
884     
885         public void
886     testGetProperty()
887     {
888         final String JavaDoc dottedName = PRIMARY_TESTEE + "." +
889                     DottedNamePropertySupport.getPropertySuffix( Testee.PROPERTY_NAME );
890                     
891         final Attribute JavaDoc attr = getAttribute( dottedName );
892         assertEquals( Testee.PROPERTY_VALUE, attr.getValue() );
893     }
894     
895         public synchronized void
896     testSetProperty()
897     {
898         final String JavaDoc dottedName = PRIMARY_TESTEE + "." +
899                     DottedNamePropertySupport.getPropertySuffix( "testProperty" );
900                     
901         final String JavaDoc TEST_VALUE = "hello";
902         
903         // set a value and re-get it to verify it was set
904
final Object JavaDoc value = mGetSetMBean.dottedNameSet( dottedName + "=" + TEST_VALUE );
905         if ( value instanceof Exception JavaDoc )
906         {
907             ((Exception JavaDoc)value).printStackTrace();
908         }
909
910         final Attribute JavaDoc attr = (Attribute JavaDoc)value;
911         assertEquals( TEST_VALUE, attr.getValue( ) );
912         assertEquals( TEST_VALUE, getAttribute( dottedName ).getValue() );
913     }
914     
915         private String JavaDoc []
916     dottedNameList( String JavaDoc name )
917     {
918         return( mGetSetMBean.dottedNameList( new String JavaDoc [] { name } ) );
919     }
920     
921         private String JavaDoc []
922     dottedNameMonitoringList( String JavaDoc name )
923     {
924         return( mGetSetMBean.dottedNameMonitoringList( new String JavaDoc [] { name } ) );
925     }
926     
927         public void
928     testListAll()
929     {
930         // list all will only access server.* variants
931
final int required =
932             NUM_SERVERS + // server itself
933
NUM_SERVERS * NUM_DOMAIN_SUBPARTS + // aliased parts
934
0;
935         
936         final String JavaDoc [] names = dottedNameList( "*" );
937         assertEquals( required, names.length );
938     }
939     
940         public void
941     testListEmpty()
942     {
943         final String JavaDoc [] names = dottedNameList( "" );
944         assertEquals( 0, names.length );
945     }
946         
947         String JavaDoc
948     setToString( final Set JavaDoc s )
949     {
950         final Iterator JavaDoc iter = s.iterator();
951         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
952         
953         while ( iter.hasNext() )
954         {
955             buf.append( (String JavaDoc)iter.next() + "\n" );
956         }
957         
958         return( buf.toString() );
959     }
960     
961         public void
962     testListNoMatch()
963     {
964         assertEquals( 0, dottedNameList( "*foo*" ).length );
965     }
966         public void
967     testListDomain()
968     {
969         // domain only works if it starts with "domain"
970
assertEquals( 0, dottedNameList( "*" + DOMAIN + "*" ).length );
971         assertEquals( 0, dottedNameList( "*" + DOMAIN ).length );
972         
973         assertEquals( 1 + NUM_DOMAIN_SUBPARTS, dottedNameList( DOMAIN + "*" ).length );
974     }
975     
976         public void
977     testListServer()
978     {
979         final int required = NUM_SERVERS + NUM_SERVERS * NUM_DOMAIN_SUBPARTS;
980         assertEquals( required, dottedNameList( SERVER_NAME_BASE + "*" ).length );
981         assertEquals( required, dottedNameList( "*" + SERVER_NAME_BASE + "*" ).length );
982     }
983
984     
985         public void
986     testListRecursive()
987     {
988         final int required =
989             NUM_SERVERS + // one for each testee itself
990
NUM_SERVERS * NUM_DOMAIN_SUBPARTS + // each server aliases into domain
991
0;
992             
993         assertEquals( required, dottedNameList( SERVER_NAME_BASE + "*").length );
994     }
995     
996         public void
997     testListNonRecursive()
998     {
999         assertEquals( NUM_DOMAIN_SUBPARTS, dottedNameList( getServerDottedName( "0" ) ).length );
1000        assertEquals( NUM_DOMAIN_SUBPARTS, dottedNameList( DOMAIN ).length );
1001    }
1002    
1003        public void
1004    testListNoChildren()
1005    {
1006        assertEquals( 0, dottedNameList( getServerDottedName( "0." + DOMAIN_SUBPARTS[ 0 ] ) ).length );
1007        assertEquals( 0, dottedNameList( getConfigDottedName( "0" ) ).length );
1008    }
1009    
1010        public void
1011    testListWithChildren()
1012    {
1013        assertEquals( NUM_DOMAIN_SUBPARTS, dottedNameList( DOMAIN + ".*" ).length );
1014    }
1015    
1016        public void
1017    testListWildcardMonitoring()
1018    {
1019        assertEquals( NUM_SERVERS, dottedNameMonitoringList( "server*" ).length );
1020    }
1021    
1022        public void
1023    testAliasingOffForMonitoring()
1024    {
1025        final Object JavaDoc result = mGetSetMBean.dottedNameMonitoringGet( getConfigDottedName( "0" ) + ".*" );
1026        
1027        assert( result instanceof Attribute JavaDoc[] );
1028        
1029        assertEquals( 0, dottedNameMonitoringList( "*config*" ).length );
1030    }
1031    
1032        public void
1033    testListNonExistent()
1034    {
1035        assertEquals( 0, dottedNameList( "foo" ).length );
1036        assertEquals( 0, dottedNameList( "/" ).length );
1037    }
1038    
1039        public void
1040    testMonitoringGet()
1041    {
1042        final String JavaDoc dn = DOMAIN + ".int";
1043        assertEquals( getMonitoringAttribute( dn ), new Attribute JavaDoc( dn, new Integer JavaDoc( 0 ) ) );
1044    }
1045    
1046        public void
1047    testFunkyName() throws JMException JavaDoc
1048    {
1049        // set up testee with strange name
1050
// simulate the domain itself
1051
registerAndAddTestee( FUNKY_DOTTED_NAME, FUNKY_ID, CATEGORY_SPECIAL, mRegistry);
1052    }
1053    
1054}
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
Popular Tags