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