1 41 42 package com.sun.jmx.examples.scandir; 43 44 import java.util.concurrent.LinkedBlockingQueue ; 45 import java.util.concurrent.TimeUnit ; 46 import javax.management.InstanceNotFoundException ; 47 import javax.management.Notification ; 48 import junit.framework.*; 49 import static com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.*; 50 import com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState; 51 import java.io.IOException ; 52 import java.lang.management.ManagementFactory ; 53 import java.util.EnumSet ; 54 import java.util.HashMap ; 55 import java.util.logging.Logger ; 56 import javax.management.AttributeChangeNotification ; 57 import javax.management.JMException ; 58 import javax.management.JMX ; 59 import javax.management.ListenerNotFoundException ; 60 import javax.management.MBeanNotificationInfo ; 61 import javax.management.MBeanRegistration ; 62 import javax.management.MBeanServer ; 63 import javax.management.MBeanServerConnection ; 64 import javax.management.NotificationBroadcasterSupport ; 65 import javax.management.NotificationEmitter ; 66 import javax.management.NotificationFilter ; 67 import javax.management.NotificationListener ; 68 import javax.management.ObjectInstance ; 69 import javax.management.ObjectName ; 70 71 import static com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.*; 72 73 78 public class ScanManagerTest extends TestCase { 79 80 public ScanManagerTest(String testName) { 81 super(testName); 82 } 83 84 protected void setUp() throws Exception { 85 } 86 87 protected void tearDown() throws Exception { 88 } 89 90 public static Test suite() { 91 TestSuite suite = new TestSuite(ScanManagerTest.class); 92 93 return suite; 94 } 95 96 99 public void testMakeSingletonName() { 100 System.out.println("makeSingletonName"); 101 102 Class clazz = ScanManagerMXBean.class; 103 104 ObjectName expResult = ScanManager.SCAN_MANAGER_NAME; 105 ObjectName result = ScanManager.makeSingletonName(clazz); 106 assertEquals(expResult, result); 107 108 } 109 110 113 public void testRegister() throws Exception { 114 System.out.println("register"); 115 116 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 117 118 119 ScanManagerMXBean result = ScanManager.register(mbs); 120 try { 121 assertEquals(STOPPED,result.getState()); 122 } finally { 123 try { 124 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 125 } catch (Exception x) { 126 System.err.println("Failed to cleanup: "+x); 127 } 128 } 129 130 } 131 132 public interface Call { 133 public void call() throws Exception ; 134 public void cancel() throws Exception ; 135 } 136 137 140 public void testAddNotificationListener() throws Exception { 141 System.out.println("addNotificationListener"); 142 143 final ScanManagerMXBean manager = ScanManager.register(); 144 final Call op = new Call() { 145 public void call() throws Exception { 146 manager.schedule(100000,0); 147 } 148 public void cancel() throws Exception { 149 manager.stop(); 150 } 151 }; 152 try { 153 doTestOperation(manager,op, 154 EnumSet.of(RUNNING,SCHEDULED), 155 "schedule"); 156 } finally { 157 try { 158 ManagementFactory.getPlatformMBeanServer(). 159 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 160 } catch (Exception x) { 161 System.err.println("Failed to cleanup: "+x); 162 } 163 } 164 } 165 166 169 private void doTestOperation( 170 ScanManagerMXBean proxy, 171 Call op, 172 EnumSet <ScanState> after, 173 String testName) 174 throws Exception { 175 System.out.println("doTestOperation: "+testName); 176 177 final LinkedBlockingQueue <Notification > queue = 178 new LinkedBlockingQueue <Notification >(); 179 180 NotificationListener listener = new NotificationListener () { 181 public void handleNotification(Notification notification, 182 Object handback) { 183 try { 184 queue.put(notification); 185 } catch (Exception x) { 186 System.err.println("Failed to queue notif: "+x); 187 } 188 } 189 }; 190 NotificationFilter filter = null; 191 Object handback = null; 192 final ScanState before; 193 final NotificationEmitter emitter = (NotificationEmitter )proxy; 194 emitter.addNotificationListener(listener, filter, handback); 195 before = proxy.getState(); 196 op.call(); 197 try { 198 final Notification notification = 199 queue.poll(3000,TimeUnit.MILLISECONDS); 200 assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, 201 notification.getType()); 202 assertEquals(AttributeChangeNotification .class, 203 notification.getClass()); 204 assertEquals(ScanManager.SCAN_MANAGER_NAME, 205 notification.getSource()); 206 AttributeChangeNotification acn = 207 (AttributeChangeNotification )notification; 208 assertEquals("State",acn.getAttributeName()); 209 assertEquals(ScanState.class.getName(),acn.getAttributeType()); 210 assertEquals(before,ScanState.valueOf((String )acn.getOldValue())); 211 assertContained(after,ScanState.valueOf((String )acn.getNewValue())); 212 emitter.removeNotificationListener(listener,filter,handback); 213 } finally { 214 try { 215 op.cancel(); 216 } catch (Exception x) { 217 System.err.println("Failed to cleanup: "+x); 218 } 219 } 220 } 221 222 225 public void testPreRegister() throws Exception { 226 System.out.println("preRegister"); 227 228 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 229 ObjectName name = new ObjectName ("DownUnder:type=Wombat"); 230 ScanManager instance = new ScanManager(); 231 232 ObjectName expResult = ScanManager.SCAN_MANAGER_NAME; 233 ObjectName result; 234 try { 235 result = instance.preRegister(server, name); 236 throw new RuntimeException ("bad name accepted!"); 237 } catch (IllegalArgumentException x) { 238 result = instance.preRegister(server, null); 240 } 241 assertEquals(expResult, result); 242 result = instance.preRegister(server, ScanManager.SCAN_MANAGER_NAME); 243 assertEquals(expResult, result); 244 } 245 246 247 250 public void testGetState() throws IOException , InstanceNotFoundException { 251 System.out.println("getState"); 252 253 ScanManager instance = new ScanManager(); 254 255 ScanState expResult = ScanState.STOPPED; 256 ScanState result = instance.getState(); 257 assertEquals(expResult, result); 258 instance.start(); 259 final ScanState afterStart = instance.getState(); 260 assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED),afterStart); 261 instance.stop(); 262 assertEquals(STOPPED,instance.getState()); 263 instance.schedule(1000000L,1000000L); 264 assertEquals(SCHEDULED,instance.getState()); 265 instance.stop(); 266 assertEquals(STOPPED,instance.getState()); 267 } 268 269 272 public void testSchedule() throws Exception { 273 System.out.println("schedule"); 274 275 final long delay = 10000L; 276 final long interval = 10000L; 277 278 final ScanManagerMXBean manager = ScanManager.register(); 279 final Call op = new Call() { 280 public void call() throws Exception { 281 manager.schedule(delay,interval); 282 assertEquals(SCHEDULED,manager.getState()); 283 } 284 public void cancel() throws Exception { 285 manager.stop(); 286 } 287 }; 288 try { 289 doTestOperation(manager,op,EnumSet.of(SCHEDULED), 290 "schedule"); 291 } finally { 292 try { 293 ManagementFactory.getPlatformMBeanServer(). 294 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 295 } catch (Exception x) { 296 System.err.println("Failed to cleanup: "+x); 297 } 298 } 299 } 300 301 public static void assertContained(EnumSet <ScanState> allowed, 302 ScanState state) { 303 final String msg = String.valueOf(state) + " is not one of " + allowed; 304 assertTrue(msg,allowed.contains(state)); 305 } 306 307 310 public void testStop() throws Exception { 311 System.out.println("stop"); 312 final ScanManagerMXBean manager = ScanManager.register(); 313 try { 314 manager.schedule(1000000,0); 315 assertContained(EnumSet.of(SCHEDULED),manager.getState()); 316 final Call op = new Call() { 317 public void call() throws Exception { 318 manager.stop(); 319 assertEquals(STOPPED,manager.getState()); 320 } 321 public void cancel() throws Exception { 322 if (manager.getState() != STOPPED) 323 manager.stop(); 324 } 325 }; 326 doTestOperation(manager,op,EnumSet.of(STOPPED),"stop"); 327 } finally { 328 try { 329 ManagementFactory.getPlatformMBeanServer(). 330 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 331 } catch (Exception x) { 332 System.err.println("Failed to cleanup: "+x); 333 } 334 } 335 } 336 337 340 public void testStart() throws Exception { 341 final ScanManagerMXBean manager = ScanManager.register(); 342 try { 343 final Call op = new Call() { 344 public void call() throws Exception { 345 assertEquals(STOPPED,manager.getState()); 346 manager.start(); 347 assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED), 348 manager.getState()); 349 } 350 public void cancel() throws Exception { 351 manager.stop(); 352 } 353 }; 354 doTestOperation(manager,op,EnumSet.of(RUNNING,SCHEDULED,COMPLETED), 355 "start"); 356 } finally { 357 try { 358 ManagementFactory.getPlatformMBeanServer(). 359 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 360 } catch (Exception x) { 361 System.err.println("Failed to cleanup: "+x); 362 } 363 } 364 } 365 366 } 367 | Popular Tags |