1
2
3
4
5
6
7
8
9
10
11 package org.mule.management;
12
13 import org.mule.impl.internal.notifications.ComponentNotification;
14 import org.mule.impl.internal.notifications.ComponentNotificationListener;
15 import org.mule.impl.internal.notifications.CustomNotification;
16 import org.mule.impl.internal.notifications.CustomNotificationListener;
17 import org.mule.impl.internal.notifications.ManagerNotification;
18 import org.mule.impl.internal.notifications.ManagerNotificationListener;
19 import org.mule.impl.internal.notifications.ModelNotification;
20 import org.mule.impl.internal.notifications.ModelNotificationListener;
21 import org.mule.tck.AbstractMuleTestCase;
22 import org.mule.tck.testmodels.fruit.Apple;
23 import org.mule.umo.manager.UMOManager;
24 import org.mule.umo.manager.UMOServerNotification;
25 import org.mule.umo.model.UMOModel;
26
27 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
28 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
29 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
30 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
31
32 public class ServerNotificationsTestCase extends AbstractMuleTestCase
33 implements ModelNotificationListener, ManagerNotificationListener
34 {
35
36 private final AtomicBoolean managerStopped = new AtomicBoolean(false);
37 private final AtomicInteger managerStoppedEvents = new AtomicInteger(0);
38 private final AtomicBoolean modelStopped = new AtomicBoolean(false);
39 private final AtomicInteger componentStartedCount = new AtomicInteger(0);
40 private final AtomicInteger customNotificationCount = new AtomicInteger(0);
41 private UMOModel model;
42 private UMOManager manager;
43
44
45 protected void doSetUp() throws Exception
46 {
47 managerStoppedEvents.set(0);
48 manager = getManager(true);
49 manager.start();
50 model = getDefaultModel();
51 }
52
53
54 protected void doTearDown() throws Exception
55 {
56 manager.stop();
57 managerStopped.set(true);
58 managerStoppedEvents.set(0);
59 }
60
61 public void testStandardNotifications() throws Exception
62 {
63 manager.registerListener(this);
64 manager.stop();
65 assertTrue(modelStopped.get());
66 assertTrue(managerStopped.get());
67 }
68
69 public void testMultipleRegistrations() throws Exception
70 {
71 manager.registerListener(this);
72 manager.registerListener(this);
73 manager.stop();
74 assertTrue(managerStopped.get());
75 assertEquals(2, managerStoppedEvents.get());
76 }
77
78 public void testUnregistering() throws Exception
79 {
80 manager.registerListener(this);
81 manager.unregisterListener(this);
82 manager.stop();
83
84 assertFalse(modelStopped.get());
85 assertFalse(managerStopped.get());
86 }
87
88 public void testMismatchingUnregistrations() throws Exception
89 {
90 manager.registerListener(this);
91 manager.registerListener(this);
92 manager.unregisterListener(this);
93 manager.stop();
94
95
96 assertTrue(managerStopped.get());
97 assertEquals(1, managerStoppedEvents.get());
98 }
99
100 public void testStandardNotificationsWithSubscription() throws Exception
101 {
102 final CountDownLatch latch = new CountDownLatch(1);
103 manager.registerListener(new ComponentNotificationListener()
104 {
105 public void onNotification(UMOServerNotification notification)
106 {
107 if (notification.getAction() == ComponentNotification.COMPONENT_STARTED)
108 {
109 componentStartedCount.incrementAndGet();
110 assertEquals("component1", notification.getResourceIdentifier());
111 latch.countDown();
112 }
113 }
114 }, "component1");
115
116 model.registerComponent(getTestDescriptor("component2", Apple.class.getName()));
117 model.registerComponent(getTestDescriptor("component1", Apple.class.getName()));
118
119
120 latch.await(20000, TimeUnit.MILLISECONDS);
121 assertEquals(1, componentStartedCount.get());
122 }
123
124 public void testStandardNotificationsWithWildcardSubscription() throws Exception
125 {
126 final CountDownLatch latch = new CountDownLatch(2);
127
128 manager.registerListener(new ComponentNotificationListener()
129 {
130 public void onNotification(UMOServerNotification notification)
131 {
132 if (notification.getAction() == ComponentNotification.COMPONENT_STARTED)
133 {
134 componentStartedCount.incrementAndGet();
135 assertFalse("noMatchComponent".equals(notification.getResourceIdentifier()));
136 latch.countDown();
137 }
138 }
139 }, "component*");
140
141 model.registerComponent(getTestDescriptor("component2", Apple.class.getName()));
142 model.registerComponent(getTestDescriptor("component1", Apple.class.getName()));
143 model.registerComponent(getTestDescriptor("noMatchComponent", Apple.class.getName()));
144
145
146 latch.await(2000, TimeUnit.MILLISECONDS);
147 assertEquals(2, componentStartedCount.get());
148 }
149
150 public void testCustomNotifications() throws Exception
151 {
152 final CountDownLatch latch = new CountDownLatch(2);
153
154 manager.registerListener(new DummyNotificationListener()
155 {
156 public void onNotification(UMOServerNotification notification)
157 {
158 if (notification.getAction() == DummyNotification.EVENT_RECEIVED)
159 {
160 customNotificationCount.incrementAndGet();
161 assertEquals("hello", notification.getSource());
162 latch.countDown();
163 }
164 }
165 });
166
167 manager.fireNotification(new DummyNotification("hello", DummyNotification.EVENT_RECEIVED));
168 manager.fireNotification(new DummyNotification("hello", DummyNotification.EVENT_RECEIVED));
169
170
171 latch.await(2000, TimeUnit.MILLISECONDS);
172 assertEquals(2, customNotificationCount.get());
173 }
174
175 public void testCustomNotificationsWithWildcardSubscription() throws Exception
176 {
177
178 final CountDownLatch latch = new CountDownLatch(2);
179
180 manager.registerListener(new DummyNotificationListener()
181 {
182 public void onNotification(UMOServerNotification notification)
183 {
184 if (notification.getAction() == DummyNotification.EVENT_RECEIVED)
185 {
186 customNotificationCount.incrementAndGet();
187 assertFalse("e quick bro".equals(notification.getResourceIdentifier()));
188 latch.countDown();
189 }
190 }
191 }, "* quick brown*");
192
193 manager.fireNotification(new DummyNotification("the quick brown fox jumped over the lazy dog",
194 DummyNotification.EVENT_RECEIVED));
195 manager.fireNotification(new DummyNotification("e quick bro", DummyNotification.EVENT_RECEIVED));
196 manager.fireNotification(new DummyNotification(" quick brown", DummyNotification.EVENT_RECEIVED));
197
198
199 latch.await(2000, TimeUnit.MILLISECONDS);
200 assertEquals(2, customNotificationCount.get());
201 }
202
203 public void onNotification(UMOServerNotification notification)
204 {
205 if (notification.getAction() == ModelNotification.MODEL_STOPPED)
206 {
207 modelStopped.set(true);
208 }
209 else if (notification.getAction() == ManagerNotification.MANAGER_STOPPED)
210 {
211 managerStopped.set(true);
212 managerStoppedEvents.incrementAndGet();
213 }
214 }
215
216 public static interface DummyNotificationListener extends CustomNotificationListener
217 {
218
219 }
220
221 public class DummyNotification extends CustomNotification
222 {
223
224
225
226 private static final long serialVersionUID = -1117307108932589331L;
227
228 public static final int EVENT_RECEIVED = -999999;
229
230 public DummyNotification(String message, int action)
231 {
232 super(message, action);
233 resourceIdentifier = message;
234 }
235 }
236 }