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