1 23 24 29 package com.sun.enterprise.management.base; 30 31 import java.util.Set ; 32 import java.util.HashSet ; 33 import java.util.Map ; 34 import java.util.Iterator ; 35 import java.lang.reflect.Method ; 36 37 import javax.management.ObjectName ; 38 import javax.management.MBeanInfo ; 39 import javax.management.AttributeList ; 40 import javax.management.MBeanAttributeInfo ; 41 import javax.management.AttributeNotFoundException ; 42 import javax.management.MBeanServerConnection ; 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 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 containeeObjectName ) 91 throws Exception 92 { 93 final MBeanServerConnection 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 return; 102 } 103 104 final ObjectName containerObjectName = (ObjectName ) 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 <AMX> containees = container.getContaineeSet(); 130 final Set <ObjectName > 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 148 { 149 testAll( "checkContainerContainsChild" ); 150 } 151 152 153 public void 154 checkIsContainer( final ObjectName objectName ) 155 throws Exception 156 { 157 final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class); 158 159 try 160 { 161 final Set <String > 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 e ) 171 { 172 } 173 174 } 175 176 public void 177 testIsContainer() 178 throws Exception 179 { 180 testAll( "checkIsContainer" ); 181 } 182 183 private void 184 checkMapAgreesWithSet( final Container container ) 185 throws Exception 186 { 187 final Set <String > containedJ2EETypes = container.getContaineeJ2EETypes(); 188 189 for ( final String j2eeType : containedJ2EETypes ) 190 { 191 final Map <String ,AMX> containeeMap = 192 container.getContaineeMap( j2eeType ); 193 194 final Set <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 <String > namesSet = Util.getNames( containeeSet ); 203 assert( containeeMap.keySet().equals( namesSet ) ); 204 } 205 } 206 207 public void 208 checkMapAgreesWithSet( final AMX container ) 209 throws Exception 210 { 211 checkMapAgreesWithSet( (Container)container ); 212 } 213 214 private void 215 checkContaineeMap( final Container container ) 216 throws Exception 217 { 218 final Set <String > containedJ2EETypes = container.getContaineeJ2EETypes(); 219 assert( containedJ2EETypes != null ); 220 assert( ! containedJ2EETypes.contains( null ) ); 221 222 assert( container.getMultiContaineeMap( (Set <String >)null ) != null ); 223 assert( container.getMultiContaineeMap( containedJ2EETypes ) != null ); 224 225 for ( final String j2eeType : containedJ2EETypes ) 226 { 227 final Map <String ,AMX> containeeMap = 228 container.getContaineeMap( j2eeType ); 229 assert( containeeMap != null ) : 230 "getContaineeObjectNameMap failed for " + j2eeType ; 231 final Set <String > 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 240 { 241 checkContaineeMap( (Container)container ); 242 } 243 244 245 private void 246 checkContaineeSet( final Container container ) 247 throws Exception 248 { 249 final Set <String > containedJ2EETypes = container.getContaineeJ2EETypes(); 250 assert( containedJ2EETypes != null ); 251 assert( ! containedJ2EETypes.contains( null ) ); 252 assert( container.getContaineeSet() != null ); 253 254 for ( final String j2eeType : containedJ2EETypes ) 255 { 256 final Set <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 <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 276 { 277 checkContaineeSet( (Container)container ); 278 } 279 280 281 private void 282 checkGetByName( final Container container ) 283 throws Exception 284 { 285 final Set <String > containedJ2EETypes = container.getContaineeJ2EETypes(); 286 287 for ( final String j2eeType : containedJ2EETypes ) 288 { 289 final Map <String ,AMX> containeeMap = 290 container.getContaineeMap( j2eeType ); 291 292 for( final String 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 <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 313 { 314 checkGetByName( (Container)container ); 315 } 316 317 318 public void 319 testMapAgreesWithSet() 320 throws Exception 321 { 322 testAllProxies( getAllContainers(), "checkMapAgreesWithSet" ); 323 } 324 325 public void 326 testContaineeMap() 327 throws Exception 328 { 329 testAllProxies( getAllContainers(), "checkContaineeMap" ); 330 } 331 332 public void 333 testContaineeSet() 334 throws Exception 335 { 336 testAllProxies( getAllContainers(), "checkContaineeSet" ); 337 } 338 339 340 private <T extends AMX> boolean 341 setsEqual( 342 final Set <T> s1, 343 final Set <T> s2 ) 344 { 345 final Set <ObjectName > t1 = Util.toObjectNames( s1 ); 346 final Set <ObjectName > t2 = Util.toObjectNames( s2 ); 347 348 return t1.equals( t2 ); 349 } 350 351 private <T extends AMX> boolean 352 mapsEqual( 353 final Map <String ,T> m1, 354 final Map <String ,T> m2 ) 355 { 356 final Map <String ,ObjectName > t1 = Util.toObjectNames( m1 ); 357 final Map <String ,ObjectName > t2 = Util.toObjectNames( m2 ); 358 359 return t1.equals( t2 ); 360 } 361 362 private <T extends AMX> boolean 363 mapsOfMapsEqual( 364 final Map <String ,Map <String ,T>> m1, 365 final Map <String ,Map <String ,T>> m2 ) 366 { 367 boolean equals = false; 368 369 if ( m1.keySet().equals( m2.keySet() ) ) 370 { 371 equals = true; 372 for( final String key : m1.keySet() ) 373 { 374 final Map <String ,T> x1 = m1.get( key ); 375 final Map <String ,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 j2eeType ) 394 { 395 final Set <String > j2eeTypes = c.getContaineeJ2EETypes(); 396 397 final Map <String ,Map <String ,AMX>> all = c.getMultiContaineeMap( j2eeTypes ); 398 final Map <String ,Map <String ,AMX>> allFromNull = c.getMultiContaineeMap( null ); 399 assert( mapsOfMapsEqual( all, allFromNull) ); 400 401 final Map <String ,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 <AMX> s = c.getContaineeSet( j2eeType ); 411 final Set <AMX> sByType = new HashSet <AMX>( byType.values() ); 412 assert setsEqual( sByType, s ) ; 413 414 final Set <String > 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 itsName = amx.getName(); 422 final String itsType = amx.getJ2EEType(); 423 424 final Set <String > types = GSetUtil.newStringSet( itsType ); 425 final Set <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 <String ,Map <String ,AMX>> all = c.getMultiContaineeMap( null ); 437 438 for( final String j2eeType : all.keySet() ) 439 { 440 testContaineesOfType( c, j2eeType ); 441 } 442 } 443 444 public void 445 testContainees() 446 { 447 final Set <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 460 { 461 testAllProxies( getAllContainers(), "checkGetByName" ); 462 } 463 464 465 private Set <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 |