KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ScanManagerTest.java
3  * JUnit based test
4  *
5  * Created on July 10, 2006, 5:57 PM
6  *
7  * @(#)ScanManagerTest.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 java.util.concurrent.LinkedBlockingQueue JavaDoc;
45 import java.util.concurrent.TimeUnit JavaDoc;
46 import javax.management.InstanceNotFoundException JavaDoc;
47 import javax.management.Notification JavaDoc;
48 import junit.framework.*;
49 import static com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.*;
50 import com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState;
51 import java.io.IOException JavaDoc;
52 import java.lang.management.ManagementFactory JavaDoc;
53 import java.util.EnumSet JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56 import javax.management.AttributeChangeNotification JavaDoc;
57 import javax.management.JMException JavaDoc;
58 import javax.management.JMX JavaDoc;
59 import javax.management.ListenerNotFoundException JavaDoc;
60 import javax.management.MBeanNotificationInfo JavaDoc;
61 import javax.management.MBeanRegistration JavaDoc;
62 import javax.management.MBeanServer JavaDoc;
63 import javax.management.MBeanServerConnection JavaDoc;
64 import javax.management.NotificationBroadcasterSupport JavaDoc;
65 import javax.management.NotificationEmitter JavaDoc;
66 import javax.management.NotificationFilter JavaDoc;
67 import javax.management.NotificationListener JavaDoc;
68 import javax.management.ObjectInstance JavaDoc;
69 import javax.management.ObjectName JavaDoc;
70
71 import static com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.*;
72
73 /**
74  * Unit tests for {@code ScanManager}
75  *
76  * @author Sun Microsystems, 2006 - All rights reserved.
77  */

78 public class ScanManagerTest extends TestCase {
79     
80     public ScanManagerTest(String JavaDoc testName) {
81         super(testName);
82     }
83
84     protected void setUp() throws Exception JavaDoc {
85     }
86
87     protected void tearDown() throws Exception JavaDoc {
88     }
89
90     public static Test suite() {
91         TestSuite suite = new TestSuite(ScanManagerTest.class);
92         
93         return suite;
94     }
95
96     /**
97      * Test of makeSingletonName method, of class com.sun.jmx.examples.scandir.ScanManager.
98      */

99     public void testMakeSingletonName() {
100         System.out.println("makeSingletonName");
101         
102         Class JavaDoc clazz = ScanManagerMXBean.class;
103         
104         ObjectName JavaDoc expResult = ScanManager.SCAN_MANAGER_NAME;
105         ObjectName JavaDoc result = ScanManager.makeSingletonName(clazz);
106         assertEquals(expResult, result);
107         
108     }
109
110     /**
111      * Test of register method, of class com.sun.jmx.examples.scandir.ScanManager.
112      */

113     public void testRegister() throws Exception JavaDoc {
114         System.out.println("register");
115         
116         MBeanServer JavaDoc mbs = ManagementFactory.getPlatformMBeanServer();
117         
118         
119         ScanManagerMXBean result = ScanManager.register(mbs);
120         try {
121             assertEquals(STOPPED,result.getState());
122         } finally {
123             try {
124                 mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
125             } catch (Exception JavaDoc x) {
126                 System.err.println("Failed to cleanup: "+x);
127             }
128         }
129         
130     }
131     
132     public interface Call {
133         public void call() throws Exception JavaDoc;
134         public void cancel() throws Exception JavaDoc;
135     }
136     
137     /**
138      * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
139      */

140     public void testAddNotificationListener() throws Exception JavaDoc {
141         System.out.println("addNotificationListener");
142         
143         final ScanManagerMXBean manager = ScanManager.register();
144         final Call op = new Call() {
145             public void call() throws Exception JavaDoc {
146                 manager.schedule(100000,0);
147             }
148             public void cancel() throws Exception JavaDoc {
149                 manager.stop();
150             }
151         };
152         try {
153             doTestOperation(manager,op,
154                             EnumSet.of(RUNNING,SCHEDULED),
155                             "schedule");
156         } finally {
157             try {
158                 ManagementFactory.getPlatformMBeanServer().
159                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
160             } catch (Exception JavaDoc x) {
161                 System.err.println("Failed to cleanup: "+x);
162             }
163         }
164     }
165     
166     /**
167      * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
168      */

169     private void doTestOperation(
170             ScanManagerMXBean proxy,
171             Call op,
172             EnumSet JavaDoc<ScanState> after,
173             String JavaDoc testName)
174         throws Exception JavaDoc {
175         System.out.println("doTestOperation: "+testName);
176         
177         final LinkedBlockingQueue JavaDoc<Notification JavaDoc> queue =
178                 new LinkedBlockingQueue JavaDoc<Notification JavaDoc>();
179         
180         NotificationListener JavaDoc listener = new NotificationListener JavaDoc() {
181             public void handleNotification(Notification JavaDoc notification,
182                         Object JavaDoc handback) {
183                 try {
184                     queue.put(notification);
185                 } catch (Exception JavaDoc x) {
186                     System.err.println("Failed to queue notif: "+x);
187                 }
188             }
189         };
190         NotificationFilter JavaDoc filter = null;
191         Object JavaDoc handback = null;
192         final ScanState before;
193         final NotificationEmitter JavaDoc emitter = (NotificationEmitter JavaDoc)proxy;
194         emitter.addNotificationListener(listener, filter, handback);
195         before = proxy.getState();
196         op.call();
197         try {
198             final Notification JavaDoc notification =
199                     queue.poll(3000,TimeUnit.MILLISECONDS);
200             assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE,
201                     notification.getType());
202             assertEquals(AttributeChangeNotification JavaDoc.class,
203                     notification.getClass());
204             assertEquals(ScanManager.SCAN_MANAGER_NAME,
205                     notification.getSource());
206             AttributeChangeNotification JavaDoc acn =
207                     (AttributeChangeNotification JavaDoc)notification;
208             assertEquals("State",acn.getAttributeName());
209             assertEquals(ScanState.class.getName(),acn.getAttributeType());
210             assertEquals(before,ScanState.valueOf((String JavaDoc)acn.getOldValue()));
211             assertContained(after,ScanState.valueOf((String JavaDoc)acn.getNewValue()));
212             emitter.removeNotificationListener(listener,filter,handback);
213         } finally {
214             try {
215                 op.cancel();
216             } catch (Exception JavaDoc x) {
217                 System.err.println("Failed to cleanup: "+x);
218             }
219         }
220     }
221
222     /**
223      * Test of preRegister method, of class com.sun.jmx.examples.scandir.ScanManager.
224      */

225     public void testPreRegister() throws Exception JavaDoc {
226         System.out.println("preRegister");
227         
228         MBeanServer JavaDoc server = ManagementFactory.getPlatformMBeanServer();
229         ObjectName JavaDoc name = new ObjectName JavaDoc("DownUnder:type=Wombat");
230         ScanManager instance = new ScanManager();
231         
232         ObjectName JavaDoc expResult = ScanManager.SCAN_MANAGER_NAME;
233         ObjectName JavaDoc result;
234         try {
235             result = instance.preRegister(server, name);
236             throw new RuntimeException JavaDoc("bad name accepted!");
237         } catch (IllegalArgumentException JavaDoc x) {
238             // OK!
239
result = instance.preRegister(server, null);
240         }
241         assertEquals(expResult, result);
242         result = instance.preRegister(server, ScanManager.SCAN_MANAGER_NAME);
243         assertEquals(expResult, result);
244     }
245
246
247     /**
248      * Test of getState method, of class com.sun.jmx.examples.scandir.ScanManager.
249      */

250     public void testGetState() throws IOException JavaDoc, InstanceNotFoundException JavaDoc {
251         System.out.println("getState");
252         
253         ScanManager instance = new ScanManager();
254         
255         ScanState expResult = ScanState.STOPPED;
256         ScanState result = instance.getState();
257         assertEquals(expResult, result);
258         instance.start();
259         final ScanState afterStart = instance.getState();
260         assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED),afterStart);
261         instance.stop();
262         assertEquals(STOPPED,instance.getState());
263         instance.schedule(1000000L,1000000L);
264         assertEquals(SCHEDULED,instance.getState());
265         instance.stop();
266         assertEquals(STOPPED,instance.getState());
267     }
268
269     /**
270      * Test of schedule method, of class com.sun.jmx.examples.scandir.ScanManager.
271      */

