1 /* 2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 package com.sun.jmx.examples.scandir; 42 43 import com.sun.jmx.examples.scandir.config.XmlConfigUtils; 44 import com.sun.jmx.examples.scandir.config.FileMatch; 45 import java.io.FileOutputStream; 46 import java.io.IOException; 47 import java.lang.management.ManagementFactory; 48 import java.util.concurrent.LinkedBlockingQueue; 49 import java.util.concurrent.TimeUnit; 50 import junit.framework.*; 51 import com.sun.jmx.examples.scandir.config.DirectoryScannerConfig; 52 import com.sun.jmx.examples.scandir.config.ScanManagerConfig; 53 import java.io.File; 54 import java.util.concurrent.BlockingQueue; 55 import javax.management.*; 56 57 /** 58 * Unit tests for {@code ScanDirConfig} 59 * 60 * @author Sun Microsystems, 2006 - All rights reserved. 61 */ 62 public class ScanDirConfigTest extends TestCase { 63 64 public ScanDirConfigTest(String testName) { 65 super(testName); 66 } 67 68 protected void setUp() throws Exception { 69 } 70 71 protected void tearDown() throws Exception { 72 } 73 74 public static Test suite() { 75 TestSuite suite = new TestSuite(ScanDirConfigTest.class); 76 77 return suite; 78 } 79 80 /** 81 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig. 82 */ 83 public void testLoad() throws Exception { 84 System.out.println("load"); 85 86 final File file = File.createTempFile("testconf",".xml"); 87 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 88 final ScanManagerConfig bean = 89 new ScanManagerConfig("testLoad"); 90 final DirectoryScannerConfig dir = 91 new DirectoryScannerConfig("tmp"); 92 dir.setRootDirectory(file.getParent()); 93 bean.putScan(dir); 94 XmlConfigUtils.write(bean,new FileOutputStream(file),false); 95 instance.load(); 96 97 assertEquals(bean,instance.getConfiguration()); 98 bean.removeScan(dir.getName()); 99 XmlConfigUtils.write(bean,new FileOutputStream(file),false); 100 101 assertNotSame(bean,instance.getConfiguration()); 102 103 instance.load(); 104 105 assertEquals(bean,instance.getConfiguration()); 106 107 } 108 109 /** 110 * Test of save method, of class com.sun.jmx.examples.scandir.ScanDirConfig. 111 */ 112 public void testSave() throws Exception { 113 System.out.println("save"); 114 115 final File file = File.createTempFile("testconf",".xml"); 116 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 117 final ScanManagerMXBean manager = ScanManager.register(mbs); 118 119 try { 120 final ScanDirConfigMXBean instance = 121 manager.createOtherConfigurationMBean("testSave",file.getAbsolutePath()); 122 assertTrue(mbs.isRegistered( 123 ScanManager.makeScanDirConfigName("testSave"))); 124 final ScanManagerConfig bean = 125 new ScanManagerConfig("testSave"); 126 final DirectoryScannerConfig dir = 127 new DirectoryScannerConfig("tmp"); 128 dir.setRootDirectory(file.getParent()); 129 bean.putScan(dir); 130 instance.setConfiguration(bean); 131 instance.save(); 132 final ScanManagerConfig loaded = 133 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 134 assertEquals(instance.getConfiguration(),loaded); 135 assertEquals(bean,loaded); 136 137 instance.getConfiguration().removeScan("tmp"); 138 instance.save(); 139 assertNotSame(loaded,instance.getConfiguration()); 140 final ScanManagerConfig loaded2 = 141 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 142 assertEquals(instance.getConfiguration(),loaded2); 143 } finally { 144 manager.close(); 145 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 146 } 147 final ObjectName all = 148 new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain()+":*"); 149 assertEquals(0,mbs.queryNames(all,null).size()); 150 } 151 152 /** 153 * Test of saveTo method, of class com.sun.jmx.examples.scandir.ScanProfile. 154 */ 155 /* 156 public void testSaveTo() throws Exception { 157 System.out.println("saveTo"); 158 159 String filename = ""; 160 ScanDirConfig instance = null; 161 162 instance.saveTo(filename); 163 164 // TODO review the generated test code and remove the default call to fail. 165 fail("The test case is a prototype."); 166 } 167 */ 168 169 /** 170 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig. 171 */ 172 public void testGetXmlConfigString() throws Exception { 173 System.out.println("getXmlConfigString"); 174 175 try { 176 final File file = File.createTempFile("testconf",".xml"); 177 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 178 final ScanManagerConfig bean = 179 new ScanManagerConfig("testGetXmlConfigString"); 180 final DirectoryScannerConfig dir = 181 new DirectoryScannerConfig("tmp"); 182 dir.setRootDirectory(file.getParent()); 183 bean.putScan(dir); 184 instance.setConfiguration(bean); 185 System.out.println("Expected: " + XmlConfigUtils.toString(bean)); 186 System.out.println("Received: " + 187 instance.getConfiguration().toString()); 188 assertEquals(XmlConfigUtils.toString(bean), 189 instance.getConfiguration().toString()); 190 } catch (Exception x) { 191 x.printStackTrace(); 192 throw x; 193 } 194 } 195 196 197 /** 198 * Test of addNotificationListener method, of class 199 * com.sun.jmx.examples.scandir.ScanDirConfig. 200 */ 201 public void testAddNotificationListener() throws Exception { 202 System.out.println("addNotificationListener"); 203 204 final File file = File.createTempFile("testconf",".xml"); 205 final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 206 final ScanManagerMXBean manager = ScanManager.register(mbs); 207 208 try { 209 final ScanDirConfigMXBean instance = 210 TestUtils.makeNotificationEmitter( 211 manager.createOtherConfigurationMBean("testSave", 212 file.getAbsolutePath()), 213 ScanDirConfigMXBean.class); 214 assertTrue(mbs.isRegistered( 215 ScanManager.makeScanDirConfigName("testSave"))); 216 DirectoryScannerConfig dir = 217 instance.addDirectoryScanner("tmp",file.getParent(),".*",0,0); 218 219 final BlockingQueue<Notification> queue = 220 new LinkedBlockingQueue<Notification>(); 221 final NotificationListener listener = new NotificationListener() { 222 public void handleNotification(Notification notification, 223 Object handback) { 224 queue.add(notification); 225 } 226 }; 227 NotificationFilter filter = null; 228 Object handback = null; 229 230 ((NotificationEmitter)instance).addNotificationListener(listener, 231 filter, handback); 232 233 instance.save(); 234 final ScanManagerConfig loaded = 235 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 236 assertEquals(instance.getConfiguration(),loaded); 237 238 final ScanManagerConfig newConfig = 239 instance.getConfiguration(); 240 newConfig.removeScan("tmp"); 241 instance.setConfiguration(newConfig); 242 instance.save(); 243 assertNotSame(loaded,instance.getConfiguration()); 244 final ScanManagerConfig loaded2 = 245 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 246 assertEquals(instance.getConfiguration(),loaded2); 247 instance.load(); 248 for (int i=0;i<4;i++) { 249 final Notification n = queue.poll(3,TimeUnit.SECONDS); 250 assertNotNull(n); 251 assertEquals(TestUtils.getObjectName(instance),n.getSource()); 252 switch(i) { 253 case 0: case 2: 254 assertEquals(ScanDirConfig.NOTIFICATION_SAVED,n.getType()); 255 break; 256 case 1: 257 assertEquals(ScanDirConfig.NOTIFICATION_MODIFIED,n.getType()); 258 break; 259 case 3: 260 assertEquals(ScanDirConfig.NOTIFICATION_LOADED,n.getType()); 261 break; 262 default: break; 263 } 264 } 265 } finally { 266 manager.close(); 267 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 268 } 269 final ObjectName all = 270 new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain()+":*"); 271 assertEquals(0,mbs.queryNames(all,null).size()); 272 } 273 274 /** 275 * Test of getConfigFilename method, of class 276 * com.sun.jmx.examples.scandir.ScanDirConfig. 277 */ 278 public void testGetConfigFilename() throws Exception { 279 System.out.println("getConfigFilename"); 280 281 final File file = File.createTempFile("testconf",".xml"); 282 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 283 284 String result = instance.getConfigFilename(); 285 assertEquals(file.getAbsolutePath(), new File(result).getAbsolutePath()); 286 287 } 288 289 /** 290 * Test of addDirectoryScanner method, of class 291 * com.sun.jmx.examples.scandir.ScanDirConfig. 292 */ 293 public void testAddDirectoryScanner() throws IOException { 294 System.out.println("addDirectoryScanner"); 295 296 System.out.println("save"); 297 298 final File file = File.createTempFile("testconf",".xml"); 299 final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath()); 300 final ScanManagerConfig bean = 301 new ScanManagerConfig("testSave"); 302 final DirectoryScannerConfig dir = 303 new DirectoryScannerConfig("tmp"); 304 dir.setRootDirectory(file.getParent()); 305 FileMatch filter = new FileMatch(); 306 filter.setFilePattern(".*"); 307 dir.setIncludeFiles(new FileMatch[] { 308 filter 309 }); 310 instance.setConfiguration(bean); 311 instance.addDirectoryScanner(dir.getName(), 312 dir.getRootDirectory(), 313 filter.getFilePattern(), 314 filter.getSizeExceedsMaxBytes(), 315 0); 316 instance.save(); 317 final ScanManagerConfig loaded = 318 new XmlConfigUtils(file.getAbsolutePath()).readFromFile(); 319 assertNotNull(loaded.getScan(dir.getName())); 320 assertEquals(dir,loaded.getScan(dir.getName())); 321 assertEquals(instance.getConfiguration(),loaded); 322 assertEquals(instance.getConfiguration().getScan(dir.getName()),dir); 323 } 324 325 } 326