View Javadoc

1   /*
2    * $Id: ServerNotificationsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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.AbstractMuleTestCase;
23  import org.mule.tck.testmodels.fruit.Apple;
24  
25  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
28  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
29  
30  public class ServerNotificationsTestCase extends AbstractMuleTestCase
31          implements ModelNotificationListener, MuleContextNotificationListener
32  {
33  
34      private final AtomicBoolean managerStopped = new AtomicBoolean(false);
35      private final AtomicInteger managerStoppedEvents = new AtomicInteger(0);
36      private final AtomicBoolean modelStopped = new AtomicBoolean(false);
37      private final AtomicInteger modelStoppedEvents = new AtomicInteger(0);
38      private final AtomicInteger componentStartedCount = new AtomicInteger(0);
39      private final AtomicInteger customNotificationCount = new AtomicInteger(0);
40  
41      public ServerNotificationsTestCase()
42      {
43          setStartContext(true);
44      }
45      
46      @Override
47      protected void doTearDown() throws Exception
48      {
49          managerStopped.set(true);
50          managerStoppedEvents.set(0);
51      }
52  
53      public void testStandardNotifications() throws Exception
54      {
55          muleContext.registerListener(this);
56          muleContext.stop();
57          assertTrue(modelStopped.get());
58          assertTrue(managerStopped.get());
59      }
60  
61      public void testMultipleRegistrations() throws Exception
62      {
63          muleContext.registerListener(this);
64          muleContext.registerListener(this);
65          muleContext.stop();
66          assertTrue(managerStopped.get());
67          assertEquals(1, managerStoppedEvents.get());
68      }
69  
70      public void testMultipleRegistrationsDifferentSubscriptions() throws Exception
71      {
72          muleContext.registerListener(this, "_mule*");
73          muleContext.registerListener(this, "_mul*");
74          muleContext.stop();
75          assertTrue(modelStopped.get());
76          assertEquals(2, modelStoppedEvents.get());
77      }
78      
79      public void testUnregistering() throws Exception
80      {
81          muleContext.registerListener(this);
82          muleContext.unregisterListener(this);
83          muleContext.stop();
84          // these should still be false because we unregistered ourselves
85          assertFalse(modelStopped.get());
86          assertFalse(managerStopped.get());
87      }
88  
89      public void testMismatchingUnregistrations() throws Exception
90      {
91          // this has changed in 2.x.  now, unregistering removes all related entries
92          muleContext.registerListener(this);
93          DummyListener dummy = new DummyListener();
94          muleContext.registerListener(dummy);
95          muleContext.registerListener(dummy);
96          muleContext.unregisterListener(dummy);
97          muleContext.stop();
98  
99          assertTrue(managerStopped.get());
100         assertEquals(1, managerStoppedEvents.get());
101     }
102 
103     public void testStandardNotificationsWithSubscription() throws Exception
104     {
105         final CountDownLatch latch = new CountDownLatch(1);
106         muleContext.registerListener(new ServiceNotificationListener<ServiceNotification>()
107         {
108             public void onNotification(ServiceNotification notification)
109             {
110                 if (notification.getAction() == ServiceNotification.SERVICE_STARTED)
111                 {
112                     componentStartedCount.incrementAndGet();
113                     assertEquals("component1", notification.getResourceIdentifier());
114                     latch.countDown();
115                 }
116             }
117         }, "component1");
118 
119         getTestService("component2", Apple.class);
120         getTestService("component1", Apple.class);
121 
122 
123         // Wait for the notifcation event to be fired as they are queued
124         latch.await(20000, TimeUnit.MILLISECONDS);
125         assertEquals(1, componentStartedCount.get());
126     }
127 
128     public void testStandardNotificationsWithWildcardSubscription() throws Exception
129     {
130         final CountDownLatch latch = new CountDownLatch(2);
131 
132         muleContext.registerListener(new ServiceNotificationListener<ServiceNotification>()
133         {
134             public void onNotification(ServiceNotification notification)
135             {
136                 if (notification.getAction() == ServiceNotification.SERVICE_STARTED)
137                 {
138                     componentStartedCount.incrementAndGet();
139                     assertFalse("noMatchComponent".equals(notification.getResourceIdentifier()));
140                     latch.countDown();
141                 }
142             }
143         }, "component*");
144 
145         //Components automatically get registered
146         getTestService("component2", Apple.class);
147         getTestService("component1", Apple.class);
148         getTestService("noMatchComponent", Apple.class);
149 
150         // Wait for the notifcation event to be fired as they are queued
151         latch.await(2000, TimeUnit.MILLISECONDS);
152         assertEquals(2, componentStartedCount.get());
153     }
154 
155     public void testCustomNotifications() throws Exception
156     {
157         final CountDownLatch latch = new CountDownLatch(2);
158 
159         muleContext.registerListener(new DummyNotificationListener()
160         {
161             public void onNotification(ServerNotification notification)
162             {
163                 if (notification.getAction() == DummyNotification.EVENT_RECEIVED)
164                 {
165                     customNotificationCount.incrementAndGet();
166                     assertEquals("hello", notification.getSource());
167                     latch.countDown();
168                 }
169             }
170         });
171 
172         muleContext.fireNotification(new DummyNotification("hello", DummyNotification.EVENT_RECEIVED));
173         muleContext.fireNotification(new DummyNotification("hello", DummyNotification.EVENT_RECEIVED));
174 
175         // Wait for the notifcation event to be fired as they are queued
176         latch.await(2000, TimeUnit.MILLISECONDS);
177         assertEquals(2, customNotificationCount.get());
178     }
179 
180     public void testCustomNotificationsWithWildcardSubscription() throws Exception
181     {
182 
183         final CountDownLatch latch = new CountDownLatch(2);
184 
185         muleContext.registerListener(new DummyNotificationListener()
186         {
187             public void onNotification(ServerNotification notification)
188             {
189                 if (notification.getAction() == DummyNotification.EVENT_RECEIVED)
190                 {
191                     customNotificationCount.incrementAndGet();
192                     assertFalse("e quick bro".equals(notification.getResourceIdentifier()));
193                     latch.countDown();
194                 }
195             }
196         }, "* quick brown*");
197 
198         muleContext.fireNotification(new DummyNotification("the quick brown fox jumped over the lazy dog",
199                                                                  DummyNotification.EVENT_RECEIVED));
200         muleContext.fireNotification(new DummyNotification("e quick bro", DummyNotification.EVENT_RECEIVED));
201         muleContext.fireNotification(new DummyNotification(" quick brown", DummyNotification.EVENT_RECEIVED));
202 
203         // Wait for the notifcation event to be fired as they are queued
204         latch.await(20000, TimeUnit.MILLISECONDS);
205         assertEquals(2, customNotificationCount.get());
206     }
207 
208     public void onNotification(ServerNotification notification)
209     {
210         if (notification.getAction() == ModelNotification.MODEL_STOPPED)
211         {
212             modelStopped.set(true);
213             modelStoppedEvents.incrementAndGet();
214         }
215         else
216         {
217             if (notification.getAction() == MuleContextNotification.CONTEXT_STOPPED)
218             {
219                 managerStopped.set(true);
220                 managerStoppedEvents.incrementAndGet();
221             }
222         }
223     }
224 
225     public static interface DummyNotificationListener extends CustomNotificationListener
226     {
227         // no methods
228     }
229 
230     public class DummyNotification extends CustomNotification
231     {
232         /**
233          * Serial version
234          */
235         private static final long serialVersionUID = -1117307108932589331L;
236 
237         public static final int EVENT_RECEIVED = -999999;
238 
239         public DummyNotification(String message, int action)
240         {
241             super(message, action);
242             resourceIdentifier = message;
243         }
244     }
245 }