Crossfire JXClient, Trunk
ScanManagerTest.java
Go to the documentation of this file.
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 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;
69 
71 
77 public class ScanManagerTest extends TestCase {
78 
79  public ScanManagerTest(String testName) {
80  super(testName);
81  }
82 
83  protected void setUp() throws Exception {
84  }
85 
86  protected void tearDown() throws Exception {
87  }
88 
89  public static Test suite() {
90  TestSuite suite = new TestSuite(ScanManagerTest.class);
91 
92  return suite;
93  }
94 
98  public void testMakeSingletonName() {
99  System.out.println("makeSingletonName");
100 
101  Class clazz = ScanManagerMXBean.class;
102 
103  ObjectName expResult = ScanManager.SCAN_MANAGER_NAME;
104  ObjectName result = ScanManager.makeSingletonName(clazz);
105  assertEquals(expResult, result);
106 
107  }
108 
112  public void testRegister() throws Exception {
113  System.out.println("register");
114 
115  MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
116 
117 
118  ScanManagerMXBean result = ScanManager.register(mbs);
119  try {
120  assertEquals(STOPPED,result.getState());
121  } finally {
122  try {
123  mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
124  } catch (Exception x) {
125  System.err.println("Failed to cleanup: "+x);
126  }
127  }
128 
129  }
130 
131  public interface Call {
132  public void call() throws Exception;
133  public void cancel() throws Exception;
134  }
135 
139  public void testAddNotificationListener() throws Exception {
140  System.out.println("addNotificationListener");
141 
142  final ScanManagerMXBean manager = ScanManager.register();
143  final Call op = new Call() {
144  public void call() throws Exception {
145  manager.schedule(100000,0);
146  }
147  public void cancel() throws Exception {
148  manager.stop();
149  }
150  };
151  try {
152  doTestOperation(manager,op,
153  EnumSet.of(RUNNING,SCHEDULED),
154  "schedule");
155  } finally {
156  try {
157  ManagementFactory.getPlatformMBeanServer().
158  unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
159  } catch (Exception x) {
160  System.err.println("Failed to cleanup: "+x);
161  }
162  }
163  }
164 
168  private void doTestOperation(
169  ScanManagerMXBean proxy,
170  Call op,
171  EnumSet<ScanState> after,
172  String testName)
173  throws Exception {
174  System.out.println("doTestOperation: "+testName);
175 
176  final LinkedBlockingQueue<Notification> queue =
177  new LinkedBlockingQueue<Notification>();
178 
179  NotificationListener listener = new NotificationListener() {
180  public void handleNotification(Notification notification,
181  Object handback) {
182  try {
183  queue.put(notification);
184  } catch (Exception x) {
185  System.err.println("Failed to queue notif: "+x);
186  }
187  }
188  };
189  NotificationFilter filter = null;
190  Object handback = null;
191  final ScanState before;
192  final NotificationEmitter emitter = (NotificationEmitter)proxy;
193  emitter.addNotificationListener(listener, filter, handback);
194  before = proxy.getState();
195  op.call();
196  try {
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());
203  assertEquals(ScanManager.SCAN_MANAGER_NAME,
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()));
210  assertContained(after,ScanState.valueOf((String)acn.getNewValue()));
211  emitter.removeNotificationListener(listener,filter,handback);
212  } finally {
213  try {
214  op.cancel();
215  } catch (Exception x) {
216  System.err.println("Failed to cleanup: "+x);
217  }
218  }
219  }
220 
224  public void testPreRegister() throws Exception {
225  System.out.println("preRegister");
226 
227  MBeanServer server = ManagementFactory.getPlatformMBeanServer();
228  ObjectName name = new ObjectName("DownUnder:type=Wombat");
229  ScanManager instance = new ScanManager();
230 
231  ObjectName expResult = ScanManager.SCAN_MANAGER_NAME;
232  ObjectName result;
233  try {
234  result = instance.preRegister(server, name);
235  throw new RuntimeException("bad name accepted!");
236  } catch (IllegalArgumentException x) {
237  // OK!
238  result = instance.preRegister(server, null);
239  }
240  assertEquals(expResult, result);
241  result = instance.preRegister(server, ScanManager.SCAN_MANAGER_NAME);
242  assertEquals(expResult, result);
243  }
244 
245 
249  public void testGetState() throws IOException, InstanceNotFoundException {
250  System.out.println("getState");
251 
252  ScanManager instance = new ScanManager();
253 
254  ScanState expResult = ScanState.STOPPED;
255  ScanState result = instance.getState();
256  assertEquals(expResult, result);
257  instance.start();
258  final ScanState afterStart = instance.getState();
259  assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED),afterStart);
260  instance.stop();
261  assertEquals(STOPPED,instance.getState());
262  instance.schedule(1000000L,1000000L);
263  assertEquals(SCHEDULED,instance.getState());
264  instance.stop();
265  assertEquals(STOPPED,instance.getState());
266  }
267 
271  public void testSchedule() throws Exception {
272  System.out.println("schedule");
273 
274  final long delay = 10000L;
275  final long interval = 10000L;
276 
277  final ScanManagerMXBean manager = ScanManager.register();
278  final Call op = new Call() {
279  public void call() throws Exception {
280  manager.schedule(delay,interval);
281  assertEquals(SCHEDULED,manager.getState());
282  }
283  public void cancel() throws Exception {
284  manager.stop();
285  }
286  };
287  try {
288  doTestOperation(manager,op,EnumSet.of(SCHEDULED),
289  "schedule");
290  } finally {
291  try {
292  ManagementFactory.getPlatformMBeanServer().
293  unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
294  } catch (Exception x) {
295  System.err.println("Failed to cleanup: "+x);
296  }
297  }
298  }
299 
300  public static void assertContained(EnumSet<ScanState> allowed,
301  ScanState state) {
302  final String msg = String.valueOf(state) + " is not one of " + allowed;
303  assertTrue(msg,allowed.contains(state));
304  }
305 
309  public void testStop() throws Exception {
310  System.out.println("stop");
311  final ScanManagerMXBean manager = ScanManager.register();
312  try {
313  manager.schedule(1000000,0);
314  assertContained(EnumSet.of(SCHEDULED),manager.getState());
315  final Call op = new Call() {
316  public void call() throws Exception {
317  manager.stop();
318  assertEquals(STOPPED,manager.getState());
319  }
320  public void cancel() throws Exception {
321  if (manager.getState() != STOPPED)
322  manager.stop();
323  }
324  };
325  doTestOperation(manager,op,EnumSet.of(STOPPED),"stop");
326  } finally {
327  try {
328  ManagementFactory.getPlatformMBeanServer().
329  unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
330  } catch (Exception x) {
331  System.err.println("Failed to cleanup: "+x);
332  }
333  }
334  }
335 
339  public void testStart() throws Exception {
340  final ScanManagerMXBean manager = ScanManager.register();
341  try {
342  final Call op = new Call() {
343  public void call() throws Exception {
344  assertEquals(STOPPED,manager.getState());
345  manager.start();
346  assertContained(EnumSet.of(RUNNING,SCHEDULED,COMPLETED),
347  manager.getState());
348  }
349  public void cancel() throws Exception {
350  manager.stop();
351  }
352  };
353  doTestOperation(manager,op,EnumSet.of(RUNNING,SCHEDULED,COMPLETED),
354  "start");
355  } finally {
356  try {
357  ManagementFactory.getPlatformMBeanServer().
358  unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
359  } catch (Exception x) {
360  System.err.println("Failed to cleanup: "+x);
361  }
362  }
363  }
364 
365 }
com.sun.jmx.examples.scandir.ScanManagerTest.testPreRegister
void testPreRegister()
Definition: ScanManagerTest.java:224
com.sun.jmx.examples.scandir.ScanManagerTest
Definition: ScanManagerTest.java:77
com.sun.jmx.examples.scandir.ScanManager
Definition: ScanManager.java:103
com.sun.jmx.examples.scandir.ScanManager.stop
void stop()
Definition: ScanManager.java:636
com.sun.jmx.examples.scandir.ScanManager.register
static ScanManagerMXBean register(MBeanServerConnection mbs)
Definition: ScanManager.java:275
com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState
Definition: ScanManagerMXBean.java:77
com.sun.jmx.examples.scandir.ScanManagerMXBean.stop
void stop()
com.sun.jmx.examples.scandir.ScanManagerTest.Call.cancel
void cancel()
com.sun.jmx.examples.scandir.ScanManagerTest.doTestOperation
void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet< ScanState > after, String testName)
Definition: ScanManagerTest.java:168
com.sun.jmx.examples.scandir
com.sun.jmx.examples.scandir.ScanManager.start
void start()
Definition: ScanManager.java:624
com.sun.jmx.examples.scandir.ScanManagerTest.testSchedule
void testSchedule()
Definition: ScanManagerTest.java:271
com.sun.jmx.examples.scandir.ScanManagerTest.testMakeSingletonName
void testMakeSingletonName()
Definition: ScanManagerTest.java:98
com.sun.jmx.examples.scandir.ScanManagerTest.testStart
void testStart()
Definition: ScanManagerTest.java:339
com.sun.jmx.examples.scandir.ScanManagerTest.assertContained
static void assertContained(EnumSet< ScanState > allowed, ScanState state)
Definition: ScanManagerTest.java:300
com.sun.jmx.examples
com.sun.jmx.examples.scandir.ScanManagerTest.testGetState
void testGetState()
Definition: ScanManagerTest.java:249
com.sun.jmx.examples.scandir.ScanManagerMXBean.getState
ScanState getState()
com.sun.jmx.examples.scandir.ScanManager.makeSingletonName
static final ObjectName makeSingletonName(Class clazz)
Definition: ScanManager.java:197
com.sun
com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState.STOPPED
STOPPED
Definition: ScanManagerMXBean.java:97
com.sun.jmx.examples.scandir.ScanManagerMXBean.start
void start()
com.sun.jmx.examples.scandir.ScanManager.preRegister
ObjectName preRegister(MBeanServer server, ObjectName name)
Definition: ScanManager.java:1001
com.sun.jmx.examples.scandir.ScanManagerTest.testStop
void testStop()
Definition: ScanManagerTest.java:309
com.sun.jmx.examples.scandir.ScanManagerTest.tearDown
void tearDown()
Definition: ScanManagerTest.java:86
com.sun.jmx.examples.scandir.ScanManagerTest.testRegister
void testRegister()
Definition: ScanManagerTest.java:112
com.sun.jmx.examples.scandir.ScanManagerTest.Call
Definition: ScanManagerTest.java:131
com.sun.jmx.examples.scandir.ScanManagerMXBean.schedule
void schedule(long delay, long interval)
com.sun.jmx.examples.scandir.ScanManager.SCAN_MANAGER_NAME
static final ObjectName SCAN_MANAGER_NAME
Definition: ScanManager.java:115
com
com.sun.jmx.examples.scandir.ScanManagerTest.testAddNotificationListener
void testAddNotificationListener()
Definition: ScanManagerTest.java:139
com.sun.jmx
com.sun.jmx.examples.scandir.ScanManagerTest.setUp
void setUp()
Definition: ScanManagerTest.java:83
com.sun.jmx.examples.scandir.ScanManagerTest.suite
static Test suite()
Definition: ScanManagerTest.java:89
com.sun.jmx.examples.scandir.ScanManagerTest.ScanManagerTest
ScanManagerTest(String testName)
Definition: ScanManagerTest.java:79
com.sun.jmx.examples.scandir.ScanManager.schedule
void schedule(long delay, long interval)
Definition: ScanManager.java:587
com.sun.jmx.examples.scandir.ScanManagerMXBean
Definition: ScanManagerMXBean.java:66
com.sun.jmx.examples.scandir.ScanManagerTest.Call.call
void call()
com.sun.jmx.examples.scandir.ScanManager.getState
ScanState getState()
Definition: ScanManager.java:504