1 23 package com.sun.enterprise.management.util.jmx; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 28 import java.io.IOException ; 29 30 import javax.management.*; 31 32 import com.sun.appserv.management.util.jmx.NotificationListenerBase; 33 import com.sun.appserv.management.util.jmx.MBeanRegistrationListener; 34 import com.sun.appserv.management.util.jmx.JMXUtil; 35 36 37 public class NotificationListenerBaseTest extends JMXTestBase 38 { 39 private MBeanServer mServer; 40 41 private static final String TEST = "t"; 42 43 public 44 NotificationListenerBaseTest() 45 { 46 mServer = null; 47 } 48 49 protected ObjectName 50 registerMBean( final Object mbean, final ObjectName objectName ) 51 throws InstanceAlreadyExistsException, 52 NotCompliantMBeanException, MBeanRegistrationException 53 { 54 return mServer.registerMBean( mbean, objectName ).getObjectName(); 55 } 56 57 public void 58 setUp() throws Exception 59 { 60 mServer = MBeanServerFactory.newMBeanServer( "test" ); 61 } 62 63 public void 64 tearDown() 65 throws Exception 66 { 67 mServer = null; 68 } 69 70 private static final class MyListener extends NotificationListenerBase 71 { 72 public 73 MyListener( 74 final MBeanServerConnection conn, 75 final ObjectName pattern ) 76 throws InstanceNotFoundException, IOException 77 { 78 super( conn, pattern, null ); 79 } 80 81 public 82 MyListener( 83 final MBeanServerConnection conn, 84 final ObjectName pattern, 85 final NotificationFilter filter ) 86 throws InstanceNotFoundException, IOException 87 { 88 super( conn, pattern, filter ); 89 } 90 91 public void 92 handleNotification( final Notification notif, final Object handback) 93 { 94 } 95 } 96 97 98 private MyListener 99 create( final String pattern ) 100 throws Exception 101 { 102 final MyListener listener = 103 new MyListener( mServer, JMXUtil.newObjectName( pattern ), null ); 104 105 return listener; 106 } 107 108 public void 109 testCreate() 110 throws Exception 111 { 112 final MyListener b = create( "*:*" ); 113 assert( b.getListenees().size() != 0 ); 114 b.getMBeanServerConnection(); 115 b.cleanup(); 116 assert( b.getListenees().size() == 0 ); 117 } 118 119 public interface DummyMBean extends NotificationEmitter 120 { 121 int getDummy(); 122 void setDummy( int value ); 123 } 124 125 public static final class Dummy 126 extends NotificationBroadcasterSupport 127 implements DummyMBean 128 { 129 private int mDummy; 130 131 public int getDummy() { return mDummy; } 132 public void setDummy( int value ) { mDummy = value; } 133 } 134 135 static private final String DUMMY_OBJECT_NAME_PREFIX = TEST + ":type=dummy"; 136 137 138 private ObjectName 139 createObjectName( 140 final String domain, 141 final int id, 142 final String name ) 143 { 144 String s = domain + ":" + 145 "name=" + name + 146 ",id=" + id + 147 (",category=" + ((id % 2) == 0 ? "even":"odd")); 148 149 return JMXUtil.newObjectName( s ); 150 } 151 152 Set <ObjectName> 153 registerDummies( 154 final int count, 155 final String name ) 156 throws Exception 157 { 158 return registerDummies( TEST, count, name ); 159 } 160 161 Set <ObjectName> 162 registerDummies( 163 final String domain, 164 final int count, 165 final String name ) 166 throws Exception 167 { 168 final Set <ObjectName> s = new HashSet <ObjectName>(); 169 170 for( int i = 0; i < count; ++i ) 171 { 172 final Dummy d = new Dummy(); 173 d.setDummy( i ); 174 175 final ObjectName on = createObjectName( domain, i, name ); 176 final ObjectName objectName = registerMBean( d, on ); 177 178 s.add( objectName ); 179 } 180 181 return s; 182 } 183 184 public void 185 testGetListenees() 186 throws Exception 187 { 188 final int NUM = 10; 189 final String NAME = "bbb"; 190 191 registerDummies( NUM, NAME ); 192 193 final MyListener odd = create( TEST + ":category=odd,*" ); 194 assert( odd.getListenees().size() == NUM / 2 ); 195 for( final ObjectName item : odd.getListenees() ) 196 { 197 assert( item.getKeyProperty( "category" ).equals( "odd" ) ); 198 } 199 assert( odd.isAlive() ); 200 201 final MyListener even = create( TEST + ":category=even,*" ); 202 assert( even.getListenees().size() == NUM / 2 ); 203 for( final ObjectName item : even.getListenees() ) 204 { 205 assert( item.getKeyProperty( "category" ).equals( "even" ) ); 206 } 207 assert( even.isAlive() ); 208 209 final MyListener all = create( TEST + ":name=bbb,*" ); 210 assert( all.getListenees().size() == NUM ); 211 for( final ObjectName item : all.getListenees() ) 212 { 213 assert( item.getKeyProperty( "name" ).equals( NAME ) ); 214 } 215 assert( all.isAlive() ); 216 } 217 218 219 private final class MyRegListener extends MBeanRegistrationListener 220 { 221 public int mRegCount = 0; 222 public int mUnregCount = 0; 223 224 public 225 MyRegListener( final ObjectName constrain ) 226 throws InstanceNotFoundException, IOException 227 { 228 super( mServer, constrain ); 229 } 230 231 protected void 232 mbeanRegistered( final ObjectName objectName ) 233 { 234 ++mRegCount; 235 } 236 237 protected void 238 mbeanUnregistered( final ObjectName objectName ) 239 { 240 ++mUnregCount; 241 } 242 } 243 244 private void 245 testListenForRegistration( 246 final String domain, 247 final String name, 248 final int count, 249 final String constrainStr) 250 throws Exception 251 { 252 final ObjectName constrain = (constrainStr == null) ? null : 253 JMXUtil.newObjectName( constrainStr ); 254 255 final MyRegListener listener = new MyRegListener( constrain ); 256 assert( listener.getListenees().size() == 1 ); 257 258 final Set <ObjectName> s = registerDummies( domain, count, name ); 259 assert( listener.mRegCount == count ); 260 261 for( final ObjectName objectName : s ) 262 { 263 mServer.unregisterMBean( objectName ); 264 } 265 assert( listener.mUnregCount == count ); 266 listener.cleanup(); 267 assert( listener.getListenees().size() == 0 ); 268 269 } 270 271 272 static private final String REG_DOMAIN = "LFR"; 273 static private final int REG_COUNT = 1000; 274 275 public void 276 testListenForRegistration1() 277 throws Exception 278 { 279 final String NAME = "zzz"; 280 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null ); 281 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*"); 282 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*"); 283 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME); 284 } 285 286 public void 287 testListenForRegistration2() 288 throws Exception 289 { 290 final String NAME = "zzzz"; 291 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null ); 292 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*"); 293 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*"); 294 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME); 295 } 296 297 public void 298 testListenForRegistration3() 299 throws Exception 300 { 301 final String NAME = "zzzzz"; 302 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, null ); 303 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:*"); 304 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, REG_DOMAIN +":*"); 305 testListenForRegistration( REG_DOMAIN, NAME, REG_COUNT, "*:" + "name=" + NAME); 306 } 307 } 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | Popular Tags |