KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DirectoryScannerTest.java
3  * JUnit based test
4  *
5  * Created on July 12, 2006, 7:44 PM
6  *
7  * @(#)DirectoryScannerTest.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.DirectoryScannerConfig;
45 import com.sun.jmx.examples.scandir.config.ResultRecord;
46 import com.sun.jmx.examples.scandir.config.ScanManagerConfig;
47 import java.util.LinkedList JavaDoc;
48 import java.util.concurrent.BlockingQueue JavaDoc;
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 JavaDoc;
54 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
55 import java.util.concurrent.TimeUnit JavaDoc;
56 import javax.management.AttributeChangeNotification JavaDoc;
57 import javax.management.Notification JavaDoc;
58 import javax.management.NotificationEmitter JavaDoc;
59 import javax.management.NotificationFilter JavaDoc;
60 import javax.management.NotificationListener JavaDoc;
61
62 import static com.sun.jmx.examples.scandir.ScanManagerTest.*;
63 import static com.sun.jmx.examples.scandir.TestUtils.*;
64 import java.io.File JavaDoc;
65 import java.lang.management.ManagementFactory JavaDoc;
66 import java.util.List JavaDoc;
67
68 /**
69  * Unit tests for {@code DirectoryScanner}
70  *
71  * @author Sun Microsystems, 2006 - All rights reserved.
72  */

73 public class DirectoryScannerTest extends TestCase {
74     
75     public DirectoryScannerTest(String JavaDoc testName) {
76         super(testName);
77     }
78
79     protected void setUp() throws Exception JavaDoc {
80     }
81
82     protected void tearDown() throws Exception JavaDoc {
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 JavaDoc<ScanState> after,
95             String JavaDoc testName)
96         throws Exception JavaDoc {
97         System.out.println("doTestOperation: "+testName);
98         
99         final LinkedBlockingQueue JavaDoc<Notification JavaDoc> queue =
100                 new LinkedBlockingQueue JavaDoc<Notification JavaDoc>();
101         
102         NotificationListener JavaDoc listener = new NotificationListener JavaDoc() {
103             public void handleNotification(Notification JavaDoc notification,
104                         Object JavaDoc handback) {
105                 try {
106                     queue.put(notification);
107                 } catch (Exception JavaDoc x) {
108                     System.err.println("Failed to queue notif: "+x);
109                 }
110             }
111         };
112         NotificationFilter JavaDoc filter = null;
113         Object JavaDoc handback = null;
114         final ScanState before;
115         final NotificationEmitter JavaDoc emitter = (NotificationEmitter JavaDoc)
116                 makeNotificationEmitter(proxy,DirectoryScannerMXBean.class);
117         emitter.addNotificationListener(listener, filter, handback);
118         before = proxy.getState();
119         op.call();
120         try {
121             final Notification JavaDoc notification =
122                     queue.poll(3000,TimeUnit.MILLISECONDS);
123             assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE,
124                     notification.getType());
125             assertEquals(AttributeChangeNotification JavaDoc.class,
126                     notification.getClass());
127             assertEquals(getObjectName(proxy),
128                     notification.getSource());
129             AttributeChangeNotification JavaDoc acn =
130                     (AttributeChangeNotification JavaDoc)notification;
131             assertEquals("State",acn.getAttributeName());
132             assertEquals(ScanState.class.getName(),acn.getAttributeType());
133             assertEquals(before,ScanState.valueOf((String JavaDoc)acn.getOldValue()));
134             assertContained(after,ScanState.valueOf((String JavaDoc)acn.getNewValue()));
135             emitter.removeNotificationListener(listener,filter,handback);
136         } finally {
137             try {
138                 op.cancel();
139             } catch (Exception JavaDoc x) {
140                 System.err.println("Failed to cleanup: "+x);
141             }
142         }
143     }
144     
145
146     /**
147      * Test of getRootDirectory method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
148      */

149     public void testGetRootDirectory() throws Exception JavaDoc {
150         System.out.println("getRootDirectory");
151         
152        final ScanManagerMXBean manager = ScanManager.register();
153         try {
154             final String JavaDoc 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 JavaDoc root = bean.getRootDirectory();
160             if (root == null)
161                 throw new NullPointerException JavaDoc("bean.getRootDirectory()");
162             if (config.getConfiguration().getScan("test").getRootDirectory() == null)
163                 throw new NullPointerException JavaDoc("config.getConfig().getScan(\"test\").getRootDirectory()");
164             manager.applyConfiguration(true);
165             final DirectoryScannerMXBean proxy =
166                     manager.getDirectoryScanners().get("test");
167             final File JavaDoc tmpFile = new File JavaDoc(tmpdir);
168             final File JavaDoc rootFile = new File JavaDoc(proxy.getRootDirectory());
169             assertEquals(tmpFile,rootFile);
170         } catch (Exception JavaDoc x) {
171             x.printStackTrace();
172             throw x;
173         } finally {
174             try {
175                 ManagementFactory.getPlatformMBeanServer().
176                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
177             } catch (Exception JavaDoc x) {
178                 System.err.println("Failed to cleanup: "+x);
179             }
180         }
181     }
182
183
184     /**
185      * Test of scan method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
186      */

187     public void testScan() throws Exception JavaDoc {
188         System.out.println("scan");
189         
190         final ScanManagerMXBean manager = ScanManager.register();
191         try {
192             final String JavaDoc 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 JavaDoc {
203                     final BlockingQueue JavaDoc<Notification JavaDoc> queue =
204                             new LinkedBlockingQueue JavaDoc<Notification JavaDoc>();
205                     final NotificationListener JavaDoc listener = new NotificationListener JavaDoc() {
206                         public void handleNotification(Notification JavaDoc notification,
207                                 Object JavaDoc handback) {
208                             try {
209                                queue.put(notification);
210                             } catch (Exception JavaDoc e) {
211                                 e.printStackTrace();
212                             }
213                         }
214                     };
215                     manager.start();
216                     while(true) {
217                         final Notification JavaDoc n = queue.poll(10,TimeUnit.SECONDS);
218                         if (n == null) break;
219                         final AttributeChangeNotification JavaDoc at =
220                                 (AttributeChangeNotification JavaDoc) n;
221                         if (RUNNING == ScanState.valueOf((String JavaDoc)at.getNewValue()))
222                             break;
223                         else {
224                             System.err.println("New state: "+(String JavaDoc)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 JavaDoc {
232                     manager.stop();
233                 }
234             };
235             doTestOperation(proxy,op,
236                     EnumSet.of(RUNNING,SCHEDULED,COMPLETED),
237                     "scan");
238         } catch (Exception JavaDoc x) {
239             x.printStackTrace();
240             throw x;
241         } finally {
242             try {
243                 manager.stop();
244             } catch (Exception JavaDoc x) {
245                 System.err.println("Failed to stop: "+x);
246             }
247             try {
248                 ManagementFactory.getPlatformMBeanServer().
249                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
250             } catch (Exception JavaDoc x) {
251                 System.err.println("Failed to cleanup: "+x);
252             }
253         }
254     }
255
256     /**
257      * Test of getState method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
258      */

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     /**
278      * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
279      */

280     public void testAddNotificationListener() throws Exception JavaDoc {
281         System.out.println("addNotificationListener");
282         
283         final ScanManagerMXBean manager = ScanManager.register();
284         final Call op = new Call() {
285             public void call() throws Exception JavaDoc {
286                 manager.start();
287             }
288             public void cancel() throws Exception JavaDoc {
289                 manager.stop();
290             }
291         };
292         try {
293             final String JavaDoc 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 JavaDoc x) {
308                 System.err.println("Failed to cleanup: "+x);
309             }
310         }
311     }
312
313    
314 }
315
Popular Tags