1 41 42 package com.sun.jmx.examples.scandir; 43 44 import com.sun.jmx.examples.scandir.config.DirectoryScannerConfig; 45 import com.sun.jmx.examples.scandir.config.ResultRecord; 46 import com.sun.jmx.examples.scandir.config.ScanManagerConfig; 47 import java.util.LinkedList ; 48 import java.util.concurrent.BlockingQueue ; 49 import junit.framework.*; 50 import com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState; 51 import static com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.*; 52 import com.sun.jmx.examples.scandir.ScanManagerTest.Call; 53 import java.util.EnumSet ; 54 import java.util.concurrent.LinkedBlockingQueue ; 55 import java.util.concurrent.TimeUnit ; 56 import javax.management.AttributeChangeNotification ; 57 import javax.management.Notification ; 58 import javax.management.NotificationEmitter ; 59 import javax.management.NotificationFilter ; 60 import javax.management.NotificationListener ; 61 62 import static com.sun.jmx.examples.scandir.ScanManagerTest.*; 63 import static com.sun.jmx.examples.scandir.TestUtils.*; 64 import java.io.File ; 65 import java.lang.management.ManagementFactory ; 66 import java.util.List ; 67 68 73 public class DirectoryScannerTest extends TestCase { 74 75 public DirectoryScannerTest(String testName) { 76 super(testName); 77 } 78 79 protected void setUp() throws Exception { 80 } 81 82 protected void tearDown() throws Exception { 83 } 84 85 public static Test suite() { 86 TestSuite suite = new TestSuite(DirectoryScannerTest.class); 87 88 return suite; 89 } 90 91 private void doTestOperation( 92 DirectoryScannerMXBean proxy, 93 Call op, 94 EnumSet <ScanState> after, 95 String testName) 96 throws Exception { 97 System.out.println("doTestOperation: "+testName); 98 99 final LinkedBlockingQueue <Notification > queue = 100 new LinkedBlockingQueue <Notification >(); 101 102 NotificationListener listener = new NotificationListener () { 103 public void handleNotification(Notification notification, 104 Object handback) { 105 try { 106 queue.put(notification); 107 } catch (Exception x) { 108 System.err.println("Failed to queue notif: "+x); 109 } 110 } 111 }; 112 NotificationFilter filter = null; 113 Object handback = null; 114 final ScanState before; 115 final NotificationEmitter emitter = (NotificationEmitter ) 116 makeNotificationEmitter(proxy,DirectoryScannerMXBean.class); 117 emitter.addNotificationListener(listener, filter, handback); 118 before = proxy.getState(); 119 op.call(); 120 try { 121 final Notification notification = 122 queue.poll(3000,TimeUnit.MILLISECONDS); 123 assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, 124 notification.getType()); 125 assertEquals(AttributeChangeNotification .class, 126 notification.getClass()); 127 assertEquals(getObjectName(proxy), 128 notification.getSource()); 129 AttributeChangeNotification acn = 130 (AttributeChangeNotification )notification; 131 assertEquals("State",acn.getAttributeName()); 132 assertEquals(ScanState.class.getName(),acn.getAttributeType()); 133 assertEquals(before,ScanState.valueOf((String )acn.getOldValue())); 134 assertContained(after,ScanState.valueOf((String )acn.getNewValue())); 135 emitter.removeNotificationListener(listener,filter,handback); 136 } finally { 137 try { 138 op.cancel(); 139 } catch (Exception x) { 140 System.err.println("Failed to cleanup: "+x); 141 } 142 } 143 } 144 145 146 149 public void testGetRootDirectory() throws Exception { 150 System.out.println("getRootDirectory"); 151 152 final ScanManagerMXBean manager = ScanManager.register(); 153 try { 154 final String tmpdir = System.getProperty("java.io.tmpdir"); 155 final ScanDirConfigMXBean config = manager.getConfigurationMBean(); 156 System.err.println("Configuration MXBean is: " + config); 157 final DirectoryScannerConfig bean = 158 config.addDirectoryScanner("test",tmpdir,".*",0,0); 159 final String root = bean.getRootDirectory(); 160 if (root == null) 161 throw new NullPointerException ("bean.getRootDirectory()"); 162 if (config.getConfiguration().getScan("test").getRootDirectory() == null) 163 throw new NullPointerException ("config.getConfig().getScan(\"test\").getRootDirectory()"); 164 manager.applyConfiguration(true); 165 final DirectoryScannerMXBean proxy = 166 manager.getDirectoryScanners().get("test"); 167 final File tmpFile = new File (tmpdir); 168 final File rootFile = new File (proxy.getRootDirectory()); 169 assertEquals(tmpFile,rootFile); 170 } catch (Exception x) { 171 x.printStackTrace(); 172 throw x; 173 } finally { 174 try { 175 ManagementFactory.getPlatformMBeanServer(). 176 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 177 } catch (Exception x) { 178 System.err.println("Failed to cleanup: "+x); 179 } 180 } 181 } 182 183 184 187 public void testScan() throws Exception { 188 System.out.println("scan"); 189 190 final ScanManagerMXBean manager = ScanManager.register(); 191 try { 192 final String tmpdir = System.getProperty("java.io.tmpdir"); 193 final ScanDirConfigMXBean config = manager.getConfigurationMBean(); 194 final DirectoryScannerConfig bean = 195 config.addDirectoryScanner("test1",tmpdir,".*",0,0); 196 config.addDirectoryScanner("test2",tmpdir,".*",0,0); 197 config.addDirectoryScanner("test3",tmpdir,".*",0,0); 198 manager.applyConfiguration(true); 199 final DirectoryScannerMXBean proxy = 200 manager.getDirectoryScanners().get("test1"); 201 final Call op = new Call() { 202 public void call() throws Exception { 203 final BlockingQueue <Notification > queue = 204 new LinkedBlockingQueue <Notification >(); 205 final NotificationListener listener = new NotificationListener () { 206 public void handleNotification(Notification notification, 207 Object handback) { 208 try { 209 queue.put(notification); 210 } catch (Exception e) { 211 e.printStackTrace(); 212 } 213 } 214 }; 215 manager.start(); 216 while(true) { 217 final Notification n = queue.poll(10,TimeUnit.SECONDS); 218 if (n == null) break; 219 final AttributeChangeNotification at = 220 (AttributeChangeNotification ) n; 221 if (RUNNING == ScanState.valueOf((String )at.getNewValue())) 222 break; 223 else { 224 System.err.println("New state: "+(String )at.getNewValue() 225 +" isn't "+RUNNING); 226 } 227 } 228 assertContained(EnumSet.of(SCHEDULED,RUNNING,COMPLETED), 229 proxy.getState()); 230 } 231 public void cancel() throws Exception { 232 manager.stop(); 233 } 234 }; 235 doTestOperation(proxy,op, 236 EnumSet.of(RUNNING,SCHEDULED,COMPLETED), 237 "scan"); 238 } catch (Exception x) { 239 x.printStackTrace(); 240 throw x; 241 } finally { 242 try { 243 manager.stop(); 244 } catch (Exception x) { 245 System.err.println("Failed to stop: "+x); 246 } 247 try { 248 ManagementFactory.getPlatformMBeanServer(). 249 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 250 } catch (Exception x) { 251 System.err.println("Failed to cleanup: "+x); 252 } 253 } 254 } 255 256 259 public void testGetState() { 260 System.out.println("getState"); 261 262 final DirectoryScannerConfig bean = 263 new DirectoryScannerConfig("test"); 264 bean.setRootDirectory(System.getProperty("java.io.tmpdir")); 265 final ResultLogManager log = new ResultLogManager(); 266 DirectoryScanner instance = 267 new DirectoryScanner(bean,log); 268 269 ScanState expResult = STOPPED; 270 ScanState result = instance.getState(); 271 assertEquals(STOPPED, result); 272 instance.scan(); 273 result = instance.getState(); 274 assertEquals(COMPLETED, result); 275 } 276 277 280 public void testAddNotificationListener() throws Exception { 281 System.out.println("addNotificationListener"); 282 283 final ScanManagerMXBean manager = ScanManager.register(); 284 final Call op = new Call() { 285 public void call() throws Exception { 286 manager.start(); 287 } 288 public void cancel() throws Exception { 289 manager.stop(); 290 } 291 }; 292 try { 293 final String tmpdir = System.getProperty("java.io.tmpdir"); 294 final ScanDirConfigMXBean config = manager.getConfigurationMBean(); 295 final DirectoryScannerConfig bean = 296 config.addDirectoryScanner("test1",tmpdir,".*",0,0); 297 manager.applyConfiguration(true); 298 final DirectoryScannerMXBean proxy = 299 manager.getDirectoryScanners().get("test1"); 300 doTestOperation(proxy,op, 301 EnumSet.of(RUNNING,SCHEDULED), 302 "scan"); 303 } finally { 304 try { 305 ManagementFactory.getPlatformMBeanServer(). 306 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 307 } catch (Exception x) { 308 System.err.println("Failed to cleanup: "+x); 309 } 310 } 311 } 312 313 314 } 315 | Popular Tags |