1 23 package com.sun.enterprise.management.config; 24 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.HashSet ; 28 import java.util.HashMap ; 29 30 import javax.management.ObjectName ; 31 import javax.management.JMException ; 32 33 34 import com.sun.enterprise.management.AMXTestBase; 35 import com.sun.enterprise.management.Capabilities; 36 37 import com.sun.appserv.management.base.Util; 38 import com.sun.appserv.management.base.QueryMgr; 39 40 import com.sun.appserv.management.config.CustomMBeanConfig; 41 import com.sun.appserv.management.config.DomainConfig; 42 import com.sun.appserv.management.config.PropertiesAccess; 43 44 import com.sun.enterprise.management.support.TestDummy; 45 import com.sun.enterprise.management.support.TestDummyMBean; 46 47 import com.sun.appserv.management.helper.RefHelper; 48 49 import com.sun.appserv.management.util.misc.CollectionUtil; 50 51 53 public final class CustomMBeanConfigTest extends AMXTestBase 54 { 55 private static final String IMPL_CLASSNAME = TestDummy.class.getName(); 57 58 private static final String TEST_NAME_BASE = "custom"; 59 private static final String TEST_TYPE = "CustomMBeanConfigTest"; 60 61 public 62 CustomMBeanConfigTest () 63 { 64 if ( checkNotOffline( "ensureDefaultInstance" ) ) 65 { 66 ensureDefaultInstance( getDomainConfig() ); 67 } 68 } 69 70 public static String 71 getDefaultInstanceName() 72 { 73 return getDefaultInstanceName( "CustomMBeanConfig" ); 74 } 75 76 public static CustomMBeanConfig 77 ensureDefaultInstance( final DomainConfig domainConfig ) 78 { 79 CustomMBeanConfig result = 80 domainConfig.getCustomMBeanConfigMap().get( getDefaultInstanceName() ); 81 82 if ( result == null ) 83 { 84 result = createInstance( 85 domainConfig, 86 getDefaultInstanceName(), 87 createProps() ); 88 } 89 90 return result; 91 } 92 93 public static CustomMBeanConfig 94 createInstance( 95 final DomainConfig domainConfig, 96 final String name, 97 final Map <String ,String > optional ) 98 { 99 final CustomMBeanConfig custom = 100 domainConfig.createCustomMBeanConfig( name, IMPL_CLASSNAME, 101 createObjectName( name ), 102 false, 103 optional ); 104 105 return custom; 106 } 107 108 109 public Map <String ,CustomMBeanConfig> 110 getCustomMBeanConfigs() 111 { 112 return getDomainConfig().getCustomMBeanConfigMap(); 113 } 114 115 private void 116 _testGetAll() 117 { 118 final Map <String ,CustomMBeanConfig> all = getCustomMBeanConfigs(); 119 assert( all != null ); 120 } 121 122 private void 123 sanityCheck( final CustomMBeanConfig config ) 124 { 125 final String objectName = config.getObjectNameInConfig(); 126 127 final String implClassname = config.getImplClassname(); 128 } 129 130 private synchronized void 131 _testAttrs() 132 { 133 final Map <String ,CustomMBeanConfig> all = getCustomMBeanConfigs(); 134 135 if ( all.size() != 0 ) 136 { 137 for( final CustomMBeanConfig config : all.values() ) 140 { 141 sanityCheck( config ); 142 } 143 } 144 else 145 { 146 warning( "CustomMBeanConfigTest: No custom MBeans to test" ); 147 } 148 } 149 150 151 154 private static Map <String ,String > 155 createProps() 156 { 157 final String PRP = PropertiesAccess.PROPERTY_PREFIX; 159 final Map <String ,String > optional = new HashMap <String ,String >(); 160 161 optional.put( PRP + "Attr1", "hello" ); 163 optional.put( PRP + "Attr2", "world" ); 164 165 return optional; 166 } 167 168 private static String 169 createObjectName( final String name ) 170 { 171 return CustomMBeanConfig.JMX_DOMAIN + ":name=" + name + 172 ",type=" + TEST_TYPE; 173 } 174 175 public synchronized CustomMBeanConfig 176 create( 177 final DomainConfig domainConfig, 178 final String name, 179 final Map <String ,String > optional ) 180 { 181 return createInstance( domainConfig, name, optional ); 182 } 183 184 public synchronized void 185 verifyPropsAdded( 186 final CustomMBeanConfig config, 187 final Map <String ,String > props ) 188 { 189 for( final String key : props.keySet() ) 190 { 191 if ( key.startsWith( PropertiesAccess.PROPERTY_PREFIX ) ) 192 { 193 final String specifiedValue = props.get( key ).toString(); 194 final String propName = key.substring( 195 PropertiesAccess.PROPERTY_PREFIX.length(), key.length()); 196 197 final String actualValue = config.getPropertyValue( propName ); 198 assert( specifiedValue.equals( actualValue ) ); 199 } 200 } 201 } 202 203 204 private void 205 removeCustomMBean( final String name ) 206 { 207 getDomainConfig().removeCustomMBeanConfig( name ); 208 } 209 210 private Set <ObjectName > 211 getRegisteredCustoms() 212 { 213 final QueryMgr queryMgr = getQueryMgr(); 214 final Set <ObjectName > mbeans = 215 queryMgr.queryPatternObjectNameSet( CustomMBeanConfig.JMX_DOMAIN, "type=" + TEST_TYPE ); 216 217 return mbeans; 218 } 219 220 private void 221 unregisterAnyTestMBeans() 222 { 223 final Set <ObjectName > customs = getRegisteredCustoms(); 224 for( final ObjectName objectName : customs ) 225 { 226 if ( TEST_TYPE.equals( objectName.getKeyProperty( "type" ) ) ) 227 { 228 try 229 { 230 getMBeanServerConnection().unregisterMBean( objectName ); 231 printVerbose( "unregistered: " + objectName ); 232 } 233 catch( Exception e ) 234 { 235 } 236 } 237 } 238 } 239 240 public synchronized void 241 testCreateRemove() 242 { 243 if ( ! checkNotOffline( "testCreateRemove" ) ) 244 { 245 return; 246 } 247 248 final DomainConfig domainConfig = getDomainConfig(); 249 250 final Map <String ,String > optional = createProps(); 251 252 final Set <CustomMBeanConfig> created = new HashSet <CustomMBeanConfig>(); 253 254 final Map <String ,CustomMBeanConfig> existing = 255 getDomainConfig().getCustomMBeanConfigMap(); 256 257 unregisterAnyTestMBeans(); 258 final Set <ObjectName > customsBefore = getRegisteredCustoms(); 259 if ( customsBefore.size() != 0 ) 260 { 261 printVerbose( "custom MBeans already registered:\n" + 262 CollectionUtil.toString( customsBefore, "\n" ) ); 263 } 264 265 final int NUM = 3; 266 for( int i = 0; i < NUM; ++i ) 267 { 268 final String testName = TEST_NAME_BASE + i; 269 270 if ( existing.containsKey( testName ) ) 271 { 272 RefHelper.removeAllRefsTo( existing.get( testName ), true ); 273 removeCustomMBean( testName ); 275 } 276 277 final CustomMBeanConfig config = 278 create( domainConfig, TEST_NAME_BASE + i, optional ); 279 281 assert( getCustomMBeanConfigs().get( config.getName() ) == config ); 282 283 created.add( config ); 284 sanityCheck( config ); 285 286 verifyPropsAdded( config, optional ); 287 } 288 289 _testGetAll(); 290 _testAttrs(); 291 292 for( final CustomMBeanConfig config : created ) 293 { 294 final String name = config.getName(); 296 removeCustomMBean( name ); 297 298 assert( getCustomMBeanConfigs().get( name ) == null ); 299 } 300 301 _testGetAll(); 302 303 mySleep( 100 ); 304 final Set <ObjectName > customsAfter = getRegisteredCustoms(); 305 customsAfter.removeAll( customsBefore ); 306 if ( customsAfter.size() != 0 ) 307 { 308 warning( "after removing custom MBeans, " + 309 "they are still registered (not an AMX bug):\n" + 310 CollectionUtil.toString( customsAfter, "\n" ) ); 311 } 312 unregisterAnyTestMBeans(); 313 } 314 } 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | Popular Tags |