1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.jmx; |
24 |
| |
25 |
| import java.util.LinkedList; |
26 |
| import java.util.List; |
27 |
| |
28 |
| import javax.management.AttributeChangeNotification; |
29 |
| import javax.management.Notification; |
30 |
| import javax.management.NotificationListener; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class LifecycleNotificationTest extends CacheJmxWrapperTestBase |
39 |
| { |
40 |
| |
41 |
1
| public void testGetStateAndStateNotification() throws Exception
|
42 |
| { |
43 |
1
| CacheJmxWrapper wrapper = createWrapper(createConfiguration());
|
44 |
1
| StateNotificationListener listener = new StateNotificationListener();
|
45 |
1
| wrapper.addNotificationListener(listener, null, null);
|
46 |
| |
47 |
1
| assertEquals("Correct state after instanitation",
|
48 |
| CacheJmxWrapperMBean.UNREGISTERED, wrapper.getState()); |
49 |
| |
50 |
1
| registerWrapper(wrapper);
|
51 |
1
| assertEquals("Correct state after registration",
|
52 |
| CacheJmxWrapperMBean.REGISTERED, wrapper.getState()); |
53 |
| |
54 |
1
| wrapper.create();
|
55 |
1
| assertEquals("Correct state after create",
|
56 |
| CacheJmxWrapperMBean.CREATED, wrapper.getState()); |
57 |
| |
58 |
1
| wrapper.start();
|
59 |
1
| assertEquals("Correct state after start",
|
60 |
| CacheJmxWrapperMBean.STARTED, wrapper.getState()); |
61 |
| |
62 |
1
| wrapper.stop();
|
63 |
1
| assertEquals("Correct state after stop",
|
64 |
| CacheJmxWrapperMBean.STOPPED, wrapper.getState()); |
65 |
| |
66 |
1
| wrapper.destroy();
|
67 |
1
| assertEquals("Correct state after destroy",
|
68 |
| CacheJmxWrapperMBean.DESTROYED, wrapper.getState()); |
69 |
| |
70 |
1
| unregisterWrapper();
|
71 |
1
| assertEquals("Correct state after unregistration",
|
72 |
| CacheJmxWrapperMBean.UNREGISTERED, wrapper.getState()); |
73 |
| |
74 |
1
| System.out.println(listener.notifications);
|
75 |
1
| assertEquals("Correct number of notifications received", 4, listener.notifications.size());
|
76 |
1
| assertEquals("Correct first notification", new Integer(CacheJmxWrapperMBean.STARTING), listener.notifications.get(0));
|
77 |
1
| assertEquals("Correct second notification", new Integer(CacheJmxWrapperMBean.STARTED), listener.notifications.get(1));
|
78 |
1
| assertEquals("Correct third notification", new Integer(CacheJmxWrapperMBean.STOPPING), listener.notifications.get(2));
|
79 |
1
| assertEquals("Correct fourth notification", new Integer(CacheJmxWrapperMBean.STOPPED), listener.notifications.get(3));
|
80 |
| } |
81 |
| |
82 |
| private static class StateNotificationListener |
83 |
| implements NotificationListener |
84 |
| { |
85 |
| private List<Integer> notifications = new LinkedList<Integer>(); |
86 |
| |
87 |
6
| public void handleNotification(Notification msg, Object handback)
|
88 |
| { |
89 |
6
| if (msg instanceof AttributeChangeNotification)
|
90 |
| { |
91 |
4
| AttributeChangeNotification change = (AttributeChangeNotification) msg;
|
92 |
4
| String attrName = change.getAttributeName();
|
93 |
4
| Object newValue = change.getNewValue();
|
94 |
4
| if ("State".equals(attrName) && newValue != null && newValue instanceof Integer)
|
95 |
| { |
96 |
4
| notifications.add((Integer) newValue);
|
97 |
4
| return;
|
98 |
| } |
99 |
| } |
100 |
| } |
101 |
| } |
102 |
| |
103 |
| } |