KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > ContainerTest


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/mbeanapi-impl/tests/com/sun/enterprise/management/base/ContainerTest.java,v 1.6 2006/03/09 20:30:51 llc Exp $
26  * $Revision: 1.6 $
27  * $Date: 2006/03/09 20:30:51 $
28  */

29 package com.sun.enterprise.management.base;
30
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.MBeanInfo JavaDoc;
39 import javax.management.AttributeList JavaDoc;
40 import javax.management.MBeanAttributeInfo JavaDoc;
41 import javax.management.AttributeNotFoundException JavaDoc;
42 import javax.management.MBeanServerConnection JavaDoc;
43
44
45 import com.sun.appserv.management.DomainRoot;
46 import com.sun.appserv.management.base.AMX;
47 import com.sun.appserv.management.base.QueryMgr;
48 import com.sun.appserv.management.base.Container;
49 import com.sun.appserv.management.client.ProxyFactory;
50 import com.sun.appserv.management.base.AMX;
51 import com.sun.appserv.management.base.Container;
52 import com.sun.appserv.management.base.XTypes;
53 import com.sun.appserv.management.util.jmx.MBeanServerConnectionConnectionSource;
54 import com.sun.appserv.management.util.jmx.JMXUtil;
55
56 import com.sun.enterprise.management.support.AMXNonConfigImplBase;
57 import com.sun.enterprise.management.support.QueryMgrImpl;
58
59 import com.sun.appserv.management.base.Util;
60 import com.sun.appserv.management.util.misc.ExceptionUtil;
61 import com.sun.appserv.management.util.misc.GSetUtil;
62 import com.sun.appserv.management.util.misc.MapUtil;
63 import com.sun.appserv.management.util.misc.ClassUtil;
64 import com.sun.appserv.management.util.misc.StringUtil;
65 import com.sun.appserv.management.util.misc.TypeCast;
66 import com.sun.appserv.management.config.NamedConfigElement;
67 import com.sun.enterprise.management.support.TypeInfos;
68 import com.sun.enterprise.management.support.TypeInfo;
69
70
71 import com.sun.enterprise.management.AMXTestBase;
72 import com.sun.enterprise.management.Capabilities;
73
74 /**
75  */

