KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > examples > scandir > ScanDirConfigTest


1 /*
2  * ScanDirConfigTest.java
3  * JUnit based test
4  *
5  * Created on 15 juillet 2006, 20:17
6  *
7  * @(#)ScanDirConfigTest.java 1.2 06/08/02
8  *
9  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * -Redistribution of source code must retain the above copyright notice, this
15  * list of conditions and the following disclaimer.
16  *
17  * -Redistribution in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  *
25  * This software is provided "AS IS," without a warranty of any kind. ALL
26  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
27  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
28  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
29  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
30  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
31  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
32  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
33  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
34  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
35  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36  *
37  * You acknowledge that this software is not designed, licensed or intended
38  * for use in the design, construction, operation or maintenance of any
39  * nuclear facility.
40  */

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 JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.lang.management.ManagementFactory JavaDoc;
49 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
50 import java.util.concurrent.TimeUnit JavaDoc;
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 JavaDoc;
55 import java.util.concurrent.BlockingQueue JavaDoc;
56 import javax.management.*;
57
58 /**
59  * Unit tests for {@code ScanDirConfig}
60  *
61  * @author Sun Microsystems, 2006 - All rights reserved.
62  */

63 public class ScanDirConfigTest extends TestCase {
64     
65     public ScanDirConfigTest(String JavaDoc testName) {
66         super(testName);
67     }
68
69     protected void setUp() throws Exception JavaDoc {
70     }
71
72     protected void tearDown() throws Exception JavaDoc {
73     }
74
75     public static Test suite() {
76         TestSuite suite = new TestSuite(ScanDirConfigTest.class);
77         
78         return suite;
79     }
80
81     /**
82      * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
83      */

84     public void testLoad() throws Exception JavaDoc {
85         System.out.println("load");
86         
87         final File JavaDoc 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 JavaDoc(file),false);
96         instance.load();
97         
98         assertEquals(bean,instance.getConfiguration());
99         bean.removeScan(dir.getName());
100         XmlConfigUtils.write(bean,new FileOutputStream JavaDoc(file),false);
101         
102         assertNotSame(bean,instance.getConfiguration());
103         
104         instance.load();
105         
106         assertEquals(bean,instance.getConfiguration());
107         
108     }
109
110     /**
111      * Test of save method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
112      */

113     public void testSave() throws Exception JavaDoc {
114         System.out.println("save");
115
116         final File JavaDoc 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     /**
154      * Test of saveTo method, of class com.sun.jmx.examples.scandir.ScanProfile.
155      */

156     /*
157     public void testSaveTo() throws Exception {
158         System.out.println("saveTo");
159         
160         String filename = "";
161         ScanDirConfig instance = null;
162         
163         instance.saveTo(filename);
164         
165         // TODO review the generated test code and remove the default call to fail.
166         fail("The test case is a prototype.");
167     }
168     */

169     
170     /**
171      * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
172      */

173     public void testGetXmlConfigString() throws Exception JavaDoc {
174         System.out.println("getXmlConfigString");
175         
176         try {
177             final File JavaDoc 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 JavaDoc x) {
192             x.printStackTrace();
193             throw x;
194         }
195     }
196
197
198     /**
199      * Test of addNotificationListener method, of class
200      * com.sun.jmx.examples.scandir.ScanDirConfig.
201      */

202     public void testAddNotificationListener() throws Exception JavaDoc {
203         System.out.println("addNotificationListener");
204         
205         final File JavaDoc 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 JavaDoc<Notification> queue =
221                     new LinkedBlockingQueue JavaDoc<Notification>();
222             final NotificationListener listener = new NotificationListener() {
223                 public void handleNotification(Notification notification,
224                             Object JavaDoc handback) {
225                     queue.add(notification);
226                 }
227             };
228             NotificationFilter filter = null;
229             Object JavaDoc 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     /**
276      * Test of getConfigFilename method, of class
277      * com.sun.jmx.examples.scandir.ScanDirConfig.
278      */

279     public void testGetConfigFilename() throws Exception JavaDoc {
280         System.out.println("getConfigFilename");
281         
282         final File JavaDoc file = File.createTempFile("testconf",".xml");
283         final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
284         
285         String JavaDoc result = instance.getConfigFilename();
286         assertEquals(file.getAbsolutePath(), new File JavaDoc(result).getAbsolutePath());
287         
288     }
289
290     /**
291      * Test of addDirectoryScanner method, of class
292      * com.sun.jmx.examples.scandir.ScanDirConfig.
293      */

294     public void testAddDirectoryScanner() throws IOException JavaDoc {
295         System.out.println("addDirectoryScanner");
296         
297         System.out.println("save");
298
299         final File JavaDoc 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