View Javadoc

1   /*
2    * $Id: ServerNotificationManagerTestCase.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.config.spring.parsers.specific;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleContext;
16  import org.mule.api.context.notification.SecurityNotificationListener;
17  import org.mule.api.context.notification.ServerNotification;
18  import org.mule.api.context.notification.ServerNotificationListener;
19  import org.mule.api.security.UnauthorisedException;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.context.notification.ListenerSubscriptionPair;
22  import org.mule.context.notification.SecurityNotification;
23  import org.mule.context.notification.ServerNotificationManager;
24  import org.mule.tck.FunctionalTestCase;
25  import org.mule.transport.NullPayload;
26  
27  import java.util.Collection;
28  
29  public class ServerNotificationManagerTestCase extends FunctionalTestCase
30  {
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "org/mule/config/spring/parsers/specific/server-notification-manager-test.xml";
36      }
37  
38      public void testDynamicAttribute()
39      {
40          ServerNotificationManager manager = muleContext.getNotificationManager();
41          assertTrue(manager.isNotificationDynamic());
42      }
43  
44      public void testRoutingConfiguration()
45      {
46          ServerNotificationManager manager = muleContext.getNotificationManager();
47          assertTrue(manager.getInterfaceToTypes().size() > 2);
48          Object ifaces = manager.getInterfaceToTypes().get(TestInterface.class);
49          assertNotNull(ifaces);
50          assertTrue(ifaces instanceof Collection);
51          assertTrue(((Collection) ifaces).contains(TestEvent.class));
52          ifaces = manager.getInterfaceToTypes().get(TestInterface2.class);
53          assertNotNull(ifaces);
54          assertTrue(ifaces instanceof Collection);
55          assertTrue(((Collection) ifaces).contains(SecurityNotification.class));
56      }
57  
58      public void testSimpleNotification() throws InterruptedException
59      {
60          ServerNotificationManager manager = muleContext.getNotificationManager();
61          Collection listeners = manager.getListeners();
62          //Now all transformers are registered as listeners in order to get a context disposing notification
63          assertTrue(listeners.size() > 5);
64          TestListener listener = (TestListener) muleContext.getRegistry().lookupObject("listener");
65          assertNotNull(listener);
66          assertFalse(listener.isCalled());
67          manager.fireNotification(new TestEvent());
68          Thread.sleep(1000); // asynch events
69          assertTrue(listener.isCalled());
70      }
71  
72      public void testExplicitlyConiguredNotificationListenerRegistration() throws InterruptedException
73      {
74          ServerNotificationManager manager = muleContext.getNotificationManager();
75          assertTrue(manager.getListeners().contains(
76              new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
77                  "listener"), null)));
78          assertTrue(manager.getListeners().contains(
79              new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
80                  "listener2"), null)));
81          assertTrue(manager.getListeners().contains(
82              new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
83                  "securityListener"), null)));
84          assertTrue(manager.getListeners().contains(
85              new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
86                  "listener3"), "*")));
87      }
88  
89      public void testAdhocNotificationListenerRegistrations() throws InterruptedException
90      {
91          ServerNotificationManager manager = muleContext.getNotificationManager();
92  
93          // Not registered asad-hoc listener with null subscription as this is defined
94          // explicitly.
95          assertFalse(manager.getListeners().contains(
96              new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
97                  "listener3"), null)));
98  
99          // Registered as configured
100         assertTrue(manager.getListeners().contains(
101             new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
102                 "listener4"), null)));
103     }
104 
105     public void testDisabledNotification() throws InterruptedException
106     {
107         ServerNotificationManager manager = muleContext.getNotificationManager();
108         Collection listeners = manager.getListeners();
109         //Now all transformers are registered as listeners in order to get a context disposing notification
110         assertTrue(listeners.size() > 5);
111         TestListener2 listener2 = (TestListener2) muleContext.getRegistry().lookupObject("listener2");
112         assertNotNull(listener2);
113         assertFalse(listener2.isCalled());
114         TestSecurityListener adminListener = (TestSecurityListener) muleContext.getRegistry().lookupObject(
115             "securityListener");
116         assertNotNull(adminListener);
117         assertFalse(adminListener.isCalled());
118         manager.fireNotification(new TestSecurityEvent(muleContext));
119         Thread.sleep(1000); // asynch events
120         assertTrue(listener2.isCalled());
121         assertFalse(adminListener.isCalled());
122     }
123 
124     protected static interface TestInterface extends ServerNotificationListener
125     {
126         // empty
127     }
128 
129     protected static interface TestInterface2 extends ServerNotificationListener
130     {
131         // empty
132     }
133 
134     protected static class TestListener implements TestInterface
135     {
136 
137         private boolean called = false;
138 
139         public boolean isCalled()
140         {
141             return called;
142         }
143 
144         public void onNotification(ServerNotification notification)
145         {
146             called = true;
147         }
148 
149     }
150     protected static class TestListener2 implements TestInterface2
151     {
152 
153         private boolean called = false;
154 
155         public boolean isCalled()
156         {
157             return called;
158         }
159 
160         public void onNotification(ServerNotification notification)
161         {
162             called = true;
163         }
164 
165     }
166 
167     protected static class TestListener3 implements TestInterface2
168     {
169 
170         private boolean called = false;
171 
172         public boolean isCalled()
173         {
174             return called;
175         }
176 
177         public void onNotification(ServerNotification notification)
178         {
179             called = true;
180         }
181 
182     }
183 
184     protected static class TestListener4 implements TestInterface2
185     {
186 
187         private boolean called = false;
188 
189         public boolean isCalled()
190         {
191             return called;
192         }
193 
194         public void onNotification(ServerNotification notification)
195         {
196             called = true;
197         }
198 
199     }
200 
201     protected static class TestSecurityListener implements SecurityNotificationListener<SecurityNotification>
202     {
203 
204         private boolean called = false;
205 
206         public boolean isCalled()
207         {
208             return called;
209         }
210 
211         public void onNotification(SecurityNotification notification)
212         {
213             called = true;
214         }
215 
216     }
217 
218     protected static class TestEvent extends ServerNotification
219     {
220 
221         public TestEvent()
222         {
223             super(new Object(), 0);
224         }
225 
226     }
227 
228     protected static class TestSecurityEvent extends SecurityNotification
229     {
230 
231         public TestSecurityEvent(MuleContext muleContext)
232         {
233             super(new UnauthorisedException(CoreMessages.createStaticMessage("dummy"),
234                 new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(), muleContext), 
235                 null, null)), 0);
236         }
237 
238     }
239 
240 }