76 public final class ContainerTest extends AMXTestBase
77 {
78         public
79     ContainerTest( )
80     {
81     }
82
83         public static Capabilities
84     getCapabilities()
85     {
86         return getOfflineCapableCapabilities( true );
87     }
88     
89         public void
90     checkContainerContainsChild( final ObjectName JavaDoc containeeObjectName )
91         throws Exception JavaDoc
92     {
93         final MBeanServerConnection JavaDoc conn = getMBeanServerConnection();
94         
95         assert( containeeObjectName != null );
96         assert( conn.isRegistered( containeeObjectName ) );
97         final AMX containedProxy = getProxyFactory().getProxy( containeeObjectName, AMX.class);
98         if ( containedProxy instanceof DomainRoot )
99         {
100             // DomainRoot has no Container
101
return;
102         }
103         
104         final ObjectName JavaDoc containerObjectName = (ObjectName JavaDoc)
105             conn.getAttribute( containeeObjectName, "ContainerObjectName" );
106         if ( ! conn.isRegistered( containerObjectName ) )
107         {
108             warning( "Container " + StringUtil.quote( containerObjectName ) +
109                 " for " + StringUtil.quote( containeeObjectName ) +
110                 " is not registered." );
111             return;
112         }
113         
114         final AMX parentProxy = containedProxy.getContainer( );
115         
116         if ( parentProxy instanceof Container )
117         {
118             if ( ! ( parentProxy instanceof Container ) )
119             {
120                 trace( "WARNING: proxy is instance of Container, but not Container: " +
121                     Util.getExtra( parentProxy ).getObjectName() );
122             }
123             else
124             {
125                 final Container container = (Container)parentProxy;
126                     
127                 if ( container != null )
128                 {
129                     final Set JavaDoc<AMX> containees = container.getContaineeSet();
130                     final Set JavaDoc<ObjectName JavaDoc> containeeObjectNames = Util.toObjectNames( containees );
131                     
132                     if ( ! containeeObjectNames.contains( Util.getExtra( containedProxy ).getObjectName() ) )
133                     {
134                         trace( "ERROR: Container " + Util.getExtra( parentProxy ).getObjectName() +
135                             " does not contain its child: " + containeeObjectName );
136                         assertTrue( false );
137                     }
138                 }
139             }
140         }
141     }
142     
143     
144     
145         public void
146     testContainersContainChildren()
147         throws Exception JavaDoc
148     {
149         testAll( "checkContainerContainsChild" );
150     }
151     
152     
153         public void
154     checkIsContainer( final ObjectName JavaDoc objectName )
155         throws Exception JavaDoc
156     {
157         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class);
158
159         try
160         {
161             final Set JavaDoc<String JavaDoc> containedJ2EETypes = TypeCast.asSet(
162                 Util.getExtra( proxy ).getAttribute( Container.ATTR_CONTAINEE_J2EE_TYPES ) );
163             
164             if ( containedJ2EETypes != null && containedJ2EETypes.size() != 0 )
165             {
166                 assert( proxy instanceof Container ) :
167                     "proxy has ContaineeJ2EETypes but is not a Container: " + objectName;
168             }
169         }
170         catch( AttributeNotFoundException JavaDoc e )
171         {
172         }
173         
174     }
175     
176         public void
177     testIsContainer()
178         throws Exception JavaDoc
179     {
180         testAll( "checkIsContainer" );
181     }
182
183         private void
184     checkMapAgreesWithSet( final Container container )
185         throws Exception JavaDoc
186     {
187         final Set JavaDoc<String JavaDoc> containedJ2EETypes = container.getContaineeJ2EETypes();
188         
189         for ( final String JavaDoc j2eeType : containedJ2EETypes )
190         {
191             final Map JavaDoc<String JavaDoc,AMX> containeeMap =
192                 container.getContaineeMap( j2eeType );
193                 
194             final Set JavaDoc<AMX> containeeSet =
195                 container.getContaineeSet( j2eeType );
196                 
197             assert( containeeMap.keySet().size() == containeeSet.size() ) :
198                 "containeeMap has " + containeeMap.keySet().size() +
199                     " = " + toString( containeeMap ) +
200                 " but containeeSet has " + containeeSet.size() + " = " + toString( containeeSet );
201         
202             final Set JavaDoc<String JavaDoc> namesSet = Util.getNames( containeeSet );
203             assert( containeeMap.keySet().equals( namesSet ) );
204         }
205     }
206     
207         public void
208     checkMapAgreesWithSet( final AMX container )
209         throws Exception JavaDoc
210     {
211         checkMapAgreesWithSet( (Container)container );
212     }
213     
214         private void
215     checkContaineeMap( final Container container )
216         throws Exception JavaDoc
217     {
218         final Set JavaDoc<String JavaDoc> containedJ2EETypes = container.getContaineeJ2EETypes();
219         assert( containedJ2EETypes != null );
220         assert( ! containedJ2EETypes.contains( null ) );
221         
222         assert( container.getMultiContaineeMap( (Set JavaDoc<String JavaDoc>)null ) != null );
223         assert( container.getMultiContaineeMap( containedJ2EETypes ) != null );
224         
225         for ( final String JavaDoc j2eeType : containedJ2EETypes )
226         {
227             final Map JavaDoc<String JavaDoc,AMX> containeeMap =
228                 container.getContaineeMap( j2eeType );
229             assert( containeeMap != null ) :
230                 "getContaineeObjectNameMap failed for " + j2eeType ;
231             final Set JavaDoc<String JavaDoc> nullValueKeys = MapUtil.getNullValueKeys( containeeMap );
232             assert( nullValueKeys.size() == 0 ) :
233                 "getContaineeObjectNameMap contains nulls for " + toString( nullValueKeys );
234         }
235     }
236     
237         public void
238     checkContaineeMap( final AMX container )
239         throws Exception JavaDoc
240     {
241         checkContaineeMap( (Container)container );
242     }
243     
244     
245         private void
246     checkContaineeSet( final Container container )
247         throws Exception JavaDoc
248     {
249         final Set JavaDoc<String JavaDoc> containedJ2EETypes = container.getContaineeJ2EETypes();
250         assert( containedJ2EETypes != null );
251         assert( ! containedJ2EETypes.contains( null ) );
252         assert( container.getContaineeSet() != null );
253         
254         for ( final String JavaDoc j2eeType : containedJ2EETypes )
255         {
256             final Set JavaDoc<AMX> containeeSet =
257                 container.getContaineeSet( j2eeType );
258             assert( containeeSet != null ) :
259                 "getContaineeSet for " + j2eeType ;
260             assert( ! containeeSet.contains( null ) ) :
261                 "getContaineeSet contains null for " + j2eeType ;
262                 
263             final Set JavaDoc<AMX> fromSet =
264                 container.getContaineeSet( GSetUtil.newStringSet( j2eeType ) );
265             assert( fromSet.equals( containeeSet ) );
266                 
267             if ( containeeSet.size() == 1 )
268             {
269                 assert( container.getContainee( j2eeType ) != null );
270             }
271         }
272     }
273         public void
274     checkContaineeSet( final AMX container )
275         throws Exception JavaDoc
276     {
277         checkContaineeSet( (Container)container );
278     }
279     
280
281         private void
282     checkGetByName( final Container container )
283         throws Exception JavaDoc
284     {
285         final Set JavaDoc<String JavaDoc> containedJ2EETypes = container.getContaineeJ2EETypes();
286         
287         for ( final String JavaDoc j2eeType : containedJ2EETypes )
288         {
289             final Map JavaDoc<String JavaDoc,AMX> containeeMap =
290                 container.getContaineeMap( j2eeType );
291             
292             for( final String JavaDoc name : containeeMap.keySet() )
293             {
294                 final AMX containee = container.getContainee( j2eeType, name );
295                     
296                 assert( containee != null ) :
297                     "can't get containee of type " + j2eeType + ", name = " + name +
298                     " in " + Util.getObjectName( container );
299                     
300                 assert( containee.getJ2EEType().equals( j2eeType ) );
301                 assert( containee.getName( ).equals( name ) );
302                 
303                 final Set JavaDoc<AMX> byName =
304                     container.getByNameContaineeSet( GSetUtil.newStringSet( j2eeType ), name );
305                 assert( byName.size() == 1 );
306                 assert( byName.iterator().next() == containee );
307             }
308         }
309     }
310         public void
311     checkGetByName( final AMX container )
312         throws Exception JavaDoc
313     {
314         checkGetByName( (Container)container );
315     }
316     
317     
318         public void
319     testMapAgreesWithSet()
320         throws Exception JavaDoc
321     {
322         testAllProxies( getAllContainers(), "checkMapAgreesWithSet" );
323     }
324     
325         public void
326     testContaineeMap()
327         throws Exception JavaDoc
328     {
329         testAllProxies( getAllContainers(), "checkContaineeMap" );
330     }
331     
332         public void
333     testContaineeSet()
334         throws Exception JavaDoc
335     {
336         testAllProxies( getAllContainers(), "checkContaineeSet" );
337     }
338     
339     
340         private <T extends AMX> boolean
341     setsEqual(
342         final Set JavaDoc<T> s1,
343         final Set JavaDoc<T> s2 )
344     {
345         final Set JavaDoc<ObjectName JavaDoc> t1 = Util.toObjectNames( s1 );
346         final Set JavaDoc<ObjectName JavaDoc> t2 = Util.toObjectNames( s2 );
347         
348         return t1.equals( t2 );
349     }
350     
351         private <T extends AMX> boolean
352     mapsEqual(
353         final Map JavaDoc<String JavaDoc,T> m1,
354         final Map JavaDoc<String JavaDoc,T> m2 )
355     {
356         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> t1 = Util.toObjectNames( m1 );
357         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> t2 = Util.toObjectNames( m2 );
358         
359         return t1.equals( t2 );
360     }
361     
362         private <T extends AMX> boolean
363     mapsOfMapsEqual(
364         final Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,T>> m1,
365         final Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,T>> m2 )
366     {
367         boolean equals = false;
368         
369         if ( m1.keySet().equals( m2.keySet() ) )
370         {
371             equals = true;
372             for( final String JavaDoc key : m1.keySet() )
373             {
374                 final Map JavaDoc<String JavaDoc,T> x1 = m1.get( key );
375                 final Map JavaDoc<String JavaDoc,T> x2 = m2.get( key );
376                 if ( ! mapsEqual( x1, x2 ) )
377                 {
378                     trace( "x1: " + MapUtil.toString( x1 ) );
379                     trace( "x2: " + MapUtil.toString( x2 ) );
380                     equals = false;
381                     break;
382                 }
383             }
384         }
385         
386         return equals;
387     }
388     
389     
390         private void
391     testContaineesOfType(
392         final Container c,
393         final String JavaDoc j2eeType )
394     {
395         final Set JavaDoc<String JavaDoc> j2eeTypes = c.getContaineeJ2EETypes();
396         
397         final Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,AMX>> all = c.getMultiContaineeMap( j2eeTypes );
398         final Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,AMX>> allFromNull = c.getMultiContaineeMap( null );
399         assert( mapsOfMapsEqual( all, allFromNull) );
400         
401         final Map JavaDoc<String JavaDoc,AMX> byType = c.getContaineeMap( j2eeType );
402         assert mapsEqual( byType, all.get( j2eeType ) );
403         
404         if ( byType.keySet().size() == 1 )
405         {
406             final AMX cc = c.getContainee( j2eeType );
407             assert cc == byType.values().iterator().next();
408         }
409         
410         final Set JavaDoc<AMX> s = c.getContaineeSet( j2eeType );
411         final Set JavaDoc<AMX> sByType = new HashSet JavaDoc<AMX>( byType.values() );
412         assert setsEqual( sByType, s ) ;
413         
414         final Set JavaDoc<String JavaDoc> nullSet = null;
415         
416         assert( setsEqual( c.getContaineeSet( nullSet ), c.getContaineeSet( j2eeTypes ) ) );
417         assert( setsEqual( c.getContaineeSet(), c.getContaineeSet( nullSet ) ) );
418         
419         for( final AMX amx : byType.values() )
420         {
421             final String JavaDoc itsName = amx.getName();
422             final String JavaDoc itsType = amx.getJ2EEType();
423             
424             final Set JavaDoc<String JavaDoc> types = GSetUtil.newStringSet( itsType );
425             final Set JavaDoc<AMX> x = c.getByNameContaineeSet( types, itsName );
426             
427             assert( x.size() == 1 );
428             assert( amx == x.iterator().next() );
429             assert( c.getContainee( itsType, itsName ) == amx );
430         }
431     }
432     
433         private void
434     testContainee( final Container c )
435     {
436         final Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,AMX>> all = c.getMultiContaineeMap( null );
437         
438         for( final String JavaDoc j2eeType : all.keySet() )
439         {
440             testContaineesOfType( c, j2eeType );
441         }
442     }
443     
444         public void
445     testContainees()
446     {
447         final Set JavaDoc<Container> containers = getAllContainers();
448         
449         for( final Container c : containers )
450         {
451             testContainee( c );
452         }
453         
454     }
455     
456     
457         public void
458     testGetByName()
459         throws Exception JavaDoc
460     {
461         testAllProxies( getAllContainers(), "checkGetByName" );
462     }
463     
464     
465         private Set JavaDoc<Container>
466     getAllContainers()
467     {
468         return getTestUtil().getAllAMX( Container.class );
469     }
470 }
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
Popular Tags