41 package com.sun.jmx.examples.scandir;
43 import java.util.concurrent.LinkedBlockingQueue;
44 import java.util.concurrent.TimeUnit;
45 import javax.management.InstanceNotFoundException;
46 import javax.management.Notification;
47 import junit.framework.*;
50 import java.io.IOException;
51 import java.lang.management.ManagementFactory;
52 import java.util.EnumSet;
53 import java.util.HashMap;
54 import java.util.logging.Logger;
55 import javax.management.AttributeChangeNotification;
56 import javax.management.JMException;
57 import javax.management.JMX;
58 import javax.management.ListenerNotFoundException;
59 import javax.management.MBeanNotificationInfo;
60 import javax.management.MBeanRegistration;
61 import javax.management.MBeanServer;
62 import javax.management.MBeanServerConnection;
63 import javax.management.NotificationBroadcasterSupport;
64 import javax.management.NotificationEmitter;
65 import javax.management.NotificationFilter;
66 import javax.management.NotificationListener;
67 import javax.management.ObjectInstance;
68 import javax.management.ObjectName;
83 protected void setUp() throws Exception {
99 System.out.println(
"makeSingletonName");
105 assertEquals(expResult,
result);
113 System.out.println(
"register");
115 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
120 assertEquals(STOPPED,
result.getState());
124 }
catch (Exception x) {
125 System.err.println(
"Failed to cleanup: "+x);
132 public void call()
throws Exception;
133 public void cancel()
throws Exception;
140 System.out.println(
"addNotificationListener");
144 public void call()
throws Exception {
147 public void cancel()
throws Exception {
153 EnumSet.of(RUNNING,SCHEDULED),
157 ManagementFactory.getPlatformMBeanServer().
159 }
catch (Exception x) {
160 System.err.println(
"Failed to cleanup: "+x);
171 EnumSet<ScanState> after,
174 System.out.println(
"doTestOperation: "+testName);
176 final LinkedBlockingQueue<Notification> queue =
177 new LinkedBlockingQueue<Notification>();
179 NotificationListener listener =
new NotificationListener() {
180 public void handleNotification(Notification notification,
183 queue.put(notification);
184 }
catch (Exception x) {
185 System.err.println(
"Failed to queue notif: "+x);
189 NotificationFilter filter =
null;
190 Object handback =
null;
192 final NotificationEmitter emitter = (NotificationEmitter)proxy;
193 emitter.addNotificationListener(listener, filter, handback);
194 before = proxy.getState();
197 final Notification notification =
198 queue.poll(3000,TimeUnit.MILLISECONDS);
199 assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE,
200 notification.getType());
201 assertEquals(AttributeChangeNotification.class,
202 notification.getClass());
204 notification.getSource());
205 AttributeChangeNotification acn =
206 (AttributeChangeNotification)notification;
207 assertEquals(
"State",acn.getAttributeName());
208 assertEquals(
ScanState.class.getName(),acn.getAttributeType());
209 assertEquals(before,
ScanState.valueOf((String)acn.getOldValue()));
211 emitter.removeNotificationListener(listener,filter,handback);
215 }
catch (Exception x) {
216 System.err.println(
"Failed to cleanup: "+x);
225 System.out.println(
"preRegister");
227 MBeanServer
server = ManagementFactory.getPlatformMBeanServer();
228 ObjectName
name =
new ObjectName(
"DownUnder:type=Wombat");
235 throw new RuntimeException(
"bad name accepted!");
236 }
catch (IllegalArgumentException x) {
240 assertEquals(expResult,
result);
242 assertEquals(expResult,
result);
249 public void testGetState() throws IOException, InstanceNotFoundException {
250 System.out.println(
"getState");
256 assertEquals(expResult,
result);
261 assertEquals(STOPPED,instance.
getState());
262 instance.
schedule(1000000L,1000000L);
263 assertEquals(SCHEDULED,instance.
getState());
265 assertEquals(STOPPED,instance.
getState());
272 System.out.println(
"schedule");
274 final long delay = 10000L;
275 final long interval = 10000L;
279 public void call()
throws Exception {
281 assertEquals(SCHEDULED,manager.
getState());
283 public void cancel()
throws Exception {
292 ManagementFactory.getPlatformMBeanServer().
294 }
catch (Exception x) {
295 System.err.println(
"Failed to cleanup: "+x);
302 final String msg = String.valueOf(state) +
" is not one of " + allowed;
303 assertTrue(msg,allowed.contains(state));
310 System.out.println(
"stop");
316 public void call()
throws Exception {
318 assertEquals(STOPPED,manager.
getState());
320 public void cancel()
throws Exception {
328 ManagementFactory.getPlatformMBeanServer().
330 }
catch (Exception x) {
331 System.err.println(
"Failed to cleanup: "+x);
343 public void call()
throws Exception {
344 assertEquals(STOPPED,manager.
getState());
349 public void cancel()
throws Exception {
357 ManagementFactory.getPlatformMBeanServer().
359 }
catch (Exception x) {
360 System.err.println(
"Failed to cleanup: "+x);