272     public void testSchedule() throws Exception JavaDoc {
273         System.out.println("schedule");
274         
275         final long delay = 10000L;
276         final long interval = 10000L;
277         
278         final ScanManagerMXBean manager = ScanManager.register();
279         final Call op = new Call() {
280             public void call() throws Exception JavaDoc {
281                 manager.schedule(delay,interval);
282                 assertEquals(SCHEDULED,manager.getState());
283             }
284             public void cancel() throws Exception JavaDoc {
285                 manager.stop();
286             }
287         };
288         try {
289             doTestOperation(manager,op,EnumSet.of(SCHEDULED),
290                     "schedule");
291         } finally {
292             try {
293                 ManagementFactory.getPlatformMBeanServer().
294                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
295             } catch (Exception JavaDoc x) {
296                 System.err.println("Failed to cleanup: "+x);
297             }
298         }
299     }
300        
301     public static void assertContained(EnumSet JavaDoc<ScanState> allowed,
302             ScanState state) {
303          final String JavaDoc msg = String.valueOf(state) + " is not one of " + allowed;
304          assertTrue(msg,allowed.contains(state));
305     }
306
307     /**
308      * Test of stop method, of class com.sun.jmx.examples.scandir.ScanManager.
309      */

310     public void testStop() throws Exception JavaDoc {
311         System.out.println("stop");
312         final ScanManagerMXBean manager = ScanManager.register();
313         try {
314             manager.schedule(1000000,0);
315             assertContained(EnumSet.of(SCHEDULED),manager.getState());
316             final Call op = new Call() {
317                 public void call() throws Exception JavaDoc {
318                     manager.stop();
319                     assertEquals(STOPPED,manager.getState());
320                 }
321                 public void cancel() throws Exception JavaDoc {
322                     if (manager.getState() != STOPPED)
323                         manager.stop();
324                 }
325             };
326             doTestOperation(manager,op,EnumSet.of(STOPPED),"stop");
327         } finally {
328             try {
329                 ManagementFactory.getPlatformMBeanServer().
330                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
331             } catch (Exception JavaDoc x) {
332                 System.err.println("Failed to cleanup: "+x);
333             }
334         }
335     }
336
337     /**
338      * Test of start method, of class com.sun.jmx.examples.scandir.ScanManager.
339      */

340     public void testStart() throws Exception JavaDoc {
341         final ScanManagerMXBean manager = ScanManager.register();
342         try {
343             final Call op = new Call() {
344                 public void call() throws Exception JavaDoc {
345                     assertEquals(STOPPED,manager.getState());
346                     manager.start();
347                     assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED),
348                             manager.getState());
349                 }
350                 public void cancel() throws Exception JavaDoc {
351                     manager.stop();
352                 }
353             };
354             doTestOperation(manager,op,EnumSet.of(RUNNING,SCHEDULED,COMPLETED),
355                     "start");
356         } finally {
357             try {
358                 ManagementFactory.getPlatformMBeanServer().
359                         unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
360             } catch (Exception JavaDoc x) {
361                 System.err.println("Failed to cleanup: "+x);
362             }
363         }
364     }
365     
366 }
367
Popular Tags