1 41 42 package com.sun.jmx.examples.scandir; 43 44 import com.sun.jmx.examples.scandir.config.XmlConfigUtils; 45 import com.sun.jmx.examples.scandir.config.FileMatch; 46 import java.io.FileOutputStream ; 47 import java.io.IOException ; 48 import java.lang.management.ManagementFactory ; 49 import java.util.concurrent.LinkedBlockingQueue ; 50 import java.util.concurrent.TimeUnit ; 51 import junit.framework.*; 52 import com.sun.jmx.examples.scandir.config.DirectoryScannerConfig; 53 import com.sun.jmx.examples.scandir.config.ScanManagerConfig; 54 import java.io.File ; 55 import java.util.concurrent.BlockingQueue ; 56 import javax.management.*; 57 58 63 public class ScanDirConfigTest extends TestCase { 64 65 public ScanDirConfigTest(String testName) { 66 super(testName); 67 } 68 69 protected void setUp() throws Exception { 70 } 71 72 protected void tearDown() throws Exception { 73 } 74 75 public static Test suite() { 76 TestSuite suite = new TestSuite(ScanDirConfigTest.class); 77 78 return suite; 79 } 80 81 84 public void testLoad() throws Exception { 85 System.out.println("load"); 86 87 final File file = File.createTempFile("testconf",".xml"); 88 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 89 final ScanManagerConfig bean = 90 new ScanManagerConfig("testLoad"); 91 final DirectoryScannerConfig dir = 92 new DirectoryScannerConfig("tmp"); 93 dir.setRootDirectory(file.getParent()); 94 bean.putScan(dir); 95 XmlConfigUtils.write(bean,new FileOutputStream (file),false); 96 instance.load(); 97 98 assertEquals(bean,instance.getConfiguration()); 99 bean.removeScan(dir.getName()); 100 XmlConfigUtils.write(bean,new FileOutputStream (file),false); 101 102 assertNotSame(bean,instance.getConfiguration()); 103 104 instance.load(); 105 106 assertEquals(bean,instance.getConfiguration()); 107 108 } 109 110 113 public void testSave() throws Exception { 114 System.out.println("save"); 115 116 final File file = File.createTempFile("testconf",".xml"); 117 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 118 final ScanManagerMXBean manager = ScanManager.register(mbs); 119 120 try { 121 final ScanDirConfigMXBean instance = 122 manager.createOtherConfigurationMBean("testSave",file.getAbsolutePath()); 123 assertTrue(mbs.isRegistered( 124 ScanManager.makeScanDirConfigName("testSave"))); 125 final ScanManagerConfig bean = 126 new ScanManagerConfig("testSave"); 127 final DirectoryScannerConfig dir = 128 new DirectoryScannerConfig("tmp"); 129 dir.setRootDirectory(file.getParent()); 130 bean.putScan(dir); 131 instance.setConfiguration(bean); 132 instance.save(); 133 final ScanManagerConfig loaded = 134 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 135 assertEquals(instance.getConfiguration(),loaded); 136 assertEquals(bean,loaded); 137 138 instance.getConfiguration().removeScan("tmp"); 139 instance.save(); 140 assertNotSame(loaded,instance.getConfiguration()); 141 final ScanManagerConfig loaded2 = 142 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 143 assertEquals(instance.getConfiguration(),loaded2); 144 } finally { 145 manager.close(); 146 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 147 } 148 final ObjectName all = 149 new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain()+":*"); 150 assertEquals(0,mbs.queryNames(all,null).size()); 151 } 152 153 156 169 170 173 public void testGetXmlConfigString() throws Exception { 174 System.out.println("getXmlConfigString"); 175 176 try { 177 final File file = File.createTempFile("testconf",".xml"); 178 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 179 final ScanManagerConfig bean = 180 new ScanManagerConfig("testGetXmlConfigString"); 181 final DirectoryScannerConfig dir = 182 new DirectoryScannerConfig("tmp"); 183 dir.setRootDirectory(file.getParent()); 184 bean.putScan(dir); 185 instance.setConfiguration(bean); 186 System.out.println("Expected: " + XmlConfigUtils.toString(bean)); 187 System.out.println("Received: " + 188 instance.getConfiguration().toString()); 189 assertEquals(XmlConfigUtils.toString(bean), 190 instance.getConfiguration().toString()); 191 } catch (Exception x) { 192 x.printStackTrace(); 193 throw x; 194 } 195 } 196 197 198 202 public void testAddNotificationListener() throws Exception { 203 System.out.println("addNotificationListener"); 204 205 final File file = File.createTempFile("testconf",".xml"); 206 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 207 final ScanManagerMXBean manager = ScanManager.register(mbs); 208 209 try { 210 final ScanDirConfigMXBean instance = 211 TestUtils.makeNotificationEmitter( 212 manager.createOtherConfigurationMBean("testSave", 213 file.getAbsolutePath()), 214 ScanDirConfigMXBean.class); 215 assertTrue(mbs.isRegistered( 216 ScanManager.makeScanDirConfigName("testSave"))); 217 DirectoryScannerConfig dir = 218 instance.addDirectoryScanner("tmp",file.getParent(),".*",0,0); 219 220 final BlockingQueue <Notification> queue = 221 new LinkedBlockingQueue <Notification>(); 222 final NotificationListener listener = new NotificationListener() { 223 public void handleNotification(Notification notification, 224 Object handback) { 225 queue.add(notification); 226 } 227 }; 228 NotificationFilter filter = null; 229 Object handback = null; 230 231 ((NotificationEmitter)instance).addNotificationListener(listener, 232 filter, handback); 233 234 instance.save(); 235 final ScanManagerConfig loaded = 236 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 237 assertEquals(instance.getConfiguration(),loaded); 238 239 final ScanManagerConfig newConfig = 240 instance.getConfiguration(); 241 newConfig.removeScan("tmp"); 242 instance.setConfiguration(newConfig); 243 instance.save(); 244 assertNotSame(loaded,instance.getConfiguration()); 245 final ScanManagerConfig loaded2 = 246 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 247 assertEquals(instance.getConfiguration(),loaded2); 248 instance.load(); 249 for (int i=0;i<4;i++) { 250 final Notification n = queue.poll(3,TimeUnit.SECONDS); 251 assertNotNull(n); 252 assertEquals(TestUtils.getObjectName(instance),n.getSource()); 253 switch(i) { 254 case 0: case 2: 255 assertEquals(ScanDirConfig.NOTIFICATION_SAVED,n.getType()); 256 break; 257 case 1: 258 assertEquals(ScanDirConfig.NOTIFICATION_MODIFIED,n.getType()); 259 break; 260 case 3: 261 assertEquals(ScanDirConfig.NOTIFICATION_LOADED,n.getType()); 262 break; 263 default: break; 264 } 265 } 266 } finally { 267 manager.close(); 268 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 269 } 270 final ObjectName all = 271 new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain()+":*"); 272 assertEquals(0,mbs.queryNames(all,null).size()); 273 } 274 275 279 public void testGetConfigFilename() throws Exception { 280 System.out.println("getConfigFilename"); 281 282 final File file = File.createTempFile("testconf",".xml"); 283 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 284 285 String result = instance.getConfigFilename(); 286 assertEquals(file.getAbsolutePath(), new File (result).getAbsolutePath()); 287 288 } 289 290 294 public void testAddDirectoryScanner() throws IOException { 295 System.out.println("addDirectoryScanner"); 296 297 System.out.println("save"); 298 299 final File file = File.createTempFile("testconf",".xml"); 300 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 301 final ScanManagerConfig bean = 302 new ScanManagerConfig("testSave"); 303 final DirectoryScannerConfig dir = 304 new DirectoryScannerConfig("tmp"); 305 dir.setRootDirectory(file.getParent()); 306 FileMatch filter = new FileMatch(); 307 filter.setFilePattern(".*"); 308 dir.setIncludeFiles(new FileMatch[] { 309 filter 310 }); 311 instance.setConfiguration(bean); 312 instance.addDirectoryScanner(dir.getName(), 313 dir.getRootDirectory(), 314 filter.getFilePattern(), 315 filter.getSizeExceedsMaxBytes(), 316 0); 317 instance.save(); 318 final ScanManagerConfig loaded = 319 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 320 assertNotNull(loaded.getScan(dir.getName())); 321 assertEquals(dir,loaded.getScan(dir.getName())); 322 assertEquals(instance.getConfiguration(),loaded); 323 assertEquals(instance.getConfiguration().getScan(dir.getName()),dir); 324 } 325 326 } 327 | Popular Tags |